AI Series Interview Question 6: Three Core Methodologies of AI Agent: ReAct, Plan-and-Solve, and Reflection
Three Core Methodologies of AI Agent: ReAct, Plan-and-Solve, and Reflection
AI Agent is an intelligent entity that can autonomously perceive the environment, make decisions, and execute actions. Its core methodologies mainly include three types: ReAct, Plan-and-Solve, and Reflection. Below, we introduce each with flowcharts and code examples.
1. ReAct (Reasoning + Acting)
Core Idea: Interleave reasoning and acting. At each step, the agent first thinks about the current state and next plan (reasoning), then executes an action (e.g., calling a tool, searching for information), and continues reasoning based on the result.
Flowchart:
[Initial State] → [Reasoning: Think Next Step] → [Action: Execute Action] → [Observe Result] → [Reasoning: Update Plan] → ... → [Final Answer]
Example Code (Pseudocode):
def react_agent(question):
context = []
while not solved:
# Reasoning: generate thought step
thought = llm.generate_thought(question, context)
# Action: choose action based on thought
action = llm.choose_action(thought)
# Execute action, get observation
observation = execute_action(action)
# Add thought, action, observation to context
context.append((thought, action, observation))
return final_answer
Example:
- User asks: "What's the weather in Beijing today?"
- Agent reasons: "I need to query the weather API, need city name and date."
- Action: Call weather API (parameters: Beijing, today)
- Observation: Returns "Sunny, 25°C"
- Reasoning: "Information obtained, can answer."
- Output: "Beijing today is sunny, 25°C."
2. Plan-and-Solve
Core Idea: First create a complete plan, then execute step by step. In the planning phase, decompose the complex task into sub-steps; in the execution phase, complete them in order, possibly adjusting the plan based on intermediate results.
Flowchart:
[Task] → [Create Plan: Decompose into Sub-steps] → [Execute Step 1] → [Execute Step 2] → ... → [Execute Step N] → [Final Answer]
Example Code:
def plan_and_solve(task):
# Planning phase
plan = llm.generate_plan(task) # e.g., ["Search materials", "Organize information", "Write report"]
context = {}
for step in plan:
# Execute each step
result = execute_step(step, context)
context[step] = result
# Synthesize results
final = llm.synthesize(context)
return final
Example:
- Task: "Write a blog about AI Agent"
- Plan:
1. Search for AI Agent definition and latest developments
2. Read and organize key points
3. Write blog outline
4. Fill in content
5. Proofread and publish
- Execution: Complete each step sequentially, finally output the blog.
3. Reflection
Core Idea: The agent reflects on its own behavior during or after execution, evaluates results, and improves subsequent actions. It often involves self-criticism, error correction, or strategy optimization.
Flowchart:
[Action] → [Observe Result] → [Reflection: Evaluate Success] → [If Failed: Adjust Strategy] → [Act Again] → ... → [Success]
Example Code:
def reflection_agent(task):
max_attempts = 3
for attempt in range(max_attempts):
action = llm.generate_action(task)
result = execute(action)
# Reflection
reflection = llm.reflect(task, action, result)
if reflection['success']:
return result
else:
# Adjust task description or strategy based on reflection
task = reflection['improved_task']
return None
Example:
- Task: "Calculate 1234 * 5678"
- Action: Direct calculation, get result 7006652
- Reflection: Check calculation process, find carry error
- Adjustment: Recalculate, get correct result 7006652 (actually correct)
- If still wrong, continue reflecting until correct.
Summary Comparison
| Methodology | Characteristics | Applicable Scenarios |
|---|---|---|
| ReAct | Interleaved reasoning and acting, dynamic adjustment | Tasks requiring real-time information interaction (e.g., Q&A, search) |
| Plan-and-Solve | Plan first then execute, structured decomposition | Complex multi-step tasks (e.g., writing, data analysis) |
| Reflection | Self-reflection and correction, iterative optimization | Tasks requiring high accuracy (e.g., math calculation, code generation) |
In practice, these three are often combined, such as adding reflection mechanism to ReAct, or reflecting after each step in Plan-and-Solve.
评论
暂无已展示的评论。
发表评论(匿名)