跳转到主要内容

概述

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

用法

要为一个智能体启用推理功能,只需在创建智能体时设置 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,  # Enable reasoning
    max_reasoning_attempts=3  # Optional: Set a maximum number of reasoning attempts
)

工作原理

启用推理后,在执行任务之前,智能体将:
  1. 对任务进行思考并制定详细计划
  2. 评估是否准备好执行该任务
  3. 根据需要优化计划,直到准备就绪或达到 `max_reasoning_attempts`
  4. 在执行前将推理计划注入到任务描述中
这个过程帮助智能体将复杂任务分解为可管理的步骤,并在开始前识别潜在的挑战。

配置选项

reasoning
bool
默认值:"False"
启用或禁用推理
max_reasoning_attempts
int
默认值:"None"
在继续执行之前,优化计划的最大尝试次数。如果为 `None`(默认值),智能体将继续优化直到准备就绪。

示例

这是一个完整的示例
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)

错误处理

推理过程设计得非常稳健,内置了错误处理机制。如果在推理过程中发生错误,智能体将继续执行任务,但不使用推理计划。这确保了即使推理过程失败,任务仍然可以执行。 以下是如何在代码中处理潜在错误:
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.
这个推理计划帮助智能体组织其处理任务的方法,考虑潜在的挑战,并确保它能交付预期的输出。