router

package
v1.2.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

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 added in v1.0.0

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