Skip to contents

Stream handler for chat completions

Stream handler for chat completions

Details

R6 class that allows to handle chat completions chunk by chunk. It also adds methods to retrieve relevant data. This class DOES NOT make the request.

Because curl::curl_fetch_stream blocks the R console until the stream finishes, this class can take a shiny session object to handle communication with JS without recurring to a shiny::observe inside a module server.

Public fields

current_value

The content of the stream. It updates constantly until the stream ends.

chunks

The list of chunks streamed. It updates constantly until the stream ends.

shinySession

Holds the session provided at initialization

user_message

The user_prompt provided at initialization after being formatted with markdown.

Methods


Method new()

Start a StreamHandler. Recommended to be assigned to the stream_handler name.

Usage

StreamHandler$new(session = NULL, user_prompt = NULL)

Arguments

session

The shiny session it will send the message to (optional).

user_prompt

The prompt for the chat completion. Only to be displayed in an HTML tag containing the prompt. (Optional).


Method handle_streamed_element()

The main reason this class exists. It reduces to stream to chunks and its current value. If the object finds a shiny session will send a render-stream message to JS.

Usage

StreamHandler$handle_streamed_element(x)

Arguments

x

The streamed element. Preferably after conversion from raw.


Method extract_message()

Extract the message content as a message ready to be styled or appended to the chat history. Useful after the stream ends.

Usage

StreamHandler$extract_message()


Method clone()

The objects of this class are cloneable with this method.

Usage

StreamHandler$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.