> ## Documentation Index
> Fetch the complete documentation index at: https://nvd-54.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 提供商和模型

> 了解 LangChain 如何使用提供商为你提供适用于任何提供商的任何模型的统一 API

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

```mermaid theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
graph LR
    subgraph "你的代码"
        A["LangChain API<br/>(invoke, stream, bind_tools)"]
    end

    subgraph "提供商"
        B["OpenAI"]
        C["Anthropic"]
        D["Google"]
        E["AWS Bedrock"]
        F["...更多"]
    end

    A --> B
    A --> C
    A --> D
    A --> E
    A --> F

    classDef code fill:#E5F4FF,stroke:#006DDD,stroke-width:2px,color:#030710
    classDef provider fill:#EBD0F0,stroke:#885270,stroke-width:2px,color:#441E33

    class A code
    class B,C,D,E,F provider
```

## 任何模型的统一 API

每个 LangChain 聊天模型，无论提供商是谁，都实现相同的接口。这意味着你可以：

* **切换提供商**而无需重写应用逻辑
* **并行比较模型**使用完全相同的代码
* **使用高级功能**如[工具调用](/oss/javascript/langchain/tools)、[结构化输出](/oss/javascript/langchain/structured-output)和[流式输出](/oss/javascript/langchain/streaming)跨所有提供商

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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-openai`、`langchain-anthropic`），为该提供商的模型实现标准的 LangChain 接口。这意味着：

* **每个提供商有专用包**，具有适当的版本管理和依赖管理
* **提供商特定功能**在你需要时可用（例如 OpenAI 的 Responses API、Anthropic 的扩展思考）
* **自动 API 密钥处理**通过环境变量

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
npm install @langchain/openai       # OpenAI 模型
npm install @langchain/anthropic    # Anthropic 模型
npm install @langchain/google-genai # Google 模型
```

提供商包的完整列表请参阅[集成页面](/oss/javascript/integrations/providers/overview)。

## 查找模型名称

每个提供商支持特定的模型名称，你在初始化聊天模型时传入。有两种指定模型的方式：

<CodeGroup>
  ```typescript Provider prefix format theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import { initChatModel } from "langchain/chat_models/universal";

  const model = await initChatModel("openai:gpt-5.4");
  ```

  ```typescript Direct class instantiation theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import { ChatOpenAI } from "@langchain/openai";

  const model = new ChatOpenAI({ model: "gpt-5.4" });
  ```
</CodeGroup>

当使用 [`init_chat_model`](https://reference.langchain.com/javascript/langchain/chat_models/universal/initChatModel) 的 `provider:model` 格式时，LangChain 会自动解析提供商并加载正确的集成包。如果模型名称无歧义（例如 `"gpt-5.4"` 解析为 OpenAI），你也可以省略提供商前缀。

要查找提供商的可用模型名称，请参考提供商自己的文档。以下是一些热门提供商：

| 提供商                                                           | 在哪里找到模型名称                                                                                   |
| :------------------------------------------------------------ | :------------------------------------------------------------------------------------------ |
| [OpenAI](/oss/javascript/integrations/providers/openai)       | [OpenAI 模型页面](https://platform.openai.com/docs/models)                                      |
| [Anthropic](/oss/javascript/integrations/providers/anthropic) | [Anthropic 模型页面](https://docs.anthropic.com/en/docs/about-claude/models)                    |
| [Google](/oss/javascript/integrations/providers/google)       | [Google AI 模型页面](https://ai.google.dev/gemini-api/docs/models)                              |
| [AWS Bedrock](/oss/javascript/integrations/providers/aws)     | [Bedrock 支持的模型](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html) |

## 立即使用新模型

由于 LangChain 提供商包会将模型名称直接传递给提供商的 API，你可以在提供商发布新模型的那一刻就使用它们（无需更新 LangChain）。只需传入新的模型名称：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const model = await initChatModel("google_genai:gemini-mythos");
```

只要你的提供商包版本支持模型所需的 API 版本，新模型名称就能立即工作。大多数情况下，模型发布是向后兼容的，不需要包更新。

## 模型能力

不同的提供商和模型支持不同的功能。
有关聊天模型集成及其能力的列表，请参阅[聊天模型集成页面](/oss/javascript/integrations/chat)。

## 路由器和代理

**路由器**（也称为代理或网关）让你通过单个 API 和凭据访问多个提供商的模型。它们可以简化计费，让你在不更改集成的情况下切换模型，并提供自动回退和负载均衡等功能。

| 提供商                                  | 集成                                                               | 描述                                     |
| :----------------------------------- | :--------------------------------------------------------------- | :------------------------------------- |
| [OpenRouter](https://openrouter.ai/) | [`ChatOpenRouter`](/oss/javascript/integrations/chat/openrouter) | 统一访问 OpenAI、Anthropic、Google、Meta 等的模型 |

路由器在以下场景很有用：

* **使用单个 API 密钥和计费账户访问多个提供商**
* **动态切换模型**而无需管理多个提供商凭据
* **使用回退模型**，在主模型失败时自动重试其他模型

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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](https://platform.openai.com/docs/api-reference/chat) 兼容的端点。你可以使用 [`ChatOpenAI`](/oss/javascript/integrations/chat/openai) 和自定义的 `base_url` 来连接这些端点：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatOpenAI } from "@langchain/openai";

const model = new ChatOpenAI({
    configuration: { baseURL: "https://your-provider.com/v1" },
    apiKey: "your-api-key",
    model: "provider-model-name",
});
```

<Warning>
  `ChatOpenAI` 仅针对[官方 OpenAI API 规范](https://github.com/openai/openai-openapi)。第三方提供商的非标准响应字段不会被提取或保留。当你需要访问非标准功能时，请使用专用的提供商包或路由器。
</Warning>

## 后续步骤

<CardGroup cols={2}>
  <Card title="模型指南" icon="cpu" href="/oss/javascript/langchain/models">
    了解如何使用模型：调用、流式输出、批处理、工具调用等。
  </Card>

  <Card title="聊天模型集成" icon="message" href="/oss/javascript/integrations/chat">
    浏览所有聊天模型集成及其能力。
  </Card>

  <Card title="所有提供商" icon="grid-dots" href="/oss/javascript/integrations/providers/overview">
    查看提供商包和集成的完整列表。
  </Card>

  <Card title="智能体" icon="robot" href="/oss/javascript/langchain/agents">
    构建使用模型作为推理引擎的智能体。
  </Card>
</CardGroup>

***

<div className="source-links">
  <Callout icon="terminal-2">
    [连接这些文档](/use-these-docs)到 Claude、VSCode 等工具，通过 MCP 获取实时解答。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/concepts/providers-and-models.mdx)或[提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
