Documentation
¶
Index ¶
- Constants
- Variables
- func BoolPtr(v bool) *bool
- func Float64Ptr(v float64) *float64
- func IntPtr(v int) *int
- func RegisterProvider(name string, factory ProviderFactory)
- type AnthropicProvider
- type BaseProvider
- type ChunkType
- type Client
- func (c *Client) AddProvider(name string, provider Provider) error
- func (c *Client) Complete(ctx context.Context, req *Request) (*Response, error)
- func (c *Client) Models() []ModelInfo
- func (c *Client) Providers() []string
- func (c *Client) Stream(ctx context.Context, req *Request) (StreamReader, error)
- type ClientOption
- func WithAnthropic(apiKey string, baseURL ...string) ClientOption
- func WithDeepSeek(apiKey string, baseURL ...string) ClientOption
- func WithDefaults(maxTokens int, temperature float64) ClientOption
- func WithGLM(apiKey string, baseURL ...string) ClientOption
- func WithGemini(apiKey string, baseURL ...string) ClientOption
- func WithOpenAI(apiKey string, baseURL ...string) ClientOption
- func WithOpenRouter(apiKey string, baseURL ...string) ClientOption
- func WithProvider(name string, provider Provider) ClientOption
- func WithProviderConfig(name string, config ProviderConfig) ClientOption
- func WithQwen(apiKey string, baseURL ...string) ClientOption
- func WithResilience(config ResilienceConfig) ClientOption
- func WithRetries(maxRetries int, initialDelay time.Duration) ClientOption
- func WithTimeout(timeout time.Duration) ClientOption
- type DeepSeekProvider
- type DefaultConfig
- type FunctionCall
- type FunctionSchema
- type GLMProvider
- type GeminiProvider
- type JSONSchema
- type Message
- type ModelCapability
- type ModelInfo
- type OpenAIProvider
- type OpenRouterProvider
- type Provider
- func CreateProvider(name string, config ProviderConfig) (Provider, error)
- func NewAnthropicProvider(config ProviderConfig) Provider
- func NewDeepSeekProvider(config ProviderConfig) Provider
- func NewGLMProvider(config ProviderConfig) Provider
- func NewGeminiProvider(config ProviderConfig) Provider
- func NewOpenAIProvider(config ProviderConfig) Provider
- func NewOpenRouterProvider(config ProviderConfig) Provider
- func NewQwenProvider(config ProviderConfig) Provider
- type ProviderConfig
- type ProviderFactory
- type QwenProvider
- type ReasoningChunk
- type ReasoningData
- type Request
- type ResilienceConfig
- type ResilientHTTPClient
- type Response
- type ResponseFormat
- type StreamChunk
- type StreamReader
- type Tool
- type ToolCall
- type ToolCallDelta
- type Usage
Constants ¶
const ( ResponseFormatText = "text" ResponseFormatJSONObject = "json_object" ResponseFormatJSONSchema = "json_schema" )
Response format type constants
Variables ¶
var ProviderRegistry = make(map[string]ProviderFactory)
ProviderRegistry holds all registered provider factories
Functions ¶
func BoolPtr ¶ added in v1.2.1
BoolPtr returns a pointer to a bool value Helper function to make it easier to set optional bool fields
func Float64Ptr ¶
Float64Ptr returns a pointer to a float64 value Helper function to make it easier to set optional float64 fields
func IntPtr ¶
IntPtr returns a pointer to an int value Helper function to make it easier to set optional int fields
func RegisterProvider ¶
func RegisterProvider(name string, factory ProviderFactory)
RegisterProvider registers a provider factory
Types ¶
type AnthropicProvider ¶
type AnthropicProvider struct {
*BaseProvider
}
AnthropicProvider implements the Provider interface for Anthropic
func (*AnthropicProvider) Models ¶
func (p *AnthropicProvider) Models() []ModelInfo
Models returns the list of supported models
func (*AnthropicProvider) Stream ¶
func (p *AnthropicProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)
type BaseProvider ¶
type BaseProvider struct {
// contains filtered or unexported fields
}
BaseProvider provides common functionality for all providers
func NewBaseProvider ¶
func NewBaseProvider(name string, config ProviderConfig) *BaseProvider
NewBaseProvider creates a new base provider
func (*BaseProvider) Config ¶
func (p *BaseProvider) Config() ProviderConfig
Config returns the provider configuration
func (*BaseProvider) HTTPClient ¶
func (p *BaseProvider) HTTPClient() *ResilientHTTPClient
HTTPClient returns the resilient HTTP client
func (*BaseProvider) ResilienceConfig ¶ added in v1.2.2
func (p *BaseProvider) ResilienceConfig() ResilienceConfig
ResilienceConfig returns the resilience configuration
func (*BaseProvider) Validate ¶
func (p *BaseProvider) Validate() error
Validate checks if the provider is properly configured
type ChunkType ¶
type ChunkType string
ChunkType defines the type of streaming chunk
const ( ChunkTypeContent ChunkType = "content" // Regular content ChunkTypeReasoning ChunkType = "reasoning" // Reasoning content ChunkTypeToolCall ChunkType = "tool_call" // Complete tool call ChunkTypeToolCallDelta ChunkType = "tool_call_delta" // Tool call arguments delta ChunkTypeUsage ChunkType = "usage" // Usage statistics ChunkTypeDone ChunkType = "done" // Completion marker ChunkTypeError ChunkType = "error" // Error )
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the main LLM client
func New ¶
func New(opts ...ClientOption) *Client
New creates a new LiteLLM client with optional configuration
func (*Client) AddProvider ¶
AddProvider adds a provider to the client
type ClientOption ¶
type ClientOption func(*Client)
ClientOption defines options for configuring the client
func WithAnthropic ¶
func WithAnthropic(apiKey string, baseURL ...string) ClientOption
WithAnthropic adds Anthropic provider with custom configuration
func WithDeepSeek ¶ added in v1.1.0
func WithDeepSeek(apiKey string, baseURL ...string) ClientOption
WithDeepSeek adds DeepSeek provider with custom configuration
func WithDefaults ¶
func WithDefaults(maxTokens int, temperature float64) ClientOption
WithDefaults sets default configuration values
func WithGLM ¶ added in v1.2.0
func WithGLM(apiKey string, baseURL ...string) ClientOption
WithGLM adds GLM provider with custom configuration
func WithGemini ¶
func WithGemini(apiKey string, baseURL ...string) ClientOption
WithGemini adds Gemini provider with custom configuration
func WithOpenAI ¶
func WithOpenAI(apiKey string, baseURL ...string) ClientOption
WithOpenAI adds OpenAI provider with custom configuration
func WithOpenRouter ¶ added in v1.1.0
func WithOpenRouter(apiKey string, baseURL ...string) ClientOption
WithOpenRouter adds OpenRouter provider with custom configuration
func WithProvider ¶
func WithProvider(name string, provider Provider) ClientOption
WithProvider adds a custom provider
func WithProviderConfig ¶
func WithProviderConfig(name string, config ProviderConfig) ClientOption
WithProviderConfig adds a provider using ProviderConfig
func WithQwen ¶ added in v1.1.0
func WithQwen(apiKey string, baseURL ...string) ClientOption
WithQwen adds Qwen provider with custom configuration
func WithResilience ¶ added in v1.2.2
func WithResilience(config ResilienceConfig) ClientOption
WithResilience sets default resilience configuration
func WithRetries ¶ added in v1.2.2
func WithRetries(maxRetries int, initialDelay time.Duration) ClientOption
WithRetries sets retry configuration for all providers
func WithTimeout ¶ added in v1.2.2
func WithTimeout(timeout time.Duration) ClientOption
WithTimeout sets request timeout for all providers
type DeepSeekProvider ¶ added in v1.1.0
type DeepSeekProvider struct {
*BaseProvider
}
DeepSeekProvider implements the Provider interface for DeepSeek
func (*DeepSeekProvider) Complete ¶ added in v1.1.0
Complete implements the Provider interface for DeepSeek
func (*DeepSeekProvider) Models ¶ added in v1.1.0
func (p *DeepSeekProvider) Models() []ModelInfo
Models returns the list of available DeepSeek models
func (*DeepSeekProvider) Stream ¶ added in v1.1.0
func (p *DeepSeekProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)
Stream implements streaming for DeepSeek
type DefaultConfig ¶
type DefaultConfig struct {
MaxTokens int `json:"max_tokens"`
Temperature float64 `json:"temperature"`
Resilience ResilienceConfig `json:"resilience"`
}
DefaultConfig holds default configuration values
type FunctionCall ¶
type FunctionCall struct {
Name string `json:"name"` // Function name
Arguments string `json:"arguments"` // JSON string of arguments
}
FunctionCall represents the function to be called
type FunctionSchema ¶
type FunctionSchema struct {
Name string `json:"name"` // Function name
Description string `json:"description"` // Function description
Parameters any `json:"parameters"` // JSON schema for parameters
}
FunctionSchema defines a callable function
type GLMProvider ¶ added in v1.2.0
type GLMProvider struct {
*BaseProvider
}
GLMProvider implements the Provider interface for ZhiPu GLM-4.5 models
func (*GLMProvider) Models ¶ added in v1.2.0
func (p *GLMProvider) Models() []ModelInfo
Models returns the list of supported GLM models
func (*GLMProvider) Stream ¶ added in v1.2.0
func (p *GLMProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)
Stream implements streaming chat completions for GLM
type GeminiProvider ¶
type GeminiProvider struct {
*BaseProvider
}
GeminiProvider implements the Provider interface for Google Gemini
func (*GeminiProvider) Models ¶
func (p *GeminiProvider) Models() []ModelInfo
Models returns the list of supported models
func (*GeminiProvider) Stream ¶
func (p *GeminiProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)
type JSONSchema ¶ added in v1.2.1
type JSONSchema struct {
Name string `json:"name"` // Schema name
Description string `json:"description,omitempty"` // Schema description
Schema any `json:"schema"` // JSON schema definition
Strict *bool `json:"strict,omitempty"` // Whether to enforce strict adherence
}
JSONSchema defines a JSON schema for structured output
type Message ¶
type Message struct {
Role string `json:"role"` // "user", "assistant", "system", "tool"
Content string `json:"content"` // Message content
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // Tool calls made by assistant
ToolCallID string `json:"tool_call_id,omitempty"` // ID for tool response
}
Message represents a conversation message
type ModelCapability ¶
type ModelCapability string
ModelCapability represents what a model can do
const ( CapabilityChat ModelCapability = "chat" // Chat completion CapabilityCompletion ModelCapability = "completion" // Text completion CapabilityEmbedding ModelCapability = "embedding" // Text embedding CapabilityFunctionCall ModelCapability = "function_call" // Function calling CapabilityVision ModelCapability = "vision" // Image understanding CapabilityReasoning ModelCapability = "reasoning" // Step-by-step reasoning CapabilityCode ModelCapability = "code" // Code generation CapabilityMultimodal ModelCapability = "multimodal" // Multiple input types )
type ModelInfo ¶
type ModelInfo struct {
ID string `json:"id"` // Model identifier
Provider string `json:"provider"` // Provider name
Name string `json:"name"` // Display name
Description string `json:"description,omitempty"` // Model description
MaxTokens int `json:"max_tokens,omitempty"` // Maximum context tokens
Capabilities []ModelCapability `json:"capabilities,omitempty"` // Model capabilities
Extra map[string]any `json:"extra,omitempty"` // Provider-specific info
}
ModelInfo represents information about a model
type OpenAIProvider ¶
type OpenAIProvider struct {
*BaseProvider
}
OpenAIProvider implements the Provider interface for OpenAI
func (*OpenAIProvider) Models ¶
func (p *OpenAIProvider) Models() []ModelInfo
Models returns the list of supported models
func (*OpenAIProvider) Stream ¶
func (p *OpenAIProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)
type OpenRouterProvider ¶ added in v1.1.0
type OpenRouterProvider struct {
*BaseProvider
}
OpenRouterProvider implements the Provider interface for OpenRouter
func (*OpenRouterProvider) Models ¶ added in v1.1.0
func (p *OpenRouterProvider) Models() []ModelInfo
Models returns the list of available models for OpenRouter
func (*OpenRouterProvider) Stream ¶ added in v1.1.0
func (p *OpenRouterProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)
Stream implements the Provider interface for streaming responses
func (*OpenRouterProvider) Validate ¶ added in v1.1.0
func (p *OpenRouterProvider) Validate() error
Validate checks if the provider configuration is valid
type Provider ¶
type Provider interface {
// Name returns the provider name
Name() string
// Complete performs a completion request
Complete(ctx context.Context, req *Request) (*Response, error)
// Stream performs a streaming completion request
Stream(ctx context.Context, req *Request) (StreamReader, error)
// Models returns the list of supported models
Models() []ModelInfo
// Validate checks if the provider is properly configured
Validate() error
}
Provider defines the interface that all LLM providers must implement
func CreateProvider ¶
func CreateProvider(name string, config ProviderConfig) (Provider, error)
CreateProvider creates a provider instance by name
func NewAnthropicProvider ¶
func NewAnthropicProvider(config ProviderConfig) Provider
NewAnthropicProvider creates a new Anthropic provider
func NewDeepSeekProvider ¶ added in v1.1.0
func NewDeepSeekProvider(config ProviderConfig) Provider
NewDeepSeekProvider creates a new DeepSeek provider
func NewGLMProvider ¶ added in v1.2.0
func NewGLMProvider(config ProviderConfig) Provider
NewGLMProvider creates a new GLM provider instance
func NewGeminiProvider ¶
func NewGeminiProvider(config ProviderConfig) Provider
NewGeminiProvider creates a new Gemini provider
func NewOpenAIProvider ¶
func NewOpenAIProvider(config ProviderConfig) Provider
NewOpenAIProvider creates a new OpenAI provider
func NewOpenRouterProvider ¶ added in v1.1.0
func NewOpenRouterProvider(config ProviderConfig) Provider
NewOpenRouterProvider creates a new OpenRouter provider
func NewQwenProvider ¶ added in v1.1.0
func NewQwenProvider(config ProviderConfig) Provider
NewQwenProvider creates a new Qwen provider
type ProviderConfig ¶
type ProviderConfig struct {
APIKey string `json:"api_key"` // API key
BaseURL string `json:"base_url,omitempty"` // Custom base URL
Resilience ResilienceConfig `json:"resilience,omitempty"` // Network resilience config
Extra map[string]any `json:"extra,omitempty"` // Provider-specific config
}
ProviderConfig holds configuration for a provider
type ProviderFactory ¶
type ProviderFactory func(config ProviderConfig) Provider
ProviderFactory creates a provider instance
type QwenProvider ¶ added in v1.1.0
type QwenProvider struct {
*BaseProvider
}
QwenProvider implements the Provider interface for Qwen (Alibaba Cloud DashScope)
func (*QwenProvider) Models ¶ added in v1.1.0
func (p *QwenProvider) Models() []ModelInfo
Models returns the list of supported models
func (*QwenProvider) Stream ¶ added in v1.1.0
func (p *QwenProvider) Stream(ctx context.Context, req *Request) (StreamReader, error)
Stream performs a streaming completion request
type ReasoningChunk ¶
type ReasoningChunk struct {
Content string `json:"content,omitempty"` // Reasoning content delta
Summary string `json:"summary,omitempty"` // Reasoning summary delta
}
ReasoningChunk represents a reasoning content chunk
type ReasoningData ¶
type ReasoningData struct {
Content string `json:"content,omitempty"` // Reasoning process content
Summary string `json:"summary,omitempty"` // Reasoning summary
TokensUsed int `json:"tokens_used,omitempty"` // Tokens used for reasoning
}
ReasoningData contains reasoning process information
type Request ¶
type Request struct {
Model string `json:"model"` // Model identifier in "provider/model" format
Messages []Message `json:"messages"` // Conversation messages
MaxTokens *int `json:"max_tokens,omitempty"` // Maximum tokens to generate
Temperature *float64 `json:"temperature,omitempty"` // Sampling temperature
Stream bool `json:"stream,omitempty"` // Enable streaming
Tools []Tool `json:"tools,omitempty"` // Available tools
ToolChoice any `json:"tool_choice,omitempty"` // Tool choice strategy
// Response format for structured outputs
ResponseFormat *ResponseFormat `json:"response_format,omitempty"` // Response format specification
// Reasoning model parameters (OpenAI o-series)
ReasoningEffort string `json:"reasoning_effort,omitempty"` // "low", "medium", "high"
ReasoningSummary string `json:"reasoning_summary,omitempty"` // "concise", "detailed", "auto"
UseResponsesAPI bool `json:"use_responses_api,omitempty"` // Force Responses API usage
// Extension fields for provider-specific features
Extra map[string]any `json:"extra,omitempty"` // Provider-specific parameters
}
Request represents a completion request
type ResilienceConfig ¶ added in v1.2.2
type ResilienceConfig struct {
MaxRetries int `json:"max_retries"` // Maximum retry attempts, default 3
InitialDelay time.Duration `json:"initial_delay"` // Initial delay, default 1 second
MaxDelay time.Duration `json:"max_delay"` // Maximum delay, default 30 seconds
Multiplier float64 `json:"multiplier"` // Backoff multiplier, default 2.0
Jitter bool `json:"jitter"` // Whether to add jitter, default true
RequestTimeout time.Duration `json:"request_timeout"` // Single request timeout, default 30 seconds
ConnectTimeout time.Duration `json:"connect_timeout"` // Connection timeout, default 10 seconds
}
ResilienceConfig holds network resilience configuration
func DefaultResilienceConfig ¶ added in v1.2.2
func DefaultResilienceConfig() ResilienceConfig
DefaultResilienceConfig returns default resilience configuration
type ResilientHTTPClient ¶ added in v1.2.2
type ResilientHTTPClient struct {
// contains filtered or unexported fields
}
ResilientHTTPClient wraps http.Client with retry logic
func NewResilientHTTPClient ¶ added in v1.2.2
func NewResilientHTTPClient(config ResilienceConfig) *ResilientHTTPClient
NewResilientHTTPClient creates a new resilient HTTP client
type Response ¶
type Response struct {
Content string `json:"content"` // Generated content
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // Tool calls requested
Usage Usage `json:"usage"` // Token usage statistics
Model string `json:"model"` // Actual model used
Provider string `json:"provider"` // Provider name
// Reasoning data (for reasoning models)
Reasoning *ReasoningData `json:"reasoning,omitempty"` // Reasoning process data
// Metadata
FinishReason string `json:"finish_reason,omitempty"` // Why generation stopped
Extra map[string]any `json:"extra,omitempty"` // Provider-specific data
}
Response represents a completion response
type ResponseFormat ¶ added in v1.2.1
type ResponseFormat struct {
Type string `json:"type"` // "text", "json_object", "json_schema"
JSONSchema *JSONSchema `json:"json_schema,omitempty"` // JSON schema for structured output
}
ResponseFormat specifies the format of the model's output
func NewResponseFormatJSONObject ¶ added in v1.2.1
func NewResponseFormatJSONObject() *ResponseFormat
NewResponseFormatJSONObject creates a JSON object response format
func NewResponseFormatJSONSchema ¶ added in v1.2.1
func NewResponseFormatJSONSchema(name, description string, schema any, strict bool) *ResponseFormat
NewResponseFormatJSONSchema creates a JSON schema response format
func NewResponseFormatText ¶ added in v1.2.1
func NewResponseFormatText() *ResponseFormat
NewResponseFormatText creates a text response format
type StreamChunk ¶
type StreamChunk struct {
Type ChunkType `json:"type"` // Chunk type
Content string `json:"content,omitempty"` // Content delta
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // Complete tool calls
Usage *Usage `json:"usage,omitempty"` // Final usage stats
Done bool `json:"done"` // Stream completion flag
Error error `json:"error,omitempty"` // Error if any
// Reasoning data
Reasoning *ReasoningChunk `json:"reasoning,omitempty"` // Reasoning chunk
// Tool call delta data
ToolCallDelta *ToolCallDelta `json:"tool_call_delta,omitempty"` // Tool call incremental data
// Metadata
Model string `json:"model,omitempty"` // Model name
Provider string `json:"provider,omitempty"` // Provider name
FinishReason string `json:"finish_reason,omitempty"` // Completion reason
Extra map[string]any `json:"extra,omitempty"` // Provider-specific data
}
StreamChunk represents a streaming response chunk
type StreamReader ¶
type StreamReader interface {
Read() (*StreamChunk, error) // Read next chunk
Close() error // Close the stream
Err() error // Get any error that occurred
}
StreamReader provides an interface for reading streaming responses
type Tool ¶
type Tool struct {
Type string `json:"type"` // Always "function" for now
Function FunctionSchema `json:"function"` // Function schema
}
Tool represents a function that can be called
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"` // Unique identifier
Type string `json:"type"` // Always "function" for now
Function FunctionCall `json:"function"` // Function details
}
ToolCall represents a function call request
type ToolCallDelta ¶
type ToolCallDelta struct {
Index int `json:"index,omitempty"` // Tool call index in the array
ID string `json:"id,omitempty"` // Tool call ID
Type string `json:"type,omitempty"` // Tool type (e.g., "function")
FunctionName string `json:"function_name,omitempty"` // Function name
ArgumentsDelta string `json:"arguments_delta,omitempty"` // Incremental arguments
}
ToolCallDelta represents incremental tool call data
type Usage ¶
type Usage struct {
PromptTokens int `json:"prompt_tokens"` // Input tokens
CompletionTokens int `json:"completion_tokens"` // Output tokens
TotalTokens int `json:"total_tokens"` // Total tokens
ReasoningTokens int `json:"reasoning_tokens,omitempty"` // Reasoning tokens (o-series)
}
Usage represents token usage statistics