Skip to content
CrewAI 文档版本 跟随上游 源快照 a240195e 更新于 2026-10-18

推理

了解如何启用和使用智能体推理来改进任务执行。

概述

智能体推理是一项功能,允许智能体在执行任务之前对任务进行反思并制定计划。这有助于智能体更有条理地处理任务,并确保它们准备好执行所分配的工作。

用量

要为智能体启用推理,只需在创建智能体时设置 reasoning=True:

python
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,  # Enable reasoning
    max_reasoning_attempts=3  # Optional: Set a maximum number of reasoning attempts
)

工作原理

启用推理后,在执行任务之前,智能体将:

  1. 对任务进行反思并制定详细计划
  2. 评估是否已准备好执行该任务
  3. 根据需要完善计划,直到准备就绪或达到 max_reasoning_attempts
  4. 在执行之前将推理计划注入任务描述

这个过程帮助智能体将复杂任务拆解为可管理的步骤,并在开始前识别潜在挑战。

配置选项

bool默认 False

启用或禁用推理

int默认 None

在继续执行之前完善计划的最大尝试次数。如果为 None(默认值),智能体将持续完善,直到准备就绪。

示例

下面是一个完整示例:

python
from crewai import Agent, Task, Crew

# Create an agent with reasoning enabled
analyst = 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 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
)

# Create a crew and run the task
crew = Crew(agents=[analyst], tasks=[analysis_task])
result = crew.kickoff()

print(result)

错误处理

推理过程设计为健壮的,并内置了错误处理。如果在推理期间发生错误,智能体将在没有推理计划的情况下继续执行任务。这确保即使推理过程失败,任务仍可被执行。

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

python
from crewai import Agent, Task
import logging

# Set up logging to capture any reasoning errors
logging.basicConfig(level=logging.INFO)

# Create an agent with reasoning enabled
agent = Agent(
    role="Data Analyst",
    goal="Analyze data and provide insights",
    reasoning=True,
    max_reasoning_attempts=3
)

# Create a 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
)

# Execute the task
# If an error occurs during reasoning, it will be logged and execution will continue
result = agent.execute_task(task)

推理输出示例

以下是一个数据分析任务的推理计划可能是什么样子的示例:

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.

这个推理计划帮助智能体组织其处理任务的方式,考虑潜在挑战,并确保交付预期的输出。

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。