(mock_openai_client: MockOpenAIClient)
| 29 | |
| 30 | |
| 31 | def test_tool_call(mock_openai_client: MockOpenAIClient): |
| 32 | expected_location = "San Francisco" |
| 33 | |
| 34 | # set up mock to record function calls |
| 35 | get_weather_mock = Mock() |
| 36 | |
| 37 | def get_weather(location): |
| 38 | get_weather_mock(location=location) |
| 39 | return "It's sunny today." |
| 40 | |
| 41 | agent = Agent(name="Test Agent", functions=[get_weather]) |
| 42 | messages = [ |
| 43 | {"role": "user", "content": "What's the weather like in San Francisco?"} |
| 44 | ] |
| 45 | |
| 46 | # set mock to return a response that triggers function call |
| 47 | mock_openai_client.set_sequential_responses( |
| 48 | [ |
| 49 | create_mock_response( |
| 50 | message={"role": "assistant", "content": ""}, |
| 51 | function_calls=[ |
| 52 | {"name": "get_weather", "args": {"location": expected_location}} |
| 53 | ], |
| 54 | ), |
| 55 | create_mock_response( |
| 56 | {"role": "assistant", "content": DEFAULT_RESPONSE_CONTENT} |
| 57 | ), |
| 58 | ] |
| 59 | ) |
| 60 | |
| 61 | # set up client and run |
| 62 | client = Swarm(client=mock_openai_client) |
| 63 | response = client.run(agent=agent, messages=messages) |
| 64 | |
| 65 | get_weather_mock.assert_called_once_with(location=expected_location) |
| 66 | assert response.messages[-1]["role"] == "assistant" |
| 67 | assert response.messages[-1]["content"] == DEFAULT_RESPONSE_CONTENT |
| 68 | |
| 69 | |
| 70 | def test_execute_tools_false(mock_openai_client: MockOpenAIClient): |
nothing calls this directly
no test coverage detected
searching dependent graphs…