LangChain 为你提供一个统一 API,用于使用任何提供商的模型。安装提供商包,选择模型名称,然后开始构建。无论你使用 OpenAI、Anthropic、Google,还是任何其他受支持的提供商,相同代码都可以工作。

一个 API,适用于任何模型

每个 LangChain chat model 都会实现相同接口,无论它来自哪个提供商。这意味着你可以:
import { initChatModel } from "langchain/chat_models/universal";

const openaiModel = await initChatModel("openai:gpt-5.4");
const anthropicModel = await initChatModel("anthropic:claude-opus-4-6");
const googleModel = await initChatModel("google-genai:gemini-3.1-pro-preview");

for (const model of [openaiModel, anthropicModel, googleModel]) {
    const response = await model.invoke("Explain quantum computing in one sentence.");
    console.log(response.text);
}

什么是提供商?

提供商是托管 AI 模型并通过 API 暴露这些模型的公司或平台。示例包括 OpenAI、Anthropic、Google 和 AWS Bedrock。 在 LangChain 中,每个提供商都有专用的集成包,例如 langchain-openailangchain-anthropic,用于为该提供商的模型实现标准 LangChain 接口。这意味着:
  • 每个提供商都有专用包,并具备适当的版本控制和依赖管理
  • 当你需要时,可以使用提供商特定功能,例如 OpenAI 的 Responses API、Anthropic 的 extended thinking
  • 通过环境变量自动处理 API key
npm install @langchain/openai       # For OpenAI models
npm install @langchain/anthropic    # For Anthropic models
npm install @langchain/google-genai # For Google models
如需查看提供商包的完整列表,请参阅集成页面

查找模型名称

每个提供商都支持特定模型名称,你可以在初始化 chat model 时传入这些名称。有两种方式可以指定模型:
import { initChatModel } from "langchain/chat_models/universal";

const model = await initChatModel("openai:gpt-5.4");
使用 provider:model 格式调用 init_chat_model 时,LangChain 会自动解析提供商并加载正确的集成包。如果模型名称明确,也可以省略提供商前缀,例如 "gpt-5.4" 会解析为 OpenAI。 要查找某个提供商可用的模型名称,请参阅该提供商自己的文档。下面是一些热门提供商:

立即使用新模型

由于 LangChain 提供商包会把模型名称直接传给提供商 API,你可以在提供商发布新模型时立即使用它们,无需等待 LangChain 更新。只需传入新模型名称:
const model = await initChatModel("google_genai:gemini-mythos");
只要你的提供商包版本支持该模型所需的 API 版本,新模型名称就可以立即工作。在大多数情况下,模型发布是向后兼容的,不需要更新包。

模型能力

不同提供商和模型支持不同功能。 如需查看 chat model 集成及其能力列表,请参阅 chat model 集成页面

Router 和代理

Router(也称为代理或 gateway)让你可以通过单一 API 和凭据访问来自多个提供商的模型。它们可以简化计费,让你在不更改集成的情况下切换模型,并提供自动 fallback 和负载均衡等功能。
提供商集成描述
OpenRouterChatOpenRouter统一访问 OpenAI、Anthropic、Google、Meta 等提供商的模型
在以下情况下,router 很有用:
  • 使用单个 API key 和计费账户访问许多提供商
  • 动态切换模型,而无需管理多个提供商凭据
  • 使用 fallback model,在主模型失败时自动使用不同模型重试
import { initChatModel } from "langchain/chat_models/universal";

const model = await initChatModel("openrouter:anthropic/claude-sonnet-4-6");
const response = await model.invoke("Hello!");

OpenAI 兼容端点

许多提供商提供与 OpenAI Chat Completions API 兼容的端点。你可以使用带有自定义 base_urlChatOpenAI 连接这些端点:
import { ChatOpenAI } from "@langchain/openai";

const model = new ChatOpenAI({
    configuration: { baseURL: "https://your-provider.com/v1" },
    apiKey: "your-api-key",
    model: "provider-model-name",
});
ChatOpenAI 仅面向官方 OpenAI API 规范。来自第三方提供商的非标准响应字段不会被提取或保留。当你需要访问非标准功能时,请使用专用提供商包或 router。

后续步骤

模型指南

了解如何使用模型:invoke、stream、batch、工具调用等。

Chat model 集成

浏览所有 chat model 集成及其能力。

所有提供商

查看提供商包和集成的完整列表。

Agents

构建使用模型作为推理引擎的 agent。