mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 08:38:30 +00:00
Update reasoning interval example with comprehensive documentation
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -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:
|
This example shows how to:
|
||||||
1. Use reasoning_interval to make an agent reason every X steps
|
1. Create an agent with a fixed reasoning interval (reason every X steps)
|
||||||
2. Use adaptive_reasoning to let the agent decide when to reason
|
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 import Agent, Task, Crew
|
||||||
from crewai.tools import WebBrowserTool
|
from crewai.tools import SerperDevTool, WebBrowserTool
|
||||||
|
|
||||||
|
search_tool = SerperDevTool()
|
||||||
browser_tool = WebBrowserTool()
|
browser_tool = WebBrowserTool()
|
||||||
|
|
||||||
interval_agent = Agent(
|
interval_reasoning_agent = Agent(
|
||||||
role="Research Analyst",
|
role="Research Analyst",
|
||||||
goal="Find and analyze information about a specific topic",
|
goal="Find comprehensive information about a topic",
|
||||||
backstory="You are a skilled researcher who methodically analyzes information.",
|
backstory="""You are a skilled research analyst who methodically
|
||||||
|
approaches information gathering with periodic reflection.""",
|
||||||
verbose=True,
|
verbose=True,
|
||||||
|
allow_delegation=False,
|
||||||
reasoning=True,
|
reasoning=True,
|
||||||
reasoning_interval=3,
|
reasoning_interval=3,
|
||||||
tools=[browser_tool]
|
tools=[search_tool, browser_tool]
|
||||||
)
|
)
|
||||||
|
|
||||||
adaptive_agent = Agent(
|
adaptive_reasoning_agent = Agent(
|
||||||
role="Research Analyst",
|
role="Strategic Advisor",
|
||||||
goal="Find and analyze information about a specific topic",
|
goal="Provide strategic advice based on market research",
|
||||||
backstory="You are a skilled researcher who methodically analyzes information.",
|
backstory="""You are an experienced strategic advisor who adapts your
|
||||||
|
approach based on the information you discover.""",
|
||||||
verbose=True,
|
verbose=True,
|
||||||
|
allow_delegation=False,
|
||||||
reasoning=True,
|
reasoning=True,
|
||||||
adaptive_reasoning=True,
|
adaptive_reasoning=True,
|
||||||
tools=[browser_tool]
|
tools=[search_tool, browser_tool]
|
||||||
)
|
)
|
||||||
|
|
||||||
research_task = Task(
|
interval_task = Task(
|
||||||
description="""
|
description="""Research the latest developments in renewable energy
|
||||||
Research the latest developments in renewable energy technology.
|
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
|
||||||
|
)
|
||||||
|
|
||||||
1. Find information about recent breakthroughs in solar energy
|
adaptive_task = Task(
|
||||||
2. Research advancements in wind power technology
|
description="""Analyze the competitive landscape of the electric vehicle
|
||||||
3. Analyze trends in energy storage solutions
|
market. Identify key players, their market share, recent innovations,
|
||||||
4. Compare the cost-effectiveness of different renewable energy sources
|
and strategic moves. Provide recommendations for a new entrant.""",
|
||||||
5. Summarize your findings in a comprehensive report
|
expected_output="""A strategic analysis of the electric vehicle market
|
||||||
""",
|
with recommendations for new entrants.""",
|
||||||
expected_output="A comprehensive report on the latest developments in renewable energy technology",
|
agent=adaptive_reasoning_agent
|
||||||
agent=interval_agent # Use the interval_agent for this example
|
|
||||||
)
|
)
|
||||||
|
|
||||||
crew = Crew(
|
crew = Crew(
|
||||||
agents=[interval_agent],
|
agents=[interval_reasoning_agent, adaptive_reasoning_agent],
|
||||||
tasks=[research_task],
|
tasks=[interval_task, adaptive_task],
|
||||||
verbose=2
|
verbose=2 # Set to 2 to see reasoning events in the output
|
||||||
)
|
)
|
||||||
|
|
||||||
result = crew.kickoff()
|
results = crew.kickoff()
|
||||||
print("\nResult:")
|
|
||||||
|
print("\n==== RESULTS ====\n")
|
||||||
|
for i, result in enumerate(results):
|
||||||
|
print(f"Task {i+1} Result:")
|
||||||
print(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.
|
||||||
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user