router

package
v1.4.6 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LLMProviderStatusActive = llmcore.LLMProviderStatusActive
	RoleAssistant           = llmcore.RoleAssistant
)

Variables

View Source
var (
	ErrNoAvailableAPIKey  = errors.New("no available API key")
	ErrAllKeysRateLimited = errors.New("all API keys are rate limited")
)
View Source
var (
	ErrNoAvailableModel = errors.New("no available model")
	ErrBudgetExceeded   = errors.New("budget exceeded")
)

Functions

This section is empty.

Types

type ABMetrics

type ABMetrics struct {
	VariantName    string
	TotalRequests  int64
	SuccessCount   int64
	FailureCount   int64
	TotalLatencyMs int64
	TotalCost      float64
	QualityScores  []float64
	// contains filtered or unexported fields
}

ABMetrics收集了每个变量的请求量度.

func (*ABMetrics) GetAvgLatencyMs

func (m *ABMetrics) GetAvgLatencyMs() float64

GetAvgLatencyMs 返回以毫秒为单位的平均纬度.

func (*ABMetrics) GetAvgQualityScore

func (m *ABMetrics) GetAvgQualityScore() float64

GetAvg质量Score返回平均质量分.

func (*ABMetrics) GetSuccessRate

func (m *ABMetrics) GetSuccessRate() float64

GetSuccessRate 返回成功率为 0 到 1 之间的值 。

func (*ABMetrics) RecordRequest

func (m *ABMetrics) RecordRequest(latencyMs int64, cost float64, success bool, qualityScore float64)

记录请求记录一个请求结果。

type ABRouter

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

ABRouter是一个A/B测试路由器,用于执行lmpkg. 供养者.

func NewABRouter

func NewABRouter(config ABTestConfig, logger *zap.Logger) (*ABRouter, error)

NewAB Router创建了新的A/B测试路由器.

func (*ABRouter) Completion

func (r *ABRouter) Completion(ctx context.Context, req *llmpkg.ChatRequest) (*llmpkg.ChatResponse, error)

完成器件为lmpkg. 供养者.

func (*ABRouter) Endpoints

func (r *ABRouter) Endpoints() llmpkg.ProviderEndpoints

Endpoints 返回第一个变体提供者的端点信息。

func (*ABRouter) GetMetrics

func (r *ABRouter) GetMetrics() map[string]*ABMetrics

GetMetrics 返回每个变体的指标(深拷贝,避免数据竞争 — N2 修复).

func (*ABRouter) GetReport

func (r *ABRouter) GetReport() map[string]map[string]any

GetReport 返回所有变体的摘要报告 。

func (*ABRouter) HealthCheck

func (r *ABRouter) HealthCheck(ctx context.Context) (*llmpkg.HealthStatus, error)

健康检查设备为lmpkg。 供养者. 所有变体必须健康.

func (*ABRouter) ListModels

func (r *ABRouter) ListModels(ctx context.Context) ([]llmpkg.Model, error)

ListModels 执行 llmpkg 。 供养者. 它将来自所有变体的模型列表和由模型ID来分解.

func (*ABRouter) Name

func (r *ABRouter) Name() string

名称工具 llmpkg 。 供养者.

func (*ABRouter) Stream

func (r *ABRouter) Stream(ctx context.Context, req *llmpkg.ChatRequest) (<-chan llmpkg.StreamChunk, error)

流式设备 ltmpkg. 供养者.

func (*ABRouter) SupportsNativeFunctionCalling

func (r *ABRouter) SupportsNativeFunctionCalling() bool

支持 NativeFunctionCalling 设备 llmpkg 。 供养者. 只有当所有变体都支持时, 才会返回真实 。

func (*ABRouter) UpdateWeights

func (r *ABRouter) UpdateWeights(weights map[string]int) error

更新Weights动态地调整了变位权重. 重量必须等于100。

type ABTestConfig

type ABTestConfig struct {
	// 名称表示此测试 。
	Name string
	// 变体列出了测试变体.
	Variants []ABVariant
	// 粘接可以确定同一用户/会话的路径。
	StickyRouting bool
	// 粘接Key选择请求字段使用的:"user id","session id",或"tenant id".
	StickyKey string
	// 开始时间是测试开始的时候.
	StartTime time.Time
	// EndTime是测试结束的时候(零值表示无限期).
	EndTime time.Time
}

ABTestConfig持有A/B测试的配置.

type ABVariant

type ABVariant struct {
	// 名称是变体标识符(例如"control","experiment a").
	Name string
	// 提供方是这个变体所使用的LLM提供者.
	Provider llmpkg.Provider
	// 重量为交通重量(0-100). 所有变相权重必须相加为100.
	Weight int
	// 元数据为这个变体持有任意的密钥值对.
	Metadata map[string]string
}

ABVariant代表A/B测试中的一个变体.

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

func (p *APIKeyPool) GetStats() map[uint]*APIKeyStats

GetStats 获取统计信息

func (*APIKeyPool) LoadKeys

func (p *APIKeyPool) LoadKeys(ctx context.Context) error

LoadKeys 从数据库加载 API Keys

func (*APIKeyPool) RecordFailure

func (p *APIKeyPool) RecordFailure(ctx context.Context, keyID uint, errMsg string) error

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 CanaryConfig

type CanaryConfig = llmcore.CanaryConfig

func NewCanaryConfig

func NewCanaryConfig(db *gorm.DB, logger *zap.Logger) *CanaryConfig

type ChatChoice

type ChatChoice = llmcore.ChatChoice

type ChatRequest

type ChatRequest = llmcore.ChatRequest

type ChatResponse

type ChatResponse = llmcore.ChatResponse

type DefaultProviderFactory

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

DefaultProviderFactory 是 ProviderFactory 的线程安全默认实现。

func NewDefaultProviderFactory

func NewDefaultProviderFactory() *DefaultProviderFactory

NewDefaultProviderFactory 创建默认工厂实现。

func (*DefaultProviderFactory) CreateProvider

func (f *DefaultProviderFactory) CreateProvider(providerCode string, apiKey string, baseURL string) (Provider, error)

CreateProvider 根据 provider code 创建实例。

func (*DefaultProviderFactory) RegisterProvider

func (f *DefaultProviderFactory) RegisterProvider(code string, constructor func(apiKey, baseURL string) (Provider, error))

RegisterProvider 注册 provider 构造器。

type Error

type Error = llmcore.Error

type HealthChecker

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

HealthChecker 健康检查器

func NewHealthChecker

func NewHealthChecker(router *WeightedRouter, interval time.Duration, logger *zap.Logger) *HealthChecker

NewHealthChecker 创建健康检查器

func NewHealthCheckerWithProviders

func NewHealthCheckerWithProviders(router *WeightedRouter, providers map[string]llmpkg.Provider, interval, timeout time.Duration, logger *zap.Logger) *HealthChecker

NewHealthCheckerWithProviders 创建健康检查器(带 Provider 探活能力)。

func (*HealthChecker) Start

func (h *HealthChecker) Start(ctx context.Context)

Start 启动健康检查

func (*HealthChecker) Stop

func (h *HealthChecker) Stop()

Stop 停止健康检查

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 的健康统计

func (*HealthMonitor) GetCurrentQPS

func (m *HealthMonitor) GetCurrentQPS(providerCode string) int

GetCurrentQPS 获取当前 QPS

func (*HealthMonitor) GetHealthScore

func (m *HealthMonitor) GetHealthScore(providerCode string) float64

GetHealthScore 获取 Provider 的健康分数 (0-1)

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 = llmcore.HealthStatus

type IntentClassification

type IntentClassification struct {
	Intent     IntentType        `json:"intent"`
	Confidence float64           `json:"confidence"`
	SubIntents []IntentType      `json:"sub_intents,omitempty"`
	Entities   map[string]string `json:"entities,omitempty"`
	Metadata   map[string]any    `json:"metadata,omitempty"`
}

意向分类代表意向分类的结果.

type IntentType

type IntentType string

intentType代表一种分类意图.

const (
	IntentCodeGeneration  IntentType = "code_generation"
	IntentCodeReview      IntentType = "code_review"
	IntentQA              IntentType = "question_answering"
	IntentSummarization   IntentType = "summarization"
	IntentTranslation     IntentType = "translation"
	IntentCreativeWriting IntentType = "creative_writing"
	IntentDataAnalysis    IntentType = "data_analysis"
	IntentMath            IntentType = "math"
	IntentReasoning       IntentType = "reasoning"
	IntentChat            IntentType = "chat"
	IntentToolUse         IntentType = "tool_use"
	IntentUnknown         IntentType = "unknown"
)

type LLMModel

type LLMModel = llmcore.LLMModel

type LLMProvider

type LLMProvider = llmcore.LLMProvider

type LLMProviderAPIKey

type LLMProviderAPIKey = llmcore.LLMProviderAPIKey

type LLMProviderModel

type LLMProviderModel = llmcore.LLMProviderModel

type Message

type Message = llmcore.Message

type Model

type Model = llmcore.Model

type ModelCandidate

type ModelCandidate struct {
	ProviderCode   string
	ModelID        string
	ModelName      string
	Tags           []string
	PriceInput     float64
	PriceOutput    float64
	MaxTokens      int
	Weight         int
	CostWeight     float64
	LatencyWeight  float64
	QualityWeight  float64
	MaxCostPerReq  float64 // SLA: 单次请求最大成本
	MaxLatencyMs   int     // SLA: 最大延迟(毫秒)
	MinSuccessRate float64 // SLA: 最小成功率
	Health         *ModelHealth
	Enabled        bool
}

ModelCandidate 候选模型

type ModelHealth

type ModelHealth struct {
	ModelID      string
	IsHealthy    bool
	SuccessRate  float64 // 成功率 (0-1)
	AvgLatencyMs int     // 平均延迟
	LastError    string
	LastErrorAt  *time.Time
	UpdatedAt    time.Time
}

ModelHealth 模型健康状态

type ModelRouter

type ModelRouter interface {
	// Select 选择最佳模型
	Select(ctx context.Context, req *RouteRequest) (*RouteResult, error)
	// UpdateHealth 更新模型健康状态
	UpdateHealth(modelID string, health *ModelHealth)
	// UpdateWeights 更新路由权重
	UpdateWeights(weights []config.RoutingWeight)
}

ModelRouter 模型路由器接口

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

func (r *MultiProviderRouter) SelectProviderByCodeWithModel(ctx context.Context, providerCode, modelName string, strategy RoutingStrategy) (*ProviderSelection, error)

SelectProviderByCodeWithModel selects a provider by explicit provider code and model.

func (*MultiProviderRouter) SelectProviderWithModel

func (r *MultiProviderRouter) SelectProviderWithModel(ctx context.Context, modelName string, strategy RoutingStrategy) (*ProviderSelection, error)

SelectProviderWithModel 根据模型名选择最佳提供商(支持多对多)

func (*MultiProviderRouter) Stop

func (r *MultiProviderRouter) Stop()

Stop 停止后台监控资源。

type PrefixRouter

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

PrefixRouter 前缀路由器 通过模型 ID 前缀快速路由到指定 Provider

func NewPrefixRouter

func NewPrefixRouter(rules []PrefixRule) *PrefixRouter

NewPrefixRouter 创建前缀路由器 rules 应按前缀长度降序排列(最长前缀优先)

func (*PrefixRouter) GetRules

func (r *PrefixRouter) GetRules() []PrefixRule

GetRules 获取所有路由规则(用于调试)

func (*PrefixRouter) RouteByModelID

func (r *PrefixRouter) RouteByModelID(modelID string) (string, bool)

RouteByModelID 根据模型 ID 前缀路由 返回:providerCode, found

type PrefixRule

type PrefixRule struct {
	Prefix   string // 模型 ID 前缀(如 "gpt-4o", "claude-3-5-sonnet")
	Provider string // Provider 代码(如 "openai", "anthropic")
}

PrefixRule 前缀路由规则

type Provider

type Provider = llmcore.Provider

type ProviderEndpoints

type ProviderEndpoints = llmcore.ProviderEndpoints

type ProviderFactory

type ProviderFactory interface {
	CreateProvider(providerCode string, apiKey string, baseURL string) (Provider, error)
}

ProviderFactory 定义 runtime/router 内部使用的 provider 构造接口。

type ProviderHealthStats

type ProviderHealthStats struct {
	ProviderCode string
	HealthScore  float64
	CurrentQPS   int
	ErrorRate    float64
	LatencyP95   time.Duration
	LastCheckAt  time.Time
}

type ProviderProbeResult

type ProviderProbeResult struct {
	Healthy     bool
	Latency     time.Duration
	ErrorRate   float64
	LastError   string
	LastCheckAt time.Time
}

type ProviderSelection

type ProviderSelection struct {
	Provider     Provider
	ProviderID   uint
	APIKeyID     uint
	ProviderCode string
	ModelID      uint
	ModelName    string
	RemoteModel  string
	IsCanary     bool
	Strategy     RoutingStrategy
}

提供者选择代表选定的提供者

type QPSCounter

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

type RouteConfig

type RouteConfig struct {
	Intent           IntentType `json:"intent"`
	PreferredModels  []string   `json:"preferred_models"`
	FallbackModels   []string   `json:"fallback_models,omitempty"`
	MaxTokens        int        `json:"max_tokens,omitempty"`
	Temperature      float32    `json:"temperature,omitempty"`
	RequiredFeatures []string   `json:"required_features,omitempty"` // e.g., "function_calling", "vision"
}

RouteConfig定义了意图的路由配置.

type RouteRequest

type RouteRequest struct {
	TaskType     string   // 任务类型:chat/completion/embedding
	TenantID     string   // 租户 ID
	Tags         []string // 期望的标签:jsonify/cheap/fast
	MaxCost      float64  // 最大成本预算
	MaxLatencyMs int      // 最大延迟要求
	PreferModel  string   // 优先模型(可选)
}

RouteRequest 路由请求

type RouteResult

type RouteResult struct {
	ProviderCode string
	ModelName    string
	ModelID      string
	Score        float64
	Reason       string
}

RouteResult 路由结果

type RoutedChatProvider

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

RoutedChatProvider routes chat requests to providers selected by MultiProviderRouter.

func NewRoutedChatProvider

func NewRoutedChatProvider(router *MultiProviderRouter, opts RoutedChatProviderOptions) *RoutedChatProvider

NewRoutedChatProvider creates a routed provider entrypoint.

func (*RoutedChatProvider) Completion

func (p *RoutedChatProvider) Completion(ctx context.Context, req *ChatRequest) (*ChatResponse, error)

func (*RoutedChatProvider) Endpoints

func (p *RoutedChatProvider) Endpoints() ProviderEndpoints

func (*RoutedChatProvider) HealthCheck

func (p *RoutedChatProvider) HealthCheck(ctx context.Context) (*HealthStatus, error)

func (*RoutedChatProvider) ListModels

func (p *RoutedChatProvider) ListModels(ctx context.Context) ([]Model, error)

func (*RoutedChatProvider) Name

func (p *RoutedChatProvider) Name() string

func (*RoutedChatProvider) Stream

func (p *RoutedChatProvider) Stream(ctx context.Context, req *ChatRequest) (<-chan StreamChunk, error)

func (*RoutedChatProvider) SupportsNativeFunctionCalling

func (p *RoutedChatProvider) SupportsNativeFunctionCalling() bool

type RoutedChatProviderOptions

type RoutedChatProviderOptions struct {
	DefaultStrategy RoutingStrategy
	Fallback        Provider
	Logger          *zap.Logger
}

RoutedChatProviderOptions controls routed provider behavior.

type Router

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

Router 是 MultiProviderRouter 的基础结构体,提供 DB、健康监控等基础设施。

func NewRouter

func NewRouter(db *gorm.DB, providers map[string]Provider, opts RouterOptions) *Router

NewRouter 创建基础路由器(仅供 MultiProviderRouter 内部使用)

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"
	StrategyLatencyBased RoutingStrategy = "latency"
	StrategyCanary       RoutingStrategy = "canary"
)

type SemanticRouter

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

语义鲁特路线请求基于意向分类.

func NewSemanticRouter

func NewSemanticRouter(classifier llm.Provider, providers map[string]llm.Provider, config SemanticRouterConfig, logger *zap.Logger) *SemanticRouter

新语义路透创造出一个新的语义路由器.

func (*SemanticRouter) AddProvider

func (r *SemanticRouter) AddProvider(name string, provider llm.Provider)

添加提供者 。

func (*SemanticRouter) AddRoute

func (r *SemanticRouter) AddRoute(intent IntentType, config RouteConfig)

添加Route 添加或更新一个路由配置 。

func (*SemanticRouter) ClassifyIntent

func (r *SemanticRouter) ClassifyIntent(ctx context.Context, req *llm.ChatRequest) (*IntentClassification, error)

分类意向对请求的意图进行分类.

func (*SemanticRouter) Route

路线将请求和路线分类到适当的提供者.

type SemanticRouterConfig

type SemanticRouterConfig struct {
	ClassifierModel      string                     `json:"classifier_model"`
	DefaultRoute         RouteConfig                `json:"default_route"`
	Routes               map[IntentType]RouteConfig `json:"routes"`
	CacheClassifications bool                       `json:"cache_classifications"`
	CacheTTL             time.Duration              `json:"cache_ttl"`
}

语义路由器 Config 配置语义路由器.

func DefaultSemanticRouterConfig

func DefaultSemanticRouterConfig() SemanticRouterConfig

默认Semantic Router Config 返回合理的默认值 。

type StreamChunk

type StreamChunk = llmcore.StreamChunk

type WeightedRouter

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

WeightedRouter 加权路由器实现

func NewWeightedRouter

func NewWeightedRouter(logger *zap.Logger, prefixRules []config.PrefixRule) *WeightedRouter

NewWeightedRouter 创建加权路由器

func (*WeightedRouter) GetCandidates

func (r *WeightedRouter) GetCandidates() map[string]*ModelCandidate

GetCandidates 获取所有候选模型(用于调试)

func (*WeightedRouter) LoadCandidates

func (r *WeightedRouter) LoadCandidates(cfg *config.LLMConfig)

LoadCandidates 加载候选模型

func (*WeightedRouter) Select

func (r *WeightedRouter) Select(ctx context.Context, req *RouteRequest) (*RouteResult, error)

Select 选择最佳模型

func (*WeightedRouter) UpdateHealth

func (r *WeightedRouter) UpdateHealth(modelID string, health *ModelHealth)

UpdateHealth 更新模型健康状态

func (*WeightedRouter) UpdateWeights

func (r *WeightedRouter) UpdateWeights(weights []config.RoutingWeight)

UpdateWeights 更新路由权重

Jump to

Keyboard shortcuts

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