Reasoning

了解如何启用和使用 agent reasoning 来提升任务执行效果。

概述

Agent reasoning 是一项功能,它允许 agent 在执行前先对任务进行思考并制定计划。这有助于 agent 以更有条理的方式处理任务,并确保其已准备好完成所分配的工作。

用法

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

  1. from crewai import Agent
  2. agent = Agent(
  3. role="Data Analyst",
  4. goal="Analyze complex datasets and provide insights",
  5. backstory="You are an experienced data analyst with expertise in finding patterns in complex data.",
  6. reasoning=True, # Enable reasoning
  7. max_reasoning_attempts=3 # Optional: Set a maximum number of reasoning attempts
  8. )

工作原理

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

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

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

配置选项

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

示例

下面是一个完整示例:

  1. from crewai import Agent, Task, Crew
  2. # Create an agent with reasoning enabled
  3. analyst = Agent(
  4. role="Data Analyst",
  5. goal="Analyze data and provide insights",
  6. backstory="You are an expert data analyst.",
  7. reasoning=True,
  8. max_reasoning_attempts=3 # Optional: Set a limit on reasoning attempts
  9. )
  10. # Create a task
  11. analysis_task = Task(
  12. description="Analyze the provided sales data and identify key trends.",
  13. expected_output="A report highlighting the top 3 sales trends.",
  14. agent=analyst
  15. )
  16. # Create a crew and run the task
  17. crew = Crew(agents=[analyst], tasks=[analysis_task])
  18. result = crew.kickoff()
  19. print(result)

错误处理

Reasoning 过程在设计上具有较强的健壮性,并内置了错误处理机制。如果在 reasoning 过程中发生错误,agent 会在没有 reasoning plan 的情况下继续执行任务。这可以确保即使 reasoning 过程失败,任务仍然能够被执行。

下面展示了如何在代码中处理潜在错误:

  1. from crewai import Agent, Task
  2. import logging
  3. # Set up logging to capture any reasoning errors
  4. logging.basicConfig(level=logging.INFO)
  5. # Create an agent with reasoning enabled
  6. agent = Agent(
  7. role="Data Analyst",
  8. goal="Analyze data and provide insights",
  9. reasoning=True,
  10. max_reasoning_attempts=3
  11. )
  12. # Create a task
  13. task = Task(
  14. description="Analyze the provided sales data and identify key trends.",
  15. expected_output="A report highlighting the top 3 sales trends.",
  16. agent=agent
  17. )
  18. # Execute the task
  19. # If an error occurs during reasoning, it will be logged and execution will continue
  20. result = agent.execute_task(task)

Reasoning 输出示例

下面是一个针对数据分析任务的 reasoning plan 示例:

  1. Task: Analyze the provided sales data and identify key trends.
  2. Reasoning Plan:
  3. I'll analyze the sales data to identify the top 3 trends.
  4. 1. Understanding of the task:
  5. I need to analyze sales data to identify key trends that would be valuable for business decision-making.
  6. 2. Key steps I'll take:
  7. - First, I'll examine the data structure to understand what fields are available
  8. - Then I'll perform exploratory data analysis to identify patterns
  9. - Next, I'll analyze sales by time periods to identify temporal trends
  10. - I'll also analyze sales by product categories and customer segments
  11. - Finally, I'll identify the top 3 most significant trends
  12. 3. Approach to challenges:
  13. - If the data has missing values, I'll decide whether to fill or filter them
  14. - If the data has outliers, I'll investigate whether they're valid data points or errors
  15. - If trends aren't immediately obvious, I'll apply statistical methods to uncover patterns
  16. 4. Use of available tools:
  17. - I'll use data analysis tools to explore and visualize the data
  18. - I'll use statistical tools to identify significant patterns
  19. - I'll use knowledge retrieval to access relevant information about sales analysis
  20. 5. Expected outcome:
  21. A concise report highlighting the top 3 sales trends with supporting evidence from the data.
  22. READY: I am ready to execute the task.

这个 reasoning plan 有助于 agent 组织其处理任务的方式、考虑潜在挑战,并确保交付符合预期的输出。