Documentation
¶
Overview ¶
Package streaming provides a streaming interface for LLMs.
This package implements utilities for handling streaming responses from language models, allowing for real-time processing of generated content. It supports three main types of chunks: text chunks for regular content, reasoning chunks for step-by-step thinking processes, and tool call chunks for function/tool invocations.
The streaming interface uses a callback-based approach where each chunk is processed as it arrives, enabling applications to display partial results and interactive responses. This is particularly useful for long-form content generation, reasoning steps visualization, and tool-using agents.
Basic usage involves registering a callback function that handles chunks as they arrive:
streamingFunc := func(ctx context.Context, chunk streaming.Chunk) error {
switch chunk.Type {
case streaming.ChunkTypeText:
// Process text content
fmt.Print(chunk.Content)
case streaming.ChunkTypeReasoning:
// Process reasoning/thinking content
fmt.Print(chunk.ReasoningContent)
case streaming.ChunkTypeToolCall:
// Process tool call
fmt.Printf("Tool call: %s\n", chunk.ToolCall.String())
}
return nil
}
The callback function can be passed to LLM implementations that support streaming.
Index ¶
- Variables
- func AppendToolCall(src ToolCall, dst *ToolCall)
- func CallWithReasoning(ctx context.Context, cb Callback, reasoning string) error
- func CallWithText(ctx context.Context, cb Callback, text string) error
- func CallWithToolCall(ctx context.Context, cb Callback, toolCall ToolCall) error
- type Callback
- type Chunk
- type ChunkType
- type ToolCall
Constants ¶
This section is empty.
Variables ¶
var ( ErrToolCallIDRequired = errors.New("tool call id is required") ErrToolCallNameRequired = errors.New("tool call name is required") )
Functions ¶
func AppendToolCall ¶
func CallWithReasoning ¶
Types ¶
type Chunk ¶
type Chunk struct {
Type ChunkType `json:"type"`
Content string `json:"content"`
ReasoningContent string `json:"reasoning_content"`
ToolCall ToolCall `json:"tool_call"`
}