provider

package
v0.4.9 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultTimeoutDuration = 180 * time.Second
	DefaultMaxRetries      = 3
	DefaultRetryDelay      = 1 * time.Second
	MaxIdleConns           = 100
	MaxIdleConnsPerHost    = 10
	IdleConnTimeout        = 90 * time.Second
)

HTTP client configuration constants

Variables

This section is empty.

Functions

func ConvertMessages

func ConvertMessages(messages []types.Message) []map[string]interface{}

ConvertMessages converts internal Message type to OpenAI-compatible format

func NewHTTPClient

func NewHTTPClient(opts ...HTTPOption) *http.Client

NewHTTPClient creates a new HTTP client with optimized defaults

func NewVLLMProvider

func NewVLLMProvider(baseURL, model string) *vLLMProvider

func ParseStreamResponse

func ParseStreamResponse(body io.Reader, handler StreamHandler) error

ParseStreamResponse parses a standard OpenAI-compatible streaming response

func ParseStreamResponseWithTools

func ParseStreamResponseWithTools(body io.Reader, handler StreamHandler) error

ParseStreamResponseWithTools parses streaming response handling tool calls

func ParseStreamWithParser

func ParseStreamWithParser(body io.Reader, handler StreamHandler, parser StreamParser, config *StreamConfig) error

ParseStreamWithParser parses streaming response using a specific parser

func StreamWithTimeout

func StreamWithTimeout(ctx context.Context, duration time.Duration, body io.Reader, handler StreamHandler) error

StreamWithTimeout performs streaming with automatic timeout

Types

type AnthropicProvider

type AnthropicProvider struct {
	// contains filtered or unexported fields
}

AnthropicProvider implements the Anthropic Claude API

func NewAnthropicProvider

func NewAnthropicProvider(apiKey, model string) *AnthropicProvider

NewAnthropicProvider creates a new Anthropic provider

func (*AnthropicProvider) Chat

func (p *AnthropicProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*AnthropicProvider) ChatWithTools

func (p *AnthropicProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*AnthropicProvider) GetCapabilities

func (p *AnthropicProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Anthropic

func (*AnthropicProvider) Name

func (p *AnthropicProvider) Name() string

func (*AnthropicProvider) Stream

func (p *AnthropicProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

func (*AnthropicProvider) StreamWithTools

func (p *AnthropicProvider) StreamWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}, handler StreamHandler) error

StreamWithTools implements the StreamingToolCaller interface

type AnthropicStreamParser

type AnthropicStreamParser struct{}

AnthropicStreamParser parses Anthropic SSE streams

func (*AnthropicStreamParser) GetFormat

func (p *AnthropicStreamParser) GetFormat() string

func (*AnthropicStreamParser) IsDone

func (p *AnthropicStreamParser) IsDone(line string) bool

func (*AnthropicStreamParser) Parse

func (p *AnthropicStreamParser) Parse(line string) (*StreamResponse, error)

Parse handles Anthropic's SSE format

type BaseProvider

type BaseProvider struct {
	// HTTP client configuration
	Client     *http.Client
	BaseURL    string
	APIVersion string
	Timeout    time.Duration

	// Authentication
	APIKey       string
	APIKeyPrefix string // e.g., "Bearer" for OAuth or "sk-ant-api03" for Anthropic

	// Retry and resilience
	RetryConfig  *RetryConfig
	RetryEnabled bool

	// Health and metrics
	Metrics      *RequestMetrics
	HealthStatus HealthStatus

	// Configuration validation
	Validated bool
	// contains filtered or unexported fields
}

BaseProvider provides common HTTP functionality and infrastructure for all providers Inspired by Cortex Agent's Provider Resolution pattern with enhanced resilience features

func NewBaseProvider

func NewBaseProvider(baseURL string) *BaseProvider

NewBaseProvider creates a new base provider with default configuration

func NewBaseProviderWithConfig

func NewBaseProviderWithConfig(baseURL string, timeout time.Duration) *BaseProvider

NewBaseProviderWithConfig creates a base provider with custom configuration

func (*BaseProvider) DoRequest

func (bp *BaseProvider) DoRequest(ctx context.Context, method, url string, body interface{}, headers map[string]string) ([]byte, int, error)

DoRequest performs an HTTP request with error handling and optional retry

func (*BaseProvider) DoStreamRequest

func (bp *BaseProvider) DoStreamRequest(ctx context.Context, url string, body interface{}, headers map[string]string) (*http.Response, error)

DoStreamRequest performs a streaming HTTP request

func (*BaseProvider) GetHealthState

func (bp *BaseProvider) GetHealthState() CircuitState

GetHealthState returns the current circuit breaker state

func (*BaseProvider) GetMetrics

func (bp *BaseProvider) GetMetrics() (total, failed int64, avgLatency time.Duration, usage Usage)

GetMetrics returns a snapshot of provider metrics

func (*BaseProvider) IsHealthy

func (bp *BaseProvider) IsHealthy() bool

IsHealthy returns whether the provider can accept requests

func (*BaseProvider) ParseAPIError

func (bp *BaseProvider) ParseAPIError(body []byte, statusCode int) error

ParseAPIError parses API error responses with support for multiple formats

func (*BaseProvider) RecordFailure

func (bp *BaseProvider) RecordFailure()

RecordFailure records a failed request for circuit breaker tracking

func (*BaseProvider) RecordSuccess

func (bp *BaseProvider) RecordSuccess()

RecordSuccess records a successful request for circuit breaker tracking

func (*BaseProvider) TransitionToHalfOpen

func (bp *BaseProvider) TransitionToHalfOpen() bool

TransitionToHalfOpen attempts to transition from open to half-open state

func (*BaseProvider) Validate

func (bp *BaseProvider) Validate() error

Validate checks if the provider is properly configured

func (*BaseProvider) WithAPIKey

func (bp *BaseProvider) WithAPIKey(apiKey string) *BaseProvider

WithAPIKey sets the API key for authentication

func (*BaseProvider) WithAPIKeyPrefix

func (bp *BaseProvider) WithAPIKeyPrefix(prefix string) *BaseProvider

WithAPIKeyPrefix sets the API key prefix (e.g., "Bearer", "sk-ant-api03")

func (*BaseProvider) WithRetryDisabled

func (bp *BaseProvider) WithRetryDisabled() *BaseProvider

WithRetryDisabled disables automatic retries

type BaseStreamProvider

type BaseStreamProvider struct {
	*OpenAICompatibleProvider
	StreamEndpoint string
}

BaseStreamProvider provides common streaming functionality for non-OpenAI-compatible providers

func NewBaseStreamProvider

func NewBaseStreamProvider(name, apiKey, baseURL, model string) *BaseStreamProvider

NewBaseStreamProvider creates a new provider with streaming support

func (*BaseStreamProvider) Stream

func (bp *BaseStreamProvider) Stream(ctx context.Context, messages []types.Message, handler StreamHandler) error

Stream implements the Streamer interface for base provider

func (*BaseStreamProvider) StreamWithTools

func (bp *BaseStreamProvider) StreamWithTools(ctx context.Context, messages []types.Message, tools []map[string]interface{}, handler StreamHandler) error

StreamWithTools implements the StreamingToolCaller interface for base provider

type BufferedStreamReader

type BufferedStreamReader struct {
	// contains filtered or unexported fields
}

BufferedStreamReader provides buffering for stream responses

func NewBufferedStreamReader

func NewBufferedStreamReader(reader io.Reader, config *StreamConfig) *BufferedStreamReader

NewBufferedStreamReader creates a new buffered stream reader

func (*BufferedStreamReader) Close

func (b *BufferedStreamReader) Close() error

Close closes the reader

func (*BufferedStreamReader) Read

func (b *BufferedStreamReader) Read(p []byte) (n int, err error)

Read implements io.Reader

type Capabilities

type Capabilities struct {
	ToolCalling    bool `json:"tool_calling"`
	Streaming      bool `json:"streaming"`
	StreamingTools bool `json:"streaming_tools"`
	MultiModal     bool `json:"multi_modal"`
	Vision         bool `json:"vision"`
}

Capabilities declares what features a provider supports

func DefaultCapabilities

func DefaultCapabilities() *Capabilities

DefaultCapabilities returns default capabilities for OpenAI-compatible providers

func GetCapabilities

func GetCapabilities(p Provider) *Capabilities

GetCapabilities returns the capabilities of a provider, or default if not specified

func NoStreamCapabilities

func NoStreamCapabilities() *Capabilities

NoStreamCapabilities returns capabilities without streaming

type CapableProvider

type CapableProvider interface {
	GetCapabilities() *Capabilities
}

CapableProvider is an optional interface for providers that declare their capabilities

type ChatResponse

type ChatResponse struct {
	Content   string           `json:"content"`
	ToolCalls []types.ToolCall `json:"tool_calls,omitempty"`
	Usage     *Usage           `json:"usage,omitempty"`
}

ChatResponse represents a chat response with optional usage info

type CircuitState

type CircuitState int

CircuitState represents the circuit breaker state

const (
	CircuitClosed   CircuitState = iota // Normal operation
	CircuitOpen                         // Failing, reject requests
	CircuitHalfOpen                     // Testing recovery
)

func (CircuitState) String

func (s CircuitState) String() string

type CohereProvider

type CohereProvider struct {
	// contains filtered or unexported fields
}

CohereProvider implements the Cohere API

func NewCohereProvider

func NewCohereProvider(apiKey, baseURL, model string) *CohereProvider

NewCohereProvider creates a new Cohere provider

func (*CohereProvider) Chat

func (p *CohereProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*CohereProvider) ChatWithTools

func (p *CohereProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*CohereProvider) GetCapabilities

func (p *CohereProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Cohere

func (*CohereProvider) Name

func (p *CohereProvider) Name() string

func (*CohereProvider) Stream

func (p *CohereProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

type ConfigManager

type ConfigManager struct {
	// contains filtered or unexported fields
}

ConfigManager manages provider configurations with hot reload support

func NewConfigManager

func NewConfigManager() *ConfigManager

NewConfigManager creates a new configuration manager

func (*ConfigManager) Close

func (cm *ConfigManager) Close()

Close stops watching and releases resources

func (*ConfigManager) Delete

func (cm *ConfigManager) Delete(name string)

Delete removes a provider configuration

func (*ConfigManager) EnableHotReload

func (cm *ConfigManager) EnableHotReload(onChange func(string)) error

EnableHotReload enables watching the config file for changes

func (*ConfigManager) Get

func (cm *ConfigManager) Get(name string) (*ProviderConfig, error)

Get returns a provider configuration

func (*ConfigManager) List

func (cm *ConfigManager) List() []*ProviderConfig

List returns all provider configurations

func (*ConfigManager) LoadFromFile

func (cm *ConfigManager) LoadFromFile(path string) error

LoadFromFile loads provider configurations from a JSON file

func (*ConfigManager) Save

func (cm *ConfigManager) Save() error

Save saves the configuration to file

func (*ConfigManager) Set

func (cm *ConfigManager) Set(pc *ProviderConfig) error

Set sets a provider configuration

type DashScopeProvider

type DashScopeProvider struct {
	// contains filtered or unexported fields
}

DashScopeProvider 阿里云DashScope (兼容OpenAI格式)

func NewDashScopeProvider

func NewDashScopeProvider(apiKey, baseURL, model string) *DashScopeProvider

func (*DashScopeProvider) Chat

func (p *DashScopeProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

func (*DashScopeProvider) ChatWithTools

func (p *DashScopeProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface for DashScope

func (*DashScopeProvider) Name

func (p *DashScopeProvider) Name() string

type DeepSeekProvider

type DeepSeekProvider struct {
	*OpenAICompatibleProvider
}

DeepSeekProvider implements the DeepSeek API (OpenAI-compatible)

func NewDeepSeekProvider

func NewDeepSeekProvider(apiKey, model string) *DeepSeekProvider

NewDeepSeekProvider creates a new DeepSeek provider

func (*DeepSeekProvider) GetCapabilities

func (p *DeepSeekProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of DeepSeek

func (*DeepSeekProvider) Name

func (p *DeepSeekProvider) Name() string

func (*DeepSeekProvider) Stream

func (p *DeepSeekProvider) Stream(ctx context.Context, messages []types.Message, handler StreamHandler) error

Stream implements the Streamer interface (inherited from OpenAICompatibleProvider)

func (*DeepSeekProvider) StreamWithTools

func (p *DeepSeekProvider) StreamWithTools(ctx context.Context, messages []types.Message, tools []map[string]interface{}, handler StreamHandler) error

StreamWithTools implements the StreamingToolCaller interface

type DoubaoProvider

type DoubaoProvider struct {
	// contains filtered or unexported fields
}

DoubaoProvider implements the Doubao (Volcengine) API

func NewDoubaoProvider

func NewDoubaoProvider(apiKey, baseURL, model string) *DoubaoProvider

NewDoubaoProvider creates a new Doubao provider

func NewDoubaoProviderWithEndpoint

func NewDoubaoProviderWithEndpoint(apiKey, endpointID string) *DoubaoProvider

NewDoubaoProviderWithEndpoint creates a Doubao provider with custom endpoint

func (*DoubaoProvider) Chat

func (p *DoubaoProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*DoubaoProvider) ChatWithTools

func (p *DoubaoProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*DoubaoProvider) GetCapabilities

func (p *DoubaoProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Doubao

func (*DoubaoProvider) Name

func (p *DoubaoProvider) Name() string

func (*DoubaoProvider) Stream

func (p *DoubaoProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

type ExtendedChatResponse

type ExtendedChatResponse struct {
	Content   string        `json:"content"`
	ToolCalls []interface{} `json:"tool_calls,omitempty"`
	Usage     *Usage        `json:"usage,omitempty"`
}

ChatResponse extends types.ChatResponse with usage info

type GeminiProvider

type GeminiProvider struct {
	// contains filtered or unexported fields
}

GeminiProvider implements the Google Gemini API

func NewGeminiProvider

func NewGeminiProvider(apiKey, baseURL, model string) *GeminiProvider

NewGeminiProvider creates a new Gemini provider

func (*GeminiProvider) Chat

func (p *GeminiProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*GeminiProvider) ChatWithTools

func (p *GeminiProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*GeminiProvider) GetCapabilities

func (p *GeminiProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Gemini

func (*GeminiProvider) Name

func (p *GeminiProvider) Name() string

func (*GeminiProvider) Stream

func (p *GeminiProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

type GroqProvider

type GroqProvider struct {
	*BaseProvider
	// contains filtered or unexported fields
}

GroqProvider implements the Groq API (fast inference) Groq uses OpenAI-compatible format but with different base URL and specific models

func NewGroqProvider

func NewGroqProvider(apiKey, baseURL, model string) *GroqProvider

NewGroqProvider creates a new Groq provider

func (*GroqProvider) Chat

func (p *GroqProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*GroqProvider) ChatWithTools

func (p *GroqProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*GroqProvider) GetCapabilities

func (p *GroqProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Groq

func (*GroqProvider) GetMetrics

func (p *GroqProvider) GetMetrics() (total, failed int64, avgLatency time.Duration, usage Usage)

GetMetrics returns provider metrics

func (*GroqProvider) HealthCheck

func (p *GroqProvider) HealthCheck(ctx context.Context) error

HealthCheck checks if the provider is reachable

func (*GroqProvider) Name

func (p *GroqProvider) Name() string

func (*GroqProvider) Stream

func (p *GroqProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

func (*GroqProvider) StreamWithTools

func (p *GroqProvider) StreamWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}, handler StreamHandler) error

StreamWithTools implements streaming with tool calls

type HTTPOption

type HTTPOption func(*http.Client)

HTTPOption is a functional option for HTTP client configuration

func WithTimeout

func WithTimeout(d time.Duration) HTTPOption

WithTimeout sets the timeout for the HTTP client

func WithTransport

func WithTransport(t *http.Transport) HTTPOption

WithTransport sets a custom transport for the HTTP client

type HealthCheck

type HealthCheck struct {
	Provider  string
	URL       string
	LastCheck time.Time
	Healthy   bool
	Failures  int
	Successes int
	// contains filtered or unexported fields
}

HealthCheck represents a health check for a provider

type HealthStatus

type HealthStatus struct {
	State       CircuitState
	Failures    int
	Successes   int
	LastFailure time.Time
	LastSuccess time.Time
	CoolingDown bool
	CooledAt    time.Time
}

HealthStatus tracks provider health state for circuit breaker

type HunyuanProvider

type HunyuanProvider struct {
	// contains filtered or unexported fields
}

HunyuanProvider implements the Tencent Hunyuan API

func NewHunyuanProvider

func NewHunyuanProvider(apiKey, baseURL, model string) *HunyuanProvider

NewHunyuanProvider creates a new Hunyuan provider

func (*HunyuanProvider) Chat

func (p *HunyuanProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*HunyuanProvider) ChatWithTools

func (p *HunyuanProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*HunyuanProvider) GetCapabilities

func (p *HunyuanProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Hunyuan

func (*HunyuanProvider) Name

func (p *HunyuanProvider) Name() string

func (*HunyuanProvider) Stream

func (p *HunyuanProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

type HuoshanProvider

type HuoshanProvider struct {
	*OpenAICompatibleProvider
}

HuoshanProvider 火山方舟 (字节跳动,兼容OpenAI格式)

func NewHuoshanProvider

func NewHuoshanProvider(apiKey, baseURL, model string) *HuoshanProvider

NewHuoshanProvider creates a new Huoshan provider

func (*HuoshanProvider) GetCapabilities

func (p *HuoshanProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Huoshan

func (*HuoshanProvider) Name

func (p *HuoshanProvider) Name() string

type KimiProvider

type KimiProvider struct {
	*OpenAICompatibleProvider
}

KimiProvider 月之暗面 Kimi (兼容OpenAI格式)

func NewKimiProvider

func NewKimiProvider(apiKey, baseURL, model string) *KimiProvider

NewKimiProvider creates a new Kimi provider

func (*KimiProvider) GetCapabilities

func (p *KimiProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Kimi

func (*KimiProvider) Name

func (p *KimiProvider) Name() string

type LoadBalancingStrategy

type LoadBalancingStrategy int

LoadBalancingStrategy defines how to select providers

const (
	// RoundRobin selects providers in rotation
	RoundRobin LoadBalancingStrategy = iota
	// Random selects a random provider
	Random
	// Weighted selects based on configured weights
	Weighted
	// LeastUsed selects the provider with least usage
	LeastUsed
)

type Message

type Message = types.Message

Message is an alias for types.Message

type MiMoProvider

type MiMoProvider struct {
	// contains filtered or unexported fields
}

MiMoProvider implements the Xiaomi MiMo API

func NewMiMoProvider

func NewMiMoProvider(apiKey, baseURL, model string) *MiMoProvider

NewMiMoProvider creates a new MiMo provider

func (*MiMoProvider) Chat

func (p *MiMoProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*MiMoProvider) ChatWithTools

func (p *MiMoProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*MiMoProvider) GetCapabilities

func (p *MiMoProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of MiMo

func (*MiMoProvider) Name

func (p *MiMoProvider) Name() string

func (*MiMoProvider) Stream

func (p *MiMoProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

type MiniMaxProvider

type MiniMaxProvider struct {
	*OpenAICompatibleProvider
}

MiniMaxProvider MiniMax (兼容OpenAI格式)

func NewMiniMaxProvider

func NewMiniMaxProvider(apiKey, baseURL, model string) *MiniMaxProvider

NewMiniMaxProvider creates a new MiniMax provider

func (*MiniMaxProvider) GetCapabilities

func (p *MiniMaxProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of MiniMax

func (*MiniMaxProvider) Name

func (p *MiniMaxProvider) Name() string

type MistralProvider

type MistralProvider struct {
	// contains filtered or unexported fields
}

MistralProvider implements the Mistral AI API

func NewMistralProvider

func NewMistralProvider(apiKey, baseURL, model string) *MistralProvider

NewMistralProvider creates a new Mistral AI provider

func (*MistralProvider) Chat

func (p *MistralProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*MistralProvider) ChatWithTools

func (p *MistralProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*MistralProvider) GetCapabilities

func (p *MistralProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Mistral AI

func (*MistralProvider) Name

func (p *MistralProvider) Name() string

func (*MistralProvider) Stream

func (p *MistralProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

type MoonshotProvider

type MoonshotProvider struct {
	// contains filtered or unexported fields
}

MoonshotProvider implements the Moonshot (Kimi) AI API

func NewMoonshotProvider

func NewMoonshotProvider(apiKey, model string) *MoonshotProvider

NewMoonshotProvider creates a new Moonshot (Kimi) provider

func (*MoonshotProvider) Chat

func (p *MoonshotProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*MoonshotProvider) ChatWithTools

func (p *MoonshotProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*MoonshotProvider) GetCapabilities

func (p *MoonshotProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Moonshot

func (*MoonshotProvider) Name

func (p *MoonshotProvider) Name() string

func (*MoonshotProvider) Stream

func (p *MoonshotProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

type OllamaProvider

type OllamaProvider struct {
	// contains filtered or unexported fields
}

func NewOllamaProvider

func NewOllamaProvider(baseURL, model string) *OllamaProvider

func (*OllamaProvider) Chat

func (p *OllamaProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

func (*OllamaProvider) ChatWithTools

func (p *OllamaProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface for Ollama

func (*OllamaProvider) Name

func (p *OllamaProvider) Name() string

type OpenAICompatibleProvider

type OpenAICompatibleProvider struct {
	*BaseProvider

	Model string
	// contains filtered or unexported fields
}

OpenAICompatibleProvider provides OpenAI-compatible API functionality

func NewOpenAICompatibleProvider

func NewOpenAICompatibleProvider(name, apiKey, baseURL, model string) *OpenAICompatibleProvider

NewOpenAICompatibleProvider creates a new OpenAI-compatible provider

func (*OpenAICompatibleProvider) Chat

Chat implements the Provider interface

func (*OpenAICompatibleProvider) ChatWithTools

func (p *OpenAICompatibleProvider) ChatWithTools(ctx context.Context, messages []types.Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*OpenAICompatibleProvider) Name

func (p *OpenAICompatibleProvider) Name() string

Name returns the provider name

func (*OpenAICompatibleProvider) Stream

func (p *OpenAICompatibleProvider) Stream(ctx context.Context, messages []types.Message, handler StreamHandler) error

Stream implements the Streamer interface

func (*OpenAICompatibleProvider) StreamWithTools

func (p *OpenAICompatibleProvider) StreamWithTools(ctx context.Context, messages []types.Message, tools []map[string]interface{}, handler StreamHandler) error

StreamWithTools implements the StreamingToolCaller interface

type OpenAIProvider

type OpenAIProvider struct {
	*OpenAICompatibleProvider
}

OpenAIProvider implements the OpenAI API

func NewOpenAIProvider

func NewOpenAIProvider(apiKey, baseURL, model string) *OpenAIProvider

NewOpenAIProvider creates a new OpenAI provider

func (*OpenAIProvider) GetCapabilities

func (p *OpenAIProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of OpenAI

func (*OpenAIProvider) Name

func (p *OpenAIProvider) Name() string

type OpenAIStreamParser

type OpenAIStreamParser struct{}

OpenAIStreamParser parses OpenAI-compatible SSE streams

func (*OpenAIStreamParser) GetFormat

func (p *OpenAIStreamParser) GetFormat() string

func (*OpenAIStreamParser) IsDone

func (p *OpenAIStreamParser) IsDone(line string) bool

func (*OpenAIStreamParser) Parse

func (p *OpenAIStreamParser) Parse(line string) (*StreamResponse, error)

type OpenRouterProvider

type OpenRouterProvider struct {
	*OpenAICompatibleProvider
}

OpenRouterProvider OpenRouter (兼容OpenAI格式)

func NewOpenRouterProvider

func NewOpenRouterProvider(apiKey, baseURL, model string) *OpenRouterProvider

NewOpenRouterProvider creates a new OpenRouter provider

func (*OpenRouterProvider) GetCapabilities

func (p *OpenRouterProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of OpenRouter

func (*OpenRouterProvider) Name

func (p *OpenRouterProvider) Name() string

type PerplexityProvider

type PerplexityProvider struct {
	// contains filtered or unexported fields
}

PerplexityProvider implements the Perplexity API

func NewPerplexityProvider

func NewPerplexityProvider(apiKey, baseURL, model string) *PerplexityProvider

NewPerplexityProvider creates a new Perplexity provider

func (*PerplexityProvider) Chat

func (p *PerplexityProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*PerplexityProvider) ChatWithTools

func (p *PerplexityProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*PerplexityProvider) GetCapabilities

func (p *PerplexityProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Perplexity

func (*PerplexityProvider) Name

func (p *PerplexityProvider) Name() string

func (*PerplexityProvider) Stream

func (p *PerplexityProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

type Provider

type Provider interface {
	Chat(ctx context.Context, messages []Message) (*ChatResponse, error)
	Name() string
}

Provider is the interface for LLM providers.

type ProviderConfig

type ProviderConfig struct {
	// Provider identification
	Name    string `json:"name"`
	Display string `json:"display"`

	// API configuration
	APIKey  string `json:"api_key,omitempty"`
	BaseURL string `json:"base_url,omitempty"`
	Model   string `json:"model,omitempty"`

	// Optional configuration
	APIVersion string            `json:"api_version,omitempty"`
	Extra      map[string]string `json:"extra,omitempty"`

	// Defaults
	DefaultModel string   `json:"default_model,omitempty"`
	Models       []string `json:"models,omitempty"`

	// Rate limiting
	MaxRPM int `json:"max_rpm,omitempty"` // requests per minute
	MaxTPM int `json:"max_tpm,omitempty"` // tokens per minute

	// Validation state
	Validated bool `json:"-"`

	// File path if loaded from file
	FilePath string `json:"-"`
}

ProviderConfig represents configuration for a provider

func (*ProviderConfig) Validate

func (pc *ProviderConfig) Validate() error

Validate checks if the provider config is valid

type ProviderRouter

type ProviderRouter struct {
	// contains filtered or unexported fields
}

ProviderRouter provides unified routing with model fallback, health checks, circuit breakers, and load balancing support

func NewProviderRouter

func NewProviderRouter(config *RouterConfig) *ProviderRouter

NewProviderRouter creates a new enhanced provider router

func (*ProviderRouter) Close

func (r *ProviderRouter) Close()

Close shuts down the router

func (*ProviderRouter) GetAllStats

func (r *ProviderRouter) GetAllStats() map[string]RouterStats

GetAllStats returns stats for all providers

func (*ProviderRouter) GetHealthStatus

func (r *ProviderRouter) GetHealthStatus(provider string) (healthy bool, lastCheck time.Time)

GetHealthStatus returns the health status of a provider

func (*ProviderRouter) GetProvider

func (r *ProviderRouter) GetProvider(name string) (Provider, error)

GetProvider returns a provider by name

func (*ProviderRouter) GetProviderForModel

func (r *ProviderRouter) GetProviderForModel(model string) (Provider, string, error)

GetProviderForModel returns the appropriate provider for a given model

func (*ProviderRouter) GetStats

func (r *ProviderRouter) GetStats(provider string) (stats RouterStats)

GetStats returns routing statistics

func (*ProviderRouter) ListProviders

func (r *ProviderRouter) ListProviders() []string

ListProviders returns all registered provider names

func (*ProviderRouter) Register

func (r *ProviderRouter) Register(name string, provider Provider)

Register registers a provider with the router

func (*ProviderRouter) RegisterFallback

func (r *ProviderRouter) RegisterFallback(provider string, fallbacks []string)

RegisterFallback registers fallback providers for a given provider

func (*ProviderRouter) RegisterModelRouting

func (r *ProviderRouter) RegisterModelRouting(modelPrefix, provider string)

RegisterModelRouting registers model prefix -> provider mapping

func (*ProviderRouter) RouteWithFallback

func (r *ProviderRouter) RouteWithFallback(ctx context.Context, model string,
	execute func(ctx context.Context, provider Provider) error) error

RouteWithFallback executes a request with automatic fallback

func (*ProviderRouter) SelectProvider

func (r *ProviderRouter) SelectProvider(providers []string) string

SelectProvider selects a provider based on load balancing strategy

func (*ProviderRouter) SetLoadBalancing

func (r *ProviderRouter) SetLoadBalancing(strategy LoadBalancingStrategy)

SetLoadBalancing sets the load balancing strategy

type ProviderToolFormat

type ProviderToolFormat struct {
	Format     string // "openai", "anthropic", "gemini"
	UsesTools  bool   // Uses tools array in request
	UsesChoice bool   // Uses choice.tool_call format
}

ProviderToolFormat specifies the expected tool format for a provider

func GetProviderToolFormat

func GetProviderToolFormat(provider string) ProviderToolFormat

GetProviderToolFormat returns the expected tool format for a provider

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages provider instances.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new provider registry

func (*Registry) Get

func (r *Registry) Get(name string) (Provider, error)

Get returns a provider by name

func (*Registry) List

func (r *Registry) List() []string

List returns all registered provider names

func (*Registry) Register

func (r *Registry) Register(p Provider)

Register registers a provider in the registry

type RequestMetrics

type RequestMetrics struct {
	TotalRequests  int64
	FailedRequests int64
	TotalLatency   time.Duration
	TokenUsage     Usage
	// contains filtered or unexported fields
}

RequestMetrics tracks request statistics

func (*RequestMetrics) AddFailure

func (m *RequestMetrics) AddFailure()

AddFailure records a failed request

func (*RequestMetrics) AddRequest

func (m *RequestMetrics) AddRequest(latency time.Duration, tokens *Usage)

AddRequest records a successful request

func (*RequestMetrics) GetStats

func (m *RequestMetrics) GetStats() (total, failed int64, avgLatency time.Duration, usage Usage)

GetStats returns current metrics snapshot

type RetryConfig

type RetryConfig struct {
	MaxRetries     int
	InitialDelay   time.Duration
	MaxDelay       time.Duration
	BackoffFactor  float64
	RetryableCodes []int // HTTP status codes that should be retried
}

RetryConfig configures retry behavior

func DefaultRetryConfig

func DefaultRetryConfig() *RetryConfig

DefaultRetryConfig returns sensible retry defaults

type RouterConfig

type RouterConfig struct {
	// Enable health checks
	HealthCheckEnabled bool
	// Health check interval
	HealthCheckInterval time.Duration
	// Timeout for health check requests
	HealthCheckTimeout time.Duration
	// Number of consecutive failures before marking unhealthy
	UnhealthyThreshold int
	// Number of consecutive successes before marking healthy
	HealthyThreshold int

	// Load balancing strategy
	LoadBalancing LoadBalancingStrategy

	// Fallback configuration
	EnableFallback bool
	FallbackDelay  time.Duration

	// Circuit breaker settings
	CircuitBreakerEnabled bool
	FailureThreshold      int
	RecoveryTimeout       time.Duration
}

RouterConfig configures router behavior

func DefaultRouterConfig

func DefaultRouterConfig() *RouterConfig

DefaultRouterConfig returns sensible router defaults

type RouterStats

type RouterStats struct {
	TotalRequests      int64
	FailedRequests     int64
	SuccessRequests    int64
	RequestsByProvider map[string]int64
	LastUsed           time.Time
	LastError          error
	// contains filtered or unexported fields
}

RouterStats tracks routing statistics

type StreamAccumulator

type StreamAccumulator struct {
	// contains filtered or unexported fields
}

CreateStreamHandler creates a handler that accumulates responses

func NewStreamAccumulator

func NewStreamAccumulator(handler StreamHandler) *StreamAccumulator

NewStreamAccumulator creates a new stream accumulator

func (*StreamAccumulator) GetContent

func (sa *StreamAccumulator) GetContent() string

GetContent returns the accumulated content

func (*StreamAccumulator) GetToolCalls

func (sa *StreamAccumulator) GetToolCalls() []types.ToolCall

GetToolCalls returns the accumulated tool calls

func (*StreamAccumulator) Handle

func (sa *StreamAccumulator) Handle(resp *StreamResponse)

Handle processes a stream response

func (*StreamAccumulator) OnFinal

func (sa *StreamAccumulator) OnFinal(handler StreamHandler) *StreamAccumulator

OnFinal sets a handler to call on stream completion

type StreamConfig

type StreamConfig struct {
	// Timeout for reading chunks
	ReadTimeout time.Duration
	// Whether to accumulate content for final callback
	AccumulateContent bool
	// Buffer size for the reader
	BufferSize int
	// Heartbeat interval for keep-alive
	HeartbeatInterval time.Duration
}

StreamConfig configures streaming behavior

func DefaultStreamConfig

func DefaultStreamConfig() *StreamConfig

DefaultStreamConfig returns sensible streaming defaults

type StreamContext

type StreamContext struct {
	// contains filtered or unexported fields
}

StreamContext provides context-aware streaming with cancellation support

func NewStreamContext

func NewStreamContext(parent context.Context) (*StreamContext, context.Context)

NewStreamContext creates a new streaming context with cancellation support

func (*StreamContext) Cancel

func (sc *StreamContext) Cancel()

Cancel cancels the streaming operation

func (*StreamContext) Close

func (sc *StreamContext) Close(err error)

Close marks the streaming as complete

func (*StreamContext) Wait

func (sc *StreamContext) Wait() error

Wait waits for the streaming to complete

type StreamHandler

type StreamHandler func(resp *StreamResponse)

StreamHandler is a callback function for handling streaming responses

func WrappedStreamHandler

func WrappedStreamHandler(handler StreamHandler, accumulator *StreamAccumulator) StreamHandler

WrappedStreamHandler wraps a handler for accumulation

type StreamParser

type StreamParser interface {
	// Parse parses a single SSE data line
	Parse(line string) (*StreamResponse, error)
	// IsDone checks if this line indicates stream completion
	IsDone(line string) bool
	// GetFormat returns the provider format name
	GetFormat() string
}

StreamParser handles SSE stream parsing for different provider formats

type StreamResponse

type StreamResponse struct {
	Content   string           `json:"content,omitempty"`
	ToolCall  *types.ToolCall  `json:"tool_call,omitempty"`
	ToolCalls []types.ToolCall `json:"tool_calls,omitempty"`
	StreamTCs []StreamToolCall `json:"-"` // Internal use for delta tracking
	Done      bool             `json:"done"`
	Error     error            `json:"error,omitempty"`
	Usage     *Usage           `json:"usage,omitempty"`
}

StreamResponse represents a chunk of streaming response

type StreamToolCall

type StreamToolCall struct {
	Index     int
	ID        string
	Type      string
	Name      string
	Arguments string
}

StreamToolCall represents a tool call with index for streaming delta

type Streamer

type Streamer interface {
	Stream(ctx context.Context, messages []Message, handler StreamHandler) error
}

Streamer is an optional interface for providers that support streaming.

type StreamingToolCaller

type StreamingToolCaller interface {
	StreamWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}, handler StreamHandler) error
}

StreamingToolCaller is for providers that support both streaming and tool calling

type TogetherProvider

type TogetherProvider struct {
	// contains filtered or unexported fields
}

TogetherProvider implements the Together AI API

func NewTogetherProvider

func NewTogetherProvider(apiKey, baseURL, model string) *TogetherProvider

NewTogetherProvider creates a new Together AI provider

func (*TogetherProvider) Chat

func (p *TogetherProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*TogetherProvider) ChatWithTools

func (p *TogetherProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*TogetherProvider) GetCapabilities

func (p *TogetherProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Together AI

func (*TogetherProvider) ListModels

func (p *TogetherProvider) ListModels(ctx context.Context) ([]string, error)

ListModels returns available models from Together AI

func (*TogetherProvider) Name

func (p *TogetherProvider) Name() string

func (*TogetherProvider) Stream

func (p *TogetherProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

type ToolCaller

type ToolCaller interface {
	ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)
}

ToolCaller is an optional interface for providers that support tool calling.

type ToolConverter

type ToolConverter struct{}

ToolConverter converts tool definitions between different provider formats

func NewToolConverter

func NewToolConverter() *ToolConverter

NewToolConverter creates a new tool converter

func (*ToolConverter) ConvertToAnthropic

func (tc *ToolConverter) ConvertToAnthropic(tools []map[string]interface{}) []map[string]interface{}

ConvertToAnthropic converts tools to Anthropic tool use format

func (*ToolConverter) ConvertToGemini

func (tc *ToolConverter) ConvertToGemini(tools []map[string]interface{}) []map[string]interface{}

ConvertToGemini converts tools to Gemini function declaration format

func (*ToolConverter) ConvertToOpenAI

func (tc *ToolConverter) ConvertToOpenAI(tools []map[string]interface{}) []map[string]interface{}

ConvertToOpenAI converts tools to OpenAI function format

func (*ToolConverter) ConvertToolResult

func (tc *ToolConverter) ConvertToolResult(provider string, result interface{}, toolCallID string) (interface{}, error)

ConvertToolResult converts a tool execution result for different providers

func (*ToolConverter) NormalizeToolCall

func (tc *ToolConverter) NormalizeToolCall(toolCall *types.ToolCall) error

NormalizeToolCall normalizes a tool call to internal format

func (*ToolConverter) ParseToolCall

func (tc *ToolConverter) ParseToolCall(data interface{}) (*types.ToolCall, error)

ParseToolCall parses a tool call from various provider formats

func (*ToolConverter) ParseToolCalls

func (tc *ToolConverter) ParseToolCalls(data interface{}) []types.ToolCall

ParseToolCalls parses multiple tool calls

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage represents token usage statistics

type WenxinProvider

type WenxinProvider struct {
	// contains filtered or unexported fields
}

WenxinProvider implements the Baidu Wenxin (ERNIE) API

func NewWenxinProvider

func NewWenxinProvider(apiKey, secretKey, model string) *WenxinProvider

NewWenxinProvider creates a new Wenxin provider

func (*WenxinProvider) Chat

func (p *WenxinProvider) Chat(ctx context.Context, messages []Message) (*ChatResponse, error)

Chat implements the Provider interface

func (*WenxinProvider) ChatWithTools

func (p *WenxinProvider) ChatWithTools(ctx context.Context, messages []Message, tools []map[string]interface{}) (*ChatResponse, error)

ChatWithTools implements the ToolCaller interface

func (*WenxinProvider) GetCapabilities

func (p *WenxinProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Wenxin

func (*WenxinProvider) Name

func (p *WenxinProvider) Name() string

func (*WenxinProvider) Stream

func (p *WenxinProvider) Stream(ctx context.Context, messages []Message, handler StreamHandler) error

Stream implements the Streamer interface

type ZhipuProvider

type ZhipuProvider struct {
	*OpenAICompatibleProvider
}

ZhipuProvider 智谱AI (兼容OpenAI格式)

func NewZhipuProvider

func NewZhipuProvider(apiKey, baseURL, model string) *ZhipuProvider

NewZhipuProvider creates a new Zhipu provider

func (*ZhipuProvider) GetCapabilities

func (p *ZhipuProvider) GetCapabilities() *Capabilities

GetCapabilities returns the capabilities of Zhipu

func (*ZhipuProvider) Name

func (p *ZhipuProvider) Name() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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