← 返回列表

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

An AI Agent is an intelligent entity capable of autonomously perceiving its environment, making decisions, and executing actions. Its core methodologies mainly include three types: ReAct, Plan-and-Solve, and Reflection. Below, we introduce each, supplemented 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 reasoning: "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 formulate a complete plan, then execute step by step. The planning phase decomposes complex tasks into sub-steps, and the execution phase completes them sequentially, possibly adjusting the plan based on intermediate results.

Flowchart:

[Task] → [Formulate 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 info", "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 advances
2. Read and organize key points
3. Write blog outline
4. Fill in content
5. Proofread and publish
- Execution: Complete each step in order, 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 includes 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 Features Applicable Scenarios
ReAct Interleaved reasoning and action, 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 a reflection mechanism to ReAct, or reflecting after each step in Plan-and-Solve.

评论

暂无已展示的评论。

发表评论(匿名)