Reasoning
了解如何启用和使用 agent reasoning 来提升任务执行效果。
概述
Agent reasoning 是一项功能,它允许 agent 在执行前先对任务进行思考并制定计划。这有助于 agent 以更有条理的方式处理任务,并确保其已准备好完成所分配的工作。
用法
要为 agent 启用 reasoning,只需在创建 agent 时设置 reasoning=True:
from crewai import Agentagent = Agent(role="Data Analyst",goal="Analyze complex datasets and provide insights",backstory="You are an experienced data analyst with expertise in finding patterns in complex data.",reasoning=True, # Enable reasoningmax_reasoning_attempts=3 # Optional: Set a maximum number of reasoning attempts)
工作原理
启用 reasoning 后,在执行任务之前,agent 会:
- 对任务进行思考并创建详细计划
- 评估自己是否已经准备好执行任务
- 根据需要不断优化计划,直到准备就绪或达到
max_reasoning_attempts - 在执行前将 reasoning plan 注入到任务描述中
这个过程有助于 agent 将复杂任务拆解为可管理的步骤,并在开始之前识别潜在挑战。
配置选项
示例
下面是一个完整示例:
from crewai import Agent, Task, Crew# Create an agent with reasoning enabledanalyst = Agent(role="Data Analyst",goal="Analyze data and provide insights",backstory="You are an expert data analyst.",reasoning=True,max_reasoning_attempts=3 # Optional: Set a limit on reasoning attempts)# Create a taskanalysis_task = Task(description="Analyze the provided sales data and identify key trends.",expected_output="A report highlighting the top 3 sales trends.",agent=analyst)# Create a crew and run the taskcrew = Crew(agents=[analyst], tasks=[analysis_task])result = crew.kickoff()print(result)
错误处理
Reasoning 过程在设计上具有较强的健壮性,并内置了错误处理机制。如果在 reasoning 过程中发生错误,agent 会在没有 reasoning plan 的情况下继续执行任务。这可以确保即使 reasoning 过程失败,任务仍然能够被执行。
下面展示了如何在代码中处理潜在错误:
from crewai import Agent, Taskimport logging# Set up logging to capture any reasoning errorslogging.basicConfig(level=logging.INFO)# Create an agent with reasoning enabledagent = Agent(role="Data Analyst",goal="Analyze data and provide insights",reasoning=True,max_reasoning_attempts=3)# Create a tasktask = Task(description="Analyze the provided sales data and identify key trends.",expected_output="A report highlighting the top 3 sales trends.",agent=agent)# Execute the task# If an error occurs during reasoning, it will be logged and execution will continueresult = agent.execute_task(task)
Reasoning 输出示例
下面是一个针对数据分析任务的 reasoning plan 示例:
Task: Analyze the provided sales data and identify key trends.Reasoning Plan:I'll analyze the sales data to identify the top 3 trends.1. Understanding of the task:I need to analyze sales data to identify key trends that would be valuable for business decision-making.2. Key steps I'll take:- First, I'll examine the data structure to understand what fields are available- Then I'll perform exploratory data analysis to identify patterns- Next, I'll analyze sales by time periods to identify temporal trends- I'll also analyze sales by product categories and customer segments- Finally, I'll identify the top 3 most significant trends3. Approach to challenges:- If the data has missing values, I'll decide whether to fill or filter them- If the data has outliers, I'll investigate whether they're valid data points or errors- If trends aren't immediately obvious, I'll apply statistical methods to uncover patterns4. Use of available tools:- I'll use data analysis tools to explore and visualize the data- I'll use statistical tools to identify significant patterns- I'll use knowledge retrieval to access relevant information about sales analysis5. Expected outcome:A concise report highlighting the top 3 sales trends with supporting evidence from the data.READY: I am ready to execute the task.
这个 reasoning plan 有助于 agent 组织其处理任务的方式、考虑潜在挑战,并确保交付符合预期的输出。
