| 1 | from unittest.mock import MagicMock |
| 2 | from swarm.types import ChatCompletionMessage, ChatCompletionMessageToolCall, Function |
| 3 | from openai import OpenAI |
| 4 | from openai.types.chat.chat_completion import ChatCompletion, Choice |
| 5 | import json |
| 6 | |
| 7 | |
| 8 | def create_mock_response(message, function_calls=[], model="gpt-4o"): |
| 9 | role = message.get("role", "assistant") |
| 10 | content = message.get("content", "") |
| 11 | tool_calls = ( |
| 12 | [ |
| 13 | ChatCompletionMessageToolCall( |
| 14 | id="mock_tc_id", |
| 15 | type="function", |
| 16 | function=Function( |
| 17 | name=call.get("name", ""), |
| 18 | arguments=json.dumps(call.get("args", {})), |
| 19 | ), |
| 20 | ) |
| 21 | for call in function_calls |
| 22 | ] |
| 23 | if function_calls |
| 24 | else None |
| 25 | ) |
| 26 | |
| 27 | return ChatCompletion( |
| 28 | id="mock_cc_id", |
| 29 | created=1234567890, |
| 30 | model=model, |
| 31 | object="chat.completion", |
| 32 | choices=[ |
| 33 | Choice( |
| 34 | message=ChatCompletionMessage( |
| 35 | role=role, content=content, tool_calls=tool_calls |
| 36 | ), |
| 37 | finish_reason="stop", |
| 38 | index=0, |
| 39 | ) |
| 40 | ], |
| 41 | ) |
| 42 | |
| 43 | |
| 44 | class MockOpenAIClient: |
| 45 | def __init__(self): |
| 46 | self.chat = MagicMock() |
| 47 | self.chat.completions = MagicMock() |
| 48 | |
| 49 | def set_response(self, response: ChatCompletion): |
| 50 | """ |
| 51 | Set the mock to return a specific response. |
| 52 | :param response: A ChatCompletion response to return. |
| 53 | """ |
| 54 | self.chat.completions.create.return_value = response |
| 55 | |
| 56 | def set_sequential_responses(self, responses: list[ChatCompletion]): |
| 57 | """ |
| 58 | Set the mock to return different responses sequentially. |
| 59 | :param responses: A list of ChatCompletion responses to return in order. |
| 60 | """ |
| 61 | self.chat.completions.create.side_effect = responses |
| 62 | |
| 63 | def assert_create_called_with(self, **kwargs): |
| 64 | self.chat.completions.create.assert_called_with(**kwargs) |
| 65 | |
| 66 | |
| 67 | # Initialize the mock client |
| 68 | client = MockOpenAIClient() |
| 69 | |
| 70 | # Set a sequence of mock responses |
| 71 | client.set_sequential_responses( |
| 72 | [ |
| 73 | create_mock_response( |
| 74 | {"role": "assistant", "content": "First response"}, |
| 75 | [ |
| 76 | { |
| 77 | "name": "process_refund", |
| 78 | "args": {"item_id": "item_123", "reason": "too expensive"}, |
| 79 | } |
| 80 | ], |
| 81 | ), |
| 82 | create_mock_response({"role": "assistant", "content": "Second"}), |
| 83 | ] |
| 84 | ) |
| 85 | |
| 86 | # This should return the first mock response |
| 87 | first_response = client.chat.completions.create() |
| 88 | print( |
| 89 | first_response.choices[0].message |
| 90 | ) # Outputs: role='agent' content='First response' |
| 91 | |
| 92 | # This should return the second mock response |
| 93 | second_response = client.chat.completions.create() |
| 94 | print( |
| 95 | second_response.choices[0].message |
| 96 | ) # Outputs: role='agent' content='Second response' |