跳转到内容

推理

Agent 推理是一项让 agents 在执行前先反思任务并制定计划的功能。这有助于 agents 更有条理地处理任务,并确保它们已经准备好执行分配的工作。

要为 agent 启用 reasoning,只需在创建 agent 时设置 reasoning=True

from crewai import Agent
agent = 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, # 启用 reasoning
max_reasoning_attempts=3 # 可选:设置 reasoning 的最大尝试次数
)

启用 reasoning 后,在执行 task 之前,agent 会:

  1. 反思任务并创建详细计划
  2. 评估自己是否已准备好执行任务
  3. 根据需要不断优化计划,直到准备就绪或达到 max_reasoning_attempts
  4. 在执行前将 reasoning 计划注入 task 描述

这个过程有助于 agent 将复杂任务拆解成可管理的步骤,并在开始前识别潜在挑战。

reasoning bool default: False

启用或禁用 reasoning

max_reasoning_attempts int default: None

在开始执行前,用于优化计划的最大尝试次数。如果为 None(默认值),agent 会持续优化,直到准备就绪。

下面是一个完整示例:

from crewai import Agent, Task, Crew
# 创建一个启用了 reasoning 的 agent
analyst = Agent(
role="Data Analyst",
goal="Analyze data and provide insights",
backstory="You are an expert data analyst.",
reasoning=True,
max_reasoning_attempts=3 # 可选:设置 reasoning 尝试次数上限
)
# 创建一个 task
analysis_task = Task(
description="Analyze the provided sales data and identify key trends.",
expected_output="A report highlighting the top 3 sales trends.",
agent=analyst
)
# 创建一个 crew 并运行 task
crew = Crew(agents=[analyst], tasks=[analysis_task])
result = crew.kickoff()
print(result)

reasoning 流程经过了稳健设计,内置了错误处理。如果 reasoning 过程中发生错误,agent 会直接继续执行 task,而不会使用 reasoning 计划。这确保即使 reasoning 失败,任务仍然可以继续执行。

以下是如何在代码中处理潜在错误:

from crewai import Agent, Task
import logging
# 设置日志,以捕获任何 reasoning 错误
logging.basicConfig(level=logging.INFO)
# 创建一个启用了 reasoning 的 agent
agent = Agent(
role="Data Analyst",
goal="Analyze data and provide insights",
reasoning=True,
max_reasoning_attempts=3
)
# 创建一个 task
task = Task(
description="Analyze the provided sales data and identify key trends.",
expected_output="A report highlighting the top 3 sales trends.",
agent=agent
)
# 执行 task
# 如果 reasoning 过程中发生错误,它会被记录,执行仍将继续
result = agent.execute_task(task)

下面是一个用于数据分析任务的 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 trends
3. 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 patterns
4. 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 analysis
5. 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 组织自己的方法,考虑潜在挑战,并确保它能够交付预期结果。