LLM Agent 框架设计模式
LLM Agent 框架定义了智能体的决策循环和执行策略。不同框架在自主性、可靠性和效率之间做出不同权衡。
主流框架模式
python
# ReAct 模式
def react_loop(task, model, tools):
for _ in range(MAX_STEPS):
thought = model.think(task, history)
action = model.select_action(thought, tools)
observation = execute(action)
history.append((thought, action, observation))
if is_complete(observation):
return observation
# Plan-and-Execute 模式
def plan_and_execute(task, model, tools):
plan = model.plan(task)
for step in plan:
result = model.execute(step, tools)
if result.needs_replan:
plan = model.replan(task, result)| 框架 | 自主性 | 可靠性 | 适用场景 |
|---|---|---|---|
| ReAct | 高 | 中 | 探索性任务 |
| Plan-Execute | 中 | 高 | 结构化任务 |
| Reflexion | 高 | 高 | 需要迭代的任务 |
框架选择
简单任务用 ReAct,复杂项目用 Plan-Execute,需要自我修正的用 Reflexion。