Documentation
¶
Overview ¶
Package core provides the Iris SDK client and types.
Package core provides the Iris SDK client and types for interacting with AI providers.
Iris is a Go-native framework for building, orchestrating, and deploying AI agents and multimodal workflows. The core package defines the fundamental abstractions that all providers implement.
Client and Provider ¶
The primary entry point is Client, which wraps a Provider and adds telemetry, retry logic, and a fluent builder API:
provider := openai.New(os.Getenv("OPENAI_API_KEY"))
client := core.NewClient(provider,
core.WithTelemetry(myTelemetryHook),
core.WithRetryPolicy(core.DefaultRetryPolicy()),
)
ChatBuilder ¶
The ChatBuilder provides a fluent API for constructing chat requests:
resp, err := client.Chat("gpt-4o").
System("You are a helpful assistant.").
User("Hello!").
Temperature(0.7).
GetResponse(ctx)
ChatBuilder is NOT thread-safe. Each goroutine should create its own builder instance. Use ChatBuilder.Clone to create independent copies from a base configuration:
base := client.Chat(model).System("You are helpful.").Temperature(0.7)
go func() { resp1, _ := base.Clone().User("Q1").GetResponse(ctx) }()
go func() { resp2, _ := base.Clone().User("Q2").GetResponse(ctx) }()
Streaming ¶
Iris treats streaming as a first-class primitive. Use ChatBuilder.Stream for streaming responses:
stream, err := client.Chat(model).User("Tell me a story.").Stream(ctx)
if err != nil {
return err
}
for chunk := range stream.Ch {
fmt.Print(chunk.Delta)
}
The ChatStream type provides three channels:
- Ch: Emits text deltas in order
- Err: Emits at most one error
- Final: Emits the complete response with usage and tool calls
Use DrainStream as a convenience to accumulate all chunks into a final response.
Provider Interface ¶
All providers implement the Provider interface:
type Provider interface {
ID() string
Models() []ModelInfo
Supports(feature Feature) bool
Chat(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
StreamChat(ctx context.Context, req *ChatRequest) (*ChatStream, error)
}
Providers SHOULD be safe for concurrent use. Use [Provider.Supports] to check capabilities before making requests:
if provider.Supports(core.FeatureToolCalling) {
// Safe to use tools
}
Features ¶
Providers declare their capabilities through Feature constants:
- FeatureChat: Basic chat completion
- FeatureChatStreaming: Streaming chat completion
- FeatureToolCalling: Function/tool calling support
- FeatureReasoning: Extended reasoning capabilities
- FeatureBuiltInTools: Web search, file search, code interpreter
- FeatureResponseChain: Multi-turn response chaining
- FeatureEmbeddings: Text embedding generation
- FeatureContextualizedEmbeddings: Document-aware embeddings
- FeatureReranking: Search result reranking
Error Handling ¶
The package defines sentinel errors for common failure modes:
- ErrUnauthorized: Invalid or missing API key
- ErrRateLimited: Provider rate limit exceeded
- ErrBadRequest: Invalid request parameters
- ErrServer: Provider server error (5xx)
- ErrNetwork: Network connectivity issues
- ErrDecode: Response parsing failed
- ErrModelRequired: Model ID not specified
- ErrNoMessages: No messages in request
Use errors.Is to check error types:
if errors.Is(err, core.ErrRateLimited) {
// Handle rate limiting
}
Telemetry ¶
Implement TelemetryHook to observe request lifecycle:
type MyTelemetry struct{}
func (t MyTelemetry) OnRequestStart(e RequestStartEvent) {
log.Printf("Starting %s request to %s", e.Model, e.Provider)
}
func (t MyTelemetry) OnRequestEnd(e RequestEndEvent) {
log.Printf("Completed in %v, tokens: %d", e.End.Sub(e.Start), e.Usage.TotalTokens)
}
Retry Policy ¶
Configure retry behavior with RetryPolicy:
policy := &core.ExponentialBackoff{
MaxRetries: 3,
BaseDelay: time.Second,
MaxDelay: 30 * time.Second,
}
client := core.NewClient(provider, core.WithRetryPolicy(policy))
The default policy retries transient errors (rate limits, server errors) with exponential backoff.
Multimodal Messages ¶
For vision and document analysis, use MessageBuilder:
resp, err := client.Chat(model).
UserMultimodal().
Text("What's in this image?").
ImageURL("https://example.com/image.jpg").
Done().
GetResponse(ctx)
Convenience methods are also available:
resp, err := client.Chat(model).
UserWithImageURL("Describe this:", "https://example.com/image.jpg").
GetResponse(ctx)
Thread Safety ¶
Client is safe for concurrent use across goroutines. ChatBuilder and MessageBuilder are NOT thread-safe. ChatStream channels may be read by one goroutine at a time. Providers SHOULD be safe for concurrent calls (check provider documentation).
Package core provides the Iris SDK client and types.
Index ¶
- Variables
- type APIEndpoint
- type AgentCompleteEvent
- type AgentConfig
- type AgentHooks
- type AgentResult
- type AgentRunner
- func (r *AgentRunner) Resume(ctx context.Context, snapshot *AgentSnapshot) (*AgentResult, error)
- func (r *AgentRunner) Run(ctx context.Context) (*AgentResult, error)
- func (r *AgentRunner) RunStream(ctx context.Context) (*AgentResult, error)
- func (r *AgentRunner) Snapshot() (*AgentSnapshot, error)
- func (r *AgentRunner) WithConfig(cfg AgentConfig) *AgentRunner
- func (r *AgentRunner) WithContinueOnToolError(cont bool) *AgentRunner
- func (r *AgentRunner) WithHooks(hooks AgentHooks) *AgentRunner
- func (r *AgentRunner) WithIterationTimeout(d time.Duration) *AgentRunner
- func (r *AgentRunner) WithMaxIterations(n int) *AgentRunner
- func (r *AgentRunner) WithMaxParallelTools(n int) *AgentRunner
- func (r *AgentRunner) WithMaxToolCalls(n int) *AgentRunner
- func (r *AgentRunner) WithMemory(cfg *MemoryConfig) *AgentRunner
- func (r *AgentRunner) WithParallelTools(enabled bool) *AgentRunner
- func (r *AgentRunner) WithStopSequences(seqs ...string) *AgentRunner
- func (r *AgentRunner) WithToolFilter(f func(ToolCall) bool) *AgentRunner
- func (r *AgentRunner) WithToolTimeout(d time.Duration) *AgentRunner
- type AgentSnapshot
- type AgentStopReason
- type BuiltInTool
- type ChatBuilder
- func (b *ChatBuilder) Agent(executor ToolExecutor) *AgentRunner
- func (b *ChatBuilder) Assistant(s string) *ChatBuilder
- func (b *ChatBuilder) BuiltInTool(toolType string) *ChatBuilder
- func (b *ChatBuilder) Clone() *ChatBuilder
- func (b *ChatBuilder) CodeInterpreter() *ChatBuilder
- func (b *ChatBuilder) ContinueFrom(responseID string) *ChatBuilder
- func (b *ChatBuilder) FileSearch(vectorStoreIDs ...string) *ChatBuilder
- func (b *ChatBuilder) GetResponse(ctx context.Context) (*ChatResponse, error)
- func (b *ChatBuilder) Instructions(s string) *ChatBuilder
- func (b *ChatBuilder) MaxTokens(n int) *ChatBuilder
- func (b *ChatBuilder) ReasoningEffort(level ReasoningEffort) *ChatBuilder
- func (b *ChatBuilder) Stream(ctx context.Context) (*ChatStream, error)
- func (b *ChatBuilder) System(s string) *ChatBuilder
- func (b *ChatBuilder) Temperature(v float32) *ChatBuilder
- func (b *ChatBuilder) Timeout(d time.Duration) *ChatBuilder
- func (b *ChatBuilder) ToolError(assistantResp *ChatResponse, callID string, err error) *ChatBuilder
- func (b *ChatBuilder) ToolResult(assistantResp *ChatResponse, callID string, content any) *ChatBuilder
- func (b *ChatBuilder) ToolResults(assistantResp *ChatResponse, results []ToolResult) *ChatBuilder
- func (b *ChatBuilder) Tools(ts ...Tool) *ChatBuilder
- func (b *ChatBuilder) Truncation(mode string) *ChatBuilder
- func (b *ChatBuilder) User(s string) *ChatBuilder
- func (b *ChatBuilder) UserMultimodal() *MessageBuilder
- func (b *ChatBuilder) UserWithFileID(text, fileID string) *ChatBuilder
- func (b *ChatBuilder) UserWithFileURL(text, fileURL string) *ChatBuilder
- func (b *ChatBuilder) UserWithImageFileID(text, fileID string) *ChatBuilder
- func (b *ChatBuilder) UserWithImageURL(text, imageURL string) *ChatBuilder
- func (b *ChatBuilder) WebSearch() *ChatBuilder
- type ChatChunk
- type ChatRequest
- type ChatResponse
- type ChatStream
- type Client
- type ClientOption
- type ContentPart
- type ContextualizedEmbeddingProvider
- type ContextualizedEmbeddingRequest
- type ContextualizedEmbeddingResponse
- type Conversation
- type ConversationOption
- type EmbeddingInput
- type EmbeddingProvider
- type EmbeddingRequest
- type EmbeddingResponse
- type EmbeddingUsage
- type EmbeddingVector
- type EncodingFormat
- type Feature
- type FileSearchResources
- type ImageAction
- type ImageBackground
- type ImageChunk
- type ImageData
- type ImageDetail
- type ImageEditRequest
- type ImageFormat
- type ImageGenerateRequest
- type ImageGenerator
- type ImageInput
- type ImageInputFidelity
- type ImageQuality
- type ImageResponse
- type ImageSize
- type ImageStream
- type ImageUsage
- type InMemoryStore
- func (m *InMemoryStore) AddMessage(msg Message)
- func (m *InMemoryStore) AddMessages(msgs []Message)
- func (m *InMemoryStore) Clear()
- func (m *InMemoryStore) GetHistory() []Message
- func (m *InMemoryStore) GetLastN(n int) []Message
- func (m *InMemoryStore) Len() int
- func (m *InMemoryStore) SetMessages(msgs []Message)
- type InputFile
- type InputImage
- type InputText
- type InputType
- type IterationEndEvent
- type IterationStartEvent
- type Memory
- type MemoryConfig
- type Message
- type MessageBuilder
- func (m *MessageBuilder) Done() *ChatBuilder
- func (m *MessageBuilder) FileBase64(filename, base64Data string) *MessageBuilder
- func (m *MessageBuilder) FileID(fileID string) *MessageBuilder
- func (m *MessageBuilder) FileURL(url string) *MessageBuilder
- func (m *MessageBuilder) ImageFileID(fileID string) *MessageBuilder
- func (m *MessageBuilder) ImageFileIDWithDetail(fileID string, detail ImageDetail) *MessageBuilder
- func (m *MessageBuilder) ImageURL(url string) *MessageBuilder
- func (m *MessageBuilder) ImageURLWithDetail(url string, detail ImageDetail) *MessageBuilder
- func (m *MessageBuilder) Text(s string) *MessageBuilder
- type ModelID
- type ModelInfo
- type NoopTelemetryHook
- type OutputDType
- type Provider
- type ProviderError
- type ReasoningEffort
- type ReasoningOutput
- type RequestEndEvent
- type RequestStartEvent
- type RerankRequest
- type RerankResponse
- type RerankResult
- type RerankUsage
- type RerankerProvider
- type RetryConfig
- type RetryPolicy
- type Role
- type Secret
- type SummarizationEvent
- type TelemetryHook
- type TokenUsage
- type Tool
- type ToolCall
- type ToolCallEndEvent
- type ToolCallStartEvent
- type ToolExecution
- type ToolExecutor
- type ToolResources
- type ToolResult
- type ToolResultBuilder
- func (b *ToolResultBuilder) Build() []ToolResult
- func (b *ToolResultBuilder) Error(callID string, err error) *ToolResultBuilder
- func (b *ToolResultBuilder) FromExecution(callID string, result any, err error) *ToolResultBuilder
- func (b *ToolResultBuilder) Success(callID string, content any) *ToolResultBuilder
- type TypedToolResult
- type TypedToolResultBuilder
Constants ¶
This section is empty.
Variables ¶
var ( ErrRateLimited = errors.New("rate limited") ErrBadRequest = errors.New("bad request") ErrNotFound = errors.New("not found") ErrServer = errors.New("server error") ErrNetwork = errors.New("network error") ErrDecode = errors.New("decode error") ErrNotSupported = errors.New("operation not supported") )
Sentinel errors for classification.
var ( ErrModelRequired = errors.New("model required: pass a model ID to Client.Chat(), e.g., client.Chat(\"gpt-4\")") ErrNoMessages = errors.New("no messages: add at least one message using .System(), .User(), or .Assistant()") )
Validation errors with actionable guidance.
Functions ¶
This section is empty.
Types ¶
type APIEndpoint ¶
type APIEndpoint string
APIEndpoint represents which API endpoint a model uses.
const ( // APIEndpointCompletions is the Chat Completions API (default for older models). APIEndpointCompletions APIEndpoint = "completions" // APIEndpointResponses is the Responses API (for newer models like GPT-5.x). APIEndpointResponses APIEndpoint = "responses" )
type AgentCompleteEvent ¶ added in v0.11.0
type AgentCompleteEvent struct {
Iterations int
TotalToolCalls int
TotalDuration time.Duration
TotalTokens TokenUsage
StopReason AgentStopReason
FinalResponse *ChatResponse
}
AgentCompleteEvent is emitted when the agent loop finishes.
type AgentConfig ¶ added in v0.11.0
type AgentConfig struct {
// MaxIterations is the maximum number of LLM calls before stopping.
// Each iteration may execute multiple tools.
// Default: 10. Set to 0 for unlimited (use with caution).
MaxIterations int
// MaxToolCalls is the maximum total tool calls across all iterations.
// Default: 50. Set to 0 for unlimited.
MaxToolCalls int
// IterationTimeout is the timeout for each individual LLM call.
// Does not include tool execution time.
// Default: 30s.
IterationTimeout time.Duration
// ToolTimeout is the timeout for each individual tool execution.
// Default: 60s.
ToolTimeout time.Duration
// ParallelTools enables concurrent execution of tools within an iteration.
// When false, tools execute sequentially in order.
// Default: true.
ParallelTools bool
// MaxParallelTools limits concurrent tool executions.
// Only applies when ParallelTools is true.
// Default: 5. Set to 0 for unlimited.
MaxParallelTools int
// ContinueOnToolError determines behavior when a tool fails.
// If true, the error is passed to the model as a tool result.
// If false, the agent loop returns immediately with the error.
// Default: true (pass errors to model).
ContinueOnToolError bool
// StopSequences are additional strings that stop the agent.
// If the model's output contains any of these, the loop terminates.
// Default: empty.
StopSequences []string
// ToolFilter optionally filters which tools can be executed.
// Return false to skip a tool (error sent to model).
// Default: nil (all tools allowed).
ToolFilter func(call ToolCall) bool
// Hooks for observability and control.
Hooks AgentHooks
// Memory configures conversation memory management with auto-summarization.
// If nil, no memory management is performed (may hit context limits).
Memory *MemoryConfig
}
AgentConfig configures the behavior of an agent loop.
func DefaultAgentConfig ¶ added in v0.11.0
func DefaultAgentConfig() AgentConfig
DefaultAgentConfig returns a configuration with sensible defaults.
type AgentHooks ¶ added in v0.11.0
type AgentHooks struct {
// OnIterationStart is called at the start of each LLM call.
// Return an error to abort the agent loop.
OnIterationStart func(ctx context.Context, e IterationStartEvent) error
// OnIterationEnd is called after each LLM response.
OnIterationEnd func(ctx context.Context, e IterationEndEvent)
// OnToolCallStart is called before executing each tool.
// Return an error to skip this tool (error sent to model).
OnToolCallStart func(ctx context.Context, e ToolCallStartEvent) error
// OnToolCallEnd is called after each tool execution.
OnToolCallEnd func(ctx context.Context, e ToolCallEndEvent)
// OnAgentComplete is called when the agent loop finishes.
OnAgentComplete func(ctx context.Context, e AgentCompleteEvent)
// OnTextDelta is called for each text chunk during streaming.
// Only used with RunStream.
OnTextDelta func(ctx context.Context, delta string)
}
AgentHooks provides callbacks for observing and controlling agent execution.
type AgentResult ¶ added in v0.11.0
type AgentResult struct {
// FinalResponse is the last response from the model.
FinalResponse *ChatResponse
// Iterations is the number of LLM calls made.
Iterations int
// ToolHistory contains all tool executions in order.
ToolHistory []ToolExecution
// TotalTokens is the sum of tokens across all iterations.
TotalTokens TokenUsage
// Duration is the total time from start to finish.
Duration time.Duration
// StopReason indicates why the agent stopped.
StopReason AgentStopReason
// Error is set if the agent stopped due to an error.
// May be nil even if StopReason is StopReasonError.
Error error
}
AgentResult contains the complete result of an agent execution.
func (*AgentResult) FailedToolCalls ¶ added in v0.11.0
func (r *AgentResult) FailedToolCalls() []ToolExecution
FailedToolCalls returns tool executions that failed.
func (*AgentResult) HasError ¶ added in v0.11.0
func (r *AgentResult) HasError() bool
HasError returns true if the agent encountered an error.
func (*AgentResult) SuccessfulToolCalls ¶ added in v0.11.0
func (r *AgentResult) SuccessfulToolCalls() []ToolExecution
SuccessfulToolCalls returns tool executions that succeeded.
func (*AgentResult) TotalToolCalls ¶ added in v0.11.0
func (r *AgentResult) TotalToolCalls() int
TotalToolCalls returns the number of tool executions.
type AgentRunner ¶ added in v0.11.0
type AgentRunner struct {
// contains filtered or unexported fields
}
AgentRunner executes an agentic loop with the configured tools.
func (*AgentRunner) Resume ¶ added in v0.11.0
func (r *AgentRunner) Resume(ctx context.Context, snapshot *AgentSnapshot) (*AgentResult, error)
Resume restores agent state from a snapshot and continues execution.
func (*AgentRunner) Run ¶ added in v0.11.0
func (r *AgentRunner) Run(ctx context.Context) (*AgentResult, error)
Run executes the agent loop synchronously. Returns when the model completes or a termination condition is met.
func (*AgentRunner) RunStream ¶ added in v0.11.0
func (r *AgentRunner) RunStream(ctx context.Context) (*AgentResult, error)
RunStream executes the agent loop with streaming. Text deltas are sent to hooks.OnTextDelta.
func (*AgentRunner) Snapshot ¶ added in v0.11.0
func (r *AgentRunner) Snapshot() (*AgentSnapshot, error)
Snapshot captures the current agent state for later resumption.
func (*AgentRunner) WithConfig ¶ added in v0.11.0
func (r *AgentRunner) WithConfig(cfg AgentConfig) *AgentRunner
WithConfig sets the agent configuration.
func (*AgentRunner) WithContinueOnToolError ¶ added in v0.11.0
func (r *AgentRunner) WithContinueOnToolError(cont bool) *AgentRunner
WithContinueOnToolError sets whether to continue when tools fail.
func (*AgentRunner) WithHooks ¶ added in v0.11.0
func (r *AgentRunner) WithHooks(hooks AgentHooks) *AgentRunner
WithHooks sets the agent hooks for observability.
func (*AgentRunner) WithIterationTimeout ¶ added in v0.11.0
func (r *AgentRunner) WithIterationTimeout(d time.Duration) *AgentRunner
WithIterationTimeout sets the timeout for each LLM call.
func (*AgentRunner) WithMaxIterations ¶ added in v0.11.0
func (r *AgentRunner) WithMaxIterations(n int) *AgentRunner
WithMaxIterations sets the maximum iterations.
func (*AgentRunner) WithMaxParallelTools ¶ added in v0.11.0
func (r *AgentRunner) WithMaxParallelTools(n int) *AgentRunner
WithMaxParallelTools sets the maximum number of concurrent tool executions.
func (*AgentRunner) WithMaxToolCalls ¶ added in v0.11.0
func (r *AgentRunner) WithMaxToolCalls(n int) *AgentRunner
WithMaxToolCalls sets the maximum tool calls.
func (*AgentRunner) WithMemory ¶ added in v0.11.0
func (r *AgentRunner) WithMemory(cfg *MemoryConfig) *AgentRunner
WithMemory enables memory management with auto-summarization.
func (*AgentRunner) WithParallelTools ¶ added in v0.11.0
func (r *AgentRunner) WithParallelTools(enabled bool) *AgentRunner
WithParallelTools enables or disables parallel tool execution.
func (*AgentRunner) WithStopSequences ¶ added in v0.11.0
func (r *AgentRunner) WithStopSequences(seqs ...string) *AgentRunner
WithStopSequences sets strings that terminate the agent loop.
func (*AgentRunner) WithToolFilter ¶ added in v0.11.0
func (r *AgentRunner) WithToolFilter(f func(ToolCall) bool) *AgentRunner
WithToolFilter sets a filter function for tool execution.
func (*AgentRunner) WithToolTimeout ¶ added in v0.11.0
func (r *AgentRunner) WithToolTimeout(d time.Duration) *AgentRunner
WithToolTimeout sets the timeout for individual tool executions.
type AgentSnapshot ¶ added in v0.11.0
type AgentSnapshot struct {
// Version for forward compatibility
Version string `json:"version"`
// Conversation state
Messages []Message `json:"messages"`
// Execution progress
Iteration int `json:"iteration"`
TotalToolCalls int `json:"total_tool_calls"`
ToolHistory []ToolExecution `json:"tool_history"`
// Timing (for metrics continuity)
StartTime time.Time `json:"start_time"`
ElapsedTime time.Duration `json:"elapsed_time"`
TotalTokens TokenUsage `json:"total_tokens"`
// Configuration hash for validation on restore
ConfigHash string `json:"config_hash"`
// Last response (if paused mid-iteration)
PendingToolCalls []ToolCall `json:"pending_tool_calls,omitempty"`
}
AgentSnapshot captures the complete state of an agent execution for resumability.
func LoadSnapshot ¶ added in v0.11.0
func LoadSnapshot(data []byte) (*AgentSnapshot, error)
LoadSnapshot deserializes a snapshot from JSON.
func (*AgentSnapshot) SaveJSON ¶ added in v0.11.0
func (s *AgentSnapshot) SaveJSON() ([]byte, error)
SaveJSON serializes the snapshot to JSON for storage.
type AgentStopReason ¶ added in v0.11.0
type AgentStopReason string
AgentStopReason indicates why the agent loop terminated.
const ( StopReasonComplete AgentStopReason = "complete" // Model finished (no tool calls) StopReasonMaxIterations AgentStopReason = "max_iterations" // Hit MaxIterations limit StopReasonMaxToolCalls AgentStopReason = "max_tool_calls" // Hit MaxToolCalls limit StopReasonStopSequence AgentStopReason = "stop_sequence" // Output contained stop sequence StopReasonHookAbort AgentStopReason = "hook_abort" // Hook returned error StopReasonError AgentStopReason = "error" // Unrecoverable error StopReasonCanceled AgentStopReason = "canceled" // Context canceled )
type BuiltInTool ¶
type BuiltInTool struct {
Type string `json:"type"` // "web_search", "file_search", "code_interpreter"
}
BuiltInTool represents a built-in tool available in the Responses API.
type ChatBuilder ¶
type ChatBuilder struct {
// contains filtered or unexported fields
}
ChatBuilder provides a fluent API for building chat requests. ChatBuilder is NOT thread-safe and should not be shared across goroutines.
func (*ChatBuilder) Agent ¶ added in v0.11.0
func (b *ChatBuilder) Agent(executor ToolExecutor) *AgentRunner
Agent creates an agent runner from a ChatBuilder. The builder should already have messages, tools, and other options set. The executor parameter is typically a *tools.Registry.
func (*ChatBuilder) Assistant ¶
func (b *ChatBuilder) Assistant(s string) *ChatBuilder
Assistant appends an assistant message.
func (*ChatBuilder) BuiltInTool ¶
func (b *ChatBuilder) BuiltInTool(toolType string) *ChatBuilder
BuiltInTool adds a built-in tool to the request.
func (*ChatBuilder) Clone ¶ added in v0.10.0
func (b *ChatBuilder) Clone() *ChatBuilder
Clone creates a deep copy of the ChatBuilder. This is useful for reusing a base configuration across multiple requests:
base := client.Chat(model).System("You are a helpful assistant").Temperature(0.7)
resp1, _ := base.Clone().User("Question 1").GetResponse(ctx)
resp2, _ := base.Clone().User("Question 2").GetResponse(ctx)
The original builder remains unchanged after cloning.
func (*ChatBuilder) CodeInterpreter ¶
func (b *ChatBuilder) CodeInterpreter() *ChatBuilder
CodeInterpreter adds the code_interpreter built-in tool.
func (*ChatBuilder) ContinueFrom ¶
func (b *ChatBuilder) ContinueFrom(responseID string) *ChatBuilder
ContinueFrom chains this request to a previous response.
func (*ChatBuilder) FileSearch ¶
func (b *ChatBuilder) FileSearch(vectorStoreIDs ...string) *ChatBuilder
FileSearch adds the file_search built-in tool with optional vector store IDs.
func (*ChatBuilder) GetResponse ¶
func (b *ChatBuilder) GetResponse(ctx context.Context) (*ChatResponse, error)
GetResponse executes the chat request and returns the response. It applies validation, telemetry, and retry logic. If Timeout was set and ctx has no deadline, a timeout context is created internally.
func (*ChatBuilder) Instructions ¶
func (b *ChatBuilder) Instructions(s string) *ChatBuilder
Instructions sets the system instructions (Responses API style). For Chat Completions API, this is equivalent to adding a system message.
func (*ChatBuilder) MaxTokens ¶
func (b *ChatBuilder) MaxTokens(n int) *ChatBuilder
MaxTokens sets the maximum tokens parameter.
func (*ChatBuilder) ReasoningEffort ¶
func (b *ChatBuilder) ReasoningEffort(level ReasoningEffort) *ChatBuilder
ReasoningEffort sets the reasoning effort level for models that support it.
func (*ChatBuilder) Stream ¶
func (b *ChatBuilder) Stream(ctx context.Context) (*ChatStream, error)
Stream executes the chat request and returns a streaming response. It applies validation and telemetry.
Note: The Timeout() setting is NOT applied to streaming requests because the context must outlive this method call. For streaming with timeouts, create the context externally:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
stream, err := client.Chat(model).User("...").Stream(ctx)
func (*ChatBuilder) System ¶
func (b *ChatBuilder) System(s string) *ChatBuilder
System appends a system message.
func (*ChatBuilder) Temperature ¶
func (b *ChatBuilder) Temperature(v float32) *ChatBuilder
Temperature sets the temperature parameter.
func (*ChatBuilder) Timeout ¶ added in v0.10.0
func (b *ChatBuilder) Timeout(d time.Duration) *ChatBuilder
Timeout sets an optional timeout for the request. When set, GetResponse and Stream will create a context with this timeout if a context.Background() or context without deadline is passed. This provides a convenient alternative to manually creating contexts:
// Instead of:
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
resp, err := client.Chat(model).User("Hello").GetResponse(ctx)
// You can write:
resp, err := client.Chat(model).User("Hello").Timeout(30*time.Second).GetResponse(context.Background())
func (*ChatBuilder) ToolError ¶ added in v0.11.0
func (b *ChatBuilder) ToolError(assistantResp *ChatResponse, callID string, err error) *ChatBuilder
ToolError is a convenience method for adding a single tool error result. Returns a new builder (immutable).
func (*ChatBuilder) ToolResult ¶ added in v0.11.0
func (b *ChatBuilder) ToolResult(assistantResp *ChatResponse, callID string, content any) *ChatBuilder
ToolResult is a convenience method for adding a single successful tool result. Returns a new builder (immutable).
func (*ChatBuilder) ToolResults ¶ added in v0.11.0
func (b *ChatBuilder) ToolResults(assistantResp *ChatResponse, results []ToolResult) *ChatBuilder
ToolResults returns a new ChatBuilder with tool execution results appended. This automatically formats results according to the provider's expected format. The assistant message containing the original tool calls is automatically included.
IMPORTANT: This method returns a NEW builder (immutable). The original builder is unchanged.
If fewer results are provided than tool calls, a warning is logged but execution continues. If result IDs don't match any tool calls, a warning is logged.
func (*ChatBuilder) Tools ¶
func (b *ChatBuilder) Tools(ts ...Tool) *ChatBuilder
Tools sets the tools available for the request.
func (*ChatBuilder) Truncation ¶
func (b *ChatBuilder) Truncation(mode string) *ChatBuilder
Truncation sets the truncation mode for the request.
func (*ChatBuilder) User ¶
func (b *ChatBuilder) User(s string) *ChatBuilder
User appends a user message.
func (*ChatBuilder) UserMultimodal ¶
func (b *ChatBuilder) UserMultimodal() *MessageBuilder
UserMultimodal starts building a multimodal user message.
func (*ChatBuilder) UserWithFileID ¶
func (b *ChatBuilder) UserWithFileID(text, fileID string) *ChatBuilder
UserWithFileID adds a user message with text and a file ID. This is a convenience method for document analysis with uploaded files.
func (*ChatBuilder) UserWithFileURL ¶
func (b *ChatBuilder) UserWithFileURL(text, fileURL string) *ChatBuilder
UserWithFileURL adds a user message with text and a file URL. This is a convenience method for document analysis use cases.
func (*ChatBuilder) UserWithImageFileID ¶
func (b *ChatBuilder) UserWithImageFileID(text, fileID string) *ChatBuilder
UserWithImageFileID adds a user message with text and an image file ID. This is a convenience method for vision use cases with uploaded files.
func (*ChatBuilder) UserWithImageURL ¶
func (b *ChatBuilder) UserWithImageURL(text, imageURL string) *ChatBuilder
UserWithImageURL adds a user message with text and an image URL. This is a convenience method for common vision use cases.
func (*ChatBuilder) WebSearch ¶
func (b *ChatBuilder) WebSearch() *ChatBuilder
WebSearch adds the web_search built-in tool.
type ChatChunk ¶
type ChatChunk struct {
Delta string `json:"delta"`
}
ChatChunk represents an incremental streaming response. Delta contains incremental assistant text.
type ChatRequest ¶
type ChatRequest struct {
Model ModelID `json:"model"`
Messages []Message `json:"messages"`
Temperature *float32 `json:"temperature,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
Tools []Tool `json:"-"` // Tools are handled separately by providers
// Responses API fields (ignored for Chat Completions API)
Instructions string `json:"instructions,omitempty"`
ReasoningEffort ReasoningEffort `json:"reasoning_effort,omitempty"`
BuiltInTools []BuiltInTool `json:"builtin_tools,omitempty"`
PreviousResponseID string `json:"previous_response_id,omitempty"`
Truncation string `json:"truncation,omitempty"`
ToolResources *ToolResources `json:"tool_resources,omitempty"`
}
ChatRequest represents a request to a chat model.
type ChatResponse ¶
type ChatResponse struct {
ID string `json:"id"`
Model ModelID `json:"model"`
Output string `json:"output"`
Usage TokenUsage `json:"usage"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
// Responses API fields
Reasoning *ReasoningOutput `json:"reasoning,omitempty"`
Status string `json:"status,omitempty"`
}
ChatResponse represents a response from a chat model. For providers returning multiple choices, v0.1 uses only the first choice.
func DrainStream ¶
func DrainStream(ctx context.Context, s *ChatStream) (*ChatResponse, error)
DrainStream accumulates all deltas and returns the final ChatResponse. Blocks until stream completes or context cancels.
Behavior:
- Read all chunks from Ch, accumulating Delta into output string
- Check Err channel for any errors
- Wait for Final to get complete response with usage/tool calls
- If Final includes Output, use it; otherwise use accumulated deltas
- Handle context cancellation gracefully
func (*ChatResponse) FirstToolCall ¶ added in v0.10.0
func (r *ChatResponse) FirstToolCall() *ToolCall
FirstToolCall returns the first tool call, or nil if there are none. This is convenient for single-tool scenarios:
if tc := resp.FirstToolCall(); tc != nil {
// handle tool call
}
func (*ChatResponse) HasReasoning ¶ added in v0.10.0
func (r *ChatResponse) HasReasoning() bool
HasReasoning reports whether the response contains reasoning output.
func (*ChatResponse) HasToolCalls ¶ added in v0.10.0
func (r *ChatResponse) HasToolCalls() bool
HasToolCalls reports whether the response contains any tool calls.
type ChatStream ¶
type ChatStream struct {
// Ch emits text deltas in order. Closed when stream ends.
Ch <-chan ChatChunk
// Err emits at most one error. MUST be closed when stream ends.
// If error occurs after setup, send on Err then close all channels.
Err <-chan error
// Final sent exactly once (or zero if setup fails) after stream completion.
// Includes usage and tool calls if available.
// Providers may send partial ChatResponse with Output empty.
Final <-chan *ChatResponse
}
ChatStream represents a streaming response from a provider.
Channel Rules:
- Providers MUST close Ch, Err, and Final when finished
- On context cancellation, providers MUST terminate promptly and close channels
- Err channel emits at most one error
- Final channel emits exactly once on success (or zero times on setup failure)
- If providers cannot compute Usage for streaming, they MAY leave it zeroed
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the main entry point for interacting with LLM providers. Client is safe for concurrent use.
func NewClient ¶
func NewClient(p Provider, opts ...ClientOption) *Client
NewClient creates a new Client with the given provider and options.
func (*Client) Chat ¶
func (c *Client) Chat(model ModelID) *ChatBuilder
Chat returns a ChatBuilder for constructing and executing a chat request.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption configures a Client.
func WithRetryPolicy ¶
func WithRetryPolicy(r RetryPolicy) ClientOption
WithRetryPolicy sets the retry policy for the client.
func WithTelemetry ¶
func WithTelemetry(h TelemetryHook) ClientOption
WithTelemetry sets the telemetry hook for the client.
type ContentPart ¶
type ContentPart interface {
// ContentType returns the type identifier for this content part.
ContentType() string
}
ContentPart represents a part of multimodal content in a message. Types implementing this interface can be used in multimodal messages with the OpenAI Responses API.
type ContextualizedEmbeddingProvider ¶
type ContextualizedEmbeddingProvider interface {
// CreateContextualizedEmbeddings generates context-aware embeddings for document chunks.
// Each inner slice in Inputs represents chunks from a single document.
CreateContextualizedEmbeddings(ctx context.Context, req *ContextualizedEmbeddingRequest) (*ContextualizedEmbeddingResponse, error)
}
ContextualizedEmbeddingProvider is an optional interface for providers that support context-aware chunk embeddings. These embeddings encode not just the local content of each chunk, but also context from surrounding chunks in the same document.
type ContextualizedEmbeddingRequest ¶
type ContextualizedEmbeddingRequest struct {
// Model specifies the embedding model to use.
Model ModelID `json:"model"`
// Inputs is a list of documents, where each document is a list of chunks.
// Chunks within the same document are encoded with awareness of each other.
Inputs [][]string `json:"inputs"`
// InputType specifies whether inputs are queries or documents.
InputType InputType `json:"input_type,omitempty"`
// OutputDimension specifies the number of dimensions for output embeddings.
OutputDimension *int `json:"output_dimension,omitempty"`
// OutputDType specifies the data type for embeddings (float, int8, etc.).
OutputDType OutputDType `json:"output_dtype,omitempty"`
// EncodingFormat specifies the encoding format (float array or base64).
EncodingFormat EncodingFormat `json:"encoding_format,omitempty"`
}
ContextualizedEmbeddingRequest represents a request to generate contextualized embeddings.
type ContextualizedEmbeddingResponse ¶
type ContextualizedEmbeddingResponse struct {
// Embeddings contains the vectors grouped by document, then by chunk.
// embeddings[i][j] is the embedding for chunk j of document i.
Embeddings [][]EmbeddingVector `json:"embeddings"`
// Model is the model that generated the embeddings.
Model ModelID `json:"model"`
// Usage contains token consumption information.
Usage EmbeddingUsage `json:"usage"`
}
ContextualizedEmbeddingResponse contains the generated contextualized embeddings.
type Conversation ¶ added in v0.11.0
type Conversation struct {
// contains filtered or unexported fields
}
Conversation provides a high-level API for managing multi-turn conversations with automatic history management.
func NewConversation ¶ added in v0.11.0
func NewConversation(client *Client, model ModelID, opts ...ConversationOption) *Conversation
NewConversation creates a new conversation session with the given client and model.
func (*Conversation) Clear ¶ added in v0.11.0
func (c *Conversation) Clear()
Clear resets the conversation history. If a system message was provided, it will be re-added.
func (*Conversation) GetHistory ¶ added in v0.11.0
func (c *Conversation) GetHistory() []Message
GetHistory returns the full conversation history.
func (*Conversation) MessageCount ¶ added in v0.11.0
func (c *Conversation) MessageCount() int
MessageCount returns the number of messages in the conversation.
func (*Conversation) Send ¶ added in v0.11.0
func (c *Conversation) Send(userMessage string) (*ChatResponse, error)
Send sends a user message and returns the assistant's response. Automatically manages conversation history. Uses context.Background() internally.
func (*Conversation) SendWithContext ¶ added in v0.11.0
func (c *Conversation) SendWithContext(ctx context.Context, userMessage string) (*ChatResponse, error)
SendWithContext sends a user message with context and returns the assistant's response.
type ConversationOption ¶ added in v0.11.0
type ConversationOption func(*Conversation)
ConversationOption configures a Conversation.
func WithMemoryStore ¶ added in v0.11.0
func WithMemoryStore(memory Memory) ConversationOption
WithMemoryStore sets a custom memory store for the conversation.
func WithSystemMessage ¶ added in v0.11.0
func WithSystemMessage(system string) ConversationOption
WithSystemMessage sets a system message for the conversation.
type EmbeddingInput ¶
type EmbeddingInput struct {
Text string `json:"text"`
ID string `json:"id,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
EmbeddingInput represents a single text to embed with optional metadata.
type EmbeddingProvider ¶
type EmbeddingProvider interface {
// CreateEmbeddings generates embeddings for the given input texts.
CreateEmbeddings(ctx context.Context, req *EmbeddingRequest) (*EmbeddingResponse, error)
}
EmbeddingProvider is an optional interface for providers that support embeddings.
type EmbeddingRequest ¶
type EmbeddingRequest struct {
Model ModelID `json:"model"`
Input []EmbeddingInput `json:"input"`
EncodingFormat EncodingFormat `json:"encoding_format,omitempty"`
Dimensions *int `json:"dimensions,omitempty"`
User string `json:"user,omitempty"`
InputType InputType `json:"input_type,omitempty"`
OutputDType OutputDType `json:"output_dtype,omitempty"`
Truncation *bool `json:"truncation,omitempty"`
}
EmbeddingRequest represents a request to generate embeddings.
type EmbeddingResponse ¶
type EmbeddingResponse struct {
Vectors []EmbeddingVector `json:"vectors"`
Model ModelID `json:"model"`
Usage EmbeddingUsage `json:"usage"`
}
EmbeddingResponse contains the generated embeddings.
type EmbeddingUsage ¶
type EmbeddingUsage struct {
PromptTokens int `json:"prompt_tokens"`
TotalTokens int `json:"total_tokens"`
}
EmbeddingUsage tracks token consumption for embeddings.
type EmbeddingVector ¶
type EmbeddingVector struct {
Index int `json:"index"`
ID string `json:"id,omitempty"`
Vector []float32 `json:"vector,omitempty"`
VectorB64 string `json:"vector_b64,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
EmbeddingVector represents a single embedding result.
type EncodingFormat ¶
type EncodingFormat string
EncodingFormat specifies the embedding output format.
const ( // EncodingFormatFloat returns embeddings as float arrays. EncodingFormatFloat EncodingFormat = "float" // EncodingFormatBase64 returns embeddings as base64-encoded strings. EncodingFormatBase64 EncodingFormat = "base64" )
type Feature ¶
type Feature string
Feature represents a capability that a provider may support.
const ( FeatureChat Feature = "chat" FeatureChatStreaming Feature = "chat_streaming" FeatureToolCalling Feature = "tool_calling" FeatureReasoning Feature = "reasoning" FeatureBuiltInTools Feature = "builtin_tools" FeatureResponseChain Feature = "response_chain" FeatureEmbeddings Feature = "embeddings" FeatureContextualizedEmbeddings Feature = "contextualized_embeddings" FeatureReranking Feature = "reranking" )
const FeatureImageGeneration Feature = "image_generation"
FeatureImageGeneration indicates support for image generation.
type FileSearchResources ¶
type FileSearchResources struct {
VectorStoreIDs []string `json:"vector_store_ids"`
}
FileSearchResources contains vector store IDs for file search.
type ImageAction ¶
type ImageAction string
ImageAction represents the action to take (for Responses API).
const ( ImageActionAuto ImageAction = "auto" ImageActionGenerate ImageAction = "generate" ImageActionEdit ImageAction = "edit" )
type ImageBackground ¶
type ImageBackground string
ImageBackground represents the background style.
const ( ImageBackgroundOpaque ImageBackground = "opaque" ImageBackgroundTransparent ImageBackground = "transparent" ImageBackgroundAuto ImageBackground = "auto" )
type ImageChunk ¶
type ImageChunk struct {
PartialImageIndex int `json:"partial_image_index"`
B64JSON string `json:"b64_json"`
}
ImageChunk represents an incremental streaming response for images.
type ImageData ¶
type ImageData struct {
B64JSON string `json:"b64_json,omitempty"`
URL string `json:"url,omitempty"`
RevisedPrompt string `json:"revised_prompt,omitempty"`
}
ImageData represents a single generated image.
type ImageDetail ¶
type ImageDetail string
ImageDetail specifies the level of detail for image processing.
const ( // ImageDetailAuto lets the model decide the appropriate detail level. ImageDetailAuto ImageDetail = "auto" // ImageDetailLow uses fewer tokens for faster processing. ImageDetailLow ImageDetail = "low" // ImageDetailHigh uses more tokens for detailed analysis. ImageDetailHigh ImageDetail = "high" )
type ImageEditRequest ¶
type ImageEditRequest struct {
Model ModelID `json:"model"`
Prompt string `json:"prompt"`
// Image inputs - at least one required
Images []ImageInput `json:"-"` // Handled separately for multipart
// Optional mask for inpainting
Mask *ImageInput `json:"-"`
// Optional parameters
N int `json:"n,omitempty"`
Size ImageSize `json:"size,omitempty"`
Quality ImageQuality `json:"quality,omitempty"`
Format ImageFormat `json:"output_format,omitempty"`
Compression *int `json:"output_compression,omitempty"`
Background ImageBackground `json:"background,omitempty"`
InputFidelity ImageInputFidelity `json:"input_fidelity,omitempty"`
User string `json:"user,omitempty"`
}
ImageEditRequest represents a request to edit images.
type ImageFormat ¶
type ImageFormat string
ImageFormat represents the output file format.
const ( ImageFormatPNG ImageFormat = "png" ImageFormatJPEG ImageFormat = "jpeg" ImageFormatWebP ImageFormat = "webp" )
func (ImageFormat) IsValid ¶
func (f ImageFormat) IsValid() bool
IsValid reports whether the image format is a recognized value.
type ImageGenerateRequest ¶
type ImageGenerateRequest struct {
Model ModelID `json:"model"`
Prompt string `json:"prompt"`
// Optional parameters
N int `json:"n,omitempty"` // Number of images to generate (default 1)
Size ImageSize `json:"size,omitempty"` // Image dimensions
Quality ImageQuality `json:"quality,omitempty"` // Rendering quality
Format ImageFormat `json:"output_format,omitempty"` // Output format
Compression *int `json:"output_compression,omitempty"` // 0-100 for jpeg/webp
Background ImageBackground `json:"background,omitempty"` // Transparency setting
Moderation string `json:"moderation,omitempty"` // "auto" or "low"
User string `json:"user,omitempty"` // User identifier
ResponseFormat string `json:"response_format,omitempty"` // "b64_json" or "url"
PartialImages int `json:"partial_images,omitempty"` // For streaming (0-3)
}
ImageGenerateRequest represents a request to generate images.
type ImageGenerator ¶
type ImageGenerator interface {
// GenerateImage generates images from a text prompt.
GenerateImage(ctx context.Context, req *ImageGenerateRequest) (*ImageResponse, error)
// EditImage edits existing images using a prompt and optional mask.
EditImage(ctx context.Context, req *ImageEditRequest) (*ImageResponse, error)
// StreamImage generates images with streaming partial results.
// Not all providers support streaming.
StreamImage(ctx context.Context, req *ImageGenerateRequest) (*ImageStream, error)
}
ImageGenerator is an optional interface for providers that support image generation.
type ImageInput ¶
type ImageInput struct {
// One of these must be set
Data []byte // Raw image bytes
Base64 string // Base64-encoded image
URL string // URL to fetch image from (Responses API only)
FileID string // File ID from Files API (Responses API only)
Filename string // Optional filename hint
}
ImageInput represents an input image for editing.
func (ImageInput) GetBytes ¶
func (i ImageInput) GetBytes() ([]byte, error)
GetBytes returns the image data as bytes.
type ImageInputFidelity ¶
type ImageInputFidelity string
ImageInputFidelity represents the input image preservation level.
const ( ImageInputFidelityLow ImageInputFidelity = "low" ImageInputFidelityHigh ImageInputFidelity = "high" )
type ImageQuality ¶
type ImageQuality string
ImageQuality represents the rendering quality level.
const ( ImageQualityLow ImageQuality = "low" ImageQualityMedium ImageQuality = "medium" ImageQualityHigh ImageQuality = "high" ImageQualityAuto ImageQuality = "auto" )
func (ImageQuality) IsValid ¶
func (q ImageQuality) IsValid() bool
IsValid reports whether the image quality is a recognized value.
type ImageResponse ¶
type ImageResponse struct {
Created int64 `json:"created"`
Data []ImageData `json:"data"`
Usage *ImageUsage `json:"usage,omitempty"`
}
ImageResponse represents a response containing generated images.
type ImageStream ¶
type ImageStream struct {
Ch <-chan ImageChunk // Partial images
Err <-chan error // At most one error
Final <-chan *ImageResponse // Complete response
}
ImageStream represents a streaming image generation response.
type ImageUsage ¶
type ImageUsage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
TotalTokens int `json:"total_tokens"`
}
ImageUsage tracks token usage for image generation.
type InMemoryStore ¶ added in v0.11.0
type InMemoryStore struct {
// contains filtered or unexported fields
}
InMemoryStore is a thread-safe in-memory implementation of the Memory interface. Suitable for single-session conversations that don't require persistence.
func NewInMemoryStore ¶ added in v0.11.0
func NewInMemoryStore() *InMemoryStore
NewInMemoryStore creates a new in-memory conversation store.
func (*InMemoryStore) AddMessage ¶ added in v0.11.0
func (m *InMemoryStore) AddMessage(msg Message)
AddMessage appends a message to the conversation history.
func (*InMemoryStore) AddMessages ¶ added in v0.11.0
func (m *InMemoryStore) AddMessages(msgs []Message)
AddMessages appends multiple messages to the conversation history.
func (*InMemoryStore) Clear ¶ added in v0.11.0
func (m *InMemoryStore) Clear()
Clear removes all messages from the conversation.
func (*InMemoryStore) GetHistory ¶ added in v0.11.0
func (m *InMemoryStore) GetHistory() []Message
GetHistory returns all messages in the conversation. Returns a copy of the messages slice.
func (*InMemoryStore) GetLastN ¶ added in v0.11.0
func (m *InMemoryStore) GetLastN(n int) []Message
GetLastN returns the last N messages in the conversation. If N is greater than the number of messages, returns all messages.
func (*InMemoryStore) Len ¶ added in v0.11.0
func (m *InMemoryStore) Len() int
Len returns the number of messages in the conversation.
func (*InMemoryStore) SetMessages ¶ added in v0.11.0
func (m *InMemoryStore) SetMessages(msgs []Message)
SetMessages replaces the entire conversation history.
type InputFile ¶
type InputFile struct {
// FileID is a file ID from the Files API.
FileID string
// FileURL is an HTTPS URL to the file.
FileURL string
// FileData contains base64-encoded file bytes.
FileData string
// Filename is the recommended filename when using FileData.
Filename string
}
InputFile represents file content in a multimodal message.
func (InputFile) ContentType ¶
ContentType returns the type identifier for InputFile.
type InputImage ¶
type InputImage struct {
// ImageURL is an HTTPS URL or data URL (data:image/jpeg;base64,...).
ImageURL string
// FileID is a file ID from the Files API.
FileID string
// Detail specifies the level of detail for image processing.
Detail ImageDetail
}
InputImage represents image content in a multimodal message.
func (InputImage) ContentType ¶
func (i InputImage) ContentType() string
ContentType returns the type identifier for InputImage.
type InputText ¶
type InputText struct {
// Text is the text content.
Text string
}
InputText represents text content in a multimodal message.
func (InputText) ContentType ¶
ContentType returns the type identifier for InputText.
type InputType ¶
type InputType string
InputType specifies the type of input for retrieval optimization.
const ( // InputTypeNone uses default embedding without retrieval optimization. InputTypeNone InputType = "" // InputTypeQuery optimizes embeddings for search queries. InputTypeQuery InputType = "query" // InputTypeDocument optimizes embeddings for documents being searched. InputTypeDocument InputType = "document" )
type IterationEndEvent ¶ added in v0.11.0
type IterationEndEvent struct {
Iteration int
Response *ChatResponse
ToolCalls []ToolCall
Duration time.Duration
TokensUsed TokenUsage
}
IterationEndEvent is emitted after each LLM response.
type IterationStartEvent ¶ added in v0.11.0
type IterationStartEvent struct {
Iteration int
MessageCount int
ToolCount int // Tools executed so far
}
IterationStartEvent is emitted at the start of each LLM call.
type Memory ¶ added in v0.11.0
type Memory interface {
// AddMessage appends a message to the conversation history.
AddMessage(msg Message)
// AddMessages appends multiple messages to the conversation history.
AddMessages(msgs []Message)
// GetHistory returns all messages in the conversation.
GetHistory() []Message
// GetLastN returns the last N messages in the conversation.
GetLastN(n int) []Message
// Clear removes all messages from the conversation.
Clear()
// Len returns the number of messages in the conversation.
Len() int
// SetMessages replaces the entire conversation history.
SetMessages(msgs []Message)
}
Memory is the interface for managing conversation history. Implementations provide different storage backends (in-memory, Redis, PostgreSQL, etc.).
type MemoryConfig ¶ added in v0.11.0
type MemoryConfig struct {
// MaxTokens is the target maximum tokens for the conversation.
// When exceeded, auto-summarization is triggered.
// Default: 0 (no limit - use provider's context window)
MaxTokens int
// SummarizationThreshold triggers summarization when token count
// exceeds MaxTokens * SummarizationThreshold.
// Default: 0.8 (summarize when 80% full)
SummarizationThreshold float64
// SummarizationPrompt is the system prompt used for summarization.
// Default: built-in prompt optimized for agent context preservation
SummarizationPrompt string
// PreserveLastN keeps the N most recent messages unsummarized.
// Default: 4 (keep last 2 turns)
PreserveLastN int
// OnSummarize is called when summarization occurs.
OnSummarize func(ctx context.Context, e SummarizationEvent)
}
MemoryConfig configures conversation memory management.
func DefaultMemoryConfig ¶ added in v0.11.0
func DefaultMemoryConfig() *MemoryConfig
DefaultMemoryConfig returns sensible defaults for memory management.
type Message ¶
type Message struct {
Role Role `json:"role"`
Content string `json:"content,omitempty"`
Parts []ContentPart `json:"-"` // Multimodal content parts (Responses API only)
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // For assistant messages requesting tools
ToolResults []ToolResult `json:"tool_results,omitempty"` // For tool result messages (RoleTool)
}
Message represents a single message in a conversation. For simple text messages, use Content. For multimodal messages, use Parts. If Parts is non-empty, Content is ignored.
type MessageBuilder ¶
type MessageBuilder struct {
// contains filtered or unexported fields
}
MessageBuilder provides a fluent API for building multimodal messages.
func (*MessageBuilder) Done ¶
func (m *MessageBuilder) Done() *ChatBuilder
Done completes the message and returns to the ChatBuilder.
func (*MessageBuilder) FileBase64 ¶
func (m *MessageBuilder) FileBase64(filename, base64Data string) *MessageBuilder
FileBase64 adds a file with base64-encoded content.
func (*MessageBuilder) FileID ¶
func (m *MessageBuilder) FileID(fileID string) *MessageBuilder
FileID adds a file by file ID from the Files API.
func (*MessageBuilder) FileURL ¶
func (m *MessageBuilder) FileURL(url string) *MessageBuilder
FileURL adds a file by URL.
func (*MessageBuilder) ImageFileID ¶
func (m *MessageBuilder) ImageFileID(fileID string) *MessageBuilder
ImageFileID adds an image by file ID from the Files API.
func (*MessageBuilder) ImageFileIDWithDetail ¶
func (m *MessageBuilder) ImageFileIDWithDetail(fileID string, detail ImageDetail) *MessageBuilder
ImageFileIDWithDetail adds an image by file ID with a specific detail level.
func (*MessageBuilder) ImageURL ¶
func (m *MessageBuilder) ImageURL(url string) *MessageBuilder
ImageURL adds an image by URL (HTTPS or data URL).
func (*MessageBuilder) ImageURLWithDetail ¶
func (m *MessageBuilder) ImageURLWithDetail(url string, detail ImageDetail) *MessageBuilder
ImageURLWithDetail adds an image by URL with a specific detail level.
func (*MessageBuilder) Text ¶
func (m *MessageBuilder) Text(s string) *MessageBuilder
Text adds a text content part to the message.
type ModelID ¶
type ModelID string
ModelID is a string identifier for a model. Using string avoids coupling to provider-specific enums.
type ModelInfo ¶
type ModelInfo struct {
ID ModelID `json:"id"`
DisplayName string `json:"display_name"`
Capabilities []Feature `json:"capabilities"`
APIEndpoint APIEndpoint `json:"api_endpoint,omitempty"` // defaults to completions
}
ModelInfo describes a model available from a provider.
func (ModelInfo) GetAPIEndpoint ¶
func (m ModelInfo) GetAPIEndpoint() APIEndpoint
GetAPIEndpoint returns the API endpoint for the model, defaulting to completions.
func (ModelInfo) HasCapability ¶
HasCapability reports whether the model supports the given feature.
type NoopTelemetryHook ¶
type NoopTelemetryHook struct{}
NoopTelemetryHook is a no-op implementation of TelemetryHook. Use this as a default when no telemetry is configured.
func (NoopTelemetryHook) OnRequestEnd ¶
func (NoopTelemetryHook) OnRequestEnd(RequestEndEvent)
OnRequestEnd does nothing.
func (NoopTelemetryHook) OnRequestStart ¶
func (NoopTelemetryHook) OnRequestStart(RequestStartEvent)
OnRequestStart does nothing.
type OutputDType ¶
type OutputDType string
OutputDType specifies the data type for embedding vectors.
const ( // OutputDTypeFloat returns 32-bit floating point numbers (default). OutputDTypeFloat OutputDType = "float" // OutputDTypeInt8 returns 8-bit signed integers (-128 to 127). OutputDTypeInt8 OutputDType = "int8" // OutputDTypeUint8 returns 8-bit unsigned integers (0 to 255). OutputDTypeUint8 OutputDType = "uint8" // OutputDTypeBinary returns bit-packed signed integers. OutputDTypeBinary OutputDType = "binary" // OutputDTypeUbinary returns bit-packed unsigned integers. OutputDTypeUbinary OutputDType = "ubinary" )
type Provider ¶
type Provider interface {
// ID returns the provider identifier (e.g., "openai", "anthropic").
ID() string
// Models returns the list of models available from this provider.
Models() []ModelInfo
// Supports reports whether the provider supports the given feature.
Supports(feature Feature) bool
// Chat sends a non-streaming chat request.
Chat(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
// StreamChat sends a streaming chat request.
StreamChat(ctx context.Context, req *ChatRequest) (*ChatStream, error)
}
Provider is the interface that LLM providers must implement. Providers SHOULD be safe for concurrent calls. If a provider cannot be concurrent-safe, it MUST document this.
type ProviderError ¶
type ProviderError struct {
Provider string
Status int
RequestID string
Code string
Message string
Err error
}
ProviderError represents an error returned by a provider with full context.
func (*ProviderError) Error ¶
func (e *ProviderError) Error() string
Error implements the error interface.
func (*ProviderError) Unwrap ¶
func (e *ProviderError) Unwrap() error
Unwrap returns the underlying error for error chaining.
type ReasoningEffort ¶
type ReasoningEffort string
ReasoningEffort represents the level of reasoning effort for models that support it.
const ( ReasoningEffortNone ReasoningEffort = "none" ReasoningEffortLow ReasoningEffort = "low" ReasoningEffortMedium ReasoningEffort = "medium" ReasoningEffortHigh ReasoningEffort = "high" ReasoningEffortXHigh ReasoningEffort = "xhigh" )
type ReasoningOutput ¶
ReasoningOutput contains reasoning information from the model.
type RequestEndEvent ¶
type RequestEndEvent struct {
Provider string // Provider identifier
Model ModelID // Model that was called
Start time.Time // When the request started
End time.Time // When the request completed
Usage TokenUsage // Token consumption
Err error // Error if request failed, nil on success
}
RequestEndEvent contains metadata about a completed request.
Security ¶
This struct intentionally excludes:
- API keys (never logged)
- Response content (may be sensitive)
- Raw HTTP response data
The Err field contains error types, not raw error messages from providers which might inadvertently include sensitive data.
func (RequestEndEvent) Duration ¶
func (e RequestEndEvent) Duration() time.Duration
Duration returns the elapsed time for the request.
type RequestStartEvent ¶
type RequestStartEvent struct {
Provider string // Provider identifier (e.g., "openai", "anthropic")
Model ModelID // Model being called
Start time.Time // When the request started
}
RequestStartEvent contains metadata about a starting request.
Security ¶
This struct intentionally excludes:
- API keys (never logged)
- Prompt/message content (privacy sensitive)
- Request headers (may contain auth tokens)
Only operational metadata suitable for logging is included.
type RerankRequest ¶
type RerankRequest struct {
// Model specifies the reranker model to use.
Model ModelID `json:"model"`
// Query is the search query to rank documents against.
Query string `json:"query"`
// Documents is the list of documents to rerank.
Documents []string `json:"documents"`
// TopK limits the number of results to return. If nil, returns all.
TopK *int `json:"top_k,omitempty"`
// ReturnDocuments includes document text in the response if true.
ReturnDocuments bool `json:"return_documents,omitempty"`
// Truncation controls whether to truncate inputs exceeding context length.
// Defaults to true.
Truncation *bool `json:"truncation,omitempty"`
}
RerankRequest represents a request to rerank documents.
type RerankResponse ¶
type RerankResponse struct {
// Results contains the reranked documents sorted by descending relevance.
Results []RerankResult `json:"results"`
// Model is the model that performed the reranking.
Model ModelID `json:"model"`
// Usage contains token consumption information.
Usage RerankUsage `json:"usage"`
}
RerankResponse contains the reranking results.
type RerankResult ¶
type RerankResult struct {
// Index is the original position in the input documents slice.
Index int `json:"index"`
// RelevanceScore is the relevance score (higher is more relevant).
RelevanceScore float64 `json:"relevance_score"`
// Document contains the document text if ReturnDocuments was true.
Document string `json:"document,omitempty"`
}
RerankResult represents a single document's reranking result.
type RerankUsage ¶
type RerankUsage struct {
// TotalTokens is the total number of tokens used.
TotalTokens int `json:"total_tokens"`
}
RerankUsage tracks token consumption for a rerank request.
type RerankerProvider ¶
type RerankerProvider interface {
// Rerank scores and sorts documents by relevance to the query.
// Results are returned in descending order of relevance.
Rerank(ctx context.Context, req *RerankRequest) (*RerankResponse, error)
}
RerankerProvider is an optional interface for providers that support semantic reranking of documents based on query relevance.
type RetryConfig ¶
type RetryConfig struct {
MaxRetries int // Maximum number of retry attempts (default: 3)
BaseDelay time.Duration // Initial delay before first retry (default: 1s)
MaxDelay time.Duration // Maximum delay cap (default: 30s)
Jitter float64 // Jitter factor 0.0-1.0 (default: 0.2)
}
RetryConfig configures retry behavior.
type RetryPolicy ¶
type RetryPolicy interface {
// NextDelay returns the delay before the next retry attempt and whether to retry.
// If ok is false, no more retries should be attempted.
// attempt starts at 0 for the first retry after the initial failure.
NextDelay(attempt int, err error) (delay time.Duration, ok bool)
}
RetryPolicy determines retry behavior for failed requests.
func DefaultRetryPolicy ¶
func DefaultRetryPolicy() RetryPolicy
DefaultRetryPolicy returns a retry policy with sensible defaults. Uses exponential backoff with jitter, max 3 retries, 30s max delay.
func NewRetryPolicy ¶
func NewRetryPolicy(cfg RetryConfig) RetryPolicy
NewRetryPolicy creates a retry policy with the given configuration.
type Secret ¶ added in v0.10.0
type Secret struct {
// contains filtered or unexported fields
}
Secret wraps a sensitive string value with protection against accidental logging. The underlying value is never exposed through String(), GoString(), or JSON marshaling.
Use Expose() to access the actual value when needed (e.g., for HTTP headers).
Example:
secret := NewSecret("sk-abc123")
fmt.Println(secret) // prints: [REDACTED]
fmt.Printf("%#v", secret) // prints: core.Secret{[REDACTED]}
secret.Expose() // returns: "sk-abc123"
func (Secret) Expose ¶ added in v0.10.0
Expose returns the actual secret value. Use this only when the value is genuinely needed (e.g., for authentication headers).
Security note: Be careful not to log or serialize the returned value.
func (Secret) GoString ¶ added in v0.10.0
GoString returns a redacted placeholder for %#v formatting. Implements fmt.GoStringer.
func (Secret) MarshalJSON ¶ added in v0.10.0
MarshalJSON returns a redacted JSON string. This prevents accidental JSON serialization of the secret value.
func (Secret) MarshalText ¶ added in v0.10.0
MarshalText returns a redacted text representation. This prevents accidental text serialization (e.g., in YAML). Implements encoding.TextMarshaler.
type SummarizationEvent ¶ added in v0.11.0
type SummarizationEvent struct {
OriginalTokens int
SummarizedTokens int
MessagesRemoved int
Summary string
}
SummarizationEvent is emitted when auto-summarization occurs.
type TelemetryHook ¶
type TelemetryHook interface {
// OnRequestStart is called when a request to a provider begins.
OnRequestStart(e RequestStartEvent)
// OnRequestEnd is called when a request to a provider completes.
OnRequestEnd(e RequestEndEvent)
}
TelemetryHook receives notifications about request lifecycle events. Implementations can use this for logging, metrics, tracing, etc.
Security Considerations ¶
Event types are designed to NEVER include sensitive data:
- API keys are NEVER included (stored separately as core.Secret)
- Prompt content (user messages) is NEVER included
- Response content (model outputs) is NEVER included
- Only operational metadata is exposed (provider, model, timing, token counts)
This design ensures that telemetry data can be safely:
- Logged to disk without risk of credential exposure
- Sent to external monitoring systems
- Aggregated for analytics
- Stored long-term for debugging
If extending this interface, maintain these security properties. New event types must undergo security review before merge. Never add fields that could contain: API keys, user prompts, model responses, or any other potentially sensitive content.
type TokenUsage ¶
type TokenUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
TokenUsage tracks token consumption for a request.
type Tool ¶
Tool is a placeholder interface for tool definitions. Full implementation is in the tools package (Task 07).
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments"`
}
ToolCall represents a tool invocation requested by the model. Arguments MUST be valid JSON bytes and MUST preserve raw JSON (no reformatting).
type ToolCallEndEvent ¶ added in v0.11.0
type ToolCallEndEvent struct {
Iteration int
ToolCall ToolCall
Result any
Error error
Duration time.Duration
}
ToolCallEndEvent is emitted after each tool execution.
type ToolCallStartEvent ¶ added in v0.11.0
type ToolCallStartEvent struct {
Iteration int
ToolCall ToolCall
Index int // Index within this iteration's tool calls
Total int // Total tool calls in this iteration
}
ToolCallStartEvent is emitted before executing each tool.
type ToolExecution ¶ added in v0.11.0
type ToolExecution struct {
Iteration int
Call ToolCall
Result any
Error error
Duration time.Duration
Timestamp time.Time
}
ToolExecution records a single tool call and its result.
type ToolExecutor ¶ added in v0.11.0
type ToolExecutor interface {
// Execute finds a tool by name and calls it with the given arguments.
Execute(ctx context.Context, name string, args json.RawMessage) (any, error)
}
ToolExecutor is an interface for executing tools by name. This interface is implemented by tools.Registry.
type ToolResources ¶
type ToolResources struct {
FileSearch *FileSearchResources `json:"file_search,omitempty"`
}
ToolResources contains configuration for built-in tools.
type ToolResult ¶ added in v0.11.0
type ToolResult struct {
CallID string `json:"call_id"` // Must match ToolCall.ID from the response
Content any `json:"content"` // Result data (will be JSON marshaled)
IsError bool `json:"is_error"` // True if this represents an error
}
ToolResult represents the outcome of executing a tool. Use this for untyped tool results where the Content can be any JSON-serializable value.
type ToolResultBuilder ¶ added in v0.11.0
type ToolResultBuilder struct {
// contains filtered or unexported fields
}
ToolResultBuilder provides a fluent API for constructing tool results.
func NewToolResults ¶ added in v0.11.0
func NewToolResults() *ToolResultBuilder
NewToolResults creates a new builder for tool results.
func (*ToolResultBuilder) Build ¶ added in v0.11.0
func (b *ToolResultBuilder) Build() []ToolResult
Build returns the accumulated results.
func (*ToolResultBuilder) Error ¶ added in v0.11.0
func (b *ToolResultBuilder) Error(callID string, err error) *ToolResultBuilder
Error adds a failed tool result.
func (*ToolResultBuilder) FromExecution ¶ added in v0.11.0
func (b *ToolResultBuilder) FromExecution(callID string, result any, err error) *ToolResultBuilder
FromExecution adds a result from a tool execution, handling both success and error cases.
func (*ToolResultBuilder) Success ¶ added in v0.11.0
func (b *ToolResultBuilder) Success(callID string, content any) *ToolResultBuilder
Success adds a successful tool result.
type TypedToolResult ¶ added in v0.11.0
type TypedToolResult[T any] struct { CallID string `json:"call_id"` Content T `json:"content"` IsError bool `json:"is_error"` }
TypedToolResult is a type-safe tool result with compile-time type checking. Use this when you want type safety for tool results.
func (TypedToolResult[T]) ToUntyped ¶ added in v0.11.0
func (r TypedToolResult[T]) ToUntyped() ToolResult
ToUntyped converts a typed result to the untyped ToolResult for use with ChatBuilder.
type TypedToolResultBuilder ¶ added in v0.11.0
type TypedToolResultBuilder[T any] struct { // contains filtered or unexported fields }
TypedToolResultBuilder provides a type-safe fluent API for constructing tool results.
func NewTypedToolResults ¶ added in v0.11.0
func NewTypedToolResults[T any]() *TypedToolResultBuilder[T]
NewTypedToolResults creates a new type-safe builder for tool results.
func (*TypedToolResultBuilder[T]) Build ¶ added in v0.11.0
func (b *TypedToolResultBuilder[T]) Build() []ToolResult
Build returns the accumulated results as untyped ToolResults for use with ChatBuilder.
func (*TypedToolResultBuilder[T]) Success ¶ added in v0.11.0
func (b *TypedToolResultBuilder[T]) Success(callID string, content T) *TypedToolResultBuilder[T]
Success adds a successful typed tool result.