agentic_huge_data_base / wiki
页面 RAGFlow · 8.6 智能体工具与 ReAct 循环·DeepWiki 中文全文译文

8.6 · 智能体工具与 ReAct 循环

复杂文档理解与引用检索 · 聚焦本章的模块关系、源码依据与实现要点。

项目RAGFlow 章节8.6 状态全文译文 模块检索、召回与索引、界面与交互、模型调用与提供方适配、配置治理
源码线索
  • agent/canvas.py
  • agent/component/agent_with_tools.py
  • agent/component/base.py
  • agent/component/categorize.py
  • agent/component/llm.py
  • agent/tools/base.py
  • agent/tools/exesql.py
  • agent/tools/retrieval.py
  • common/mcp_tool_call_conn.py
  • rag/prompts/generator.py
模块标签
  • 检索、召回与索引
  • 界面与交互
  • 模型调用与提供方适配
  • 配置治理
  • 图谱与关系

章节正文

智能体工具与 ReAct 循环

智能体工具与 ReAct 循环

相关源文件

以下文件为本维基页面的生成提供了上下文:

  • agent/canvas.py
  • agent/component/agent_with_tools.py
  • agent/component/base.py
  • agent/component/categorize.py
  • agent/component/llm.py
  • agent/tools/base.py
  • agent/tools/exesql.py
  • agent/tools/retrieval.py
  • common/mcp_tool_call_conn.py
  • rag/prompts/generator.py
  • web/src/components/rerank.tsx
  • web/src/components/similarity-slider/index.tsx
  • web/src/components/switch-fom-field.tsx
  • web/src/components/top-n-item.tsx
  • web/src/components/use-knowledge-graph-item.tsx
  • web/src/pages/agent/canvas/node/dropdown/next-step-dropdown.tsx
  • web/src/pages/agent/canvas/node/tool-node.tsx
  • web/src/pages/agent/form/agent-form/agent-tools.tsx
  • web/src/pages/agent/form/agent-form/tool-popover/index.tsx
  • web/src/pages/agent/form/agent-form/tool-popover/tool-command.tsx
  • web/src/pages/agent/form/agent-form/tool-popover/use-update-tools.ts
  • web/src/pages/agent/form/agent-form/use-get-tools.ts
  • web/src/pages/agent/form/exesql-form/index.tsx
  • web/src/pages/agent/form/exesql-form/use-submit-form.ts
  • web/src/pages/agent/form/retrieval-form/next.tsx
  • web/src/pages/agent/form/tool-form/constant.tsx
  • web/src/pages/agent/form/tool-form/exesql-form/index.tsx
  • web/src/pages/agent/form/tool-form/retrieval-form/index.tsx
  • web/src/pages/agent/hooks/use-agent-tool-initial-values.ts
  • web/src/pages/agent/options.ts
  • web/src/pages/next-chats/hooks/use-build-form-refs.ts

本文档描述了智能体(Agent)组件的工具集成系统及其对 ReAct(推理+行动)模式的实现,用于多步骤问题求解。智能体组件扩展了基础大语言模型(LLM)组件,以编排迭代工作流,使语言模型能够推理任务、调用外部工具、观察结果并综合生成最终答案。

关于画布工作流系统和组件架构的通用信息,请参阅画布引擎与领域特定语言(DSL)(8.1)组件系统架构(8.2)。关于不使用工具的基础大语言模型(LLM)组件的详细信息,请参阅内置组件(8.3)

智能体组件概述

智能体组件是一种专门的大语言模型(LLM)组件,实现了 ReAct 风格的工具使用。与生成单次文本响应的标准大语言模型(LLM)组件不同,智能体可以分析任务、通过选择工具来规划行动,并反思观察结果以确定后续步骤。

智能体组件定义在 agent/component/agent_with_tools.py agent/component/agent_with_tools.py:74-74 中,同时继承自 LLMToolBase agent/component/agent_with_tools.py:73-73。它使用由 max_rounds 参数控制的迭代循环 agent/component/agent_with_tools.py:68-68

智能体与大语言模型(LLM)组件对比
特性大语言模型(LLM)组件智能体组件
基类ComponentBase agent/component/llm.py:83-83LLMToolBase agent/component/agent_with_tools.py:73-73
工具访问内置工具、MCP 和子智能体 agent/component/agent_with_tools.py:78-109
迭代次数单次多轮(max_roundsagent/component/agent_with_tools.py:90-90
参数类LLMParam agent/component/llm.py:34-34AgentParam agent/component/agent_with_tools.py:39-39

来源:agent/component/agent_with_tools.py:39-92agent/component/llm.py:34-91agent/component/agent_with_tools.py:73-74

工具集成架构

智能体架构通过将模型工具调用映射到特定的 Python 对象或远程 MCP 会话,弥合了高层大语言模型(LLM)推理与底层代码执行之间的鸿沟。

工具注册表与映射

标题:自然语言空间到代码实体空间:工具映射

RAGFlow · 工具注册表与映射 · 图 1
RAGFlow · 工具注册表与映射 · 图 1

来源:agent/component/agent_with_tools.py:133-145agent/canvas.py:30-31agent/tools/retrieval.py:86-88agent/tools/exesql.py:80-81agent/component/categorize.py:98-99

内置组件工具

内置工具是智能体可以调用的专用模块。后端通过 _load_tool_obj agent/component/agent_with_tools.py:133-145 初始化这些工具,该方法使用 component_class 工厂实例化组件及其参数对象。

关键工具:

  • 检索(Retrieval):与检索增强生成(RAG)搜索系统交互。它支持数据集过滤、关键词相似度权重和多语言支持 agent/tools/retrieval.py:37-72。它还可以应用元数据过滤器 agent/tools/retrieval.py:137-172
  • ExeSQL:在多种数据库类型(MySQL、Postgres、OceanBase 等)上执行 SQL 查询 agent/tools/exesql.py:57-57。它会执行安全检查,防止访问核心的 rag_flow 数据库 agent/tools/exesql.py:65-69
  • 分类(Categorize):使用大语言模型(LLM)将用户查询分类到预定义类别中,并确定下一个执行路径(_nextagent/component/categorize.py:157-158。它会根据类别描述动态生成系统提示词 agent/component/categorize.py:59-95
MCP(模型上下文协议)集成

智能体支持模型上下文协议,用于动态工具发现和执行。

  • 会话管理MCPToolCallSession agent/component/agent_with_tools.py:104-104 使用服务器变量和自定义请求头管理到 MCP 服务器的连接。
  • 元数据转换mcp_tool_metadata_to_openai_tool agent/component/agent_with_tools.py:108-108 将 MCP 工具模式转换为智能体的大语言模型(LLM)捆绑包所使用的 OpenAI 兼容函数格式。

ReAct 循环与执行流程

智能体的推理过程遵循 ReAct 模式,迭代调用工具,直到生成最终答案或达到 max_rounds 限制。

可视化工作流集成

标题:画布中的智能体工具连接

RAGFlow · 可视化工作流集成 · 图 2
RAGFlow · 可视化工作流集成 · 图 2

来源:agent/component/agent_with_tools.py:110-113agent/tools/base.py:50-88web/src/pages/agent/canvas/node/dropdown/next-step-dropdown.tsx:17-28agent/canvas.py:109-110

执行逻辑

Agent 组件的执行流程包括:

  1. 工具绑定:在初始化期间,工具会被索引并绑定到 LLMBundle agent/component/agent_with_tools.py:78-113
  2. 消息适配:系统提示词和用户提示词会被合并,并使用 message_fit_in 截断以适配模型的上下文窗口 agent/component/agent_with_tools.py:115-120
  3. 工具调用会话:创建一个 LLMToolPluginCallSession agent/tools/base.py:50-51 来处理大语言模型(LLM)生成的工具调用的执行。它同时支持异步(tool_call_async)和同步(tool_call)调用 agent/tools/base.py:55-58
  4. 迭代推理LLMBundle 管理实际的循环,每当需要执行工具时,就会调用回调函数(指向 self._canvas.tool_use_callbackagent/component/agent_with_tools.py:110-110

工具参数管理

工具通过继承自 ComponentParamBase agent/component/base.py:40-40ToolParamBase agent/tools/base.py:94-94 的参数类进行配置。

变量解析

Graph 类提供了 get_variable_value agent/canvas.py:195-210get_value_with_variable agent/canvas.py:168-193 方法来解析诸如 {sys.query}{component_id@variable_name} 之类的占位符。这使得像 Retrieval 这样的工具能够从上游输出中动态解析搜索查询 agent/tools/retrieval.py:100-110

检索工具配置

RetrievalParam agent/tools/retrieval.py:37-72 包括:

  • 阈值:用于混合搜索调优的 similarity_threshold(相似度阈值)和 keywords_similarity_weight(关键词相似度权重)agent/tools/retrieval.py:58-59
  • 搜索范围:用于定义搜索上下文的 dataset_ids(数据集 ID)和 memory_ids(记忆 ID)agent/tools/retrieval.py:62-64
  • 元数据过滤:支持手动、半自动和自动方法,用于基于元数据过滤文档 agent/tools/retrieval.py:137-172
SQL 工具配置

ExeSQLParam agent/tools/exesql.py:29-55 配置了:

  • 连接:数据库类型、主机、端口、用户名和密码 agent/tools/exesql.py:48-53
  • 限制max_records(最大记录数),用于防止检索过多数据 agent/tools/exesql.py:54-54
  • 执行_invoke 方法按分号拆分多条 SQL 语句,并针对不同引擎处理特定的驱动程序逻辑 agent/tools/exesql.py:126-144

来源:agent/canvas.py:168-210agent/tools/retrieval.py:37-72agent/tools/exesql.py:29-81agent/component/agent_with_tools.py:133-145agent/tools/base.py:50-88