From 0fa021dea8864f52fa9ca36f9c915d4ed1a6bfd1 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 26 Feb 2025 04:19:09 +0000 Subject: [PATCH] Fix test mocking approach to use real LLM with mocked call method Co-Authored-By: Joe Moura --- tests/agent_test.py | 76 ++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/tests/agent_test.py b/tests/agent_test.py index 4aacac17f..87c32b5e8 100644 --- a/tests/agent_test.py +++ b/tests/agent_test.py @@ -1675,25 +1675,25 @@ def test_agent_uses_task_knowledge(): mock_knowledge = MockKnowledge.return_value mock_knowledge.query.return_value = [{"content": content}] - # Create an agent with a simple mocked LLM - with patch("crewai.llm.LLM", autospec=True) as MockLLM: - mock_llm = MockLLM.return_value - mock_llm.call.return_value = "The capital of France is Paris, where the Eiffel Tower is located." - - agent = Agent( - role="Geography Teacher", - goal="Provide accurate geographic information", - backstory="You are a geography expert who teaches students about world capitals.", - llm=mock_llm, - ) - - # Create a task with knowledge - task = Task( - description="What is the capital of France?", - expected_output="The capital of France.", - agent=agent, - knowledge=mock_knowledge, - ) + # Create a real LLM but patch its call method + agent = Agent( + role="Geography Teacher", + goal="Provide accurate geographic information", + backstory="You are a geography expert who teaches students about world capitals.", + llm=LLM(model="gpt-4o-mini"), + ) + + # Create a task with knowledge + task = Task( + description="What is the capital of France?", + expected_output="The capital of France.", + agent=agent, + knowledge=mock_knowledge, + ) + + # Mock the agent's execute_task method to avoid actual LLM calls + with patch.object(agent.llm, "call") as mock_llm_call: + mock_llm_call.return_value = "The capital of France is Paris, where the Eiffel Tower is located." # Execute the task result = agent.execute_task(task) @@ -1723,25 +1723,25 @@ def test_agent_with_empty_task_knowledge(): mock_knowledge = MockKnowledge.return_value mock_knowledge.query.return_value = [] - # Create an agent with a simple mocked LLM - with patch("crewai.llm.LLM", autospec=True) as MockLLM: - mock_llm = MockLLM.return_value - mock_llm.call.return_value = "The capital of France is Paris." - - agent = Agent( - role="Geography Teacher", - goal="Provide accurate geographic information", - backstory="You are a geography expert who teaches students about world capitals.", - llm=mock_llm, - ) - - # Create a task with empty knowledge - task = Task( - description="What is the capital of France?", - expected_output="The capital of France.", - agent=agent, - knowledge=mock_knowledge, - ) + # Create a real LLM but patch its call method + agent = Agent( + role="Geography Teacher", + goal="Provide accurate geographic information", + backstory="You are a geography expert who teaches students about world capitals.", + llm=LLM(model="gpt-4o-mini"), + ) + + # Create a task with empty knowledge + task = Task( + description="What is the capital of France?", + expected_output="The capital of France.", + agent=agent, + knowledge=mock_knowledge, + ) + + # Mock the agent's execute_task method to avoid actual LLM calls + with patch.object(agent.llm, "call") as mock_llm_call: + mock_llm_call.return_value = "The capital of France is Paris." # Execute the task result = agent.execute_task(task)