From 3d5668e988fc058ab688a2aaf8d69a6a228477d7 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 27 May 2025 09:04:16 +0000 Subject: [PATCH] Fix adaptive reasoning implementation and tests, move all prompts to en.json Co-Authored-By: Joe Moura --- src/crewai/translations/en.json | 3 ++- src/crewai/utilities/reasoning_handler.py | 1 + tests/reasoning_interval_test.py | 19 ++++++------------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/crewai/translations/en.json b/src/crewai/translations/en.json index d0e17c3d5..01dc9a840 100644 --- a/src/crewai/translations/en.json +++ b/src/crewai/translations/en.json @@ -58,6 +58,7 @@ "create_plan_prompt": "You are {role} with this background: {backstory}\n\nYour primary goal is: {goal}\n\nYou have been assigned the following task:\n{description}\n\nExpected output:\n{expected_output}\n\nAvailable tools: {tools}\n\nBefore executing this task, create a detailed plan that leverages your expertise as {role} and outlines:\n1. Your understanding of the task from your professional perspective\n2. The key steps you'll take to complete it, drawing on your background and skills\n3. How you'll approach any challenges that might arise, considering your expertise\n4. How you'll strategically use the available tools based on your experience, exactly what tools to use and how to use them\n5. The expected outcome and how it aligns with your goal\n\nAfter creating your plan, assess whether you feel ready to execute the task or if you could do better.\nConclude with one of these statements:\n- \"READY: I am ready to execute the task.\"\n- \"NOT READY: I need to refine my plan because [specific reason].\"", "refine_plan_prompt": "You are {role} with this background: {backstory}\n\nYour primary goal is: {goal}\n\nYou created the following plan for this task:\n{current_plan}\n\nHowever, you indicated that you're not ready to execute the task yet.\n\nPlease refine your plan further, drawing on your expertise as {role} to address any gaps or uncertainties. As you refine your plan, be specific about which available tools you will use, how you will use them, and why they are the best choices for each step. Clearly outline your tool usage strategy as part of your improved plan.\n\nAfter refining your plan, assess whether you feel ready to execute the task.\nConclude with one of these statements:\n- \"READY: I am ready to execute the task.\"\n- \"NOT READY: I need to refine my plan further because [specific reason].\"", "adaptive_reasoning_decision": "You are {role}, a professional with the following background: {backstory}\n\nYour primary goal is: {goal}\n\nAs {role}, you are currently executing a task and need to decide whether to pause and reassess your plan based on the current context.", - "mid_execution_reasoning": "You are currently executing a task and need to reassess your plan based on progress so far.\n\nTASK DESCRIPTION:\n{description}\n\nEXPECTED OUTPUT:\n{expected_output}\n\nCURRENT PROGRESS:\nSteps completed: {current_steps}\nTools used: {tools_used}\nProgress summary: {current_progress}\n\nRECENT CONVERSATION:\n{recent_messages}\n\nBased on the current progress and context, please reassess your plan for completing this task.\nConsider what has been accomplished, what challenges you've encountered, and what steps remain.\nAdjust your strategy if needed or confirm your current approach is still optimal.\n\nProvide a detailed updated plan for completing the task.\nEnd with \"READY: I am ready to continue executing the task.\" if you're confident in your plan." + "mid_execution_reasoning": "You are currently executing a task and need to reassess your plan based on progress so far.\n\nTASK DESCRIPTION:\n{description}\n\nEXPECTED OUTPUT:\n{expected_output}\n\nCURRENT PROGRESS:\nSteps completed: {current_steps}\nTools used: {tools_used}\nProgress summary: {current_progress}\n\nRECENT CONVERSATION:\n{recent_messages}\n\nBased on the current progress and context, please reassess your plan for completing this task.\nConsider what has been accomplished, what challenges you've encountered, and what steps remain.\nAdjust your strategy if needed or confirm your current approach is still optimal.\n\nProvide a detailed updated plan for completing the task.\nEnd with \"READY: I am ready to continue executing the task.\" if you're confident in your plan.", + "mid_execution_plan": "You are {role}, a professional with the following background: {backstory}\n\nYour primary goal is: {goal}\n\nAs {role}, you are reassessing your plan during task execution based on the progress made so far." } } diff --git a/src/crewai/utilities/reasoning_handler.py b/src/crewai/utilities/reasoning_handler.py index b042da376..164252412 100644 --- a/src/crewai/utilities/reasoning_handler.py +++ b/src/crewai/utilities/reasoning_handler.py @@ -485,6 +485,7 @@ class AgentReasoning: if self.llm.supports_function_calling(): plan, ready = self.__call_with_function(mid_execution_prompt, "mid_execution_plan") else: + # Use the same prompt for system context system_prompt = self.i18n.retrieve("reasoning", "mid_execution_plan").format( role=self.agent.role, goal=self.agent.goal, diff --git a/tests/reasoning_interval_test.py b/tests/reasoning_interval_test.py index b0d72cb09..1af59f18f 100644 --- a/tests/reasoning_interval_test.py +++ b/tests/reasoning_interval_test.py @@ -190,30 +190,23 @@ def test_should_trigger_adaptive_reasoning(): tools_handler=MagicMock() ) - executor.tools_used = ["tool1", "tool2", "tool3"] - assert executor._should_adaptive_reason() is True - - executor.tools_used = ["tool1", "tool1", "tool1"] - executor.iterations = 6 # > max_iter // 2 - assert executor._should_adaptive_reason() is True - - executor.tools_used = ["tool1", "tool1", "tool1"] - executor.iterations = 2 + with patch('crewai.utilities.reasoning_handler.AgentReasoning.should_adaptive_reason_llm', return_value=True): + assert executor._should_adaptive_reason() is True + executor.messages = [ {"role": "assistant", "content": "I'll try this approach."}, {"role": "system", "content": "Error: Failed to execute the command."}, {"role": "assistant", "content": "Let me try something else."} ] assert executor._should_adaptive_reason() is True - - executor.tools_used = ["tool1", "tool1", "tool1"] - executor.iterations = 2 + executor.messages = [ {"role": "assistant", "content": "I'll try this approach."}, {"role": "system", "content": "Command executed successfully."}, {"role": "assistant", "content": "Let me continue with the next step."} ] - assert executor._should_adaptive_reason() is False + with patch('crewai.utilities.reasoning_handler.AgentReasoning.should_adaptive_reason_llm', return_value=False): + assert executor._should_adaptive_reason() is False @pytest.mark.parametrize("interval,steps,should_reason", [