> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.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("用一句话解释量子计算。");
    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 提供商前缀格式 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import { initChatModel } from "langchain/chat_models/universal";

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

  ```typescript 直接类实例化 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("你好！");
```

## 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)通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </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>
