Skip to main content

Documentation Index

Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

LangChain 为你提供单一统一的 API,可与任何提供商的模型配合使用。安装提供商包,选择模型名称,即可开始构建 — 无论你使用 OpenAI、Anthropic、Google 还是任何其他支持的提供商,相同的代码都能工作。

任何模型的统一 API

每个 LangChain 聊天模型,无论提供商如何,都实现相同的接口。这意味着你可以:
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-openailangchain-anthropic),为该提供商的模型实现标准 LangChain 接口。这意味着:
  • 每个提供商的专用包,具有正确的版本管理和依赖管理
  • 需要时可使用提供商特定功能(例如 OpenAI 的 Responses API、Anthropic 的扩展思考)
  • 通过环境变量自动处理 API 密钥
uv add langchain-openai       # OpenAI 模型
uv add langchain-anthropic    # Anthropic 模型
uv add langchain-google-genai # Google 模型
有关提供商包的完整列表,参见集成页面

查找模型名称

每个提供商支持特定的模型名称,你在初始化聊天模型时传入。有两种指定模型的方式:
from langchain.chat_models import init_chat_model

model = init_chat_model("openai:gpt-5.4")
使用 init_chat_modelprovider:model 格式时,LangChain 自动解析提供商并加载正确的集成包。如果模型名称无歧义(例如 "gpt-5.4" 解析为 OpenAI),你也可以省略提供商前缀。 要查找提供商的可用模型名称,请参考提供商自己的文档。以下是一些热门提供商:

立即使用新模型

由于 LangChain 提供商包将模型名称直接传递给提供商的 API,你可以在提供商发布新模型的那一刻使用它们(无需 LangChain 更新)。只需传入新的模型名称:
model = init_chat_model("google_genai:gemini-mythos")
新模型名称可以立即使用,只要你的提供商包版本支持模型所需的 API 版本。在大多数情况下,模型发布是向后兼容的,不需要包更新。

模型能力

不同的提供商和模型支持不同的功能。 有关聊天模型集成及其功能的列表,参见聊天模型集成页面

路由器和代理

路由器(也称为代理或网关)让你通过单一 API 和凭证访问多个提供商的模型。它们可以简化计费,让你无需更改集成即可在模型之间切换,并提供自动回退和负载均衡等功能。
提供商集成描述
OpenRouterChatOpenRouter统一访问 OpenAI、Anthropic、Google、Meta 等提供商的模型
LiteLLMChatLiteLLM100+ 提供商的统一接口,具有路由、回退和支出跟踪功能
路由器在你需要以下功能时很有用:
  • 使用单一 API 密钥和计费账户访问多个提供商
  • 无需管理多个提供商凭证即可动态切换模型
  • 使用备用模型,在主要模型失败时自动使用其他模型重试
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 兼容的端点。你可以使用自定义 base_urlChatOpenAI 连接到这些端点:
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    base_url="https://your-provider.com/v1",
    api_key="your-api-key",
    model="provider-model-name",
)
ChatOpenAI 仅针对官方 OpenAI API 规范。来自第三方提供商的非标准响应字段不会被提取或保留。当你需要访问非标准功能时,请使用专用提供商包或路由器。

下一步

模型指南

学习如何使用模型:调用、流式、批量、工具调用等。

聊天模型集成

浏览所有聊天模型集成及其功能。

所有提供商

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

智能体

构建使用模型作为推理引擎的智能体。