Documentation
¶
Index ¶
- func SimpleAI(ctx context.Context, prompt string) (string, error)
- func StructuredAI(ctx context.Context, prompt string, schema interface{}, dest interface{}) error
- type AudioData
- type AudioRequest
- type CallFunc
- type Choice
- type Client
- func (c *Client) Complete(ctx context.Context, prompt string, opts ...Option) (*Response, error)
- func (c *Client) CompleteWithMessages(ctx context.Context, messages []Message, opts ...Option) (*Response, error)
- func (c *Client) ExecuteToolCallLoop(ctx context.Context, messages []Message, tools []ToolDefinition, ...) (*Response, *ToolCallTrace, error)
- func (c *Client) ExecuteToolCallLoopResult(ctx context.Context, messages []Message, tools []ToolDefinition, ...) (*ToolCallResult, error)
- func (c *Client) StreamComplete(ctx context.Context, prompt string, opts ...Option) (<-chan StreamChunk, <-chan error)
- type Config
- type ContentPart
- type ErrorDetail
- type ErrorResponse
- type FileData
- type ImageConfig
- type ImageData
- type ImageRequest
- type ImageURLData
- type InputAudioData
- type InputFileData
- type JSONSchema
- type MediaProvider
- type MediaResponse
- type MediaRouter
- type Message
- type MessageDelta
- type OpenRouterMediaProvider
- func (p *OpenRouterMediaProvider) GenerateAudio(ctx context.Context, req AudioRequest) (*MediaResponse, error)
- func (p *OpenRouterMediaProvider) GenerateImage(ctx context.Context, req ImageRequest) (*MediaResponse, error)
- func (p *OpenRouterMediaProvider) GenerateVideo(ctx context.Context, req VideoRequest) (*MediaResponse, error)
- func (p *OpenRouterMediaProvider) Name() string
- func (p *OpenRouterMediaProvider) SupportedModalities() []string
- type Option
- func WithAPIKey(apiKey string) Option
- func WithAudioFile(path string, mediaType string) Option
- func WithAudioURL(url string, mediaType string) Option
- func WithFile(path string, mediaType string) Option
- func WithImageBytes(data []byte, mimeType string) Option
- func WithImageFile(path string) Option
- func WithImageURL(url string) Option
- func WithJSONMode() Option
- func WithMaxTokens(tokens int) Option
- func WithModel(model string) Option
- func WithSchema(schema interface{}) Option
- func WithStream() Option
- func WithSystem(content string) Option
- func WithTemperature(temp float64) Option
- func WithTools(tools []ToolDefinition) Option
- type PromptConfig
- type Request
- type Response
- type ResponseFormat
- type SSEDecoder
- type StreamChunk
- type StreamDelta
- type ToolCall
- type ToolCallConfig
- type ToolCallFunction
- type ToolCallRecord
- type ToolCallResult
- type ToolCallTrace
- type ToolDefinition
- type ToolFunction
- type Usage
- type VideoData
- type VideoRequest
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AudioData ¶
type AudioData struct {
Data string `json:"data,omitempty"`
Format string `json:"format"`
URL string `json:"url,omitempty"`
}
AudioData holds data for generated audio.
type AudioRequest ¶
type AudioRequest struct {
Text string `json:"text"`
Model string `json:"model,omitempty"`
Voice string `json:"voice,omitempty"`
Format string `json:"format,omitempty"`
}
AudioRequest holds parameters for audio generation.
type CallFunc ¶
type CallFunc func(ctx context.Context, target string, input map[string]interface{}) (map[string]interface{}, error)
CallFunc is the function signature for dispatching tool calls. It maps to agent.Call(ctx, target, input).
type Choice ¶
type Choice struct {
Index int `json:"index"`
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
}
Choice represents a completion choice.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client provides AI/LLM capabilities using OpenAI or OpenRouter API.
func (*Client) CompleteWithMessages ¶
func (c *Client) CompleteWithMessages(ctx context.Context, messages []Message, opts ...Option) (*Response, error)
CompleteWithMessages makes a chat completion request with custom messages.
func (*Client) ExecuteToolCallLoop ¶
func (c *Client) ExecuteToolCallLoop( ctx context.Context, messages []Message, tools []ToolDefinition, config ToolCallConfig, callFn CallFunc, opts ...Option, ) (*Response, *ToolCallTrace, error)
ExecuteToolCallLoop runs the LLM tool-call loop: send messages with tools, dispatch any tool calls via callFn, feed results back, repeat until the LLM produces a final text response or limits are reached.
func (*Client) ExecuteToolCallLoopResult ¶
func (c *Client) ExecuteToolCallLoopResult( ctx context.Context, messages []Message, tools []ToolDefinition, config ToolCallConfig, callFn CallFunc, opts ...Option, ) (*ToolCallResult, error)
ExecuteToolCallLoopResult runs the tool-call loop and returns a wrapped response plus the full execution trace.
func (*Client) StreamComplete ¶
func (c *Client) StreamComplete(ctx context.Context, prompt string, opts ...Option) (<-chan StreamChunk, <-chan error)
StreamComplete makes a streaming chat completion request. Returns a channel of response chunks.
type Config ¶
type Config struct {
// API Key for OpenAI or OpenRouter
APIKey string
// BaseURL can be either OpenAI or OpenRouter endpoint
// Default: https://api.openai.com/v1
// OpenRouter: https://openrouter.ai/api/v1
BaseURL string
// Default model to use (e.g., "gpt-4o", "openai/gpt-4o" for OpenRouter)
Model string
// Default temperature for responses (0.0 to 2.0)
Temperature float64
// Default max tokens for responses
MaxTokens int
// HTTP timeout for requests
Timeout time.Duration
// Optional: Site URL for OpenRouter rankings
SiteURL string
// Optional: Site name for OpenRouter rankings
SiteName string
}
Config holds AI/LLM configuration for making API calls.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a Config with sensible defaults. It reads from environment variables: - OPENAI_API_KEY or OPENROUTER_API_KEY - AI_BASE_URL (defaults to OpenAI) - AI_MODEL (defaults to gpt-4o)
func (*Config) IsOpenRouter ¶
IsOpenRouter returns true if the base URL is for OpenRouter.
type ContentPart ¶
type ContentPart struct {
Type string `json:"type"` // "text" or "image_url" or "input_audio" or "file"
Text string `json:"text,omitempty"`
ImageURL *ImageURLData `json:"image_url,omitempty"`
InputAudio *InputAudioData `json:"input_audio,omitempty"`
InputFile *InputFileData `json:"file,omitempty"`
}
type ErrorDetail ¶
type ErrorDetail struct {
Message string `json:"message"`
Type string `json:"type"`
Code string `json:"code,omitempty"`
}
ErrorDetail contains error information.
type ErrorResponse ¶
type ErrorResponse struct {
Error ErrorDetail `json:"error"`
}
ErrorResponse represents an error from the API.
type FileData ¶
type FileData struct {
URL string `json:"url,omitempty"`
Data string `json:"data,omitempty"`
MimeType string `json:"mime_type,omitempty"`
Filename string `json:"filename,omitempty"`
}
FileData holds data for a generated file.
type ImageConfig ¶
type ImageConfig struct {
AspectRatio string `json:"aspect_ratio,omitempty"`
ImageSize string `json:"image_size,omitempty"`
SuperResolutionReferences []string `json:"super_resolution_references,omitempty"`
}
ImageConfig holds OpenRouter-specific image configuration.
type ImageData ¶
type ImageData struct {
URL string `json:"url,omitempty"`
B64JSON string `json:"b64_json,omitempty"`
RevisedPrompt string `json:"revised_prompt,omitempty"`
}
ImageData holds data for a generated image.
type ImageRequest ¶
type ImageRequest struct {
Prompt string `json:"prompt"`
Model string `json:"model,omitempty"`
Size string `json:"size,omitempty"`
Quality string `json:"quality,omitempty"`
ImageConfig *ImageConfig `json:"image_config,omitempty"`
}
ImageRequest holds parameters for image generation.
type ImageURLData ¶
ImageURLData holds the URL and optional detail level for image content parts.
type InputAudioData ¶
InputAudioData holds the encoded data and the format for the audio content parts.
type InputFileData ¶
type InputFileData struct {
FileData string `json:"file_data"`
}
InputFileData holds the file data for a generic file content parts.
type JSONSchema ¶
type JSONSchema struct {
Name string `json:"name"`
Strict bool `json:"strict"`
Schema json.RawMessage `json:"schema"`
}
JSONSchema defines the structure for structured outputs.
type MediaProvider ¶
type MediaProvider interface {
Name() string
SupportedModalities() []string
GenerateImage(ctx context.Context, req ImageRequest) (*MediaResponse, error)
GenerateAudio(ctx context.Context, req AudioRequest) (*MediaResponse, error)
GenerateVideo(ctx context.Context, req VideoRequest) (*MediaResponse, error)
}
MediaProvider defines the interface for media generation backends.
type MediaResponse ¶
type MediaResponse struct {
Text string `json:"text"`
Images []ImageData `json:"images,omitempty"`
Audio *AudioData `json:"audio,omitempty"`
Files []FileData `json:"files,omitempty"`
Videos []VideoData `json:"videos,omitempty"`
RawResponse any `json:"raw_response,omitempty"`
}
MediaResponse holds the result of a media generation call.
type MediaRouter ¶
type MediaRouter struct {
// contains filtered or unexported fields
}
MediaRouter dispatches (model, capability) pairs to the correct MediaProvider.
func (*MediaRouter) Register ¶
func (r *MediaRouter) Register(prefix string, provider MediaProvider)
Register adds a provider with a model prefix. Longer prefixes match first.
func (*MediaRouter) Resolve ¶
func (r *MediaRouter) Resolve(model, capability string) (MediaProvider, error)
Resolve finds the provider for a model and capability.
type Message ¶
type Message struct {
Role string `json:"role"`
Content []ContentPart `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
func (Message) MarshalJSON ¶
MarshalJSON serializes a Message. If the content is a single text part, it serializes content as a plain string for maximum API compatibility.
func (*Message) UnmarshalJSON ¶
type MessageDelta ¶
type MessageDelta struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
}
MessageDelta represents the incremental message content.
type OpenRouterMediaProvider ¶
OpenRouterMediaProvider implements MediaProvider for OpenRouter's media APIs.
func NewOpenRouterMediaProvider ¶
func NewOpenRouterMediaProvider(apiKey string) (*OpenRouterMediaProvider, error)
NewOpenRouterMediaProvider creates a provider. If apiKey is empty, reads OPENROUTER_API_KEY. Returns error if no API key is available.
func (*OpenRouterMediaProvider) GenerateAudio ¶
func (p *OpenRouterMediaProvider) GenerateAudio(ctx context.Context, req AudioRequest) (*MediaResponse, error)
GenerateAudio uses streaming chat completions with audio modality.
func (*OpenRouterMediaProvider) GenerateImage ¶
func (p *OpenRouterMediaProvider) GenerateImage(ctx context.Context, req ImageRequest) (*MediaResponse, error)
GenerateImage uses chat completions with image modality.
func (*OpenRouterMediaProvider) GenerateVideo ¶
func (p *OpenRouterMediaProvider) GenerateVideo(ctx context.Context, req VideoRequest) (*MediaResponse, error)
GenerateVideo submits a video job, polls until complete, downloads result.
func (*OpenRouterMediaProvider) Name ¶
func (p *OpenRouterMediaProvider) Name() string
func (*OpenRouterMediaProvider) SupportedModalities ¶
func (p *OpenRouterMediaProvider) SupportedModalities() []string
type Option ¶
Option is a functional option for configuring an AI request.
func WithAPIKey ¶
WithAPIKey overrides the client's configured API key for this request only.
func WithAudioFile ¶
WithAudioFile reads a local audio file, base64 encodes it, and attaches it to the request. The format string must be explicitly provided (e.g., "mp3", "wav").
func WithAudioURL ¶
WithAudioURL downloads an audio file from the provided URL, base64 encodes it, and attaches it to the request. The network connection is safely closed after reading. The format string must be explicitly provided (e.g., "mp3", "wav").
func WithFile ¶
WithFile attaches a generic file to the request and base64 encodes it. The mediaType must be explicitly provided by the caller.
Common supported MediaTypes for AI models include:
- "application/pdf" (PDF documents)
- "text/plain" (Standard text files)
- "text/csv" (CSV data)
- "text/markdown" (Markdown files)
- "application/json" (JSON files)
- "text/html" (HTML documents)
- "application/vnd.openxmlformats-officedocument.wordprocessingml.document" (Word .docx)
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" (Excel .xlsx)
func WithImageBytes ¶
WithImageBytes attaches an image from raw bytes (SDK encodes automatically).
func WithImageURL ¶
WithImageURL attaches an image from a remote URL.
func WithSchema ¶
func WithSchema(schema interface{}) Option
WithSchema enables structured output with a JSON schema. Accepts either a Go struct (will be converted to JSON schema) or json.RawMessage.
func WithSystem ¶
WithSystem adds a system message to the request.
func WithTemperature ¶
WithTemperature sets the temperature.
func WithTools ¶
func WithTools(tools []ToolDefinition) Option
WithTools sets tool definitions for the request.
type PromptConfig ¶
type PromptConfig struct {
// ToolCallLimitReached is sent back to the model when it asks for more tool
// calls than MaxToolCalls allows.
ToolCallLimitReached string
// ToolErrorFormatter formats tool execution failures before they are sent
// back to the model. It may return a raw string or any JSON-marshable value.
ToolErrorFormatter func(toolName string, err error) interface{}
// ToolResultFormatter formats successful tool results before they are sent
// back to the model. It may return a raw string or any JSON-marshable value.
ToolResultFormatter func(toolName string, result map[string]interface{}) interface{}
}
PromptConfig customizes the tool-call loop's tool-facing prompt content.
func DefaultPromptConfig ¶
func DefaultPromptConfig() PromptConfig
DefaultPromptConfig returns the default prompt content for tool execution.
type Request ¶
type Request struct {
// Messages for the chat completion
Messages []Message `json:"messages"`
// APIKeyOverride overrides the client's configured API key for this request only.
// This is used to support per-call api_key overrides for parity with the Python SDK.
APIKeyOverride string `json:"-"`
// Model to use (overrides default)
Model string `json:"model,omitempty"`
// Temperature (0.0 to 2.0)
Temperature *float64 `json:"temperature,omitempty"`
// Maximum tokens to generate
MaxTokens *int `json:"max_tokens,omitempty"`
// Enable streaming
Stream bool `json:"stream,omitempty"`
// Response format for structured outputs
ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
// Tools available for the model to call
Tools []ToolDefinition `json:"tools,omitempty"`
// ToolChoice controls how the model selects tools ("auto", "none", or specific)
ToolChoice interface{} `json:"tool_choice,omitempty"`
}
Request represents an AI completion request.
type Response ¶
type Response struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
}
Response represents the API response from OpenAI/OpenRouter.
func (*Response) HasToolCalls ¶
HasToolCalls returns true if the response contains tool calls.
type ResponseFormat ¶
type ResponseFormat struct {
Type string `json:"type"` // "json_object" or "json_schema"
JSONSchema *JSONSchema `json:"json_schema,omitempty"`
}
ResponseFormat specifies the desired output format.
type SSEDecoder ¶
type SSEDecoder struct {
// contains filtered or unexported fields
}
SSEDecoder decodes Server-Sent Events from a stream.
func NewSSEDecoder ¶
func NewSSEDecoder(r io.Reader) *SSEDecoder
NewSSEDecoder creates a new SSE decoder.
func (*SSEDecoder) Decode ¶
func (d *SSEDecoder) Decode() (StreamChunk, error)
Decode reads and decodes the next SSE chunk.
type StreamChunk ¶
type StreamChunk struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []StreamDelta `json:"choices"`
}
StreamChunk represents a streaming response chunk.
type StreamDelta ¶
type StreamDelta struct {
Index int `json:"index"`
Delta MessageDelta `json:"delta"`
FinishReason *string `json:"finish_reason"`
}
StreamDelta represents a delta in a streaming response.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"` // "function"
Function ToolCallFunction `json:"function"`
}
ToolCall represents a tool call made by the model.
type ToolCallConfig ¶
type ToolCallConfig struct {
MaxTurns int
MaxToolCalls int
SystemPrompt string
PromptConfig *PromptConfig
}
ToolCallConfig configures the tool-call execution loop.
func DefaultToolCallConfig ¶
func DefaultToolCallConfig() ToolCallConfig
DefaultToolCallConfig returns default configuration for the tool-call loop.
type ToolCallFunction ¶
ToolCallFunction contains the function name and arguments from a tool call.
type ToolCallRecord ¶
type ToolCallRecord struct {
ToolName string
Arguments map[string]interface{}
Result map[string]interface{}
Error string
LatencyMs float64
Turn int
}
ToolCallRecord records a single tool call for observability.
type ToolCallResult ¶
type ToolCallResult struct {
Response *Response
Trace *ToolCallTrace
}
ToolCallResult wraps a tool-call response and its execution trace.
func (*ToolCallResult) Text ¶
func (r *ToolCallResult) Text() string
Text returns the final text response from the tool-call loop.
type ToolCallTrace ¶
type ToolCallTrace struct {
Calls []ToolCallRecord
TotalTurns int
TotalToolCalls int
FinalResponse string
}
ToolCallTrace records the full trace of a tool-call loop.
type ToolDefinition ¶
type ToolDefinition struct {
Type string `json:"type"` // "function"
Function ToolFunction `json:"function"`
}
ToolDefinition describes a tool available to the model.
func CapabilitiesToToolDefinitions ¶
func CapabilitiesToToolDefinitions(capabilities []types.AgentCapability) []ToolDefinition
CapabilitiesToToolDefinitions converts discovery capabilities to tool definitions.
func CapabilityToToolDefinition ¶
func CapabilityToToolDefinition(cap interface{}) ToolDefinition
CapabilityToToolDefinition converts a ReasonerCapability or SkillCapability to a ToolDefinition.
type ToolFunction ¶
type ToolFunction struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]interface{} `json:"parameters"`
}
ToolFunction describes the function within a tool definition.
type Usage ¶
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
Usage represents token usage information.
type VideoData ¶
type VideoData struct {
URL string `json:"url,omitempty"`
Data string `json:"data,omitempty"`
MimeType string `json:"mime_type,omitempty"`
Filename string `json:"filename,omitempty"`
Duration float64 `json:"duration,omitempty"`
Resolution string `json:"resolution,omitempty"`
AspectRatio string `json:"aspect_ratio,omitempty"`
HasAudio bool `json:"has_audio,omitempty"`
CostUSD float64 `json:"cost_usd,omitempty"`
}
VideoData holds data for a generated video.
type VideoRequest ¶
type VideoRequest struct {
Prompt string `json:"prompt"`
Model string `json:"model"`
Duration int `json:"duration,omitempty"`
Resolution string `json:"resolution,omitempty"`
AspectRatio string `json:"aspect_ratio,omitempty"`
GenerateAudio *bool `json:"generate_audio,omitempty"`
Seed *int `json:"seed,omitempty"`
FrameImages []map[string]any `json:"frame_images,omitempty"`
InputReferences []map[string]any `json:"input_references,omitempty"`
PollInterval time.Duration `json:"-"`
Timeout time.Duration `json:"-"`
Extra map[string]any `json:"-"`
}
VideoRequest holds parameters for video generation.