Agent 设计模式总览
目录
模式关系图
flowchart TD
Router["Router / Subagent<br/>入口路由"]
Router --> 单一Agent
Router --> 协作Agent
Router --> 记忆Agent
subgraph 单一Agent
ReAct
Plan-Exec["Plan & Execute"]
Reflection
Tree-of-Thoughts["Tree of Thoughts"]
Self-Ask["Self-Ask"]
end
subgraph 协作Agent
Multi-Agent["Multi-Agent<br/>Collaboration"]
end
subgraph 记忆Agent
Memory-Augmented["Memory-Augmented"]
end
单一Agent -.-> ToolUse["Tool Use<br/>(基础能力)"]
ReAct -.-> ToolUse
如何选择模式
按任务复杂度
flowchart TD
subgraph 简单任务["简单任务(1-2 步)"]
Tool-Use["Tool Use / Function Calling"]
Self-Ask["Self-Ask"]
end
subgraph 中等任务["中等任务(3-5 步)"]
ReAct
Reflection
end
subgraph 复杂任务["复杂任务(5+ 步)"]
Plan-Execute["Plan-and-Execute"]
Multi-Agent["Multi-Agent Collaboration"]
Router["Router / Subagent"]
end
subgraph 创造性任务["创造性/策略性任务"]
Tree-of-Thoughts["Tree of Thoughts"]
end
按需求特征
| 需求 |
推荐模式 |
| 需要调用外部 API |
Tool Use |
| 需要动态调整策略 |
ReAct |
| 需要高质量输出 |
Reflection |
| 需要并行处理 |
Plan-and-Execute |
| 需要团队分工 |
Multi-Agent |
| 需要个性化/连续性 |
Memory-Augmented |
| 需要多路径探索 |
Tree of Thoughts |
| 需要拆解复杂问题 |
Self-Ask |
| 需要领域路由 |
Router/Subagent |
组合使用
实际项目中,这些模式经常组合使用:
示例 1:ReAct + Reflection + Memory
class AdvancedAgent:
"""组合了 ReAct、Reflection 和 Memory 的 Agent"""
def __init__(self):
self.memory = VectorMemoryStore() # Memory 模式
self.tools = ToolRegistry() # Tool Use 模式
def solve(self, task):
# ReAct 循环
while not done:
# 检索相关记忆
context = self.memory.search(task) # Memory
# 生成思考 + 行动
thought, action = self.react(task, context)
# 反思当前路径
if self.should_reflect(): # Reflection
feedback = self.reflect(current_progress)
action = self.adjust(action, feedback)
示例 2:Plan-and-Execute + Multi-Agent + Reflection
# 软件项目开发流程
plan = Planner.create_plan(requirements) # Plan-and-Execute
for step in plan:
# 路由到专业团队处理
if step.type == "architecture":
team = [ArchitectAgent, ReviewerAgent] # Multi-Agent
elif step.type == "implementation":
team = [CoderAgent, TesterAgent]
output = team.execute(step)
# 反思检查质量
review = ReviewerAgent.reflect(output) # Reflection
if review.score < threshold:
output = team.refine(output, review)
参考资源
评论
评论加载中…