openrouter

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: None detected not legal advice Imports: 0 Imported by: 0

Documentation

Overview

Package openrouter 实现 OpenRouter 聚合网关的 ModelProvider.

OpenRouter 是完全 OpenAI 兼容的聚合 API,统一接入 300+ 模型. 优势:单一 API Key 访问所有 provider;统一的 reasoning 参数支持思考模式;

有 live /api/v1/models 端点,模型列表始终最新.

升华改进(ELEVATED): OpenRouter 提供统一的 reasoning 参数, 无论底层是 Anthropic thinking,OpenAI o1 还是 DeepSeek R1, 调用方只需设置 Config.DefaultThinking=true,引擎自动注入 reasoning 参数. 替代方案:<各 provider 各自实现 thinking 参数> - 否决: OpenRouter 场景下底层 provider 随时切换,各自实现无法统一.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// APIKey 是 OpenRouter API 密钥(从 openrouter.ai 获取).
	APIKey string

	// BaseURL 覆盖默认的 OpenRouter API 地址(默认 https://openrouter.ai).
	// 用于自部署 OpenRouter 兼容代理,企业内网中转,单元测试 mock server.
	//
	// 升华改进(ELEVATED): 早期方案只能写死走 openrouter.ai--
	// 私有化场景(合规要求出口流量必须过自家代理)和单元测试均无法注入.
	// 与 ollama/lmstudio/anthropic 等其他 provider 的 BaseURL 字段保持一致.
	// 替代方案:<不暴露 BaseURL,私有部署用 HTTP_PROXY 环境变量> - 否决:
	// 环境变量是进程级,无法在同一进程内同时使用多个 openrouter 实例(CLI/SDK 多场景).
	BaseURL string

	// SiteURL 用于 OpenRouter 排行榜统计(HTTP-Referer header).
	// 建议填写你的应用 URL,如 "https://yourapp.com".
	SiteURL string

	// AppName 用于 OpenRouter 排行榜展示(X-Title header).
	AppName string

	// DefaultThinking 为所有请求启用思考模式(reasoning.enabled=true).
	// 适用于需要推理能力但不关心底层模型是否原生支持思考的场景.
	DefaultThinking bool

	// DefaultThinkingTokens 设置默认思考预算(仅当 DefaultThinking=true 时有效).
	// 0 = 使用 OpenRouter 默认值.
	DefaultThinkingTokens int

	// EnableCaching 为系统消息添加 cache_control: ephemeral 标记.
	// 用于 OpenRouter → Anthropic 路径的 prompt caching(OpenRouter 透传 cache_control 给 Anthropic).
	// 注意:仅对 Anthropic 系列模型(如 anthropic/claude-sonnet-4.6)有效.
	// 系统提示 token 数需满足 Anthropic 最低阈值(Sonnet ≥1024,Haiku ≥2048),否则不会建立缓存.
	EnableCaching bool

	// HTTPClient 注入自定义 HTTP 客户端.
	// nil = 使用默认 http.Client (带 Timeout 字段配置的 ResponseHeaderTimeout).
	// 非 nil 时 consumer 完全接管超时责任, 下面的 Timeout 字段被忽略.
	HTTPClient *http.Client

	// Timeout 限制"从请求发出到收到响应首字节"的时间.
	//
	// 通过 http.Transport.ResponseHeaderTimeout 实现, **不影响** SSE 流式 body 读取.
	// 精妙之处(CLEVER): 不要误用 http.Client.Timeout - 会砍死 SSE 流.
	// 0 = 使用 defaultTimeout (60s).HTTPClient 非 nil 时忽略此字段.
	//
	// OpenRouter 特殊情况: 聚合网关会根据底层 provider 在请求间切换,
	// 某些冷启动的底层 (DeepSeek / Qwen) 响应首字节可能稍慢, 必要时调到 90-120s.
	Timeout time.Duration
}

Config 是 OpenRouter provider 的配置.

func (Config) GoString

func (c Config) GoString() string

GoString 实现 fmt.GoStringer,防止 %#v 打印时泄露 APIKey. 升华改进(ELEVATED): 参见 anthropic.Config.GoString 的说明,同理.

type Provider

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

Provider 是 OpenRouter ModelProvider 实现.

func New

func New(cfg Config) *Provider

New 创建 OpenRouter ModelProvider.

func (*Provider) Models

func (p *Provider) Models(ctx context.Context) ([]flyto.ModelInfo, error)

Models 从 OpenRouter live API 获取模型列表.

精妙之处(CLEVER): OpenRouter 是少数提供完整 live 模型列表的聚合网关-- 包含定价,上下文窗口,能力标志.我们每次 Models() 调用都实时获取, 确保模型列表始终反映 OpenRouter 当前支持的最新状态,无需人工维护静态表. 代价:每次调用有一次 HTTP 请求.可在消费层缓存(引擎不负责缓存).

func (*Provider) Name

func (p *Provider) Name() string

func (*Provider) Stream

func (p *Provider) Stream(ctx context.Context, req *flyto.Request) (<-chan flyto.Event, error)

Jump to

Keyboard shortcuts

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