streaming

package
v0.1.13-update.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2025 License: MIT Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var (
	ErrToolCallIDRequired   = errors.New("tool call id is required")
	ErrToolCallNameRequired = errors.New("tool call name is required")
)

Functions

func AppendToolCall

func AppendToolCall(src ToolCall, dst *ToolCall)

func CallWithReasoning

func CallWithReasoning(ctx context.Context, cb Callback, reasoning string) error

func CallWithText

func CallWithText(ctx context.Context, cb Callback, text string) error

func CallWithToolCall

func CallWithToolCall(ctx context.Context, cb Callback, toolCall ToolCall) error

Types

type Callback

type Callback func(ctx context.Context, chunk Chunk) error

type Chunk

type Chunk struct {
	Type             ChunkType `json:"type"`
	Content          string    `json:"content"`
	ReasoningContent string    `json:"reasoning_content"`
	ToolCall         ToolCall  `json:"tool_call"`
}

func NewReasoningChunk

func NewReasoningChunk(reasoning string) Chunk

func NewTextChunk

func NewTextChunk(text string) Chunk

func NewToolCallChunk

func NewToolCallChunk(toolCall ToolCall) Chunk

func (*Chunk) String

func (c *Chunk) String() string

type ChunkType

type ChunkType string
const (
	ChunkTypeText      ChunkType = "text"
	ChunkTypeReasoning ChunkType = "reasoning"
	ChunkTypeToolCall  ChunkType = "tool_call"
)

type ToolCall

type ToolCall struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

func NewToolCall

func NewToolCall(id, name, arguments string) ToolCall

func (*ToolCall) Parse

func (t *ToolCall) Parse() (map[string]any, error)

func (*ToolCall) String

func (t *ToolCall) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL