langchain-openrouter 是 LangChain 的 OpenRouter 集成,核心类 ChatOpenRouter 通过单一 API 与 API 密钥路由到 OpenAI、Anthropic、Google、Meta 等数百个模型。
主要能力:用 model 参数切换不同模型;用 openrouter_provider 字典指定供应商顺序、允许回退、要求参数支持、限制或排除特定供应商;对推理模型设置 reasoning(如 effort 为 low/medium/high);通过 .bind_tools() 进行工具调用;通过 .with_structured_output() 结合 Pydantic 模型获得结构化输出;设置 app_url、app_title、app_categories 用于应用归因;用 session_id 分组请求、用 trace 传递广播元数据;还支持 plugins 参数。
使用方式:安装 pip install -U langchain-openrouter,配置环境变量 export OPENROUTER_API_KEY=your_api_key,然后创建 ChatOpenRouter(model="...") 并调用 invoke 或 stream。模型 ID 列表见 https://openrouter.ai/models。
适用场景:需要在多个模型供应商之间快速切换、按供应商路由、使用推理模型、工具调用或结构化输出,以及需要请求归因和观测分组的 LLM 应用。
限制:实际可用模型、收费标准、速率限制、平台支持情况等资料未提供,需查阅 OpenRouter 官方文档与模型页确认。