ANALYSIS
- MySQL
- PostgreSQL
- Posts
- React
- 技术手册
Dify Agent 节点使用指南
概述
Agent 节点是 Dify 中高度封装的智能单元,具备自主思考和规划能力。你只需要给它设定目标(Goal)并提供工具(Tools),它就能在内部自主地思考、规划、执行任务。
Agent 节点基础
什么是 Agent 节点
Agent 节点是一个高度封装的智能单元,可以:
- 自主思考任务目标
- 规划执行步骤
- 选择合适的工具
- 处理复杂任务
- 适应不同场景
与 LLM 节点的区别
| 特性 | LLM 节点 | Agent 节点 |
|---|---|---|
| 自主性 | 低 | 高 |
| 规划能力 | 无 | 有 |
| 工具使用 | 手动配置 | 自动选择 |
| 复杂度 | 简单 | 复杂 |
| 适用场景 | 单一任务 | 复杂任务 |
适用场景
| 场景 | 说明 |
|---|---|
| 研究助手 | 自动检索和总结信息 |
| 数据分析 | 自动收集和分析数据 |
| 任务自动化 | 自动完成多步骤任务 |
| 决策支持 | 基于信息提供决策建议 |
创建 Agent 节点
添加 Agent 节点
步骤:
- 在工作流画布中点击 + 按钮
- 选择 Agent 节点
- 将节点添加到画布
基础配置
目标设置:
PRTCL // YAML
Agent 节点配置: goal: "帮助用户完成特定任务" instruction: | 你是一个智能助手,可以: 1. 搜索网络信息 2. 读取文件内容 3. 调用 API 接口 4. 分析数据
根据用户需求,自主规划并执行任务。提示词模板:
PRTCL // YAML
提示词配置: role: "智能助手" expertise: - "信息检索" - "数据分析" - "任务规划" personality: "专业、高效、可靠"工具配置
可用工具
Dify 支持多种工具类型:
| 工具类型 | 说明 | 示例 |
|---|---|---|
| 网络搜索 | 搜索网络信息 | Google, Bing, Baidu |
| 文件操作 | 读取和写入文件 | 读取文档、保存结果 |
| API 调用 | 调用外部 API | 天气 API, 股票 API |
| 数据库 | 查询和更新数据库 | MySQL, PostgreSQL |
| 计算器 | 数学计算 | 基础运算、统计分析 |
配置工具
启用工具:
PRTCL // YAML
工具配置: enabled_tools: - name: "web_search" description: "搜索网络信息" provider: "google" max_results: 10
- name: "file_read" description: "读取文件内容" allowed_paths: - "/data/documents" - "/data/reports"
- name: "api_call" description: "调用 API 接口" endpoints: - name: "weather" url: "https://api.weather.com/v1/current" method: "GET" - name: "stock" url: "https://api.stock.com/v1/quote" method: "GET"
- name: "calculator" description: "数学计算" operations: - "addition" - "subtraction" - "multiplication" - "division" - "statistics"工具权限:
PRTCL // YAML
工具权限: web_search: max_results: 10 timeout: 30
file_read: allowed_paths: - "/data/documents" - "/data/reports" max_file_size: 10485760 # 10MB
api_call: rate_limit: 60 # 每分钟 60 次 timeout: 10自定义工具
创建自定义工具:
PRTCL // YAML
自定义工具: name: "custom_analysis" description: "自定义数据分析工具" type: "api" endpoint: "https://api.example.com/analyze" method: "POST" headers: Authorization: "Bearer ${API_KEY}" parameters: - name: "data" type: "object" required: true - name: "options" type: "object" required: false推理配置
推理策略
Dify 支持多种推理策略:
| 策略 | 说明 | 适用场景 |
|---|---|---|
| Chain of Thought | 链式思维 | 简单任务 |
| Tree of Thoughts | 树状思维 | 复杂任务 |
| ReAct | 推理-行动循环 | 需要工具的任务 |
配置推理
推理设置:
PRTCL // YAML
推理配置: enabled: true strategy: "tree_of_thought" # chain_of_thought, tree_of_thought, react max_iterations: 5 max_thought_depth: 3 timeout: 300推理参数:
PRTCL // YAML
推理参数: thought_budget: 1000 # 思考预算(token 数) action_budget: 10 # 动作预算(最多执行 10 次工具调用) confidence_threshold: 0.7 # 置信度阈值推理示例
任务:搜索并分析某个主题
推理过程:
- 思考:用户想要了解 AI 的发展趋势
- 规划:搜索相关信息 → 分析内容 → 总结结论
- 执行:
- 调用 web_search 搜索”AI 发展趋势”
- 读取搜索结果
- 分析关键信息
- 生成总结
- 输出:提供详细的趋势分析报告
记忆管理
短期记忆
短期记忆配置:
PRTCL // YAML
短期记忆: enabled: true max_messages: 20 strategy: "recent" # recent, summary retain_period: 3600 # 保留 1 小时长期记忆
长期记忆配置:
PRTCL // YAML
长期记忆: enabled: true storage: "vector_db" # vector_db, file, database retrieval: top_k: 5 threshold: 0.7上下文管理
上下文配置:
PRTCL // YAML
上下文管理: enabled: true context_window: 8000 # 上下文窗口大小 retain_strategy: "hybrid" # recent, summary, hybrid compression: enabled: true threshold: 0.8使用示例
研究助手
目标:自动检索和总结信息
配置:
PRTCL // YAML
Agent 节点配置: goal: "帮助用户研究特定主题" instruction: | 你是一个研究助手,可以: 1. 搜索网络信息 2. 读取学术文献 3. 分析数据 4. 生成研究报告
研究步骤: 1. 理解研究主题 2. 搜索相关资料 3. 分析关键信息 4. 生成结构化报告 tools: - "web_search" - "file_read" - "calculator" reasoning: strategy: "tree_of_thought" max_iterations: 5数据分析助手
目标:自动收集和分析数据
配置:
PRTCL // YAML
Agent 节点配置: goal: "帮助用户分析数据" instruction: | 你是一个数据分析助手,可以: 1. 读取数据文件 2. 执行统计分析 3. 生成可视化报告 4. 提供洞察和建议
分析步骤: 1. 理解数据结构 2. 执行统计分析 3. 识别模式和趋势 4. 生成报告和建议 tools: - "file_read" - "calculator" - "database" reasoning: strategy: "react" max_iterations: 10任务自动化助手
目标:自动完成多步骤任务
配置:
PRTCL // YAML
Agent 节点配置: goal: "帮助用户完成自动化任务" instruction: | 你是一个任务自动化助手,可以: 1. 读取任务描述 2. 分解任务步骤 3. 执行每个步骤 4. 汇总结果
自动化流程: 1. 理解任务需求 2. 规划执行步骤 3. 调用相应工具 4. 监控执行过程 5. 生成执行报告 tools: - "file_read" - "api_call" - "web_search" reasoning: strategy: "chain_of_thought" max_iterations: 8高级功能
任务分解
任务分解配置:
PRTCL // YAML
任务分解: enabled: true max_subtasks: 10 parallel_execution: true dependency_resolution: true错误处理
错误处理配置:
PRTCL // YAML
错误处理: enabled: true strategy: "retry" # retry, skip, stop max_retries: 3 retry_delay: 5 fallback_to_human: true协作能力
多 Agent 协作:
PRTCL // YAML
多 Agent 配置: enabled: true agents: - id: "researcher" role: "信息收集" tools: - "web_search" - "file_read" - id: "analyst" role: "数据分析" tools: - "calculator" - "database" - id: "writer" role: "报告撰写" tools: - "file_write" collaboration: mode: "sequential" # sequential, parallel, hybrid性能优化
推理优化
减少推理次数:
PRTCL // YAML
推理优化: enabled: true thought_pruning: true # 剪枝不重要的思考 action_caching: true # 缓存工具调用结果 early_stopping: true # 提前停止工具优化
工具调用优化:
PRTCL // YAML
工具优化: enabled: true parallel_execution: true batch_size: 5 timeout: 30记忆优化
记忆压缩:
PRTCL // YAML
记忆压缩: enabled: true compression_ratio: 0.5 # 压缩到 50% strategy: "summary" # summary, keyword监控和调试
监控指标
关键指标:
PRTCL // YAML
监控指标: - name: "推理次数" target: "< 10" - name: "工具调用次数" target: "< 20" - name: "执行时间" target: "< 300s" - name: "成功率" target: "> 90%"调试工具
查看推理过程:
- 点击 Agent 节点
- 查看 推理日志
- 分析思考过程
- 识别问题
查看工具调用:
- 查看 工具调用日志
- 检查输入输出
- 验证结果
- 优化配置
日志分析
日志类型:
- 思考日志
- 行动日志
- 工具调用日志
- 错误日志
最佳实践
设计原则
- 明确目标:清晰定义 Agent 的目标
- 合理工具:提供必要的工具,避免过多
- 优化推理:选择合适的推理策略
- 监控性能:持续监控和优化
工具选择
工具选择原则:
- 选择最相关的工具
- 避免冗余工具
- 优化工具配置
- 测试工具性能
推理优化
推理优化建议:
- 选择合适的推理策略
- 设置合理的迭代次数
- 优化思考预算
- 监控推理过程
常见问题
Q1: Agent 执行时间过长怎么办?
解决方案:
- 减少 max_iterations
- 优化工具调用
- 启用 early_stopping
- 优化推理策略
Q2: Agent 选择错误的工具怎么办?
解决方案:
- 优化工具描述
- 调整推理策略
- 增加工具权限限制
- 提供更多示例
Q3: 如何提高 Agent 的准确性?
解决方案:
- 优化提示词
- 改进工具配置
- 增加推理迭代
- 收集反馈优化
资源链接
- Agent 节点文档: https://docs.dify.ai/zh/use-dify/tutorials/workflow-101/lesson-08
- 工具文档: https://docs.dify.ai/zh/use-dify/tools
- 推理策略文档: https://docs.dify.ai/zh/use-dify/reasoning
最后更新: 2026-03-12 作者: EchoHaoRan
R P
Rhine Lab Pioneer Division
Auth_Verified: 2026.04.08
Auth_Verified: 2026.04.08
