跳到内容

模型

LangGraph 通过 LangChain 库提供对LLM(语言模型)的内置支持。这使得将各种 LLM 集成到您的代理和工作流中变得容易。

初始化模型

使用 init_chat_model 初始化模型

pip install -U "langchain[openai]"
import os
from langchain.chat_models import init_chat_model

os.environ["OPENAI_API_KEY"] = "sk-..."

llm = init_chat_model("openai:gpt-4.1")

👉 阅读 OpenAI 集成文档

pip install -U "langchain[anthropic]"
import os
from langchain.chat_models import init_chat_model

os.environ["ANTHROPIC_API_KEY"] = "sk-..."

llm = init_chat_model("anthropic:claude-3-5-sonnet-latest")

👉 阅读 Anthropic 集成文档

pip install -U "langchain[openai]"
import os
from langchain.chat_models import init_chat_model

os.environ["AZURE_OPENAI_API_KEY"] = "..."
os.environ["AZURE_OPENAI_ENDPOINT"] = "..."
os.environ["OPENAI_API_VERSION"] = "2025-03-01-preview"

llm = init_chat_model(
    "azure_openai:gpt-4.1",
    azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
)

👉 阅读 Azure 集成文档

pip install -U "langchain[google-genai]"
import os
from langchain.chat_models import init_chat_model

os.environ["GOOGLE_API_KEY"] = "..."

llm = init_chat_model("google_genai:gemini-2.0-flash")

👉 阅读 Google GenAI 集成文档

pip install -U "langchain[aws]"
from langchain.chat_models import init_chat_model

# Follow the steps here to configure your credentials:
# https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started.html

llm = init_chat_model(
    "anthropic.claude-3-5-sonnet-20240620-v1:0",
    model_provider="bedrock_converse",
)

👉 阅读 AWS Bedrock 集成文档

直接实例化模型

如果模型提供程序无法通过 init_chat_model 获得,您可以直接实例化该提供程序的模型类。该模型必须实现 BaseChatModel 接口并支持工具调用。

API 参考:ChatAnthropic

# Anthropic is already supported by `init_chat_model`,
# but you can also instantiate it directly.
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
  model="claude-3-7-sonnet-latest",
  temperature=0,
  max_tokens=2048
)

工具调用支持

如果您正在构建一个需要模型调用外部工具的代理或工作流,请确保底层语言模型支持工具调用。兼容的模型可以在 LangChain 集成目录中找到。

在代理中使用

使用 create_react_agent 时,您可以通过其名称字符串指定模型,这是使用 init_chat_model 初始化模型的简写。这允许您使用模型而无需直接导入或实例化它。

from langgraph.prebuilt import create_react_agent

create_react_agent(
   model="anthropic:claude-3-7-sonnet-latest",
   # other parameters
)
from langchain_anthropic import ChatAnthropic
from langgraph.prebuilt import create_react_agent

model = ChatAnthropic(
    model="claude-3-7-sonnet-latest",
    temperature=0,
    max_tokens=2048
)
# Alternatively
# model = init_chat_model("anthropic:claude-3-7-sonnet-latest")

agent = create_react_agent(
  model=model,
  # other parameters
)

高级模型配置

禁用流式传输

要禁用单个 LLM 标记的流式传输,请在初始化模型时设置 disable_streaming=True

from langchain.chat_models import init_chat_model

model = init_chat_model(
    "anthropic:claude-3-7-sonnet-latest",
    disable_streaming=True
)
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-3-7-sonnet-latest",
    disable_streaming=True
)

有关 disable_streaming 的更多信息,请参阅API 参考

添加模型回退

您可以使用 model.with_fallbacks([...]) 添加到不同模型或不同 LLM 提供程序的回退。

from langchain.chat_models import init_chat_model

model_with_fallbacks = (
    init_chat_model("anthropic:claude-3-5-haiku-latest")
    .with_fallbacks([
        init_chat_model("openai:gpt-4.1-mini"),
    ])
)
from langchain_anthropic import ChatAnthropic
from langchain_openai import ChatOpenAI

model_with_fallbacks = (
    ChatAnthropic(model="claude-3-5-haiku-latest")
    .with_fallbacks([
        ChatOpenAI(model="gpt-4.1-mini"),
    ])
)

有关模型回退的更多信息,请参阅此指南

使用内置限速器

Langchain 包含一个内置的内存中限速器。该限速器是线程安全的,可以在同一进程中的多个线程之间共享。

API 参考:InMemoryRateLimiter | ChatAnthropic

from langchain_core.rate_limiters import InMemoryRateLimiter
from langchain_anthropic import ChatAnthropic

rate_limiter = InMemoryRateLimiter(
    requests_per_second=0.1,  # <-- Super slow! We can only make a request once every 10 seconds!!
    check_every_n_seconds=0.1,  # Wake up every 100 ms to check whether allowed to make a request,
    max_bucket_size=10,  # Controls the maximum burst size.
)

model = ChatAnthropic(
   model_name="claude-3-opus-20240229", 
   rate_limiter=rate_limiter
)

有关如何处理速率限制的更多信息,请参阅 LangChain 文档。

使用您自己的模型

如果您想要的 LLM 未得到 LangChain 的官方支持,请考虑以下选项:

  1. 实现自定义 LangChain 聊天模型:创建一个符合LangChain 聊天模型接口的模型。这可以实现与 LangGraph 代理和工作流的完全兼容,但需要了解 LangChain 框架。

  2. 带自定义流式传输的直接调用:通过使用 StreamWriter 添加自定义流式传输逻辑来直接使用您的模型。请参阅自定义流式传输文档以获取指导。此方法适用于不需要预构建代理集成的自定义工作流。

更多资源