跳转到内容

智能体

langchain.agents

使用 LangChain 构建智能体的入口点。

参考文档

本页面包含智能体的**参考文档**。有关使用智能体的概念指南、教程和示例,请参阅文档

create_agent

create_agent(
    model: str | BaseChatModel,
    tools: Sequence[BaseTool | Callable | dict[str, Any]] | None = None,
    *,
    system_prompt: str | None = None,
    middleware: Sequence[AgentMiddleware[AgentState[ResponseT], ContextT]] = (),
    response_format: ResponseFormat[ResponseT] | type[ResponseT] | None = None,
    state_schema: type[AgentState[ResponseT]] | None = None,
    context_schema: type[ContextT] | None = None,
    checkpointer: Checkpointer | None = None,
    store: BaseStore | None = None,
    interrupt_before: list[str] | None = None,
    interrupt_after: list[str] | None = None,
    debug: bool = False,
    name: str | None = None,
    cache: BaseCache | None = None,
) -> CompiledStateGraph[
    AgentState[ResponseT], ContextT, _InputAgentState, _OutputAgentState[ResponseT]
]

创建一个智能体图,该图会循环调用工具,直到满足停止条件。

有关使用 create_agent 的更多详细信息,请访问智能体文档。

参数 描述

model

用于智能体的语言模型。可以是一个字符串标识符(例如,"openai:gpt-4")或一个直接的聊天模型实例(例如,ChatOpenAI 或其他聊天模型)。

有关支持的模型字符串的完整列表,请参见 init_chat_model

类型: str | BaseChatModel

tools

一个由工具、dictsCallable 组成的列表。

如果为 None 或空列表,则智能体将仅包含一个模型节点,而没有工具调用循环。

类型: Sequence[BaseTool | Callable | dict[str, Any]] | None 默认值: None

system_prompt

一个可选的用于 LLM 的系统提示。

提示会被转换为一个 SystemMessage 并添加到消息列表的开头。

类型: str | None 默认值: None

middleware

一个应用于智能体的中间件实例序列。

中间件可以在不同阶段拦截和修改智能体的行为。请参阅完整指南

类型: Sequence[AgentMiddleware[AgentState[ResponseT], ContextT]] 默认值: ()

response_format

一个可选的用于结构化响应的配置。

可以是 ToolStrategyProviderStrategy 或一个 Pydantic 模型类。

如果提供,智能体将在对话流程中处理结构化输出。原始模式(raw schemas)将根据模型能力被包装在适当的策略中。

类型: ResponseFormat[ResponseT] | type[ResponseT] | None 默认值: None

state_schema

一个可选的扩展了 AgentStateTypedDict 模式。

提供此模式时,它将代替 AgentState 作为与中间件状态模式合并的基础模式。这允许用户添加自定义状态字段,而无需创建自定义中间件。通常建议通过中间件来使用 state_schema 扩展,以将相关扩展的作用域限定在相应的钩子/工具内。

该模式必须是 AgentState[ResponseT] 的子类。

类型: type[AgentState[ResponseT]] | None 默认值: None

context_schema

一个可选的用于运行时上下文的模式。

类型: type[ContextT] | None 默认值: None

checkpointer

一个可选的检查点保存器对象。

用于为单个线程(例如,单个对话)持久化图的状态(例如,作为聊天记忆)。

类型: Checkpointer | None 默认值: None

store

一个可选的存储对象。

用于在多个线程(例如,多个对话/用户)之间持久化数据。

类型: BaseStore | None 默认值: None

interrupt_before

一个可选的节点名称列表,在这些节点之前中断。

如果您想在执行某个动作前添加用户确认或其他中断,这将非常有用。

类型: list[str] | None 默认值: None

interrupt_after

一个可选的节点名称列表,在这些节点之后中断。

如果您想直接返回或对输出运行额外的处理,这将非常有用。

类型: list[str] | None 默认值: None

debug

是否为图执行启用详细日志记录。

启用后,会在智能体运行时打印有关每个节点执行、状态更新和转换的详细信息。这对于调试中间件行为和理解智能体执行流程很有用。

类型: bool 默认值: False

name

一个可选的 CompiledStateGraph 名称。

当将此智能体图作为子图节点添加到另一个图中时,将自动使用此名称——这对于构建多智能体系统特别有用。

类型: str | None 默认值: None

cache

一个可选的 BaseCache 实例,用于启用图执行的缓存。

类型: BaseCache | None 默认值: None

返回 描述
CompiledStateGraph[AgentState[ResponseT], ContextT, _InputAgentState, _OutputAgentState[ResponseT]]

一个已编译的 StateGraph,可用于聊天交互。

智能体节点使用消息列表(在应用系统提示后)调用语言模型。如果生成的 AIMessage 包含 tool_calls,则图将调用工具。工具节点执行工具并将响应作为 ToolMessage 对象添加到消息列表中。然后,智能体节点再次调用语言模型。该过程重复进行,直到响应中不再出现 tool_calls。最后,智能体返回完整的消息列表。

示例
from langchain.agents import create_agent


def check_weather(location: str) -> str:
    '''Return the weather forecast for the specified location.'''
    return f"It's always sunny in {location}"


graph = create_agent(
    model="anthropic:claude-sonnet-4-5-20250929",
    tools=[check_weather],
    system_prompt="You are a helpful assistant",
)
inputs = {"messages": [{"role": "user", "content": "what is the weather in sf"}]}
for chunk in graph.stream(inputs, stream_mode="updates"):
    print(chunk)

AgentState

基类: TypedDict, Generic[ResponseT]

智能体的状态模式。

© . This site is unofficial and not affiliated with LangChain, Inc.