models

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package models 提供 LLM 模型元数据注册表(上下文窗口、输出上限、价格), 数据源 OpenRouter API,编译期基线 + 运行期刷新。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SameModelID

func SameModelID(a, b string) bool

SameModelID 判断两个模型标识是否指向同一个规范模型(忽略日期后缀、大小写、点/横线差异)。

func StartPricingRefresh

func StartPricingRefresh(registry *ModelRegistry, cacheDir string)

StartPricingRefresh 起后台 goroutine 刷新模型数据。 先读磁盘缓存(24h TTL),过期或不存在则拉新数据并落盘。 cacheDir 为空时跳过磁盘缓存,仍会尝试网络拉取。

Types

type ModelEntry

type ModelEntry struct {
	Provider            string  `json:"provider"`               // OpenRouter 规范化后的厂商名 (anthropic/openai/gemini/...)
	ID                  string  `json:"id"`                     // 模型 ID (不含厂商前缀)
	Name                string  `json:"name"`                   // 展示名
	ContextWindow       int     `json:"context_window"`         // 输入窗口
	MaxTokens           int     `json:"max_tokens"`             // 单次输出上限
	InputCostPer1M      float64 `json:"input_cost_per_1m"`      // 输入价格 (USD/1M tokens)
	OutputCostPer1M     float64 `json:"output_cost_per_1m"`     // 输出价格
	CacheReadCostPer1M  float64 `json:"cache_read_cost_per_1m"` // 缓存读取价格
	CacheWriteCostPer1M float64 `json:"cache_write_cost_per_1m"`
}

ModelEntry 描述一个已知的 LLM 模型。

type ModelRegistry

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

ModelRegistry 保存已知模型,支持模糊解析与运行期合并。

func DefaultRegistry

func DefaultRegistry() *ModelRegistry

DefaultRegistry 返回全局注册表(懒加载,线程安全)。 启动阶段调用 StartPricingRefresh 可让后台刷新价格/窗口信息。

func NewModelRegistry

func NewModelRegistry() *ModelRegistry

NewModelRegistry 返回一个已加载编译期基线的注册表。

func (*ModelRegistry) List

func (r *ModelRegistry) List(filter string) []ModelEntry

List 返回所有模型(可选 filter,空字符串表示全量)。

func (*ModelRegistry) MergeModels

func (r *ModelRegistry) MergeModels(fetched []ModelEntry)

MergeModels 按 provider+id 大小写不敏感合并。 非零价格/窗口/MaxTokens/Name 会覆盖已有条目;新增条目直接追加。

func (*ModelRegistry) Resolve

func (r *ModelRegistry) Resolve(pattern string) (*ModelEntry, bool)

Resolve 按照一个模型标识(可能是 "provider/model"、完整 ID、或局部名)查找条目。

匹配顺序:

  1. 若包含 "/",按 "provider/model" 精确查找
  2. 精确/日期后缀匹配
  3. 子串匹配(ID 或 Name 包含 pattern)

命中多个时,优先返回不含日期后缀的别名(例如 claude-sonnet-4 优先于 claude-sonnet-4-20250514)。

func (*ModelRegistry) ResolveContextWindow

func (r *ModelRegistry) ResolveContextWindow(pattern string) int

ResolveContextWindow 返回某个模型的上下文窗口;未命中返回 0。

Jump to

Keyboard shortcuts

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