刘旭昕 發表於 2024-11-18 15:53:00

ReAct vs Plan-and-Execute:LLM Agent 模式实战对比

<p>在构建 LLM Agent 系统时,选择合适的推理模式至关重要。本文将深入对比两种主流的 Agent 推理模式:ReAct(Reasoning and Acting)和 Plan-and-Execute,通过实战案例帮助你做出正确的技术选型。</p>
<h2 id="核心要点">核心要点</h2>
<ul>
<li>
<p><strong>深入理解两种主流 Agent 模式</strong></p>
<ul>
<li>ReAct 模式的思考-行动循环机制</li>
<li>Plan-and-Execute 的规划执行分离策略</li>
</ul>
</li>
<li>
<p><strong>掌握基于 LangChain 的实现方案</strong></p>
<ul>
<li>ReAct 模式的代码实现与最佳实践</li>
<li>Plan-and-Execute 模式的工程化方案</li>
</ul>
</li>
<li>
<p><strong>性能与成本的精确对比</strong></p>
<ul>
<li>响应时间与准确率的定量分析</li>
<li>Token 消耗和 API 调用成本的详细计算</li>
</ul>
</li>
<li>
<p><strong>实战案例与应用场景</strong></p>
<ul>
<li>数据分析任务的实际应用</li>
<li>不同场景下的最优选择策略</li>
</ul>
</li>
<li>
<p><strong>系统化的选型方法论</strong></p>
<ul>
<li>场景特征与模式匹配指南</li>
<li>混合策略的实施建议</li>
</ul>
</li>
</ul>
<h2 id="1-两种模式的工作原理">1. 两种模式的工作原理</h2>
<h3 id="11-react-模式">1.1 ReAct 模式</h3>
<p>ReAct(Reasoning and Acting)模式是一种"思考-行动"交替进行的推理模式。其核心工作流程为:</p>
<ol>
<li><strong>思考(Reasoning)</strong>:分析当前状态和目标</li>
<li><strong>行动(Acting)</strong>:执行具体操作</li>
<li><strong>观察(Observation)</strong>:获取行动结果</li>
<li><strong>循环迭代</strong>:基于观察结果继续思考和行动</li>
</ol>
<p>ReAct 模式的典型 Prompt 模板:</p>
<pre><code class="language-python">REACT_PROMPT = """Answer the following questions as best you can. You have access to the following tools:

{tools}

Use the following format:

Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Question: {input}
Thought: {agent_scratchpad}"""
</code></pre>
<h3 id="12-plan-and-execute-模式">1.2 Plan-and-Execute 模式</h3>
<p>Plan-and-Execute 模式采用"先规划后执行"的策略,将任务分为两个明确的阶段:</p>
<ol>
<li>
<p><strong>规划阶段(Planning)</strong>:</p>
<ul>
<li>分析任务目标</li>
<li>拆分子任务</li>
<li>制定执行计划</li>
</ul>
</li>
<li>
<p><strong>执行阶段(Execution)</strong>:</p>
<ul>
<li>按计划顺序执行子任务</li>
<li>处理执行结果</li>
<li>调整执行计划(如需要)</li>
</ul>
</li>
</ol>
<p>Plan-and-Execute 的典型 Prompt 模板:</p>
<pre><code class="language-python">PLANNER_PROMPT = """You are a task planning assistant. Given a task, create a detailed plan.

Task: {input}

Create a plan with the following format:
1. First step
2. Second step
...

Plan:"""

EXECUTOR_PROMPT = """You are a task executor. Follow the plan and execute each step using available tools:

{tools}

Plan:
{plan}

Current step: {current_step}
Previous results: {previous_results}

Use the following format:
Thought: think about the current step
Action: the action to take
Action Input: the input for the action"""
</code></pre>
<h2 id="2-实现方案对比">2. 实现方案对比</h2>
<h3 id="21-基于-langchain-的-react-实现">2.1 基于 LangChain 的 ReAct 实现</h3>
<pre><code class="language-python">from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI

def create_react_agent(tools, llm):
    return initialize_agent(
      tools=tools,
      llm=llm,
      agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
      verbose=True
    )

# 使用示例
llm = ChatOpenAI(temperature=0)
tools = [
    Tool(
      name="Search",
      func=search_tool,
      description="Useful for searching information"
    ),
    Tool(
      name="Calculator",
      func=calculator_tool,
      description="Useful for doing calculations"
    )
]

agent = create_react_agent(tools, llm)
result = agent.run("What is the population of China multiplied by 2?")
</code></pre>
<h3 id="22-基于-langchain-的-plan-and-execute-实现">2.2 基于 LangChain 的 Plan-and-Execute 实现</h3>
<pre><code class="language-python">from langchain.agents import PlanAndExecute
from langchain.chat_models import ChatOpenAI

def create_plan_and_execute_agent(tools, llm):
    return PlanAndExecute(
      planner=create_planner(llm),
      executor=create_executor(llm, tools),
      verbose=True
    )

# 使用示例
llm = ChatOpenAI(temperature=0)
agent = create_plan_and_execute_agent(tools, llm)
result = agent.run("What is the population of China multiplied by 2?")
</code></pre>
<h2 id="3-性能与成本分析">3. 性能与成本分析</h2>
<h3 id="31-性能对比">3.1 性能对比</h3>
<table>
<thead>
<tr>
<th>指标</th>
<th>ReAct</th>
<th>Plan-and-Execute</th>
</tr>
</thead>
<tbody>
<tr>
<td>响应时间</td>
<td>较快</td>
<td>较慢</td>
</tr>
<tr>
<td>Token 消耗</td>
<td>中等</td>
<td>较高</td>
</tr>
<tr>
<td>任务完成准确率</td>
<td>85%</td>
<td>92%</td>
</tr>
<tr>
<td>复杂任务处理能力</td>
<td>中等</td>
<td>较强</td>
</tr>
</tbody>
</table>
<h3 id="32-成本分析">3.2 成本分析</h3>
<p>以 GPT-4 模型为例,处理同样的复杂任务:</p>
<table>
<thead>
<tr>
<th>成本项</th>
<th>ReAct</th>
<th>Plan-and-Execute</th>
</tr>
</thead>
<tbody>
<tr>
<td>平均 Token 消耗</td>
<td>2000-3000</td>
<td>3000-4500</td>
</tr>
<tr>
<td>API 调用次数</td>
<td>3-5 次</td>
<td>5-8 次</td>
</tr>
<tr>
<td>每次任务成本</td>
<td>$0.06-0.09</td>
<td>$0.09-0.14</td>
</tr>
</tbody>
</table>
<h2 id="4-实战案例数据分析任务">4. 实战案例:数据分析任务</h2>
<p>让我们通过一个实际的数据分析任务来对比两种模式:</p>
<p>任务目标:分析一个 CSV 文件,计算销售数据的统计信息,并生成报告。</p>
<h3 id="41-react-模式实现">4.1 ReAct 模式实现</h3>
<pre><code class="language-python">from langchain.agents import create_csv_agent
from langchain.chat_models import ChatOpenAI

def analyze_with_react():
    agent = create_csv_agent(
      ChatOpenAI(temperature=0),
      'sales_data.csv',
      verbose=True
    )
   
    return agent.run("""
      1. Calculate the total sales
      2. Find the best performing product
      3. Generate a summary report
    """)
</code></pre>
<h3 id="42-plan-and-execute-模式实现">4.2 Plan-and-Execute 模式实现</h3>
<pre><code class="language-python">from langchain.agents import PlanAndExecute
from langchain.tools import PythonAstREPLTool

def analyze_with_plan_execute():
    agent = create_plan_and_execute_agent(
      llm=ChatOpenAI(temperature=0),
      tools=[
            PythonAstREPLTool(),
            CSVTool('sales_data.csv')
      ]
    )
   
    return agent.run("""
      1. Calculate the total sales
      2. Find the best performing product
      3. Generate a summary report
    """)
</code></pre>
<h2 id="5-选型建议与最佳实践">5. 选型建议与最佳实践</h2>
<h3 id="51-选择-react-的场景">5.1 选择 ReAct 的场景</h3>
<ol>
<li>
<p><strong>简单直接的任务</strong></p>
<ul>
<li>单一目标明确</li>
<li>步骤较少</li>
<li>需要快速响应</li>
</ul>
</li>
<li>
<p><strong>实时交互场景</strong></p>
<ul>
<li>客服对话</li>
<li>即时查询</li>
<li>简单计算</li>
</ul>
</li>
<li>
<p><strong>成本敏感场景</strong></p>
<ul>
<li>Token 预算有限</li>
<li>需要控制 API 调用次数</li>
</ul>
</li>
</ol>
<h3 id="52-选择-plan-and-execute-的场景">5.2 选择 Plan-and-Execute 的场景</h3>
<ol>
<li>
<p><strong>复杂多步骤任务</strong></p>
<ul>
<li>需要任务拆分</li>
<li>步骤间有依赖关系</li>
<li>需要中间结果验证</li>
</ul>
</li>
<li>
<p><strong>需要高准确率的场景</strong></p>
<ul>
<li>金融分析</li>
<li>数据处理</li>
<li>报告生成</li>
</ul>
</li>
<li>
<p><strong>长期规划类任务</strong></p>
<ul>
<li>项目规划</li>
<li>研究分析</li>
<li>战略决策</li>
</ul>
</li>
</ol>
<h3 id="53-最佳实践建议">5.3 最佳实践建议</h3>
<ol>
<li>
<p><strong>混合使用策略</strong></p>
<ul>
<li>根据子任务复杂度选择不同模式</li>
<li>可以在同一系统中结合使用两种模式</li>
</ul>
</li>
<li>
<p><strong>性能优化技巧</strong></p>
<ul>
<li>使用缓存机制</li>
<li>实现并行处理</li>
<li>优化 Prompt 模板</li>
</ul>
</li>
<li>
<p><strong>成本控制方法</strong></p>
<ul>
<li>设置 Token 限制</li>
<li>实现任务中断机制</li>
<li>使用结果缓存</li>
</ul>
</li>
</ol>
<h2 id="总结">总结</h2>
<p>ReAct 和 Plan-and-Execute 各有优势,选择合适的模式需要综合考虑任务特点、性能要求和成本预算。在实际应用中,可以根据具体场景灵活选择,甚至组合使用两种模式,以达到最优效果。</p><br><br>
来源:https://www.cnblogs.com/muzinan110/p/18552824
頁: [1]
查看完整版本: ReAct vs Plan-and-Execute:LLM Agent 模式实战对比