> ## 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/python/langchain/tools)、[结构化输出](/oss/python/langchain/structured-output)和[流式传输](/oss/python/langchain/streaming)，这些功能在所有提供商中通用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain.chat_models import init_chat_model

openai_model = init_chat_model("openai:gpt-5.4")
anthropic_model = init_chat_model("anthropic:claude-opus-4-6")
google_model = init_chat_model("google-genai:gemini-3.1-pro-preview")

for model in [openai_model, anthropic_model, google_model]:
    response = model.invoke("Explain quantum computing in one sentence.")
    print(response.text)
```

## 什么是提供商？

**提供商**是托管 AI 模型并通过 API 暴露它们的公司或平台。示例包括 OpenAI、Anthropic、Google 和 AWS Bedrock。

在 LangChain 中，每个提供商都有一个专用的**集成包**（例如 `langchain-openai`、`langchain-anthropic`），该包为该提供商的模型实现了标准的 LangChain 接口。这意味着：

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

```shell theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
uv add langchain-openai       # 用于 OpenAI 模型
uv add langchain-anthropic    # 用于 Anthropic 模型
uv add langchain-google-genai # 用于 Google 模型
```

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

## 查找模型名称

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

<CodeGroup>
  ```python 提供商前缀格式 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain.chat_models import init_chat_model

  model = init_chat_model("openai:gpt-5.4")
  ```

  ```python 直接类实例化 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain_openai import ChatOpenAI

  model = ChatOpenAI(model="gpt-5.4")
  ```
</CodeGroup>

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

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

| 提供商                                                       | 查找模型名称的位置                                                                                   |
| :-------------------------------------------------------- | :------------------------------------------------------------------------------------------ |
| [OpenAI](/oss/python/integrations/providers/openai)       | [OpenAI 模型页面](https://platform.openai.com/docs/models)                                      |
| [Anthropic](/oss/python/integrations/providers/anthropic) | [Anthropic 模型页面](https://docs.anthropic.com/en/docs/about-claude/models)                    |
| [Google](/oss/python/integrations/providers/google)       | [Google AI 模型页面](https://ai.google.dev/gemini-api/docs/models)                              |
| [AWS Bedrock](/oss/python/integrations/providers/aws)     | [Bedrock 支持的模型](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html) |
| [Ollama](/oss/python/integrations/providers/ollama)       | [Ollama 模型库](https://ollama.com/library)                                                    |
| [Groq](/oss/python/integrations/providers/groq)           | [Groq 支持的模型](https://console.groq.com/docs/models)                                          |

## 立即使用新模型

因为 LangChain 提供商包将模型名称直接传递给提供商的 API，所以您可以在提供商发布新模型时立即使用它们（无需 LangChain 更新）。只需传递新的模型名称：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
model = init_chat_model("google_genai:gemini-mythos")
```

只要您的提供商包版本支持模型所需的 API 版本，新的模型名称就可以立即使用。在大多数情况下，模型发布是向后兼容的，无需更新包。

## 模型功能

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

## 路由器和代理

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

| 提供商                                  | 集成                                                           | 描述                                       |
| :----------------------------------- | :----------------------------------------------------------- | :--------------------------------------- |
| [OpenRouter](https://openrouter.ai/) | [`ChatOpenRouter`](/oss/python/integrations/chat/openrouter) | 统一访问来自 OpenAI、Anthropic、Google、Meta 等的模型 |
| [LiteLLM](https://www.litellm.ai/)   | [`ChatLiteLLM`](/oss/python/integrations/chat/litellm)       | 为 100 多个提供商提供统一接口，支持路由、故障转移和支出跟踪         |

当您想要以下操作时，路由器很有用：

* **使用单一 API 密钥和计费账户**访问多个提供商
* **动态切换模型**而无需管理多个提供商凭据
* **使用故障转移模型**，当主模型失败时自动重试使用不同的模型

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain.chat_models import init_chat_model

model = init_chat_model("openrouter:anthropic/claude-sonnet-4-6")
response = model.invoke("Hello!")
```

## OpenAI 兼容端点

许多提供商提供与 OpenAI 的 [Chat Completions API](https://platform.openai.com/docs/api-reference/chat) 兼容的端点。您可以使用 [`ChatOpenAI`](/oss/python/integrations/chat/openai) 并通过自定义 `base_url` 连接到这些端点：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    base_url="https://your-provider.com/v1",
    api_key="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/python/langchain/models">
    了解如何使用模型：调用、流式传输、批处理、工具调用等。
  </Card>

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

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

  <Card title="代理" icon="robot" href="/oss/python/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>
