LlmEx.StreamProcessor (LlmEx v0.1.0)
View SourceShared functionality for processing streaming responses from LLM providers. Handles common patterns for streaming responses and managing events.
Summary
Functions
Process JSON deltas and accumulate them for tools that send partial JSON.
Handle line-by-line streaming with SSE (Server-Sent Events) format.
Parse a single SSE (Server-Sent Events) line into event type and data.
Process a streaming response with a custom handler function.
Functions
Process JSON deltas and accumulate them for tools that send partial JSON.
Parameters
partial_json
- The partial JSON string to processtool_key
- Unique key for the tool (usually {message_id, index})get_fn
- Function to get the current stateput_fn
- Function to store the updated statefield
- Field to update (default: :input)
Returns
- The updated state
Handle line-by-line streaming with SSE (Server-Sent Events) format.
Parameters
data
- The data chunk to processstate
- Current state including buffer and processing detailsprocess_event_fn
- Function to process each event
Parse a single SSE (Server-Sent Events) line into event type and data.
Parameters
line
- The SSE line to parse
Returns
{:event, event_type, parsed_data}
- For event lines with JSON data{:data, data}
- For data lines:unknown
- For other lines
Process a streaming response with a custom handler function.
Parameters
handler_func
- Function to process each data chunkmessage_id
- ID of the message being processedpid
- Process to send events toopts
- Additional options
Returns
- A function that can be used with Req.post! :into option