跳转到主要内容

文档索引

获取完整文档索引: https://docs.crewai.com.cn/llms.txt

在深入了解之前,请使用此文件来浏览所有可用页面。

概述

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

用法

要为智能体启用推理,只需在创建智能体时设置 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.
此推理计划有助于智能体组织其任务处理方式、考虑潜在挑战,并确保其交付预期的输出。