forked from waybarrios/vllm-mlx
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathtest_guided.py
More file actions
508 lines (386 loc) · 16.5 KB
/
test_guided.py
File metadata and controls
508 lines (386 loc) · 16.5 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# SPDX-License-Identifier: Apache-2.0
"""Tests for guided generation module."""
from unittest.mock import MagicMock, patch
import pytest
class TestJsonSchemaToPydantic:
"""Tests for json_schema_to_pydantic function."""
def test_basic_string_property(self):
"""Test conversion of basic string property."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {"type": "object", "properties": {"name": {"type": "string"}}}
model = json_schema_to_pydantic(schema)
assert model is not None
assert hasattr(model, "model_validate")
# Test validation
instance = model.model_validate({"name": "test"})
assert instance.name == "test"
def test_multiple_property_types(self):
"""Test conversion of multiple property types."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"score": {"type": "number"},
"active": {"type": "boolean"},
},
}
model = json_schema_to_pydantic(schema)
assert model is not None
instance = model.model_validate(
{"name": "John", "age": 30, "score": 85.5, "active": True}
)
assert instance.name == "John"
assert instance.age == 30
assert instance.score == 85.5
assert instance.active is True
def test_required_fields(self):
"""Test required fields are enforced."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {
"type": "object",
"properties": {"name": {"type": "string"}, "email": {"type": "string"}},
"required": ["name"],
}
model = json_schema_to_pydantic(schema)
assert model is not None
# Should fail without required field
with pytest.raises(Exception):
model.model_validate({})
# Should pass with required field
instance = model.model_validate({"name": "test"})
assert instance.name == "test"
assert instance.email is None # Optional field
def test_optional_fields(self):
"""Test optional fields work correctly."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {
"type": "object",
"properties": {"optional_field": {"type": "string"}},
}
model = json_schema_to_pydantic(schema)
assert model is not None
instance = model.model_validate({})
assert instance.optional_field is None
def test_array_type(self):
"""Test array type conversion."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {
"type": "object",
"properties": {
"tags": {"type": "array", "items": {"type": "string"}},
"scores": {"type": "array", "items": {"type": "number"}},
},
}
model = json_schema_to_pydantic(schema)
assert model is not None
instance = model.model_validate(
{"tags": ["a", "b", "c"], "scores": [1.0, 2.5, 3.5]}
)
assert instance.tags == ["a", "b", "c"]
assert instance.scores == [1.0, 2.5, 3.5]
def test_nested_object(self):
"""Test nested object type conversion."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
},
}
},
}
model = json_schema_to_pydantic(schema)
assert model is not None
instance = model.model_validate({"user": {"name": "John", "age": 30}})
assert instance.user == {"name": "John", "age": 30}
def test_empty_schema(self):
"""Test empty schema returns model with no fields."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {"type": "object", "properties": {}}
model = json_schema_to_pydantic(schema)
assert model is not None
instance = model.model_validate({})
assert hasattr(instance, "model_validate")
def test_missing_properties(self):
"""Test schema without properties key."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {"type": "object"}
model = json_schema_to_pydantic(schema)
assert model is not None
def test_complex_schema(self):
"""Test complex schema with mixed types."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {
"type": "object",
"properties": {
"id": {"type": "string"},
"count": {"type": "integer"},
"price": {"type": "number"},
"is_active": {"type": "boolean"},
"items": {"type": "array", "items": {"type": "string"}},
"metadata": {
"type": "object",
"properties": {"created": {"type": "string"}},
},
},
"required": ["id", "count"],
}
model = json_schema_to_pydantic(schema)
assert model is not None
instance = model.model_validate(
{
"id": "abc123",
"count": 5,
"price": 10.99,
"is_active": True,
"items": ["a", "b"],
"metadata": {"created": "2024-01-01"},
}
)
assert instance.id == "abc123"
assert instance.count == 5
class TestIsGuidedAvailable:
"""Tests for is_guided_available function."""
@patch("vllm_mlx.api.guided.HAS_OUTLINES", True)
def test_returns_true_when_outlines_available(self):
"""Test returns True when outlines is available."""
# Need to reimport to pick up the patch
import vllm_mlx.api.guided as guided
result = guided.is_guided_available()
assert result is True
@patch("vllm_mlx.api.guided.HAS_OUTLINES", False)
def test_returns_false_when_outlines_not_available(self):
"""Test returns False when outlines is not available."""
import vllm_mlx.api.guided as guided
result = guided.is_guided_available()
assert result is False
class TestGuidedGenerator:
"""Tests for GuidedGenerator class."""
def test_init_raises_import_error_without_outlines(self):
"""Test that init raises ImportError when outlines not available."""
import vllm_mlx.api.guided as guided
# Save original state
original_has_outlines = guided.HAS_OUTLINES
original_outlines = guided.outlines
try:
guided.HAS_OUTLINES = False
guided.outlines = None
with pytest.raises(ImportError, match="outlines is required"):
guided.GuidedGenerator(None, None)
finally:
# Restore original state
guided.HAS_OUTLINES = original_has_outlines
guided.outlines = original_outlines
def test_init_succeeds_with_outlines(self):
"""Test init succeeds when outlines is available."""
import vllm_mlx.api.guided as guided
# Save original state
original_has_outlines = guided.HAS_OUTLINES
original_outlines = guided.outlines
try:
guided.HAS_OUTLINES = True
mock_outlines = MagicMock()
guided.outlines = mock_outlines
mock_model = MagicMock()
mock_tokenizer = MagicMock()
generator = guided.GuidedGenerator(mock_model, mock_tokenizer)
assert generator._model is mock_model
assert generator._tokenizer is mock_tokenizer
finally:
guided.HAS_OUTLINES = original_has_outlines
guided.outlines = original_outlines
def test_generate_json_with_mocked_outlines(self):
"""Test generate_json with mocked outlines."""
import vllm_mlx.api.guided as guided
# Save original state
original_has_outlines = guided.HAS_OUTLINES
original_outlines = guided.outlines
try:
guided.HAS_OUTLINES = True
# Create mock outlines
mock_outlines = MagicMock()
guided.outlines = mock_outlines
# Create mock model and tokenizer
mock_model = MagicMock()
mock_tokenizer = MagicMock()
# Create mock outlines model that returns JSON
mock_outlines_model = MagicMock()
mock_outlines.from_mlxlm.return_value = mock_outlines_model
# Create generator
generator = guided.GuidedGenerator(mock_model, mock_tokenizer)
# Test schema
schema = {"type": "object", "properties": {"name": {"type": "string"}}}
# Mock the result
mock_outlines_model.return_value = '{"name": "test"}'
result = generator.generate_json(
prompt="Generate a name",
json_schema=schema,
max_tokens=100,
temperature=0.5,
)
assert result == '{"name": "test"}'
mock_outlines.from_mlxlm.assert_called_once_with(mock_model, mock_tokenizer)
finally:
guided.HAS_OUTLINES = original_has_outlines
guided.outlines = original_outlines
def test_generate_json_returns_none_on_failure(self):
"""Test generate_json returns None on failure."""
import vllm_mlx.api.guided as guided
# Save original state
original_has_outlines = guided.HAS_OUTLINES
original_outlines = guided.outlines
try:
guided.HAS_OUTLINES = True
# Create mock outlines that raises exception
mock_outlines = MagicMock()
guided.outlines = mock_outlines
mock_model = MagicMock()
mock_tokenizer = MagicMock()
mock_outlines_model = MagicMock()
mock_outlines.from_mlxlm.return_value = mock_outlines_model
# Make the model raise an exception
mock_outlines_model.side_effect = Exception("Generation failed")
generator = guided.GuidedGenerator(mock_model, mock_tokenizer)
schema = {"type": "object", "properties": {"name": {"type": "string"}}}
result = generator.generate_json(prompt="Generate", json_schema=schema)
assert result is None
finally:
guided.HAS_OUTLINES = original_has_outlines
guided.outlines = original_outlines
def test_generate_json_object_with_mocked_outlines(self):
"""Test generate_json_object with mocked outlines."""
import sys
import vllm_mlx.api.guided as guided
# Save original state
original_has_outlines = guided.HAS_OUTLINES
original_outlines = guided.outlines
had_outlines = "outlines" in sys.modules
original_module = sys.modules.get("outlines")
try:
guided.HAS_OUTLINES = True
# Create mock outlines module with generate submodule
mock_outlines = MagicMock()
mock_generate = MagicMock()
mock_generator = MagicMock()
mock_generate.regex.return_value = mock_generator
mock_generator.return_value = '{"key": "value"}'
mock_outlines.generate = mock_generate
guided.outlines = mock_outlines
# Also patch sys.modules so `from outlines import generate` works
sys.modules["outlines"] = mock_outlines
sys.modules["outlines.generate"] = mock_generate
mock_model = MagicMock()
mock_tokenizer = MagicMock()
mock_outlines_model = MagicMock()
mock_outlines.from_mlxlm.return_value = mock_outlines_model
generator = guided.GuidedGenerator(mock_model, mock_tokenizer)
result = generator.generate_json_object(
prompt="Generate JSON", max_tokens=100, temperature=0.5
)
assert result == '{"key": "value"}'
mock_generate.regex.assert_called_once()
finally:
guided.HAS_OUTLINES = original_has_outlines
guided.outlines = original_outlines
if had_outlines:
sys.modules["outlines"] = original_module
else:
sys.modules.pop("outlines", None)
sys.modules.pop("outlines.generate", None)
class TestGenerateWithSchema:
"""Tests for generate_with_schema convenience function."""
def test_returns_none_when_outlines_not_available(self):
"""Test returns None when outlines is not available."""
import vllm_mlx.api.guided as guided
# Save original state
original_has_outlines = guided.HAS_OUTLINES
try:
guided.HAS_OUTLINES = False
result = guided.generate_with_schema(
model=MagicMock(),
tokenizer=MagicMock(),
prompt="Generate",
json_schema={"type": "object", "properties": {}},
)
assert result is None
finally:
guided.HAS_OUTLINES = original_has_outlines
def test_generates_json_with_schema(self):
"""Test generate_with_schema produces JSON output."""
import vllm_mlx.api.guided as guided
# Save original state
original_has_outlines = guided.HAS_OUTLINES
original_outlines = guided.outlines
try:
guided.HAS_OUTLINES = True
mock_outlines = MagicMock()
guided.outlines = mock_outlines
mock_model = MagicMock()
mock_tokenizer = MagicMock()
mock_outlines_model = MagicMock()
mock_outlines.from_mlxlm.return_value = mock_outlines_model
schema = {"type": "object", "properties": {"result": {"type": "string"}}}
mock_outlines_model.return_value = '{"result": "success"}'
result = guided.generate_with_schema(
model=mock_model,
tokenizer=mock_tokenizer,
prompt="Generate",
json_schema=schema,
max_tokens=50,
temperature=0.3,
)
assert result == '{"result": "success"}'
finally:
guided.HAS_OUTLINES = original_has_outlines
guided.outlines = original_outlines
class TestEdgeCases:
"""Test edge cases for the guided generation module."""
def test_empty_schema_with_required(self):
"""Test empty schema with required fields."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {"type": "object", "properties": {}, "required": []}
model = json_schema_to_pydantic(schema)
assert model is not None
instance = model.model_validate({})
assert instance is not None
def test_schema_with_all_optional_fields(self):
"""Test schema where all fields are optional."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {
"type": "object",
"properties": {"field1": {"type": "string"}, "field2": {"type": "integer"}},
}
model = json_schema_to_pydantic(schema)
assert model is not None
# Should validate with empty dict
instance = model.model_validate({})
assert instance.field1 is None
assert instance.field2 is None
def test_array_with_integer_items(self):
"""Test array with integer items."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {
"type": "object",
"properties": {"ids": {"type": "array", "items": {"type": "integer"}}},
}
model = json_schema_to_pydantic(schema)
assert model is not None
instance = model.model_validate({"ids": [1, 2, 3]})
assert instance.ids == [1, 2, 3]
def test_array_with_boolean_items(self):
"""Test array with boolean items."""
from vllm_mlx.api.guided import json_schema_to_pydantic
schema = {
"type": "object",
"properties": {"flags": {"type": "array", "items": {"type": "boolean"}}},
}
model = json_schema_to_pydantic(schema)
assert model is not None
instance = model.model_validate({"flags": [True, False, True]})
assert instance.flags == [True, False, True]