From ad725f44b3d61cc16ede87ee120007646d82fa65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moura?= Date: Sat, 7 Jun 2025 23:57:24 -0700 Subject: [PATCH] rmeove duplciate reasonign logs --- src/crewai/agent.py | 7 +- tests/agent_reasoning_test.py | 129 +++++++++++++++++++++------------- 2 files changed, 85 insertions(+), 51 deletions(-) diff --git a/src/crewai/agent.py b/src/crewai/agent.py index c4be058ec..f9a928705 100644 --- a/src/crewai/agent.py +++ b/src/crewai/agent.py @@ -254,8 +254,11 @@ class Agent(BaseAgent): reasoning_handler.handle_agent_reasoning() ) - # Add the reasoning plan to the task description - task.description += f"\n\nReasoning Plan:\n{reasoning_output.plan.plan}" + # Only add reasoning plan to task description if verbose mode is disabled + # In verbose mode, the reasoning plan is already displayed in a panel + is_verbose = self.verbose or (hasattr(self, "crew") and getattr(self.crew, "verbose", False)) + if not is_verbose: + task.description += f"\n\nReasoning Plan:\n{reasoning_output.plan.plan}" except Exception as e: if hasattr(self, "_logger"): self._logger.log( diff --git a/tests/agent_reasoning_test.py b/tests/agent_reasoning_test.py index eea42b7a5..fb40d0163 100644 --- a/tests/agent_reasoning_test.py +++ b/tests/agent_reasoning_test.py @@ -22,7 +22,7 @@ def mock_llm_responses(): def test_agent_with_reasoning(mock_llm_responses): """Test agent with reasoning.""" llm = LLM("gpt-3.5-turbo") - + agent = Agent( role="Test Agent", goal="To test the reasoning feature", @@ -31,29 +31,29 @@ def test_agent_with_reasoning(mock_llm_responses): reasoning=True, verbose=True ) - + task = Task( description="Simple math task: What's 2+2?", expected_output="The answer should be a number.", agent=agent ) - + agent.llm.call = lambda messages, *args, **kwargs: ( mock_llm_responses["ready"] if any("create a detailed plan" in msg.get("content", "") for msg in messages) else mock_llm_responses["execution"] ) - + result = agent.execute_task(task) - + assert result == mock_llm_responses["execution"] - assert "Reasoning Plan:" in task.description + assert "Reasoning Plan:" not in task.description def test_agent_with_reasoning_not_ready_initially(mock_llm_responses): """Test agent with reasoning that requires refinement.""" llm = LLM("gpt-3.5-turbo") - + agent = Agent( role="Test Agent", goal="To test the reasoning feature", @@ -63,15 +63,15 @@ def test_agent_with_reasoning_not_ready_initially(mock_llm_responses): max_reasoning_attempts=2, verbose=True ) - + task = Task( description="Complex math task: What's the derivative of x²?", expected_output="The answer should be a mathematical expression.", agent=agent ) - + call_count = [0] - + def mock_llm_call(messages, *args, **kwargs): if any("create a detailed plan" in msg.get("content", "") for msg in messages) or any("refine your plan" in msg.get("content", "") for msg in messages): call_count[0] += 1 @@ -81,20 +81,20 @@ def test_agent_with_reasoning_not_ready_initially(mock_llm_responses): return mock_llm_responses["ready_after_refine"] else: return "2x" - + agent.llm.call = mock_llm_call - + result = agent.execute_task(task) - + assert result == "2x" assert call_count[0] == 2 # Should have made 2 reasoning calls - assert "Reasoning Plan:" in task.description + assert "Reasoning Plan:" not in task.description def test_agent_with_reasoning_max_attempts_reached(): """Test agent with reasoning that reaches max attempts without being ready.""" llm = LLM("gpt-3.5-turbo") - + agent = Agent( role="Test Agent", goal="To test the reasoning feature", @@ -104,35 +104,35 @@ def test_agent_with_reasoning_max_attempts_reached(): max_reasoning_attempts=2, verbose=True ) - + task = Task( description="Complex math task: Solve the Riemann hypothesis.", expected_output="A proof or disproof of the hypothesis.", agent=agent ) - + call_count = [0] - + def mock_llm_call(messages, *args, **kwargs): if any("create a detailed plan" in msg.get("content", "") for msg in messages) or any("refine your plan" in msg.get("content", "") for msg in messages): call_count[0] += 1 return f"Attempt {call_count[0]}: I need more time to think.\n\nNOT READY: I need to refine my plan further." else: return "This is an unsolved problem in mathematics." - + agent.llm.call = mock_llm_call - + result = agent.execute_task(task) - + assert result == "This is an unsolved problem in mathematics." assert call_count[0] == 2 # Should have made exactly 2 reasoning calls (max_attempts) - assert "Reasoning Plan:" in task.description + assert "Reasoning Plan:" not in task.description def test_agent_reasoning_input_validation(): """Test input validation in AgentReasoning.""" llm = LLM("gpt-3.5-turbo") - + agent = Agent( role="Test Agent", goal="To test the reasoning feature", @@ -140,10 +140,10 @@ def test_agent_reasoning_input_validation(): llm=llm, reasoning=True ) - + with pytest.raises(ValueError, match="Both task and agent must be provided"): AgentReasoning(task=None, agent=agent) - + task = Task( description="Simple task", expected_output="Simple output" @@ -155,7 +155,7 @@ def test_agent_reasoning_input_validation(): def test_agent_reasoning_error_handling(): """Test error handling during the reasoning process.""" llm = LLM("gpt-3.5-turbo") - + agent = Agent( role="Test Agent", goal="To test the reasoning feature", @@ -163,25 +163,25 @@ def test_agent_reasoning_error_handling(): llm=llm, reasoning=True ) - + task = Task( description="Task that will cause an error", expected_output="Output that will never be generated", agent=agent ) - + call_count = [0] - + def mock_llm_call_error(*args, **kwargs): call_count[0] += 1 if call_count[0] <= 2: # First calls are for reasoning raise Exception("LLM error during reasoning") return "Fallback execution result" # Return a value for task execution - + agent.llm.call = mock_llm_call_error - + result = agent.execute_task(task) - + assert result == "Fallback execution result" assert call_count[0] > 2 # Ensure we called the mock multiple times @@ -189,7 +189,7 @@ def test_agent_reasoning_error_handling(): def test_agent_with_function_calling(): """Test agent with reasoning using function calling.""" llm = LLM("gpt-3.5-turbo") - + agent = Agent( role="Test Agent", goal="To test the reasoning feature", @@ -198,15 +198,15 @@ def test_agent_with_function_calling(): reasoning=True, verbose=True ) - + task = Task( description="Simple math task: What's 2+2?", expected_output="The answer should be a number.", agent=agent ) - + agent.llm.supports_function_calling = lambda: True - + def mock_function_call(messages, *args, **kwargs): if "tools" in kwargs: return json.dumps({ @@ -215,20 +215,19 @@ def test_agent_with_function_calling(): }) else: return "4" - + agent.llm.call = mock_function_call - + result = agent.execute_task(task) - + assert result == "4" - assert "Reasoning Plan:" in task.description - assert "I'll solve this simple math problem: 2+2=4." in task.description + assert "Reasoning Plan:" not in task.description def test_agent_with_function_calling_fallback(): """Test agent with reasoning using function calling that falls back to text parsing.""" llm = LLM("gpt-3.5-turbo") - + agent = Agent( role="Test Agent", goal="To test the reasoning feature", @@ -237,25 +236,57 @@ def test_agent_with_function_calling_fallback(): reasoning=True, verbose=True ) - + task = Task( description="Simple math task: What's 2+2?", expected_output="The answer should be a number.", agent=agent ) - + agent.llm.supports_function_calling = lambda: True - + def mock_function_call(messages, *args, **kwargs): if "tools" in kwargs: return "Invalid JSON that will trigger fallback. READY: I am ready to execute the task." else: return "4" - + agent.llm.call = mock_function_call - + result = agent.execute_task(task) - + assert result == "4" + # In verbose mode, reasoning plan should NOT be added to task description + assert "Reasoning Plan:" not in task.description + + +def test_agent_with_reasoning_non_verbose_mode(mock_llm_responses): + """Test agent with reasoning in non-verbose mode where reasoning plan should be added to task description.""" + llm = LLM("gpt-3.5-turbo") + + agent = Agent( + role="Test Agent", + goal="To test the reasoning feature", + backstory="I am a test agent created to verify the reasoning feature works correctly.", + llm=llm, + reasoning=True, + verbose=False # Non-verbose mode + ) + + task = Task( + description="Simple math task: What's 2+2?", + expected_output="The answer should be a number.", + agent=agent + ) + + agent.llm.call = lambda messages, *args, **kwargs: ( + mock_llm_responses["ready"] + if any("create a detailed plan" in msg.get("content", "") for msg in messages) + else mock_llm_responses["execution"] + ) + + result = agent.execute_task(task) + + assert result == mock_llm_responses["execution"] assert "Reasoning Plan:" in task.description - assert "Invalid JSON that will trigger fallback" in task.description + assert "I'll solve this simple math problem" in task.description