-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathtest_minimax_client.py
More file actions
257 lines (207 loc) · 10 KB
/
Copy pathtest_minimax_client.py
File metadata and controls
257 lines (207 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import unittest
from unittest.mock import patch, Mock, AsyncMock
from openai.types.responses import Response
from adalflow.components.model_client.minimax_client import MiniMaxClient
from adalflow.core import Generator
from adalflow.core.types import ModelType, GeneratorOutput
from adalflow.utils import get_logger
def getenv_side_effect(key):
"""Mock environment variables for testing."""
env_vars = {"MINIMAX_API_KEY": "fake_minimax_api_key"}
return env_vars.get(key, None)
class TestMiniMaxClient(unittest.IsolatedAsyncioTestCase):
"""Test MiniMax client with proper mocking."""
def setUp(self):
"""Set up test fixtures."""
self.log = get_logger(level="DEBUG")
self.prompt_kwargs = {"input_str": "What is the meaning of life?"}
# Mock response for testing using OpenAI Response API
self.mock_response = Mock(spec=Response)
self.mock_response.output_text = (
"The meaning of life is to find purpose and meaning in our existence."
)
# Create a mock usage object
mock_usage = Mock()
mock_usage.input_tokens = 25
mock_usage.output_tokens = 15
mock_usage.total_tokens = 40
self.mock_response.usage = mock_usage
self.api_kwargs = {
"input": "What is the meaning of life?",
"model": "MiniMax-M3",
}
def test_minimax_client_init(self):
"""Test MiniMax client initialization."""
with patch("os.getenv", side_effect=getenv_side_effect):
client = MiniMaxClient(api_key="fake_api_key")
# Test basic properties
self.assertEqual(client.base_url, "https://api.minimax.io/v1")
self.assertEqual(client._env_api_key_name, "MINIMAX_API_KEY")
self.assertEqual(client._input_type, "text")
def test_minimax_client_init_custom_base_url(self):
"""Test MiniMax client initialization with custom base URL."""
with patch("os.getenv", side_effect=getenv_side_effect):
custom_url = "https://custom.minimax.io/v1"
client = MiniMaxClient(api_key="fake_api_key", base_url=custom_url)
self.assertEqual(client.base_url, custom_url)
def test_minimax_client_init_messages_input_type(self):
"""Test MiniMax client initialization with messages input type."""
with patch("os.getenv", side_effect=getenv_side_effect):
client = MiniMaxClient(api_key="fake_api_key", input_type="messages")
self.assertEqual(client._input_type, "messages")
@patch("os.getenv")
def test_minimax_init_sync_client(self, mock_os_getenv):
"""Test sync client initialization."""
mock_os_getenv.return_value = "fake_api_key"
client = MiniMaxClient(api_key="fake_api_key")
# Test that sync client is properly initialized
self.assertIsNotNone(client.sync_client)
self.assertEqual(client.sync_client.api_key, "fake_api_key")
self.assertEqual(
client.sync_client.base_url, "https://api.minimax.io/v1/"
)
@patch("os.getenv")
def test_minimax_init_async_client(self, mock_os_getenv):
"""Test async client initialization."""
mock_os_getenv.return_value = "fake_api_key"
client = MiniMaxClient(api_key="fake_api_key")
# Initialize async client
client.async_client = client.init_async_client()
# Test that async client is properly initialized
self.assertIsNotNone(client.async_client)
self.assertEqual(client.async_client.api_key, "fake_api_key")
self.assertEqual(
client.async_client.base_url, "https://api.minimax.io/v1/"
)
@patch("adalflow.components.model_client.openai_client.AsyncOpenAI")
async def test_minimax_acall_llm(self, MockAsyncOpenAI):
"""Test async LLM call."""
with patch("os.getenv", side_effect=getenv_side_effect):
client = MiniMaxClient(api_key="fake_api_key")
mock_async_client = AsyncMock()
MockAsyncOpenAI.return_value = mock_async_client
mock_async_client.responses.create = AsyncMock(
return_value=self.mock_response
)
# Call the acall method
result = await client.acall(
api_kwargs=self.api_kwargs, model_type=ModelType.LLM
)
# Assertions
MockAsyncOpenAI.assert_called_once()
mock_async_client.responses.create.assert_awaited_once_with(
**self.api_kwargs
)
self.assertEqual(result, self.mock_response)
@patch(
"adalflow.components.model_client.openai_client.OpenAIClient.init_sync_client"
)
@patch("adalflow.components.model_client.openai_client.OpenAI")
def test_minimax_call(self, MockOpenAI, mock_init_sync_client):
"""Test sync LLM call."""
with patch("os.getenv", side_effect=getenv_side_effect):
client = MiniMaxClient(api_key="fake_api_key")
mock_sync_client = Mock()
MockOpenAI.return_value = mock_sync_client
mock_init_sync_client.return_value = mock_sync_client
mock_sync_client.responses.create = Mock(return_value=self.mock_response)
# Set the sync client
client.sync_client = mock_sync_client
# Call the call method
result = client.call(api_kwargs=self.api_kwargs, model_type=ModelType.LLM)
# Assertions
mock_sync_client.responses.create.assert_called_once_with(**self.api_kwargs)
self.assertEqual(result, self.mock_response)
# Test parse_chat_completion
output = client.parse_chat_completion(completion=self.mock_response)
self.assertTrue(isinstance(output, GeneratorOutput))
self.assertEqual(
output.raw_response,
"The meaning of life is to find purpose and meaning in our existence.",
)
self.assertEqual(output.usage.output_tokens, 15)
self.assertEqual(output.usage.input_tokens, 25)
self.assertEqual(output.usage.total_tokens, 40)
@patch(
"adalflow.components.model_client.openai_client.OpenAIClient.init_sync_client"
)
@patch("adalflow.components.model_client.openai_client.OpenAI")
def test_minimax_generator_integration(self, MockOpenAI, mock_init_sync_client):
"""Test MiniMax client integration with Generator."""
with patch("os.getenv", side_effect=getenv_side_effect):
client = MiniMaxClient(api_key="fake_api_key")
mock_sync_client = Mock()
MockOpenAI.return_value = mock_sync_client
mock_init_sync_client.return_value = mock_sync_client
mock_sync_client.responses.create = Mock(return_value=self.mock_response)
# Set the sync client
client.sync_client = mock_sync_client
# Create generator with mocked client
gen = Generator(
model_client=client,
model_kwargs={
"model": "MiniMax-M3",
"temperature": 0.7,
"max_tokens": 1000,
},
)
# Test generator call
response = gen(prompt_kwargs=self.prompt_kwargs)
# Verify response
self.assertIsNotNone(response)
self.log.debug(f"Response: {response}")
# Verify that the mock was called
mock_sync_client.responses.create.assert_called()
def test_minimax_convert_inputs_to_api_kwargs(self):
"""Test input conversion to API kwargs."""
with patch("os.getenv", side_effect=getenv_side_effect):
client = MiniMaxClient(api_key="fake_api_key")
# Test text input conversion
api_kwargs = client.convert_inputs_to_api_kwargs(
input="Hello, world!",
model_kwargs={
"model": "MiniMax-M3",
"temperature": 0.7,
},
model_type=ModelType.LLM,
)
# Verify the structure for Response API
self.assertIn("input", api_kwargs)
self.assertIn("model", api_kwargs)
self.assertEqual(api_kwargs["model"], "MiniMax-M3")
self.assertEqual(api_kwargs["temperature"], 0.7)
# Verify input content
self.assertEqual(api_kwargs["input"], "Hello, world!")
def test_minimax_convert_inputs_m27_highspeed(self):
"""Test input conversion with MiniMax-M2.7-highspeed model."""
with patch("os.getenv", side_effect=getenv_side_effect):
client = MiniMaxClient(api_key="fake_api_key")
api_kwargs = client.convert_inputs_to_api_kwargs(
input="Summarize this text.",
model_kwargs={
"model": "MiniMax-M2.7-highspeed",
"temperature": 0.5,
},
model_type=ModelType.LLM,
)
self.assertEqual(api_kwargs["model"], "MiniMax-M2.7-highspeed")
self.assertEqual(api_kwargs["temperature"], 0.5)
def test_minimax_from_dict_to_dict(self):
"""Test serialization and deserialization."""
with patch("os.getenv", side_effect=getenv_side_effect):
test_api_key = "fake_api_key"
client = MiniMaxClient(api_key=test_api_key)
# Test to_dict
client_dict = client.to_dict()
self.assertIn("data", client_dict)
self.assertIn("_api_key", client_dict["data"])
self.assertEqual(client_dict["data"]["_api_key"], test_api_key)
# Test from_dict
new_client = MiniMaxClient.from_dict(client_dict)
self.assertEqual(new_client.to_dict(), client_dict)
def test_minimax_inherits_openai_client(self):
"""Test that MiniMaxClient properly inherits from OpenAIClient."""
from adalflow.components.model_client.openai_client import OpenAIClient
self.assertTrue(issubclass(MiniMaxClient, OpenAIClient))
if __name__ == "__main__":
unittest.main()