Documentation
¶
Overview ¶
此文件中的缓存实现已迁移至 llm/cache/ 子包。
请使用以下导入路径:
import "github.com/BaSui01/agentflow/llm/cache"
该子包提供更完整的实现,包括:
- cache.MultiLevelCache — 多级缓存(本地 LRU + Redis),支持策略模式的缓存键生成
- cache.LRUCache — 本地 LRU 缓存,支持 Delete/Clear/Stats
- cache.CacheConfig — 缓存配置,支持 KeyStrategyType 和 CacheableCheck
- cache.CacheEntry — 缓存条目,支持 PromptVersion/ModelVersion
- cache.ErrCacheMiss — 缓存未命中错误
- cache.ToolResultCache — 工具调用结果缓存
- cache.KeyStrategy — 缓存键生成策略接口(hash/hierarchical)
由于 llm/cache/ 子包 import 了 llm 包(用于 ChatRequest 等类型), 此文件不能通过类型别名转发(会导致循环依赖)。 所有新代码应直接使用 llm/cache/ 子包。
包 llm 提供统一的大语言模型接入层,包括 Provider 抽象、路由、 缓存、重试、可观测与工具调用等能力。
概述 ¶
本包目标是屏蔽不同模型服务商在接口、鉴权、错误语义和流式协议上的差异, 对上层业务暴露一致的请求与响应模型,降低多模型接入和切换成本。
你可以使用它完成以下典型场景:
- 单一 Provider 的快速接入与调用。 - 多 Provider 路由与故障转移。 - 流式输出、函数调用与工具编排。 - 缓存、重试、熔断、限流与成本观测。
Provider 抽象 ¶
核心接口是 Provider,包含补全、流式输出、健康检查与能力声明。 基于该接口,系统可以在保持上层调用不变的前提下切换底层模型服务。
核心接口 ¶
- Provider:LLM 提供者接口,提供 Completion / Stream / HealthCheck / Name / SupportsNativeFunctionCalling
- SecurityProvider:安全认证接口,提供 Authenticate / Authorize
- AuditLogger:审计日志接口,提供 Log / Query
- RateLimiter:限流接口,提供 Allow / Wait / Limit
- Tracer / Span:分布式追踪接口
- ProviderMiddleware:Provider 中间件接口,支持链式增强
核心类型 ¶
- LLMModel:抽象模型定义(如 gpt-4、claude-3-opus)
- LLMProvider:提供商定义(如 OpenAI、Anthropic、DeepSeek)
- LLMProviderModel:提供商-模型多对多映射
- LLMProviderAPIKey:API 密钥池条目
- ChatRequest / ChatResponse:聊天请求与响应
- StreamChunk:流式输出分片
- HealthStatus:健康检查状态
- Identity:代理或用户身份信息
- AuditEvent:审计事件
- CredentialOverride:单次请求凭据覆盖,通过 context 传递
运维能力 ¶
- HealthMonitor:Provider 健康监控,周期性探测与评分
- APIKeyPool:API 密钥池,支持轮询、优先级与限流策略
- CanaryConfig / CanaryDeployment:金丝雀发布,支持灰度流量与自动回滚
主要能力 ¶
- 路由与策略:支持按模型、标签、健康状态或策略选择目标 Provider。 - 流式处理:统一流式分片结构,便于实时输出与增量聚合。 - 韧性机制:支持重试、退避、熔断、超时与降级。 - 多级缓存:支持本地与远端缓存协同,减少重复调用。 - 可观测性:支持指标、追踪与成本统计。 - 工具调用:支持函数调用与工具执行闭环。 - 中间件链:通过 ChainProviderMiddleware 组合多个中间件增强 Provider。
集成建议 ¶
- 单 Provider 场景,优先直接使用对应实现并开启基础重试。 - 多 Provider 场景,优先通过路由器统一入口并配置回退策略。 - 高并发场景,建议叠加限流、缓存和熔断配置。 - 生产场景,建议启用观测能力并接入统一告警。
相关子包 ¶
- llm/providers:各模型服务商适配实现。 - llm/router:路由策略与选择逻辑。 - llm/cache:缓存实现与策略。 - llm/retry:重试与退避策略。 - llm/observability:指标、追踪与成本观测。 - llm/tools:工具调用、ReAct 执行与外部工具集成。 - llm/embedding:文本嵌入 Provider 接口与实现。 - llm/image:图像生成与编辑 Provider 接口与实现。 - llm/video:视频分析与生成 Provider 接口与实现。 - llm/rerank:文档重排序 Provider 接口与实现。 - llm/batch:批量请求处理器。 - llm/budget:Token 预算管理与告警。 - llm/circuitbreaker:熔断器实现。 - llm/config:配置类型与路由权重定义。 - llm/factory:Provider 工厂与注册表。
Index ¶
- Constants
- Variables
- func WithCredentialOverride(ctx context.Context, c CredentialOverride) context.Context
- type APIKeyPool
- func (p *APIKeyPool) GetStats() map[uint]*APIKeyStats
- func (p *APIKeyPool) LoadKeys(ctx context.Context) error
- func (p *APIKeyPool) RecordFailure(ctx context.Context, keyID uint, errMsg string) error
- func (p *APIKeyPool) RecordSuccess(ctx context.Context, keyID uint) error
- func (p *APIKeyPool) SelectKey(ctx context.Context) (*LLMProviderAPIKey, error)
- type APIKeySelectionStrategy
- type APIKeyStats
- type AudioGenerationRequest
- type AudioGenerationResponse
- type AudioTranscriptionRequest
- type AudioTranscriptionResponse
- type AuditEvent
- type AuditFilter
- type AuditLog
- type AuditLogger
- type CanaryConfig
- func (c *CanaryConfig) GetAllDeployments() []*CanaryDeployment
- func (c *CanaryConfig) GetDeployment(providerID uint) *CanaryDeployment
- func (c *CanaryConfig) RemoveDeployment(providerID uint)
- func (c *CanaryConfig) SetDeployment(deployment *CanaryDeployment) error
- func (c *CanaryConfig) Stop()
- func (c *CanaryConfig) TriggerRollback(providerID uint, reason string) error
- func (c *CanaryConfig) UpdateStage(providerID uint, newStage CanaryStage) error
- type CanaryDeployment
- type CanaryMonitor
- type CanaryStage
- type ChatChoice
- type ChatRequest
- type ChatResponse
- type ChatUsage
- type CircuitBreakerConfig
- type CircuitState
- type CredentialOverride
- type DefaultProviderFactory
- type Embedding
- type EmbeddingProvider
- type EmbeddingRequest
- type EmbeddingResponse
- type Error
- type ErrorCode
- type FineTuningError
- type FineTuningJob
- type FineTuningJobRequest
- type FineTuningProvider
- type HealthMonitor
- func (m *HealthMonitor) ForceHealthCheck(providerCode string) error
- func (m *HealthMonitor) GetAllProviderStats() []ProviderHealthStats
- func (m *HealthMonitor) GetCurrentQPS(providerCode string) int
- func (m *HealthMonitor) GetHealthScore(providerCode string) float64
- func (m *HealthMonitor) IncrementQPS(providerCode string)
- func (m *HealthMonitor) SetMaxQPS(providerCode string, maxQPS int)
- func (m *HealthMonitor) Stop()
- func (m *HealthMonitor) UpdateProbe(providerCode string, st *HealthStatus, err error)
- type HealthStatus
- type Identity
- type Image
- type ImageContent
- type ImageGenerationRequest
- type ImageGenerationResponse
- type JSONSchemaParam
- type LLMModel
- type LLMModelExtended
- type LLMProvider
- type LLMProviderAPIKey
- type LLMProviderExtended
- type LLMProviderModel
- type LLMProviderStatus
- type Message
- type Model
- type MultiModalProvider
- type MultiProviderRouter
- func (r *MultiProviderRouter) GetAPIKeyPool(providerID uint) *APIKeyPool
- func (r *MultiProviderRouter) GetAPIKeyStats() map[uint]map[uint]*APIKeyStats
- func (r *MultiProviderRouter) InitAPIKeyPools(ctx context.Context) error
- func (r *MultiProviderRouter) RecordAPIKeyUsage(ctx context.Context, providerID uint, keyID uint, success bool, errMsg string) error
- func (r *MultiProviderRouter) SelectAPIKey(ctx context.Context, providerID uint) (*LLMProviderAPIKey, error)
- func (r *MultiProviderRouter) SelectProviderWithModel(ctx context.Context, modelName string, strategy RoutingStrategy) (*ProviderSelection, error)
- type NoOpAuditLogger
- type NoOpRateLimiter
- type NoOpSecurityProvider
- type NoOpSpan
- type NoOpTracer
- type Provider
- type ProviderEndpoints
- type ProviderFactory
- type ProviderHealthStats
- type ProviderMiddleware
- type ProviderMiddlewareFunc
- type ProviderProbeResult
- type ProviderRegistry
- func (r *ProviderRegistry) Default() (Provider, error)
- func (r *ProviderRegistry) Get(name string) (Provider, bool)
- func (r *ProviderRegistry) Len() int
- func (r *ProviderRegistry) List() []string
- func (r *ProviderRegistry) Register(name string, p Provider)
- func (r *ProviderRegistry) SetDefault(name string) error
- func (r *ProviderRegistry) Unregister(name string)
- type ProviderSelection
- type ProviderStats
- type QPSCounter
- type RateLimiter
- type ResilientConfig
- type ResilientProvider
- func (rp *ResilientProvider) Completion(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
- func (rp *ResilientProvider) Endpoints() ProviderEndpoints
- func (rp *ResilientProvider) HealthCheck(ctx context.Context) (*HealthStatus, error)
- func (rp *ResilientProvider) ListModels(ctx context.Context) ([]Model, error)
- func (rp *ResilientProvider) Name() string
- func (rp *ResilientProvider) Stream(ctx context.Context, req *ChatRequest) (<-chan StreamChunk, error)
- func (rp *ResilientProvider) SupportsNativeFunctionCalling() bool
- type ResponseFormat
- type ResponseFormatType
- type RetryPolicy
- type Role
- type Router
- type RouterOptions
- type RoutingStrategy
- type SecurityProvider
- type Span
- type StreamChunk
- type ThoughtChain
- type ThoughtSignature
- type ThoughtSignatureManager
- func (m *ThoughtSignatureManager) AddSignature(chainID string, sig ThoughtSignature) error
- func (m *ThoughtSignatureManager) CleanExpired()
- func (m *ThoughtSignatureManager) CreateChain(id string) *ThoughtChain
- func (m *ThoughtSignatureManager) GetChain(id string) *ThoughtChain
- func (m *ThoughtSignatureManager) GetLatestSignatures(chainID string, count int) []ThoughtSignature
- type ThoughtSignatureMiddleware
- func (m *ThoughtSignatureMiddleware) Completion(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
- func (m *ThoughtSignatureMiddleware) Endpoints() ProviderEndpoints
- func (m *ThoughtSignatureMiddleware) HealthCheck(ctx context.Context) (*HealthStatus, error)
- func (m *ThoughtSignatureMiddleware) ListModels(ctx context.Context) ([]Model, error)
- func (m *ThoughtSignatureMiddleware) Name() string
- func (m *ThoughtSignatureMiddleware) Stream(ctx context.Context, req *ChatRequest) (<-chan StreamChunk, error)
- func (m *ThoughtSignatureMiddleware) SupportsNativeFunctionCalling() bool
- type TokenUsage
- type ToolCall
- type ToolResult
- type ToolSchema
- type Tracer
- type TranscriptionSegment
- type Video
- type VideoGenerationRequest
- type VideoGenerationResponse
Constants ¶
const ( RoleSystem = types.RoleSystem RoleUser = types.RoleUser RoleAssistant = types.RoleAssistant RoleTool = types.RoleTool )
重导出常量.
const ( ErrInvalidRequest = types.ErrInvalidRequest ErrAuthentication = types.ErrAuthentication ErrForbidden = types.ErrForbidden ErrRateLimit = types.ErrRateLimit ErrRateLimited = types.ErrRateLimited ErrQuotaExceeded = types.ErrQuotaExceeded ErrModelNotFound = types.ErrModelNotFound ErrModelOverloaded = types.ErrModelOverloaded ErrContextTooLong = types.ErrContextTooLong ErrContentFiltered = types.ErrContentFiltered ErrUpstreamError = types.ErrUpstreamError ErrUpstreamTimeout = types.ErrUpstreamTimeout ErrTimeout = types.ErrTimeout ErrInternalError = types.ErrInternalError )
重导出错误码.
const ( CircuitClosed = circuitbreaker.StateClosed CircuitOpen = circuitbreaker.StateOpen CircuitHalfOpen = circuitbreaker.StateHalfOpen )
断路器状态常量。
Variables ¶
var ( ErrNoAvailableAPIKey = errors.New("no available API key") ErrAllKeysRateLimited = errors.New("all API keys are rate limited") )
var ErrCircuitOpen = errors.New("circuit breaker is open")
打开电路时返回 Err Circuit Open 。
Functions ¶
func WithCredentialOverride ¶
func WithCredentialOverride(ctx context.Context, c CredentialOverride) context.Context
WithCredentialOverride 在 ctx 中写入凭据覆盖信息。 传入空的 APIKey/SecretKey 不会改变 ctx。
Types ¶
type APIKeyPool ¶
type APIKeyPool struct {
// contains filtered or unexported fields
}
APIKeyPool API Key 池管理器
func NewAPIKeyPool ¶
func NewAPIKeyPool(db *gorm.DB, providerID uint, strategy APIKeySelectionStrategy, logger *zap.Logger) *APIKeyPool
NewAPIKeyPool 创建 API Key 池
func (*APIKeyPool) LoadKeys ¶
func (p *APIKeyPool) LoadKeys(ctx context.Context) error
LoadKeys 从数据库加载 API Keys
func (*APIKeyPool) RecordFailure ¶
RecordFailure 记录失败使用
func (*APIKeyPool) RecordSuccess ¶
func (p *APIKeyPool) RecordSuccess(ctx context.Context, keyID uint) error
RecordSuccess 记录成功使用
func (*APIKeyPool) SelectKey ¶
func (p *APIKeyPool) SelectKey(ctx context.Context) (*LLMProviderAPIKey, error)
SelectKey 选择一个可用的 API Key
type APIKeySelectionStrategy ¶
type APIKeySelectionStrategy string
APIKeySelectionStrategy API Key 选择策略
const ( StrategyRoundRobin APIKeySelectionStrategy = "round_robin" // 轮询 StrategyWeightedRandom APIKeySelectionStrategy = "weighted_random" // 加权随机 StrategyPriority APIKeySelectionStrategy = "priority" // 优先级 StrategyLeastUsed APIKeySelectionStrategy = "least_used" // 最少使用 )
type APIKeyStats ¶
type APIKeyStats struct {
KeyID uint `json:"key_id"`
Label string `json:"label"`
BaseURL string `json:"base_url"`
Enabled bool `json:"enabled"`
IsHealthy bool `json:"is_healthy"`
TotalRequests int64 `json:"total_requests"`
FailedRequests int64 `json:"failed_requests"`
SuccessRate float64 `json:"success_rate"`
CurrentRPM int `json:"current_rpm"`
CurrentRPD int `json:"current_rpd"`
LastUsedAt *time.Time `json:"last_used_at"`
LastErrorAt *time.Time `json:"last_error_at"`
LastError string `json:"last_error"`
}
APIKeyStats API Key 统计信息
type AudioGenerationRequest ¶
type AudioGenerationRequest struct {
Model string `json:"model"` // 模型名称
Input string `json:"input"` // 输入文本
Voice string `json:"voice,omitempty"` // 语音类型
Speed float32 `json:"speed,omitempty"` // 语速(0.25 - 4.0)
ResponseFormat string `json:"response_format,omitempty"` // 响应格式(mp3, opus, aac, flac)
}
AudioGenerationRequest 表示音频/语音生成请求.
type AudioGenerationResponse ¶
type AudioGenerationResponse struct {
Audio []byte `json:"audio"` // 音频数据
}
AudioGenerationResponse 表示音频生成响应.
type AudioTranscriptionRequest ¶
type AudioTranscriptionRequest struct {
Model string `json:"model"` // 模型名称
File []byte `json:"file"` // 音频文件数据
Language string `json:"language,omitempty"` // 语言代码(如 "en", "zh")
Prompt string `json:"prompt,omitempty"` // 可选的提示文本
ResponseFormat string `json:"response_format,omitempty"` // 响应格式(json, text, srt, vtt)
Temperature float32 `json:"temperature,omitempty"` // 采样温度
}
AudioTranscriptionRequest 表示音频转录请求.
type AudioTranscriptionResponse ¶
type AudioTranscriptionResponse struct {
Text string `json:"text"` // 转录文本
Language string `json:"language,omitempty"` // 检测到的语言
Duration float64 `json:"duration,omitempty"` // 音频时长(秒)
Segments []TranscriptionSegment `json:"segments,omitempty"` // 分段信息
}
AudioTranscriptionResponse 表示音频转录响应.
type AuditEvent ¶
type AuditEvent struct {
Timestamp time.Time
EventType string // "agent.execute", "tool.call", "provider.request"
ActorID string
ActorType string
Resource string
Action string
Result string // "success", "failure"
Error string
Metadata map[string]any
}
AuditEvent 表示可审计的事件。
type AuditFilter ¶
type AuditFilter struct {
StartTime time.Time
EndTime time.Time
EventTypes []string
ActorID string
Resource string
}
AuditFilter 用于过滤审计日志查询。
type AuditLog ¶
type AuditLog struct {
ID uint
TenantID uint
UserID uint
Action string
ResourceType string
ResourceID string
Details map[string]any
CreatedAt time.Time
}
审计日志代表审计日志条目
type AuditLogger ¶
type AuditLogger interface {
Log(ctx context.Context, event AuditEvent) error
Query(ctx context.Context, filter AuditFilter) ([]AuditEvent, error)
}
AuditLogger 提供审计日志记录功能(框架级扩展点)。
注意:项目中存在三个 AuditLogger 接口,各自服务不同领域,无法统一:
- llm.AuditLogger(本接口) — 框架级,记录 AuditEvent(通用事件)
- llm/tools.AuditLogger — 工具层,记录 *AuditEntry(工具调用/权限/成本),含 LogAsync/Close
- agent/guardrails.AuditLogger — 护栏层,记录 *AuditLogEntry(验证失败/PII/注入),含 Count
三者的事件类型、过滤器结构和方法签名均不同,统一会导致接口膨胀。
type CanaryConfig ¶
type CanaryConfig struct {
// contains filtered or unexported fields
}
func NewCanaryConfig ¶
func NewCanaryConfig(db *gorm.DB, logger *zap.Logger) *CanaryConfig
func (*CanaryConfig) GetAllDeployments ¶
func (c *CanaryConfig) GetAllDeployments() []*CanaryDeployment
GetAllDeployments 获取所有活跃的金丝雀部署
func (*CanaryConfig) GetDeployment ¶
func (c *CanaryConfig) GetDeployment(providerID uint) *CanaryDeployment
GetDeployment 获取指定 Provider 的金丝雀部署配置
func (*CanaryConfig) RemoveDeployment ¶
func (c *CanaryConfig) RemoveDeployment(providerID uint)
RemoveDeployment 移除金丝雀部署(完成或回滚后清理)
func (*CanaryConfig) SetDeployment ¶
func (c *CanaryConfig) SetDeployment(deployment *CanaryDeployment) error
SetDeployment 设置金丝雀部署
func (*CanaryConfig) Stop ¶
func (c *CanaryConfig) Stop()
func (*CanaryConfig) TriggerRollback ¶
func (c *CanaryConfig) TriggerRollback(providerID uint, reason string) error
TriggerRollback 触发回滚
func (*CanaryConfig) UpdateStage ¶
func (c *CanaryConfig) UpdateStage(providerID uint, newStage CanaryStage) error
UpdateStage 更新部署阶段
type CanaryDeployment ¶
type CanaryMonitor ¶
type CanaryMonitor struct {
// contains filtered or unexported fields
}
func NewCanaryMonitor ¶
func NewCanaryMonitor(db *gorm.DB, canaryConfig *CanaryConfig, logger *zap.Logger) *CanaryMonitor
func (*CanaryMonitor) Start ¶
func (m *CanaryMonitor) Start(ctx context.Context)
func (*CanaryMonitor) Stop ¶
func (m *CanaryMonitor) Stop()
type CanaryStage ¶
type CanaryStage string
const ( CanaryStageInit CanaryStage = "init" CanaryStage10Pct CanaryStage = "10pct" CanaryStage50Pct CanaryStage = "50pct" CanaryStage100Pct CanaryStage = "100pct" CanaryStageRollback CanaryStage = "rollback" )
type ChatChoice ¶
type ChatChoice struct {
Index int `json:"index"`
FinishReason string `json:"finish_reason,omitempty"`
Message Message `json:"message"`
}
ChatChoice 表示响应中的单个选项.
func FirstChoice ¶
func FirstChoice(resp *ChatResponse) (ChatChoice, error)
FirstChoice safely returns the first choice from a ChatResponse. Returns an error if the response is nil or has no choices.
func MustFirstChoice ¶
func MustFirstChoice(resp *ChatResponse) ChatChoice
MustFirstChoice returns the first choice or panics. Use only in contexts where empty choices is truly unexpected.
type ChatRequest ¶
type ChatRequest struct {
TraceID string `json:"trace_id"`
TenantID string `json:"tenant_id,omitempty"`
UserID string `json:"user_id,omitempty"`
Model string `json:"model"`
Messages []Message `json:"messages"`
MaxTokens int `json:"max_tokens,omitempty"`
Temperature float32 `json:"temperature,omitempty"`
TopP float32 `json:"top_p,omitempty"`
Stop []string `json:"stop,omitempty"`
Tools []ToolSchema `json:"tools,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"`
ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
Timeout time.Duration `json:"timeout,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Tags []string `json:"tags,omitempty"`
// 扩展字段
ReasoningMode string `json:"reasoning_mode,omitempty"`
PreviousResponseID string `json:"previous_response_id,omitempty"`
ThoughtSignatures []string `json:"thought_signatures,omitempty"`
}
ChatRequest 表示聊天补全请求.
type ChatResponse ¶
type ChatResponse struct {
ID string `json:"id,omitempty"`
Provider string `json:"provider,omitempty"`
Model string `json:"model"`
Choices []ChatChoice `json:"choices"`
Usage ChatUsage `json:"usage"`
CreatedAt time.Time `json:"created_at"`
ThoughtSignatures []string `json:"thought_signatures,omitempty"`
}
ChatResponse 表示聊天补全响应.
type ChatUsage ¶
type ChatUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
ChatUsage 表示响应中的 token 用量。
type CircuitBreakerConfig ¶
type CircuitBreakerConfig struct {
FailureThreshold int `json:"failure_threshold"`
SuccessThreshold int `json:"success_threshold"`
Timeout time.Duration `json:"timeout"`
}
CircuitBreakerConfig配置断路器.
func DefaultCircuitBreakerConfig ¶
func DefaultCircuitBreakerConfig() *CircuitBreakerConfig
默认 CircuitBreakerConfig 返回合理的默认值 。
type CredentialOverride ¶
CredentialOverride 用于在单次请求内覆盖 Provider 凭据。 注意:该结构仅通过 context 传递,不会从 API JSON 反序列化,避免前端直接注入敏感信息。
func CredentialOverrideFromContext ¶
func CredentialOverrideFromContext(ctx context.Context) (CredentialOverride, bool)
CredentialOverrideFromContext 从 ctx 读取凭据覆盖信息。
func (CredentialOverride) MarshalJSON ¶
func (c CredentialOverride) MarshalJSON() ([]byte, error)
func (CredentialOverride) String ¶
func (c CredentialOverride) String() string
type DefaultProviderFactory ¶
type DefaultProviderFactory struct {
// contains filtered or unexported fields
}
DefaultProviderFactory is a thread-safe in-memory implementation of ProviderFactory.
func NewDefaultProviderFactory ¶
func NewDefaultProviderFactory() *DefaultProviderFactory
NewDefaultProviderFactory creates a new DefaultProviderFactory.
func (*DefaultProviderFactory) CreateProvider ¶
func (f *DefaultProviderFactory) CreateProvider(providerCode string, apiKey string, baseURL string) (Provider, error)
CreateProvider creates a provider instance by code.
func (*DefaultProviderFactory) RegisterProvider ¶
func (f *DefaultProviderFactory) RegisterProvider(code string, constructor func(apiKey, baseURL string) (Provider, error))
RegisterProvider registers a provider constructor by code.
type Embedding ¶
type Embedding struct {
Object string `json:"object"`
Index int `json:"index"`
Embedding []float64 `json:"embedding"`
}
Embedding 表示单个嵌入向量.
type EmbeddingProvider ¶
type EmbeddingProvider interface {
Provider
// CreateEmbedding 为给定输入创建嵌入.
CreateEmbedding(ctx context.Context, req *EmbeddingRequest) (*EmbeddingResponse, error)
}
EmbeddingProvider 扩展 Provider,具有嵌入能力.
type EmbeddingRequest ¶
type EmbeddingRequest struct {
Model string `json:"model"` // 模型名称
Input []string `json:"input"` // 输入文本列表
EncodingFormat string `json:"encoding_format,omitempty"` // 编码格式(float, base64)
Dimensions int `json:"dimensions,omitempty"` // 输出维度
User string `json:"user,omitempty"` // 用户标识
}
EmbeddingRequest 表示嵌入请求.
type EmbeddingResponse ¶
type EmbeddingResponse struct {
Object string `json:"object"`
Data []Embedding `json:"data"`
Model string `json:"model"`
Usage ChatUsage `json:"usage"`
}
EmbeddingResponse 表示嵌入响应.
type FineTuningError ¶
type FineTuningError struct {
Code string `json:"code"`
Message string `json:"message"`
Param string `json:"param,omitempty"`
}
FineTuningError 表示微调错误.
type FineTuningJob ¶
type FineTuningJob struct {
ID string `json:"id"`
Object string `json:"object"`
Model string `json:"model"`
CreatedAt int64 `json:"created_at"`
FinishedAt int64 `json:"finished_at,omitempty"`
FineTunedModel string `json:"fine_tuned_model,omitempty"`
OrganizationID string `json:"organization_id"`
ResultFiles []string `json:"result_files"`
Status string `json:"status"` // queued, running, succeeded, failed, cancelled
ValidationFile string `json:"validation_file,omitempty"`
TrainingFile string `json:"training_file"`
Hyperparameters map[string]any `json:"hyperparameters"`
TrainedTokens int `json:"trained_tokens,omitempty"`
Error *FineTuningError `json:"error,omitempty"`
}
FineTuningJob 表示微调任务.
type FineTuningJobRequest ¶
type FineTuningJobRequest struct {
Model string `json:"model"` // 基础模型
TrainingFile string `json:"training_file"` // 训练文件 ID
ValidationFile string `json:"validation_file,omitempty"` // 验证文件 ID
Hyperparameters map[string]any `json:"hyperparameters,omitempty"` // 超参数
Suffix string `json:"suffix,omitempty"` // 模型名称后缀
IntegrationIDs []string `json:"integration_ids,omitempty"` // 集成 ID
}
FineTuningJobRequest 表示创建微调任务的请求.
type FineTuningProvider ¶
type FineTuningProvider interface {
Provider
// CreateFineTuningJob 创建微调任务.
CreateFineTuningJob(ctx context.Context, req *FineTuningJobRequest) (*FineTuningJob, error)
// ListFineTuningJobs 列出微调任务.
ListFineTuningJobs(ctx context.Context) ([]FineTuningJob, error)
// GetFineTuningJob 通过 ID 获取微调任务.
GetFineTuningJob(ctx context.Context, jobID string) (*FineTuningJob, error)
// CancelFineTuningJob 取消微调任务.
CancelFineTuningJob(ctx context.Context, jobID string) error
}
FineTuningProvider 扩展 Provider,具有微调能力.
type HealthMonitor ¶
type HealthMonitor struct {
// contains filtered or unexported fields
}
func NewHealthMonitor ¶
func NewHealthMonitor(db *gorm.DB) *HealthMonitor
func (*HealthMonitor) ForceHealthCheck ¶
func (m *HealthMonitor) ForceHealthCheck(providerCode string) error
ForceHealthCheck 强制立即检查指定 Provider 的健康状态
func (*HealthMonitor) GetAllProviderStats ¶
func (m *HealthMonitor) GetAllProviderStats() []ProviderHealthStats
GetAllProviderStats 获取所有 Provider 的健康统计 使用写锁,因为 getCurrentQPSUnsafe 内部调用 bumpWindow 会修改计数器状态。
func (*HealthMonitor) GetCurrentQPS ¶
func (m *HealthMonitor) GetCurrentQPS(providerCode string) int
GetCurrentQPS 获取当前 QPS 使用写锁,因为 getCurrentQPSUnsafe 内部调用 bumpWindow 会修改计数器状态。
func (*HealthMonitor) GetHealthScore ¶
func (m *HealthMonitor) GetHealthScore(providerCode string) float64
GetHealthScore 获取 Provider 的健康分数 (0-1) 使用写锁,因为 getCurrentQPSUnsafe 内部调用 bumpWindow 会修改计数器状态。
func (*HealthMonitor) IncrementQPS ¶
func (m *HealthMonitor) IncrementQPS(providerCode string)
IncrementQPS 记录一次请求
func (*HealthMonitor) SetMaxQPS ¶
func (m *HealthMonitor) SetMaxQPS(providerCode string, maxQPS int)
SetMaxQPS 设置 Provider 的最大 QPS(0 表示无限制)
func (*HealthMonitor) Stop ¶
func (m *HealthMonitor) Stop()
func (*HealthMonitor) UpdateProbe ¶
func (m *HealthMonitor) UpdateProbe(providerCode string, st *HealthStatus, err error)
type HealthStatus ¶
type HealthStatus struct {
Healthy bool `json:"healthy"`
Latency time.Duration `json:"latency"`
ErrorRate float64 `json:"error_rate"`
Message string `json:"message,omitempty"`
}
HealthStatus 表示提供者的健康检查结果。 这是 Provider 级别的统一健康状态类型,同时被 llm.Provider 和 llm/embedding.Provider 使用。
type Identity ¶
type Identity struct {
ID string
Type string // "agent", "user", "service"
Permissions []string
Roles []string
Metadata map[string]any
}
Identity 表示代理或用户身份。
type Image ¶
type Image struct {
URL string `json:"url,omitempty"` // 图片 URL
B64JSON string `json:"b64_json,omitempty"` // Base64 编码的图片
RevisedPrompt string `json:"revised_prompt,omitempty"` // 修订后的提示
}
Image 表示生成的图像.
type ImageGenerationRequest ¶
type ImageGenerationRequest struct {
Model string `json:"model"` // 模型名称
Prompt string `json:"prompt"` // 文本提示
NegativePrompt string `json:"negative_prompt,omitempty"` // 负面提示
N int `json:"n,omitempty"` // 生成图片数量
Size string `json:"size,omitempty"` // 图片尺寸(如 "1024x1024")
Quality string `json:"quality,omitempty"` // 图片质量(standard, hd)
Style string `json:"style,omitempty"` // 图片风格
ResponseFormat string `json:"response_format,omitempty"` // 响应格式(url, b64_json)
User string `json:"user,omitempty"` // 用户标识
}
ImageGenerationRequest 表示图像生成请求.
type ImageGenerationResponse ¶
ImageGenerationResponse 表示图像生成响应.
type JSONSchemaParam ¶ added in v1.0.0
type JSONSchemaParam struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Schema map[string]any `json:"schema"`
Strict *bool `json:"strict,omitempty"`
}
JSONSchemaParam 定义 JSON Schema 参数,用于 json_schema 响应格式。
type LLMModel ¶
type LLMModel struct {
ID uint `gorm:"primaryKey" json:"id"`
ModelName string `gorm:"size:100;not null;uniqueIndex" json:"model_name"`
DisplayName string `gorm:"size:200" json:"display_name"`
Description string `gorm:"type:text" json:"description"`
Enabled bool `gorm:"default:true" json:"enabled"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
LLMMOdel代表抽象模型(例如gpt-4,claude-3-opus).
type LLMModelExtended ¶
type LLMModelExtended struct {
LLMModel
ProviderModels []LLMProviderModel `gorm:"foreignKey:ModelID" json:"provider_models,omitempty"`
}
LLMMOdel Extended 扩展 LLMMOdel 与协会
type LLMProvider ¶
type LLMProvider struct {
ID uint `gorm:"primaryKey" json:"id"`
Code string `gorm:"size:50;not null;uniqueIndex" json:"code"`
Name string `gorm:"size:200;not null" json:"name"`
Description string `gorm:"type:text" json:"description"`
Status LLMProviderStatus `gorm:"default:1" json:"status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
LLMProvider 代表提供商( 如 OpenAI, Anthropic, DeepSeek)
func (LLMProvider) TableName ¶
func (LLMProvider) TableName() string
type LLMProviderAPIKey ¶
type LLMProviderAPIKey struct {
ID uint `gorm:"primaryKey" json:"id"`
ProviderID uint `gorm:"not null;index:idx_provider_api_keys_provider_id" json:"provider_id"`
APIKey string `gorm:"size:500;not null" json:"api_key"`
BaseURL string `gorm:"size:500" json:"base_url"`
Label string `gorm:"size:100" json:"label"`
Priority int `gorm:"default:100" json:"priority"`
Weight int `gorm:"default:100" json:"weight"`
Enabled bool `gorm:"default:true" json:"enabled"`
// 使用统计
TotalRequests int64 `gorm:"default:0" json:"total_requests"`
FailedRequests int64 `gorm:"default:0" json:"failed_requests"`
LastUsedAt *time.Time `json:"last_used_at"`
LastErrorAt *time.Time `json:"last_error_at"`
LastError string `gorm:"type:text" json:"last_error"`
// 限制费率
RateLimitRPM int `gorm:"default:0" json:"rate_limit_rpm"`
RateLimitRPD int `gorm:"default:0" json:"rate_limit_rpd"`
CurrentRPM int `gorm:"default:0" json:"current_rpm"`
CurrentRPD int `gorm:"default:0" json:"current_rpd"`
RPMResetAt time.Time `json:"rpm_reset_at"`
RPDResetAt time.Time `json:"rpd_reset_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Provider *LLMProvider `gorm:"foreignKey:ProviderID" json:"provider,omitempty"`
}
LLMProviderAPIKey 代表池中的 API 密钥 每个提供者支持多个 API 密钥进行负载平衡和失效
func (*LLMProviderAPIKey) IncrementUsage ¶
func (k *LLMProviderAPIKey) IncrementUsage(success bool)
递增使用计数器
func (LLMProviderAPIKey) TableName ¶
func (LLMProviderAPIKey) TableName() string
type LLMProviderExtended ¶
type LLMProviderExtended struct {
LLMProvider
APIKeys []LLMProviderAPIKey `gorm:"foreignKey:ProviderID" json:"api_keys,omitempty"`
ProviderModels []LLMProviderModel `gorm:"foreignKey:ProviderID" json:"provider_models,omitempty"`
}
LLMProvider Extended 扩展 LLMProvider 协会
type LLMProviderModel ¶
type LLMProviderModel struct {
ID uint `gorm:"primaryKey" json:"id"`
ModelID uint `gorm:"not null;index:idx_model_provider" json:"model_id"`
ProviderID uint `gorm:"not null;index:idx_model_provider;index:idx_provider_models_provider_id" json:"provider_id"`
RemoteModelName string `gorm:"size:100;not null" json:"remote_model_name"`
BaseURL string `gorm:"size:500" json:"base_url"`
PriceInput float64 `gorm:"type:decimal(10,6);default:0" json:"price_input"`
PriceCompletion float64 `gorm:"type:decimal(10,6);default:0" json:"price_completion"`
MaxTokens int `gorm:"default:0" json:"max_tokens"`
Priority int `gorm:"default:100" json:"priority"`
Enabled bool `gorm:"default:true" json:"enabled"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Model *LLMModel `gorm:"foreignKey:ModelID" json:"model,omitempty"`
Provider *LLMProvider `gorm:"foreignKey:ProviderID" json:"provider,omitempty"`
}
LLMProvider Model 代表提供商的模型实例( 多人对多人映射) 多个提供者(OpenAI、Azure、Cloudflare)可提供同一模型(例如 gpt-4)
func (LLMProviderModel) TableName ¶
func (LLMProviderModel) TableName() string
type LLMProviderStatus ¶
type LLMProviderStatus int16
LLMProvider 状态代表 LLM 提供者的地位
const ( LLMProviderStatusInactive LLMProviderStatus = 0 LLMProviderStatusActive LLMProviderStatus = 1 LLMProviderStatusDisabled LLMProviderStatus = 2 )
func (LLMProviderStatus) String ¶
func (s LLMProviderStatus) String() string
String returns the string representation of LLMProviderStatus.
type Model ¶
type Model struct {
ID string `json:"id"` // 模型 ID(API 调用时使用)
Object string `json:"object"` // 对象类型(通常是 "model")
Created int64 `json:"created"` // 创建时间戳
OwnedBy string `json:"owned_by"` // 所属组织
Permissions []string `json:"permissions"` // 权限列表
Root string `json:"root"` // 根模型
Parent string `json:"parent"` // 父模型
MaxInputTokens int `json:"max_input_tokens,omitempty"` // 最大输入 token 数
MaxOutputTokens int `json:"max_output_tokens,omitempty"` // 最大输出 token 数
Capabilities []string `json:"capabilities,omitempty"` // 模型能力列表
}
Model 表示提供者支持的一个模型.
type MultiModalProvider ¶
type MultiModalProvider interface {
Provider
// GenerateImage 从文本提示生成图像.
// 如果提供者不支持图像生成,则返回 nil。
GenerateImage(ctx context.Context, req *ImageGenerationRequest) (*ImageGenerationResponse, error)
// GenerateVideo 从文本提示生成视频.
// 如果提供者不支持视频生成,则返回 nil。
GenerateVideo(ctx context.Context, req *VideoGenerationRequest) (*VideoGenerationResponse, error)
// GenerateAudio 从文本生成音频/语音.
// 如果提供者不支持音频生成,则返回 nil。
GenerateAudio(ctx context.Context, req *AudioGenerationRequest) (*AudioGenerationResponse, error)
// TranscribeAudio 将音频转录为文本.
// 如果提供者不支持音频转录,则返回 nil。
TranscribeAudio(ctx context.Context, req *AudioTranscriptionRequest) (*AudioTranscriptionResponse, error)
}
MultiModalProvider 扩展 Provider,具有多模态能力.
type MultiProviderRouter ¶
type MultiProviderRouter struct {
*Router // 继承原有路由器
// contains filtered or unexported fields
}
MultiProviderRouter 多提供商路由器(支持同模型多提供商)
func NewMultiProviderRouter ¶
func NewMultiProviderRouter(db *gorm.DB, providerFactory ProviderFactory, opts RouterOptions) *MultiProviderRouter
NewMultiProviderRouter 创建多提供商路由器
func (*MultiProviderRouter) GetAPIKeyPool ¶
func (r *MultiProviderRouter) GetAPIKeyPool(providerID uint) *APIKeyPool
GetAPIKeyPool 获取指定提供商的 API Key 池
func (*MultiProviderRouter) GetAPIKeyStats ¶
func (r *MultiProviderRouter) GetAPIKeyStats() map[uint]map[uint]*APIKeyStats
GetAPIKeyStats 获取所有 API Key 统计信息
func (*MultiProviderRouter) InitAPIKeyPools ¶
func (r *MultiProviderRouter) InitAPIKeyPools(ctx context.Context) error
InitAPIKeyPools 初始化 API Key 池
func (*MultiProviderRouter) RecordAPIKeyUsage ¶
func (r *MultiProviderRouter) RecordAPIKeyUsage(ctx context.Context, providerID uint, keyID uint, success bool, errMsg string) error
RecordAPIKeyUsage 记录 API Key 使用情况
func (*MultiProviderRouter) SelectAPIKey ¶
func (r *MultiProviderRouter) SelectAPIKey(ctx context.Context, providerID uint) (*LLMProviderAPIKey, error)
SelectAPIKey 为指定提供商选择 API Key
func (*MultiProviderRouter) SelectProviderWithModel ¶
func (r *MultiProviderRouter) SelectProviderWithModel(ctx context.Context, modelName string, strategy RoutingStrategy) (*ProviderSelection, error)
SelectProviderWithModel 根据模型名选择最佳提供商(支持多对多)
type NoOpAuditLogger ¶
type NoOpAuditLogger struct{}
NoOpAuditLogger 是空操作实现。
func (*NoOpAuditLogger) Log ¶
func (n *NoOpAuditLogger) Log(ctx context.Context, event AuditEvent) error
func (*NoOpAuditLogger) Query ¶
func (n *NoOpAuditLogger) Query(ctx context.Context, filter AuditFilter) ([]AuditEvent, error)
type NoOpRateLimiter ¶
type NoOpRateLimiter struct{}
NoOpRateLimiter 是空操作实现。
type NoOpSecurityProvider ¶
type NoOpSecurityProvider struct{}
NoOpSecurityProvider 是空操作实现。
func (*NoOpSecurityProvider) Authenticate ¶
type Provider ¶
type Provider interface {
// Completion 发送同步聊天补全请求。
Completion(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
// Stream 发送流式聊天请求。
Stream(ctx context.Context, req *ChatRequest) (<-chan StreamChunk, error)
// HealthCheck 执行轻量级健康检查。
HealthCheck(ctx context.Context) (*HealthStatus, error)
// Name 返回提供者的唯一标识符。
Name() string
// SupportsNativeFunctionCalling 返回是否支持原生函数调用。
SupportsNativeFunctionCalling() bool
// ListModels 返回提供者支持的可用模型列表。
// 如果提供者不支持列出模型,则返回 nil。
ListModels(ctx context.Context) ([]Model, error)
// Endpoints 返回该提供者使用的所有 API 端点完整 URL,用于调试和配置验证。
Endpoints() ProviderEndpoints
}
Provider 定义了统一的 LLM 适配器接口.
func ChainProviderMiddleware ¶
func ChainProviderMiddleware(provider Provider, middlewares ...ProviderMiddleware) Provider
ChainProviderMiddleware 将多个中间件串联起来。
type ProviderEndpoints ¶ added in v1.0.0
type ProviderEndpoints struct {
Completion string `json:"completion"` // 聊天补全端点
Stream string `json:"stream,omitempty"` // 流式端点(如果与 Completion 不同)
Models string `json:"models"` // 模型列表端点
Health string `json:"health,omitempty"` // 健康检查端点(如果与 Models 不同)
BaseURL string `json:"base_url"` // 基础 URL
}
ProviderEndpoints 描述提供者使用的所有 API 端点。
type ProviderFactory ¶
type ProviderFactory interface {
CreateProvider(providerCode string, apiKey string, baseURL string) (Provider, error)
}
ProviderFactory creates Provider instances from provider code, API key, and base URL.
type ProviderHealthStats ¶
type ProviderMiddleware ¶
ProviderMiddleware 包装提供者以添加额外功能。
type ProviderMiddlewareFunc ¶
ProviderMiddlewareFunc 是 ProviderMiddleware 的函数适配器。
func (ProviderMiddlewareFunc) Wrap ¶
func (f ProviderMiddlewareFunc) Wrap(next Provider) Provider
type ProviderProbeResult ¶
type ProviderRegistry ¶
type ProviderRegistry struct {
// contains filtered or unexported fields
}
ProviderRegistry is a thread-safe registry for managing multiple LLM providers. It supports registering, retrieving, and listing providers, as well as designating a default provider for convenience.
func NewProviderRegistry ¶
func NewProviderRegistry() *ProviderRegistry
NewProviderRegistry creates an empty ProviderRegistry.
func (*ProviderRegistry) Default ¶
func (r *ProviderRegistry) Default() (Provider, error)
Default returns the default provider. Returns an error if no default has been set or the default name is not registered.
func (*ProviderRegistry) Get ¶
func (r *ProviderRegistry) Get(name string) (Provider, bool)
Get retrieves a provider by name.
func (*ProviderRegistry) Len ¶
func (r *ProviderRegistry) Len() int
Len returns the number of registered providers.
func (*ProviderRegistry) List ¶
func (r *ProviderRegistry) List() []string
List returns the sorted names of all registered providers.
func (*ProviderRegistry) Register ¶
func (r *ProviderRegistry) Register(name string, p Provider)
Register adds a provider to the registry under the given name. If a provider with the same name already exists, it is replaced.
func (*ProviderRegistry) SetDefault ¶
func (r *ProviderRegistry) SetDefault(name string) error
SetDefault designates an existing registered provider as the default. Returns an error if the name is not registered.
func (*ProviderRegistry) Unregister ¶
func (r *ProviderRegistry) Unregister(name string)
Unregister removes a provider from the registry. If the removed provider was the default, the default is cleared.
type ProviderSelection ¶
type ProviderSelection struct {
Provider Provider
ProviderID uint
ProviderCode string
ModelID uint
ModelName string
IsCanary bool
Strategy RoutingStrategy
}
提供者选择代表选定的提供者
type ProviderStats ¶
type QPSCounter ¶
type QPSCounter struct {
// contains filtered or unexported fields
}
type RateLimiter ¶
type RateLimiter interface {
// Allow 检查是否允许该请求。
Allow(ctx context.Context, key string) (bool, error)
// AllowN 检查是否允许 N 个请求。
AllowN(ctx context.Context, key string, n int) (bool, error)
// Reset 重置指定 key 的速率限制。
Reset(ctx context.Context, key string) error
}
RateLimiter 控制请求速率。
type ResilientConfig ¶
type ResilientConfig struct {
RetryPolicy *RetryPolicy
CircuitBreaker *CircuitBreakerConfig
EnableIdempotency bool
IdempotencyTTL time.Duration
}
具有弹性的Config配置有弹性的提供者.
type ResilientProvider ¶
type ResilientProvider struct {
// contains filtered or unexported fields
}
耐活性 Provider用重试,断路器和一能来包裹一个提供者.
func NewResilientProvider ¶
func NewResilientProvider(provider Provider, config *ResilientConfig, logger *zap.Logger) *ResilientProvider
NewResylient Provider创建了具有弹性的提供者包装.
func NewResilientProviderSimple ¶
func NewResilientProviderSimple(provider Provider, _ any, logger *zap.Logger) *ResilientProvider
NewResilientProviderSimple 使用默认配置创建弹性 Provider. 这是简单用例的便捷函数。
func (*ResilientProvider) Completion ¶
func (rp *ResilientProvider) Completion(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
完成器具有弹性。
func (*ResilientProvider) Endpoints ¶ added in v1.0.0
func (rp *ResilientProvider) Endpoints() ProviderEndpoints
Endpoints 委托给被包装的提供者。
func (*ResilientProvider) HealthCheck ¶
func (rp *ResilientProvider) HealthCheck(ctx context.Context) (*HealthStatus, error)
健康检查工具 提供者。
func (*ResilientProvider) ListModels ¶
func (rp *ResilientProvider) ListModels(ctx context.Context) ([]Model, error)
ListModels 执行提供者 。
func (*ResilientProvider) Stream ¶
func (rp *ResilientProvider) Stream(ctx context.Context, req *ChatRequest) (<-chan StreamChunk, error)
Stream 执行器 提供器( 不重试进行 streaming) 。
func (*ResilientProvider) SupportsNativeFunctionCalling ¶
func (rp *ResilientProvider) SupportsNativeFunctionCalling() bool
支持 NativeFunctionCalling 执行提供者.
type ResponseFormat ¶ added in v1.0.0
type ResponseFormat struct {
Type ResponseFormatType `json:"type"`
JSONSchema *JSONSchemaParam `json:"json_schema,omitempty"`
}
ResponseFormat 定义 API 级别的结构化输出格式。
type ResponseFormatType ¶ added in v1.0.0
type ResponseFormatType string
ResponseFormatType 定义响应格式类型。
const ( ResponseFormatText ResponseFormatType = "text" ResponseFormatJSONObject ResponseFormatType = "json_object" ResponseFormatJSONSchema ResponseFormatType = "json_schema" )
type RetryPolicy ¶
type RetryPolicy struct {
MaxRetries int `json:"max_retries"`
InitialBackoff time.Duration `json:"initial_backoff"`
MaxBackoff time.Duration `json:"max_backoff"`
Multiplier float64 `json:"multiplier"`
}
重试政策定义了重试行为 。
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router 是 MultiProviderRouter 的基础结构体,提供 DB、健康监控等基础设施。
type RouterOptions ¶
type RouterOptions struct {
HealthCheckInterval time.Duration
HealthCheckTimeout time.Duration
Logger *zap.Logger
}
路由选项配置路由器
type RoutingStrategy ¶
type RoutingStrategy string
路线战略
const ( StrategyTagBased RoutingStrategy = "tag" StrategyCostBased RoutingStrategy = "cost" StrategyQPSBased RoutingStrategy = "qps" StrategyHealthBased RoutingStrategy = "health" StrategyCanary RoutingStrategy = "canary" )
type SecurityProvider ¶
type SecurityProvider interface {
// Authenticate 验证凭证并返回身份。
Authenticate(ctx context.Context, credentials any) (*Identity, error)
// Authorize 检查身份是否有对资源执行操作的权限。
Authorize(ctx context.Context, identity *Identity, resource string, action string) error
}
SecurityProvider 提供认证和授权功能。
type Span ¶
type Span interface {
SetAttribute(key string, value any)
AddEvent(name string, attributes map[string]any)
SetError(err error)
End()
}
Span 表示一个追踪 span。
type StreamChunk ¶
type StreamChunk struct {
ID string `json:"id,omitempty"`
Provider string `json:"provider,omitempty"`
Model string `json:"model,omitempty"`
Index int `json:"index,omitempty"`
Delta Message `json:"delta"`
FinishReason string `json:"finish_reason,omitempty"`
Usage *ChatUsage `json:"usage,omitempty"`
Err *Error `json:"error,omitempty"`
}
StreamChunk 表示流式响应块.
type ThoughtChain ¶
type ThoughtChain struct {
ID string `json:"id"`
Signatures []ThoughtSignature `json:"signatures"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
ThoughtChain 表示一组推理签名序列.
type ThoughtSignature ¶
type ThoughtSignature struct {
ID string `json:"id"`
Signature string `json:"signature"`
Model string `json:"model"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
Metadata map[string]any `json:"metadata,omitempty"`
}
ThoughtSignature 表示一个加密推理签名.
type ThoughtSignatureManager ¶
type ThoughtSignatureManager struct {
// contains filtered or unexported fields
}
ThoughtSignatureManager 管理思维签名以维持推理连续性.
func NewThoughtSignatureManager ¶
func NewThoughtSignatureManager(ttl time.Duration) *ThoughtSignatureManager
NewThoughtSignatureManager 创建新的签名管理器.
func (*ThoughtSignatureManager) AddSignature ¶
func (m *ThoughtSignatureManager) AddSignature(chainID string, sig ThoughtSignature) error
AddSignature 将签名添加到指定的链中。
func (*ThoughtSignatureManager) CleanExpired ¶
func (m *ThoughtSignatureManager) CleanExpired()
CleanExpired 清除过期的签名。
func (*ThoughtSignatureManager) CreateChain ¶
func (m *ThoughtSignatureManager) CreateChain(id string) *ThoughtChain
CreateChain 创建一条新的思维链.
func (*ThoughtSignatureManager) GetChain ¶
func (m *ThoughtSignatureManager) GetChain(id string) *ThoughtChain
GetChain 获取指定 ID 的思维链.
func (*ThoughtSignatureManager) GetLatestSignatures ¶
func (m *ThoughtSignatureManager) GetLatestSignatures(chainID string, count int) []ThoughtSignature
GetLatestSignatures 返回链中最新的签名。
type ThoughtSignatureMiddleware ¶
type ThoughtSignatureMiddleware struct {
// contains filtered or unexported fields
}
ThoughtSignatureMiddleware 包装提供者以处理思维签名.
func NewThoughtSignatureMiddleware ¶
func NewThoughtSignatureMiddleware(provider Provider, manager *ThoughtSignatureManager) *ThoughtSignatureMiddleware
NewThoughtSignatureMiddleware 创建新的思维签名中间件.
func (*ThoughtSignatureMiddleware) Completion ¶
func (m *ThoughtSignatureMiddleware) Completion(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
Completion 用思维签名处理包装提供者的补全调用.
func (*ThoughtSignatureMiddleware) Endpoints ¶ added in v1.0.0
func (m *ThoughtSignatureMiddleware) Endpoints() ProviderEndpoints
Endpoints 委托给被包装的提供者。
func (*ThoughtSignatureMiddleware) HealthCheck ¶
func (m *ThoughtSignatureMiddleware) HealthCheck(ctx context.Context) (*HealthStatus, error)
HealthCheck 委托给被包装的提供者。
func (*ThoughtSignatureMiddleware) ListModels ¶
func (m *ThoughtSignatureMiddleware) ListModels(ctx context.Context) ([]Model, error)
ListModels 委托给被包装的提供者.
func (*ThoughtSignatureMiddleware) Name ¶
func (m *ThoughtSignatureMiddleware) Name() string
Name 返回提供者名称。
func (*ThoughtSignatureMiddleware) Stream ¶
func (m *ThoughtSignatureMiddleware) Stream(ctx context.Context, req *ChatRequest) (<-chan StreamChunk, error)
Stream 用思维签名处理包装流式调用.
func (*ThoughtSignatureMiddleware) SupportsNativeFunctionCalling ¶
func (m *ThoughtSignatureMiddleware) SupportsNativeFunctionCalling() bool
SupportsNativeFunctionCalling 委托给被包装的提供者.
type TranscriptionSegment ¶
type TranscriptionSegment struct {
ID int `json:"id"`
Seek int `json:"seek"`
Start float64 `json:"start"`
End float64 `json:"end"`
Text string `json:"text"`
Tokens []int `json:"tokens"`
Temperature float32 `json:"temperature"`
AvgLogprob float64 `json:"avg_logprob"`
CompressionRatio float64 `json:"compression_ratio"`
NoSpeechProb float64 `json:"no_speech_prob"`
}
TranscriptionSegment 表示转录音频的一个片段.
type Video ¶
type Video struct {
URL string `json:"url,omitempty"` // 视频 URL
B64JSON string `json:"b64_json,omitempty"` // Base64 编码的视频
}
Video 表示生成的视频.
type VideoGenerationRequest ¶
type VideoGenerationRequest struct {
Model string `json:"model"` // 模型名称
Prompt string `json:"prompt"` // 文本提示
Duration int `json:"duration,omitempty"` // 视频时长(秒)
FPS int `json:"fps,omitempty"` // 帧率
Resolution string `json:"resolution,omitempty"` // 分辨率(如 "1920x1080")
AspectRatio string `json:"aspect_ratio,omitempty"` // 宽高比(如 "16:9")
Style string `json:"style,omitempty"` // 视频风格
ResponseFormat string `json:"response_format,omitempty"` // 响应格式(url, b64_json)
}
VideoGenerationRequest 表示视频生成请求.
type VideoGenerationResponse ¶
type VideoGenerationResponse struct {
ID string `json:"id"`
Created int64 `json:"created"`
Data []Video `json:"data"`
}
VideoGenerationResponse 表示视频生成响应.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
包 batch 提供 LLM 请求的批处理与调度能力,通过将多个独立请求 合并为批次统一发送,降低网络开销并提升吞吐量。
|
包 batch 提供 LLM 请求的批处理与调度能力,通过将多个独立请求 合并为批次统一发送,降低网络开销并提升吞吐量。 |
|
包 budget 提供 Token 预算管理与成本控制能力,通过多时间窗口 限流与告警机制防止 LLM 调用超支。
|
包 budget 提供 Token 预算管理与成本控制能力,通过多时间窗口 限流与告警机制防止 LLM 调用超支。 |
|
包 cache 提供 LLM 请求与工具调用的多级缓存实现,通过本地 LRU 与 Redis 协同减少重复调用,降低延迟与成本。
|
包 cache 提供 LLM 请求与工具调用的多级缓存实现,通过本地 LRU 与 Redis 协同减少重复调用,降低延迟与成本。 |
|
Package circuitbreaker 提供通用熔断器实现, 支持 Closed/Open/HalfOpen 三态状态机,用于保护 LLM 调用链路。
|
Package circuitbreaker 提供通用熔断器实现, 支持 Closed/Open/HalfOpen 三态状态机,用于保护 LLM 调用链路。 |
|
Package config 提供 LLM 层的配置类型定义, 包括降级策略 (FallbackPolicy)、路由规则等配置结构。
|
Package config 提供 LLM 层的配置类型定义, 包括降级策略 (FallbackPolicy)、路由规则等配置结构。 |
|
包 embedding 提供统一的文本嵌入(Embedding)接口与多服务商实现, 用于将文本转换为向量表示以支持语义检索、分类与聚类等场景。
|
包 embedding 提供统一的文本嵌入(Embedding)接口与多服务商实现, 用于将文本转换为向量表示以支持语义检索、分类与聚类等场景。 |
|
Package factory 提供 LLM Provider 的集中式工厂, 通过名称映射创建 Provider 实例,打破 llm 包与各 provider 子包之间的循环依赖。
|
Package factory 提供 LLM Provider 的集中式工厂, 通过名称映射创建 Provider 实例,打破 llm 包与各 provider 子包之间的循环依赖。 |
|
Package idempotency 提供 LLM 请求幂等性管理, 通过请求指纹去重防止重复调用,支持内存与 Redis 两种存储后端。
|
Package idempotency 提供 LLM 请求幂等性管理, 通过请求指纹去重防止重复调用,支持内存与 Redis 两种存储后端。 |
|
包 image 提供统一的图像生成服务抽象,支持多种模型服务商的文生图、 图像编辑与变体生成能力。
|
包 image 提供统一的图像生成服务抽象,支持多种模型服务商的文生图、 图像编辑与变体生成能力。 |
|
包 middleware 提供 LLM 请求处理的中间件链机制,支持在请求发送到 上游模型服务之前和响应返回之后插入可组合的横切逻辑。
|
包 middleware 提供 LLM 请求处理的中间件链机制,支持在请求发送到 上游模型服务之前和响应返回之后插入可组合的横切逻辑。 |
|
包 moderation 提供统一的内容审核服务抽象,用于检测文本和图像中的 违规内容,支持多种审核类别与置信度评分。
|
包 moderation 提供统一的内容审核服务抽象,用于检测文本和图像中的 违规内容,支持多种审核类别与置信度评分。 |
|
包 multimodal 提供多模态内容的统一表示、格式转换与跨 Provider 路由能力, 支持文本、图像、音频、视频、文档等多种内容类型。
|
包 multimodal 提供多模态内容的统一表示、格式转换与跨 Provider 路由能力, 支持文本、图像、音频、视频、文档等多种内容类型。 |
|
包 music 提供统一的 AI 音乐生成接入层,支持多服务商适配与统一请求模型。
|
包 music 提供统一的 AI 音乐生成接入层,支持多服务商适配与统一请求模型。 |
|
包 observability 提供 LLM 调用的可观测性能力,涵盖指标采集、 分布式追踪与成本核算三大模块。
|
包 observability 提供 LLM 调用的可观测性能力,涵盖指标采集、 分布式追踪与成本核算三大模块。 |
|
openaicompat
Package openaicompat provides a shared base implementation for all OpenAI-compatible LLM providers.
|
Package openaicompat provides a shared base implementation for all OpenAI-compatible LLM providers. |
|
qwen
包 qwen 提供阿里巴巴通义千问(Qwen)系列模型的 Provider 适配实现, 基于 OpenAI 兼容协议接入 DashScope API。
|
包 qwen 提供阿里巴巴通义千问(Qwen)系列模型的 Provider 适配实现, 基于 OpenAI 兼容协议接入 DashScope API。 |
|
包 rerank 提供统一的文档重排序接入层,支持多服务商适配与标准化 请求/响应模型。
|
包 rerank 提供统一的文档重排序接入层,支持多服务商适配与标准化 请求/响应模型。 |
|
Package retry 提供 LLM 调用的重试策略, 支持指数退避、抖动和可配置的最大重试次数。
|
Package retry 提供 LLM 调用的重试策略, 支持指数退避、抖动和可配置的最大重试次数。 |
|
包 router 提供多模型场景下的路由能力。
|
包 router 提供多模型场景下的路由能力。 |
|
包 speech 提供统一的语音合成 (TTS) 与语音识别 (STT) 接入层, 支持多服务商适配与标准化请求/响应模型。
|
包 speech 提供统一的语音合成 (TTS) 与语音识别 (STT) 接入层, 支持多服务商适配与标准化请求/响应模型。 |
|
包 streaming 提供面向 LLM 流式输出场景的高性能数据传输原语, 包括零拷贝缓冲、背压流控、速率限制与流多路复用。
|
包 streaming 提供面向 LLM 流式输出场景的高性能数据传输原语, 包括零拷贝缓冲、背压流控、速率限制与流多路复用。 |
|
包 threed 提供统一的 3D 模型生成接口,支持文本转 3D 和图像转 3D 两种模式, 并适配 Meshy 与 Tripo3D 两个主流服务商。
|
包 threed 提供统一的 3D 模型生成接口,支持文本转 3D 和图像转 3D 两种模式, 并适配 Meshy 与 Tripo3D 两个主流服务商。 |
|
Package tokenizer 提供统一的 Token 计数接口, 支持 tiktoken 精确计数与 CJK 估算器,用于 LLM 请求的 Token 预算管理。
|
Package tokenizer 提供统一的 Token 计数接口, 支持 tiktoken 精确计数与 CJK 估算器,用于 LLM 请求的 Token 预算管理。 |
|
包 tools 提供大模型工具调用的注册、调度与执行能力。
|
包 tools 提供大模型工具调用的注册、调度与执行能力。 |
|
包 video 提供统一的视频生成与视频理解接口,适配 Gemini、Veo 和 Runway ML 三个服务商。
|
包 video 提供统一的视频生成与视频理解接口,适配 Gemini、Veo 和 Runway ML 三个服务商。 |