(mock_openai_client: MockOpenAIClient)
| 68 | |
| 69 | |
| 70 | def test_execute_tools_false(mock_openai_client: MockOpenAIClient): |
| 71 | expected_location = "San Francisco" |
| 72 | |
| 73 | # set up mock to record function calls |
| 74 | get_weather_mock = Mock() |
| 75 | |
| 76 | def get_weather(location): |
| 77 | get_weather_mock(location=location) |
| 78 | return "It's sunny today." |
| 79 | |
| 80 | agent = Agent(name="Test Agent", functions=[get_weather]) |
| 81 | messages = [ |
| 82 | {"role": "user", "content": "What's the weather like in San Francisco?"} |
| 83 | ] |
| 84 | |
| 85 | # set mock to return a response that triggers function call |
| 86 | mock_openai_client.set_sequential_responses( |
| 87 | [ |
| 88 | create_mock_response( |
| 89 | message={"role": "assistant", "content": ""}, |
| 90 | function_calls=[ |
| 91 | {"name": "get_weather", "args": {"location": expected_location}} |
| 92 | ], |
| 93 | ), |
| 94 | create_mock_response( |
| 95 | {"role": "assistant", "content": DEFAULT_RESPONSE_CONTENT} |
| 96 | ), |
| 97 | ] |
| 98 | ) |
| 99 | |
| 100 | # set up client and run |
| 101 | client = Swarm(client=mock_openai_client) |
| 102 | response = client.run(agent=agent, messages=messages, execute_tools=False) |
| 103 | print(response) |
| 104 | |
| 105 | # assert function not called |
| 106 | get_weather_mock.assert_not_called() |
| 107 | |
| 108 | # assert tool call is present in last response |
| 109 | tool_calls = response.messages[-1].get("tool_calls") |
| 110 | assert tool_calls is not None and len(tool_calls) == 1 |
| 111 | tool_call = tool_calls[0] |
| 112 | assert tool_call["function"]["name"] == "get_weather" |
| 113 | assert json.loads(tool_call["function"]["arguments"]) == { |
| 114 | "location": expected_location |
| 115 | } |
| 116 | |
| 117 | |
| 118 | def test_handoff(mock_openai_client: MockOpenAIClient): |
nothing calls this directly
no test coverage detected
searching dependent graphs…