Update reasoning interval example with comprehensive documentation

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-05-26 19:01:31 +00:00
parent db6b831c66
commit 0383aa1f27

View File

@@ -1,56 +1,91 @@
"""
Example demonstrating the new reasoning interval and adaptive reasoning features.
Example demonstrating the new reasoning interval and adaptive reasoning features in CrewAI.
This example shows how to:
1. Use reasoning_interval to make an agent reason every X steps
2. Use adaptive_reasoning to let the agent decide when to reason
1. Create an agent with a fixed reasoning interval (reason every X steps)
2. Create an agent with adaptive reasoning (agent decides when to reason)
3. Configure and run tasks with these reasoning capabilities
"""
from crewai import Agent, Task, Crew
from crewai.tools import WebBrowserTool
from crewai.tools import SerperDevTool, WebBrowserTool
search_tool = SerperDevTool()
browser_tool = WebBrowserTool()
interval_agent = Agent(
interval_reasoning_agent = Agent(
role="Research Analyst",
goal="Find and analyze information about a specific topic",
backstory="You are a skilled researcher who methodically analyzes information.",
goal="Find comprehensive information about a topic",
backstory="""You are a skilled research analyst who methodically
approaches information gathering with periodic reflection.""",
verbose=True,
allow_delegation=False,
reasoning=True,
reasoning_interval=3,
tools=[browser_tool]
tools=[search_tool, browser_tool]
)
adaptive_agent = Agent(
role="Research Analyst",
goal="Find and analyze information about a specific topic",
backstory="You are a skilled researcher who methodically analyzes information.",
adaptive_reasoning_agent = Agent(
role="Strategic Advisor",
goal="Provide strategic advice based on market research",
backstory="""You are an experienced strategic advisor who adapts your
approach based on the information you discover.""",
verbose=True,
allow_delegation=False,
reasoning=True,
adaptive_reasoning=True,
tools=[browser_tool]
tools=[search_tool, browser_tool]
)
research_task = Task(
description="""
Research the latest developments in renewable energy technology.
1. Find information about recent breakthroughs in solar energy
2. Research advancements in wind power technology
3. Analyze trends in energy storage solutions
4. Compare the cost-effectiveness of different renewable energy sources
5. Summarize your findings in a comprehensive report
""",
expected_output="A comprehensive report on the latest developments in renewable energy technology",
agent=interval_agent # Use the interval_agent for this example
interval_task = Task(
description="""Research the latest developments in renewable energy
technologies. Focus on solar, wind, and hydroelectric power.
Identify key innovations, market trends, and future prospects.""",
expected_output="""A comprehensive report on the latest developments
in renewable energy technologies, including innovations, market trends,
and future prospects.""",
agent=interval_reasoning_agent
)
adaptive_task = Task(
description="""Analyze the competitive landscape of the electric vehicle
market. Identify key players, their market share, recent innovations,
and strategic moves. Provide recommendations for a new entrant.""",
expected_output="""A strategic analysis of the electric vehicle market
with recommendations for new entrants.""",
agent=adaptive_reasoning_agent
)
crew = Crew(
agents=[interval_agent],
tasks=[research_task],
verbose=2
agents=[interval_reasoning_agent, adaptive_reasoning_agent],
tasks=[interval_task, adaptive_task],
verbose=2 # Set to 2 to see reasoning events in the output
)
result = crew.kickoff()
print("\nResult:")
print(result)
results = crew.kickoff()
print("\n==== RESULTS ====\n")
for i, result in enumerate(results):
print(f"Task {i+1} Result:")
print(result)
print("\n")
"""
How the reasoning features work:
1. Interval-based reasoning (reasoning_interval=3):
- The agent will reason after every 3 steps of task execution
- This creates a predictable pattern of reflection during task execution
- Useful for complex tasks where periodic reassessment is beneficial
2. Adaptive reasoning (adaptive_reasoning=True):
- The agent decides when to reason based on execution context
- Reasoning is triggered when:
a) Multiple different tools are used recently (indicating a change in approach)
b) The task is taking longer than expected (iterations > max_iter/2)
c) Recent errors or failures are detected in the execution
- This creates a more dynamic reasoning pattern adapted to the task's needs
Both approaches enhance the agent's ability to handle complex tasks by allowing
mid-execution planning and strategy adjustments.
"""