Documentation
¶
Index ¶
- func ConvertToLangChainMessages(messages []Message) []llms.MessageContent
- func ConvertToLangChainTools(tools []ToolDefinition) []llms.Tool
- type AnthropicProvider
- type ChatOption
- type ChatOptions
- type CircuitBreaker
- func (cb *CircuitBreaker) AllowRequest() bool
- func (cb *CircuitBreaker) GetState() CircuitState
- func (cb *CircuitBreaker) GetStateInfo() map[string]interface{}
- func (cb *CircuitBreaker) IsOpen() bool
- func (cb *CircuitBreaker) RecordFailure()
- func (cb *CircuitBreaker) RecordSuccess()
- func (cb *CircuitBreaker) Reset()
- type CircuitState
- type FailoverProvider
- func (p *FailoverProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition, ...) (*Response, error)
- func (p *FailoverProvider) ChatWithTools(ctx context.Context, messages []Message, tools []ToolDefinition, ...) (*Response, error)
- func (p *FailoverProvider) Close() error
- func (p *FailoverProvider) GetCircuitBreaker() *CircuitBreaker
- func (p *FailoverProvider) GetFallback() Provider
- func (p *FailoverProvider) GetPrimary() Provider
- func (p *FailoverProvider) SetFallback(fallback Provider)
- type Message
- type OpenAIProvider
- type OpenRouterProvider
- type Provider
- type ProviderProfile
- type ProviderType
- type Response
- type RotationProvider
- func (p *RotationProvider) AddProfile(name string, provider Provider, apiKey string, priority int)
- func (p *RotationProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition, ...) (*Response, error)
- func (p *RotationProvider) ChatWithTools(ctx context.Context, messages []Message, tools []ToolDefinition, ...) (*Response, error)
- func (p *RotationProvider) Close() error
- func (p *RotationProvider) GetProfile(name string) (*ProviderProfile, bool)
- func (p *RotationProvider) GetProfileStatus(name string) (map[string]interface{}, error)
- func (p *RotationProvider) ListProfiles() []string
- func (p *RotationProvider) RemoveProfile(name string)
- func (p *RotationProvider) ResetCooldown()
- type RotationStrategy
- type StreamBuffer
- type StreamCallback
- type StreamChunk
- type StreamProcessor
- func (p *StreamProcessor) GetBuffer() *StreamBuffer
- func (p *StreamProcessor) OnComplete(handler func()) *StreamProcessor
- func (p *StreamProcessor) OnContent(handler func(content string)) *StreamProcessor
- func (p *StreamProcessor) OnError(handler func(err error)) *StreamProcessor
- func (p *StreamProcessor) OnFinal(handler func(final string)) *StreamProcessor
- func (p *StreamProcessor) OnThinking(handler func(thinking string)) *StreamProcessor
- func (p *StreamProcessor) OnToolCall(handler func(toolCall ToolCall)) *StreamProcessor
- func (p *StreamProcessor) Process(chunk StreamChunk) error
- func (p *StreamProcessor) Reset()
- type StreamingAdapter
- type StreamingProvider
- type ThinkingParser
- type ToolCall
- type ToolDefinition
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertToLangChainMessages ¶
func ConvertToLangChainMessages(messages []Message) []llms.MessageContent
ConvertToLangChainMessages 转换为 LangChain 消息格式
func ConvertToLangChainTools ¶
func ConvertToLangChainTools(tools []ToolDefinition) []llms.Tool
ConvertToLangChainTools 转换为 LangChain 工具格式
Types ¶
type AnthropicProvider ¶
type AnthropicProvider struct {
// contains filtered or unexported fields
}
AnthropicProvider Anthropic 提供商
func NewAnthropicProvider ¶
func NewAnthropicProvider(apiKey, baseURL, model string, maxTokens int) (*AnthropicProvider, error)
NewAnthropicProvider 创建 Anthropic 提供商
func (*AnthropicProvider) Chat ¶
func (p *AnthropicProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
Chat 聊天
func (*AnthropicProvider) ChatWithTools ¶
func (p *AnthropicProvider) ChatWithTools(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
ChatWithTools 聊天(带工具)
type ChatOptions ¶
ChatOptions 聊天配置
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker 断路器
func NewCircuitBreaker ¶
func NewCircuitBreaker(failureThreshold int, timeout time.Duration) *CircuitBreaker
NewCircuitBreaker 创建断路器
func (*CircuitBreaker) AllowRequest ¶
func (cb *CircuitBreaker) AllowRequest() bool
AllowRequest 检查是否允许请求
func (*CircuitBreaker) GetStateInfo ¶
func (cb *CircuitBreaker) GetStateInfo() map[string]interface{}
GetStateInfo 获取状态信息
type CircuitState ¶
type CircuitState int
CircuitState 断路器状态
const ( // CircuitStateClosed 断路器关闭(正常) CircuitStateClosed CircuitState = iota // CircuitStateOpen 断路器打开(故障) CircuitStateOpen // CircuitStateHalfOpen 半开(尝试恢复) CircuitStateHalfOpen )
func (CircuitState) String ¶
func (s CircuitState) String() string
type FailoverProvider ¶
type FailoverProvider struct {
// contains filtered or unexported fields
}
FailoverProvider 支持故障转移的提供商
func NewFailoverProvider ¶
func NewFailoverProvider(primary, fallback Provider, errorClassifier errors.ErrorClassifier) *FailoverProvider
NewFailoverProvider 创建故障转移提供商
func (*FailoverProvider) Chat ¶
func (p *FailoverProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
Chat 聊天(带故障转移)
func (*FailoverProvider) ChatWithTools ¶
func (p *FailoverProvider) ChatWithTools(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
ChatWithTools 聊天(带工具,支持故障转移)
func (*FailoverProvider) GetCircuitBreaker ¶
func (p *FailoverProvider) GetCircuitBreaker() *CircuitBreaker
GetCircuitBreaker 获取断路器
func (*FailoverProvider) GetFallback ¶
func (p *FailoverProvider) GetFallback() Provider
GetFallback 获取备用提供商
func (*FailoverProvider) GetPrimary ¶
func (p *FailoverProvider) GetPrimary() Provider
GetPrimary 获取主要提供商
func (*FailoverProvider) SetFallback ¶
func (p *FailoverProvider) SetFallback(fallback Provider)
SetFallback 设置备用提供商
type Message ¶
type Message struct {
Role string `json:"role"` // user, assistant, system, tool
Content string `json:"content"`
Images []string `json:"images,omitempty"` // Image URLs or Base64
ToolCallID string `json:"tool_call_id,omitempty"` // For tool role
ToolName string `json:"tool_name,omitempty"` // For tool role - the name of the tool that was called
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // For assistant role
}
Message 消息
type OpenAIProvider ¶
type OpenAIProvider struct {
// contains filtered or unexported fields
}
OpenAIProvider OpenAI 提供商
func NewOpenAIProvider ¶
func NewOpenAIProvider(apiKey, baseURL, model string, maxTokens int) (*OpenAIProvider, error)
NewOpenAIProvider 创建 OpenAI 提供商
func (*OpenAIProvider) Chat ¶
func (p *OpenAIProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
Chat 聊天
func (*OpenAIProvider) ChatWithTools ¶
func (p *OpenAIProvider) ChatWithTools(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
ChatWithTools 聊天(带工具)
type OpenRouterProvider ¶
type OpenRouterProvider struct {
// contains filtered or unexported fields
}
OpenRouterProvider OpenRouter 提供商
func NewOpenRouterProvider ¶
func NewOpenRouterProvider(apiKey, baseURL, model string, maxTokens int) (*OpenRouterProvider, error)
NewOpenRouterProvider 创建 OpenRouter 提供商
func (*OpenRouterProvider) Chat ¶
func (p *OpenRouterProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
Chat 聊天
func (*OpenRouterProvider) ChatWithTools ¶
func (p *OpenRouterProvider) ChatWithTools(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
ChatWithTools 聊天(带工具)
type Provider ¶
type Provider interface {
// Chat 聊天
Chat(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
// ChatWithTools 聊天(带工具)
ChatWithTools(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
// Close 关闭连接
Close() error
}
Provider LLM 提供商接口
func NewOpenAIProviderFromLangChain ¶
NewOpenAIProviderFromLangChain 从 LangChain 创建提供商
func NewProvider ¶
NewProvider 创建提供商(支持故障转移和配置轮换)
func NewRotationProviderFromConfig ¶
NewRotationProviderFromConfig 从配置创建轮换提供商
type ProviderProfile ¶
type ProviderProfile struct {
Name string
Provider Provider
APIKey string
Priority int
CooldownUntil time.Time
RequestCount int64
// contains filtered or unexported fields
}
ProviderProfile 提供商配置
type ProviderType ¶
type ProviderType string
ProviderType 提供商类型
const ( ProviderTypeOpenAI ProviderType = "openai" ProviderTypeAnthropic ProviderType = "anthropic" ProviderTypeOpenRouter ProviderType = "openrouter" )
type Response ¶
type Response struct {
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
FinishReason string `json:"finish_reason"`
Usage Usage `json:"usage"`
}
Response LLM 响应
func ConvertToStreaming ¶
func ConvertToStreaming(chunks []StreamChunk) *Response
ConvertToStreaming converts a streaming response chunks to a regular Response
type RotationProvider ¶
type RotationProvider struct {
// contains filtered or unexported fields
}
RotationProvider 支持多配置轮换的提供商
func NewRotationProvider ¶
func NewRotationProvider(strategy RotationStrategy, defaultCooldown time.Duration, errorClassifier errors.ErrorClassifier) *RotationProvider
NewRotationProvider 创建轮换提供商
func (*RotationProvider) AddProfile ¶
func (p *RotationProvider) AddProfile(name string, provider Provider, apiKey string, priority int)
AddProfile 添加配置
func (*RotationProvider) Chat ¶
func (p *RotationProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
Chat 聊天(带配置轮换)
func (*RotationProvider) ChatWithTools ¶
func (p *RotationProvider) ChatWithTools(ctx context.Context, messages []Message, tools []ToolDefinition, options ...ChatOption) (*Response, error)
ChatWithTools 聊天(带工具,支持配置轮换)
func (*RotationProvider) GetProfile ¶
func (p *RotationProvider) GetProfile(name string) (*ProviderProfile, bool)
GetProfile 获取配置
func (*RotationProvider) GetProfileStatus ¶
func (p *RotationProvider) GetProfileStatus(name string) (map[string]interface{}, error)
GetProfileStatus 获取配置状态
func (*RotationProvider) ListProfiles ¶
func (p *RotationProvider) ListProfiles() []string
ListProfiles 列出所有配置
func (*RotationProvider) RemoveProfile ¶
func (p *RotationProvider) RemoveProfile(name string)
RemoveProfile 移除配置
func (*RotationProvider) ResetCooldown ¶
func (p *RotationProvider) ResetCooldown()
ResetCooldown 重置所有配置的冷却时间
type RotationStrategy ¶
type RotationStrategy string
RotationStrategy 轮换策略
const ( // RotationStrategyRoundRobin 轮询策略 RotationStrategyRoundRobin RotationStrategy = "round_robin" // RotationStrategyLeastUsed 最少使用策略 RotationStrategyLeastUsed RotationStrategy = "least_used" // RotationStrategyRandom 随机策略 RotationStrategyRandom RotationStrategy = "random" )
type StreamBuffer ¶
type StreamBuffer struct {
// contains filtered or unexported fields
}
StreamBuffer manages streaming chunks and provides buffer management
func NewStreamBuffer ¶
func NewStreamBuffer(maxSize int) *StreamBuffer
NewStreamBuffer creates a new stream buffer
func (*StreamBuffer) Add ¶
func (b *StreamBuffer) Add(chunk StreamChunk) error
Add adds a chunk to the buffer
func (*StreamBuffer) GetChunks ¶
func (b *StreamBuffer) GetChunks() []StreamChunk
GetChunks returns all chunks
func (*StreamBuffer) GetContent ¶
func (b *StreamBuffer) GetContent() string
GetContent returns the accumulated content
func (*StreamBuffer) GetFinal ¶
func (b *StreamBuffer) GetFinal() string
GetFinal returns the accumulated final content
func (*StreamBuffer) GetThinking ¶
func (b *StreamBuffer) GetThinking() string
GetThinking returns the accumulated thinking content
func (*StreamBuffer) IsDone ¶
func (b *StreamBuffer) IsDone() bool
IsDone returns whether the stream is complete
type StreamCallback ¶
type StreamCallback func(chunk StreamChunk)
StreamCallback is called for each chunk in a streaming response
type StreamChunk ¶
type StreamChunk struct {
Content string `json:"content"`
Done bool `json:"done"`
ToolCall *ToolCall `json:"tool_call,omitempty"`
ThinkingTag string `json:"thinking,omitempty"`
IsThinking bool `json:"is_thinking,omitempty"`
IsFinal bool `json:"is_final,omitempty"`
Error error `json:"error,omitempty"`
}
StreamChunk represents a chunk of streaming response
type StreamProcessor ¶
type StreamProcessor struct {
// contains filtered or unexported fields
}
StreamProcessor processes streaming chunks with custom handlers
func NewStreamProcessor ¶
func NewStreamProcessor() *StreamProcessor
NewStreamProcessor creates a new stream processor
func (*StreamProcessor) GetBuffer ¶
func (p *StreamProcessor) GetBuffer() *StreamBuffer
GetBuffer returns the buffer
func (*StreamProcessor) OnComplete ¶
func (p *StreamProcessor) OnComplete(handler func()) *StreamProcessor
OnComplete sets handler for stream completion
func (*StreamProcessor) OnContent ¶
func (p *StreamProcessor) OnContent(handler func(content string)) *StreamProcessor
OnContent sets handler for content chunks
func (*StreamProcessor) OnError ¶
func (p *StreamProcessor) OnError(handler func(err error)) *StreamProcessor
OnError sets handler for errors
func (*StreamProcessor) OnFinal ¶
func (p *StreamProcessor) OnFinal(handler func(final string)) *StreamProcessor
OnFinal sets handler for final chunks
func (*StreamProcessor) OnThinking ¶
func (p *StreamProcessor) OnThinking(handler func(thinking string)) *StreamProcessor
OnThinking sets handler for thinking chunks
func (*StreamProcessor) OnToolCall ¶
func (p *StreamProcessor) OnToolCall(handler func(toolCall ToolCall)) *StreamProcessor
OnToolCall sets handler for tool call chunks
func (*StreamProcessor) Process ¶
func (p *StreamProcessor) Process(chunk StreamChunk) error
Process processes a chunk
type StreamingAdapter ¶
type StreamingAdapter struct {
// contains filtered or unexported fields
}
StreamingAdapter wraps a Provider to add streaming capabilities
func NewStreamingAdapter ¶
func NewStreamingAdapter(provider Provider) *StreamingAdapter
NewStreamingAdapter creates a new streaming adapter
func (*StreamingAdapter) ChatStream ¶
func (a *StreamingAdapter) ChatStream(ctx context.Context, messages []Message, tools []ToolDefinition, callback StreamCallback, options ...ChatOption) error
ChatStream implements streaming chat
type StreamingProvider ¶
type StreamingProvider interface {
Provider
// ChatStream chat with streaming response
ChatStream(ctx context.Context, messages []Message, tools []ToolDefinition, callback StreamCallback, options ...ChatOption) error
}
StreamingProvider extends Provider with streaming support
type ThinkingParser ¶
type ThinkingParser struct {
// contains filtered or unexported fields
}
ThinkingParser handles parsing of thinking tags in streaming responses
func NewThinkingParser ¶
func NewThinkingParser() *ThinkingParser
NewThinkingParser creates a new thinking parser
func (*ThinkingParser) IsInFinal ¶
func (p *ThinkingParser) IsInFinal() bool
IsInFinal returns whether parser is currently in final tag
func (*ThinkingParser) IsInThinking ¶
func (p *ThinkingParser) IsInThinking() bool
IsInThinking returns whether parser is currently in thinking tag
func (*ThinkingParser) Parse ¶
func (p *ThinkingParser) Parse(content string) []StreamChunk
Parse parses a chunk and separates thinking, final, and regular content
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Name string `json:"name"`
Params map[string]interface{} `json:"params"`
Response string `json:"response,omitempty"`
}
ToolCall 工具调用
type ToolDefinition ¶
type ToolDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]interface{} `json:"parameters"`
}
ToolDefinition 工具定义