diff --git a/src/crewai/agents/crew_agent_executor.py b/src/crewai/agents/crew_agent_executor.py index ecfc838e7..ba46156ad 100644 --- a/src/crewai/agents/crew_agent_executor.py +++ b/src/crewai/agents/crew_agent_executor.py @@ -410,6 +410,7 @@ class CrewAgentExecutor(CrewAgentExecutorMixin): """ while self.ask_for_human_input: human_feedback = self._ask_human_input(formatted_answer.output) + print("Human feedback: ", human_feedback) if self.crew and self.crew._train: self._handle_crew_training_output(formatted_answer, human_feedback) @@ -472,5 +473,6 @@ class CrewAgentExecutor(CrewAgentExecutorMixin): color="red", ) self.ask_for_human_input = False + print("FINAL ANSWER: ", formatted_answer) return formatted_answer diff --git a/tests/agent_test.py b/tests/agent_test.py index 1f4b62fd0..3ea4a4d15 100644 --- a/tests/agent_test.py +++ b/tests/agent_test.py @@ -3,19 +3,20 @@ import os from unittest import mock from unittest.mock import patch + import pytest from crewai import Agent, Crew, Task from crewai.agents.cache import CacheHandler from crewai.agents.crew_agent_executor import CrewAgentExecutor from crewai.agents.parser import AgentAction, CrewAgentParser, OutputParserException +from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource from crewai.llm import LLM from crewai.tools import tool from crewai.tools.tool_calling import InstructorToolCalling from crewai.tools.tool_usage import ToolUsage from crewai.tools.tool_usage_events import ToolUsageFinished from crewai.utilities import RPMController -from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource from crewai.utilities.events import Emitter @@ -983,10 +984,9 @@ def test_agent_definition_based_on_dict(): # test for human input -@pytest.mark.vcr(filter_headers=["authorization"]) +# @pytest.mark.vcr(filter_headers=["authorization"]) def test_agent_human_input(): - from unittest.mock import patch - + # Agent configuration config = { "role": "test role", "goal": "test goal", @@ -995,6 +995,7 @@ def test_agent_human_input(): agent = Agent(**config) + # Task configuration with human input enabled task = Task( agent=agent, description="Say the word: Hi", @@ -1002,11 +1003,26 @@ def test_agent_human_input(): human_input=True, ) - with patch.object(CrewAgentExecutor, "_ask_human_input") as mock_human_input: - mock_human_input.return_value = "Don't say hi, say Hello instead!" + # Side effect function for _ask_human_input to simulate multiple feedback iterations + feedback_responses = iter( + [ + "Don't say hi, say Hello instead!", # First feedback + "looks good", # Second feedback to exit loop + ] + ) + + def ask_human_input_side_effect(*args, **kwargs): + return next(feedback_responses) + + with patch.object( + CrewAgentExecutor, "_ask_human_input", side_effect=ask_human_input_side_effect + ) as mock_human_input: + # Execute the task output = agent.execute_task(task) - mock_human_input.assert_called_once() - assert output == "Hello" + + # Assertions to ensure the agent behaves correctly + assert mock_human_input.call_count == 2 # Should have asked for feedback twice + assert output.strip().lower() == "hello" # Final output should be 'Hello' def test_interpolate_inputs():