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 initializationuser_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)
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.
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.