-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Add reasoning attribute to Agent class #2866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-Authored-By: Joe Moura <joao@crewai.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Disclaimer: This review was made by a crew of AI Agents. Code Review Comment: Add Reasoning Attribute to Agent ClassOverviewThis PR introduces valuable reasoning capabilities for agents, enabling them to plan and reflect before executing tasks. The changes span four files and include documentation, core functionality, and tests. Code Quality FindingsDocumentation (docs/concepts/reasoning.mdx)
Agent Class Changes (src/crewai/agent.py)
def execute_task(self, task: Task) -> str:
try:
if self.reasoning:
from crewai.utilities.reasoning_handler import AgentReasoning, AgentReasoningOutput
reasoning_handler = AgentReasoning(task=task, agent=self)
reasoning_output: AgentReasoningOutput = reasoning_handler._handle_agent_reasoning()
task.description += f"\n\nReasoning Plan:\n{reasoning_output.plan.plan}"
except Exception as e:
self.logger.error(f"Error during reasoning process: {str(e)}")
# Consider whether to proceed without reasoning or raise the error Reasoning Handler (src/crewai/utilities/reasoning_handler.py)
class AgentReasoning:
def __init__(self, task: Task, agent: Agent):
if not task or not agent:
raise ValueError("Both task and agent must be provided.")
self.task = task
self.agent = agent
def handle_reasoning(self) -> AgentReasoningOutput:
"""Public method for the reasoning process."""
return self.__handle_agent_reasoning()
def __handle_agent_reasoning(self) -> AgentReasoningOutput:
initial_plan = self.__create_initial_plan()
return self.__refine_plan_if_needed(initial_plan) Tests (tests/agent_reasoning_test.py)
@pytest.fixture
def mock_llm_responses():
return {
"ready": "READY: I am ready to execute the task.",
"not_ready": "NOT READY: I need to refine my plan.",
"execution": "Task execution result"
}
def test_agent_reasoning_error_handling():
"""Test error handling during the reasoning process."""
with pytest.raises(ValueError):
agent = Agent(reasoning=True)
task = Task(description="") # Invalid task
agent.execute_task(task) General Recommendations
Impact Analysis
Final VerdictThis PR effectively introduces a significant feature into the agent architecture, enhancing its capabilities. The recommendation for merging stands, contingent upon addressing the highlighted error handling and testing improvements. The architecture holds promise but requires careful refinement in critical areas to ensure stability and performance. |
…soning handler, and enhance tests and docs Co-Authored-By: Joe Moura <joao@crewai.com>
…ions Co-Authored-By: Joe Moura <joao@crewai.com>
Co-Authored-By: Joe Moura <joao@crewai.com>
Co-Authored-By: Joe Moura <joao@crewai.com>
Co-Authored-By: Joe Moura <joao@crewai.com>
Co-Authored-By: Joe Moura <joao@crewai.com>
Co-Authored-By: Joe Moura <joao@crewai.com>
* Add reasoning attribute to Agent class Co-Authored-By: Joe Moura <joao@crewai.com> * Address PR feedback: improve type hints, error handling, refactor reasoning handler, and enhance tests and docs Co-Authored-By: Joe Moura <joao@crewai.com> * Implement function calling for reasoning and move prompts to translations Co-Authored-By: Joe Moura <joao@crewai.com> * Simplify function calling implementation with better error handling Co-Authored-By: Joe Moura <joao@crewai.com> * Enhance system prompts to leverage agent context (role, goal, backstory) Co-Authored-By: Joe Moura <joao@crewai.com> * Fix lint and type-checker issues Co-Authored-By: Joe Moura <joao@crewai.com> * Enhance system prompts to better leverage agent context Co-Authored-By: Joe Moura <joao@crewai.com> * Fix backstory access in reasoning handler for Python 3.12 compatibility Co-Authored-By: Joe Moura <joao@crewai.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Joe Moura <joao@crewai.com> Co-authored-by: João Moura <joaomdmoura@gmail.com>
Add reasoning attribute to Agent class
This PR adds a new
reasoning
boolean attribute to the Agent class, enabling agents to reflect and create a plan before executing tasks. The implementation follows the same pattern as the existing crew-level planning functionality.Features
reasoning
boolean attribute to Agent class (default: False)max_reasoning_attempts
optional attribute to control refinement iterationsImplementation Details
The reasoning functionality allows agents to:
The reasoning functionality uses the agent's own LLM to create and refine the plan, ensuring consistency in the agent's thinking process.
Function Calling Implementation
When the agent's LLM supports function calling (determined by
llm.supports_function_calling()
), the reasoning process will use a structured function schema to get a more reliable response. This implementation:Translations Integration
All prompts used in the reasoning process have been moved to the translations system in
src/crewai/translations/en.json
, enabling:Example Usage
Error Handling Example
Example Reasoning Output
Testing
Added tests to verify:
Link to Devin run
https://app.devin.ai/sessions/06fe7cce3d6544cf822705ba656a6d7b
Requested by
Joe Moura (joao@crewai.com)