> ## 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/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) 兼容的端点。你可以使用自定义 `base_url` 的 [`ChatOpenAI`](/oss/python/integrations/chat/openai) 连接到这些端点：

```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)到 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>
