providers

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

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 聊天(带工具)

func (*AnthropicProvider) Close

func (p *AnthropicProvider) Close() error

Close 关闭连接

type ChatOption

type ChatOption func(*ChatOptions)

ChatOption 聊天选项

func WithMaxTokens

func WithMaxTokens(maxTokens int) ChatOption

WithMaxTokens 设置最大 tokens

func WithModel

func WithModel(model string) ChatOption

WithModel 设置模型

func WithStream

func WithStream(stream bool) ChatOption

WithStream 设置流式输出

func WithTemperature

func WithTemperature(temp float64) ChatOption

WithTemperature 设置温度

type ChatOptions

type ChatOptions struct {
	Model       string
	Temperature float64
	MaxTokens   int
	Stream      bool
}

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) GetState

func (cb *CircuitBreaker) GetState() CircuitState

GetState 获取当前状态

func (*CircuitBreaker) GetStateInfo

func (cb *CircuitBreaker) GetStateInfo() map[string]interface{}

GetStateInfo 获取状态信息

func (*CircuitBreaker) IsOpen

func (cb *CircuitBreaker) IsOpen() bool

IsOpen 检查断路器是否打开

func (*CircuitBreaker) RecordFailure

func (cb *CircuitBreaker) RecordFailure()

RecordFailure 记录失败

func (*CircuitBreaker) RecordSuccess

func (cb *CircuitBreaker) RecordSuccess()

RecordSuccess 记录成功

func (*CircuitBreaker) Reset

func (cb *CircuitBreaker) Reset()

Reset 重置断路器

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) Close

func (p *FailoverProvider) Close() error

Close 关闭连接

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 聊天(带工具)

func (*OpenAIProvider) Close

func (p *OpenAIProvider) Close() error

Close 关闭连接

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 聊天(带工具)

func (*OpenRouterProvider) Close

func (p *OpenRouterProvider) Close() error

Close 关闭连接

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

func NewOpenAIProviderFromLangChain(apiKey, baseURL, model string, maxTokens int) (Provider, error)

NewOpenAIProviderFromLangChain 从 LangChain 创建提供商

func NewProvider

func NewProvider(cfg *config.Config) (Provider, error)

NewProvider 创建提供商(支持故障转移和配置轮换)

func NewRotationProviderFromConfig

func NewRotationProviderFromConfig(cfg *config.Config) (Provider, error)

NewRotationProviderFromConfig 从配置创建轮换提供商

func NewSimpleProvider

func NewSimpleProvider(cfg *config.Config) (Provider, error)

NewSimpleProvider 创建单一提供商

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) Close

func (p *RotationProvider) Close() error

Close 关闭所有提供商

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) Clear

func (b *StreamBuffer) Clear()

Clear clears 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

func (*StreamProcessor) Reset

func (p *StreamProcessor) Reset()

Reset resets the processor

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

func (*ThinkingParser) Reset

func (p *ThinkingParser) Reset()

Reset resets the parser state

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 工具定义

type Usage

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

Usage 使用情况

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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