Documentation
¶
Index ¶
- Constants
- type BaseConfig
- type CompletionRequest
- type CompletionResponse
- type Config
- type Error
- type GroundingTool
- type ImageContent
- type Message
- type MessageContent
- type Model
- type ModelCapability
- type ModelRegistry
- type Provider
- type ResponseFormat
- type Role
- type StreamCallback
- type StreamResponse
- type TextContent
- type ThinkingConfig
- type Tool
- type ToolCall
- type ToolCallFunction
- type ToolFunction
- type ToolResult
- type Usage
Constants ¶
const ( ErrCodeInvalidConfig = "INVALID_CONFIG" ErrCodeAuthentication = "AUTHENTICATION_FAILED" ErrCodeRateLimit = "RATE_LIMIT_EXCEEDED" ErrCodeQuotaExceeded = "QUOTA_EXCEEDED" ErrCodeModelNotFound = "MODEL_NOT_FOUND" ErrCodeInvalidRequest = "INVALID_REQUEST" ErrCodeServerError = "SERVER_ERROR" ErrCodeTimeout = "TIMEOUT" ErrCodeTokenLimitExceeded = "TOKEN_LIMIT_EXCEEDED" ErrCodeContentFiltered = "CONTENT_FILTERED" )
Common error codes
const GroundingToolGoogleSearch = "google_search"
GroundingToolGoogleSearch enables Google Search grounding
const GroundingToolURLContext = "url_context"
GroundingToolURLContext enables URL context retrieval
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseConfig ¶
type BaseConfig struct {
Provider string `json:"provider"`
APIKey string `json:"api_key"`
BaseURL string `json:"base_url,omitempty"`
Timeout int `json:"timeout,omitempty"` // in seconds
MaxRetries int `json:"max_retries,omitempty"`
}
BaseConfig provides common configuration fields
func (*BaseConfig) GetProvider ¶
func (c *BaseConfig) GetProvider() string
func (*BaseConfig) Validate ¶
func (c *BaseConfig) Validate() error
type CompletionRequest ¶
type CompletionRequest struct {
Messages []*Message `json:"messages"`
Model string `json:"model"`
MaxTokens int `json:"max_tokens,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopP float64 `json:"top_p,omitempty"`
TopK int `json:"top_k,omitempty"`
Seed *int `json:"seed,omitempty"`
Stop []string `json:"stop,omitempty"`
Stream bool `json:"stream,omitempty"`
Tools []Tool `json:"tools,omitempty"`
GroundingTools []GroundingTool `json:"grounding_tools,omitempty"` // Google-specific: URL context, Google Search
ToolChoice interface{} `json:"tool_choice,omitempty"`
ThinkingConfig *ThinkingConfig `json:"thinking_config,omitempty"`
ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
CompletionRequest represents a unified completion request
type CompletionResponse ¶
type CompletionResponse struct {
ID string `json:"id"`
Model string `json:"model"`
Provider string `json:"provider"`
Message *Message `json:"message,omitempty"`
FinishReason string `json:"finish_reason,omitempty"`
Usage *Usage `json:"usage,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Created int64 `json:"created,omitempty"`
}
CompletionResponse represents a unified completion response
type Error ¶
type Error struct {
Code string `json:"code"`
Message string `json:"message"`
Provider string `json:"provider"`
Details map[string]interface{} `json:"details,omitempty"`
Cause error `json:"-"`
}
Error represents a structured error with provider context
type GroundingTool ¶ added in v1.2.4
type GroundingTool struct {
Type string `json:"type"` // "url_context" or "google_search"
}
GroundingTool represents Google-specific grounding tools (URL context, Google Search)
type ImageContent ¶
type ImageContent struct {
URL string `json:"url,omitempty"`
Base64 string `json:"base64,omitempty"`
Detail string `json:"detail,omitempty"` // "low", "high", "auto"
}
ImageContent represents image content
func (ImageContent) Type ¶
func (i ImageContent) Type() string
type Message ¶
type Message struct {
ID string `json:"id,omitempty"`
Role Role `json:"role"`
Content []MessageContent `json:"content,omitempty"`
TextData string `json:"text_data,omitempty"` // For simple text messages
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolResult *ToolResult `json:"tool_result,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
}
Message represents a unified message format across all providers
func NewContentMessage ¶
func NewContentMessage(role Role, content []MessageContent) *Message
NewContentMessage creates a message with structured content
func NewTextMessage ¶
NewTextMessage creates a new text message
type MessageContent ¶
type MessageContent interface {
Type() string
}
MessageContent represents different types of content that can be in a message
type Model ¶
type Model struct {
ID string `json:"id"`
Name string `json:"name"`
Provider string `json:"provider"`
Description string `json:"description,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
InputCost float64 `json:"input_cost,omitempty"` // Cost per 1M tokens
OutputCost float64 `json:"output_cost,omitempty"` // Cost per 1M tokens
Capabilities []string `json:"capabilities,omitempty"` // e.g., "chat", "completion", "vision", "tools"
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
Model represents a unified model across all providers
func (*Model) HasCapability ¶
func (m *Model) HasCapability(capability ModelCapability) bool
HasCapability checks if the model supports a specific capability
func (*Model) MarshalJSON ¶
MarshalJSON implements custom JSON marshaling
type ModelCapability ¶
type ModelCapability string
ModelCapability represents what a model can do
const ( CapabilityChat ModelCapability = "chat" CapabilityCompletion ModelCapability = "completion" CapabilityVision ModelCapability = "vision" CapabilityAudio ModelCapability = "audio" CapabilityVideo ModelCapability = "video" CapabilityTools ModelCapability = "tools" CapabilityStreaming ModelCapability = "streaming" CapabilityJSON ModelCapability = "json" CapabilityThinking ModelCapability = "thinking" CapabilityLive ModelCapability = "live" CapabilityTTS ModelCapability = "tts" CapabilityImage ModelCapability = "image_generation" )
type ModelRegistry ¶
type ModelRegistry struct {
// contains filtered or unexported fields
}
ModelRegistry manages available models across providers
func NewModelRegistry ¶
func NewModelRegistry() *ModelRegistry
NewModelRegistry creates a new model registry
func (*ModelRegistry) Get ¶
func (r *ModelRegistry) Get(provider, id string) (*Model, bool)
Get retrieves a model by provider and ID
func (*ModelRegistry) GetByCapability ¶
func (r *ModelRegistry) GetByCapability(capability ModelCapability) []*Model
GetByCapability returns all models that support a specific capability
func (*ModelRegistry) GetByProvider ¶
func (r *ModelRegistry) GetByProvider(provider string) []*Model
GetByProvider returns all models for a specific provider
func (*ModelRegistry) List ¶
func (r *ModelRegistry) List() []*Model
List returns all registered models
func (*ModelRegistry) Register ¶
func (r *ModelRegistry) Register(model *Model)
Register adds a model to the registry
type Provider ¶
type Provider interface {
// GetName returns the provider name
GetName() string
// Initialize sets up the provider with configuration
Initialize(config Config) error
// GetModels returns available models for this provider
GetModels(ctx context.Context) ([]*Model, error)
// Complete performs a completion request
Complete(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)
// Stream performs a streaming completion request
Stream(ctx context.Context, req *CompletionRequest, callback StreamCallback) error
// EstimateTokens estimates the token count for given messages
EstimateTokens(ctx context.Context, messages []*Message, model string) (int, error)
// ValidateModel checks if a model is supported by this provider
ValidateModel(model string) error
// Close cleans up resources
Close() error
}
Provider defines the interface that all AI providers must implement
type ResponseFormat ¶
type ResponseFormat struct {
Type string `json:"type"` // "text" or "json_object"
Schema map[string]interface{} `json:"schema,omitempty"`
}
ResponseFormat represents the format of the response
type StreamCallback ¶
type StreamCallback func(ctx context.Context, response *StreamResponse) error
StreamCallback defines the signature for streaming callbacks
type StreamResponse ¶
type StreamResponse struct {
ID string `json:"id"`
Model string `json:"model"`
Provider string `json:"provider"`
Delta *Message `json:"delta,omitempty"`
FinishReason string `json:"finish_reason,omitempty"`
Usage *Usage `json:"usage,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
StreamResponse represents a streaming response chunk
type TextContent ¶
type TextContent struct {
Text string `json:"text"`
}
TextContent represents text content
func (TextContent) Type ¶
func (t TextContent) Type() string
type ThinkingConfig ¶ added in v1.2.5
type ThinkingConfig struct {
// Optional. Indicates whether to include thoughts in the response. If true, thoughts
// are returned only if the model supports thought and thoughts are available.
IncludeThoughts bool `json:"includeThoughts,omitempty"`
// Optional. Indicates the thinking budget in tokens.
ThinkingBudget *int32 `json:"thinkingBudget,omitempty"`
}
type Tool ¶
type Tool struct {
Type string `json:"type"`
Function *ToolFunction `json:"function,omitempty"`
}
Tool represents a function/tool that can be called by the model
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ToolCallFunction `json:"function"`
Args map[string]interface{} `json:"args,omitempty"`
}
ToolCall represents a tool/function call
type ToolCallFunction ¶
type ToolFunction ¶
type ToolResult ¶
type ToolResult struct {
ToolCallID string `json:"tool_call_id"`
Content string `json:"content"`
Error string `json:"error,omitempty"`
}
ToolResult represents the result of a tool call