Skip to content

规划能力:任务拆解与依赖管理

Agent 和 Chatbot 最大的区别在于任务复杂度。 Chatbot 只能回答简单问题。 Agent 能做复杂事情(如写代码、做报表)。 做复杂事情需要规划 (Planning)

  1. Task Decomposition: 把大任务拆成小任务。
  2. Dependency Management: 任务之间有依赖关系(必须先查 API 文档,才能写代码)。
  3. Self-Correction: 如果某步失败了,重新规划。

1. 任务拆解策略 (Decomposition)

1.1 LLM-based Decomposition

直接问 LLM:“我要做 X,请列出步骤。” Prompt:

"You are a planner. Break down the following goal into step-by-step subtasks. Goal: Write a Python script to scrape stock prices and plot a chart." Output:

  1. Install requests and matplotlib.
  2. Find a free stock API.
  3. Write code to fetch data.
  4. Write code to plot data.
  5. Save the chart as stock.png.

1.2 Hierarchical Planning (分层规划)

对于特别复杂的任务,一层拆解不够。 需要递归拆解

  • Level 1: Scrape Data -> Process Data -> Plot Data.
  • Level 2 (Scrape): Find API -> Get Key -> Call Endpoint.

2. 依赖管理 (Dependency Graph)

2.1 顺序执行 (Sequential)

步骤 1 完成后,把结果传给步骤 2。 这是最简单的模式。

2.2 有向无环图 (DAG)

步骤 1 和 2 可以并行(如分别查两个 API)。 步骤 3 依赖 1 和 2 的结果(如合并数据)。 LangGraph 支持定义这种图结构 (Graph Structure)

3. 自我修正 (Self-Correction / Re-planning)

3.1 动态规划

计划不是死的。 如果步骤 2 失败了(API 挂了),Agent 必须能感知到,并重新生成计划Prompt:

"Step 2 failed with error 500. Please generate a new plan to achieve the goal without using that API." New Plan:

  1. Use yfinance library instead of direct API call.
  2. ...

3.2 反思 (Reflection)

Agent 甚至可以反思:“我为什么选了这个烂 API?” 下次规划时避开它。

4. 工程实现:LangGraph / BabyAGI

4.1 BabyAGI

这是最早的自动规划 Agent。

  • Task List: 待办事项列表。
  • Execution Agent: 执行当前任务。
  • Task Creation Agent: 根据执行结果,生成新任务。
  • Prioritization Agent: 对任务列表重新排序。

4.2 LangGraph

LangChain 的升级版,支持循环(Cycles)和状态机。 你可以定义:

  • State: 当前的任务列表、已完成的任务、中间结果。
  • Nodes: Planner, Executor, Reviewer。
  • Edges: 流程控制(If success -> Next; If fail -> Re-plan)。

规划能力是 Agent 的前额叶皮层 (Prefrontal Cortex)。 它决定了 Agent 的智商上限。 一个优秀的 Planner 能把看似不可能的任务(如写一个操作系统),拆解成无数个可执行的小步骤(如写一个 Hello World)。 这就是工程思维在 AI 时代的体现。