LangGraph Prebuilt Agent 流程图
本文档展示了LangGraph的prebuilt
模块中Agent的实现流程,重点是create_react_agent
函数构建的代理系统流程和结构。
ReAct Agent构建流程
Agent执行流程
工具节点执行流程
ValidationNode验证流程
中断处理流程
组件间关系图
关键方法和功能说明
代理创建相关
方法/类 | 文件 | 功能描述 |
---|
create_react_agent() | chat_agent_executor.py | 创建ReAct风格代理的主函数 |
AgentState | chat_agent_executor.py | 定义代理状态的字典类型 |
AgentStatePydantic | chat_agent_executor.py | 定义代理状态的Pydantic模型 |
_get_prompt_runnable() | chat_agent_executor.py | 处理不同格式的提示输入 |
_should_bind_tools() | chat_agent_executor.py | 检查是否需要绑定工具到模型 |
模型节点相关
方法/类 | 文件 | 功能描述 |
---|
call_model() | chat_agent_executor.py | 调用语言模型生成响应 |
acall_model() | chat_agent_executor.py | 异步调用语言模型 |
generate_structured_response() | chat_agent_executor.py | 生成结构化响应 |
工具节点相关
方法/类 | 文件 | 功能描述 |
---|
ToolNode | tool_node.py | 执行工具调用的节点 |
_func() | tool_node.py | 处理工具调用的主函数 |
_run_one() | tool_node.py | 执行单个工具调用 |
_parse_input() | tool_node.py | 解析输入状态和工具调用 |
inject_tool_args() | tool_node.py | 注入状态和存储到工具参数 |
_handle_tool_error() | tool_node.py | 处理工具执行错误 |
验证节点相关
方法/类 | 文件 | 功能描述 |
---|
ValidationNode | tool_validator.py | 验证工具调用参数的节点 |
_func() | tool_validator.py | 验证节点的主函数 |
_default_format_error() | tool_validator.py | 默认错误格式化函数 |
条件和路由相关
方法/类 | 文件 | 功能描述 |
---|
should_continue() | chat_agent_executor.py | 决定是执行工具还是结束 |
route_tool_responses() | chat_agent_executor.py | 路由工具响应到下一步 |
tools_condition() | tool_node.py | 检查是否需要执行工具 |
中断相关
方法/类 | 文件 | 功能描述 |
---|
HumanInterrupt | interrupt.py | 定义人类干预的中断 |
HumanInterruptConfig | interrupt.py | 配置中断选项 |
ActionRequest | interrupt.py | 表示对人类操作的请求 |
HumanResponse | interrupt.py | 人类对中断的响应 |