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 httr2::req_perform_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.

Super class

SSEparser::SSEparser -> OpenaiStreamParser

Public fields

shinySession

Holds the session provided at initialization

user_prompt

The user_prompt provided at initialization, after being formatted with markdown.

value

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

Methods

Inherited methods


Method new()

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

Usage

OpenaiStreamParser$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 append_parsed_sse()

Overwrites SSEparser$append_parsed_sse() to be able to send a custom message to a shiny session, escaping shiny's reactivity.

Usage

OpenaiStreamParser$append_parsed_sse(parsed_event)

Arguments

parsed_event

An already parsed server-sent event to append to the events field.


Method clone()

The objects of this class are cloneable with this method.

Usage

OpenaiStreamParser$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.