-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathstructured_output.py
More file actions
64 lines (52 loc) · 1.72 KB
/
Copy pathstructured_output.py
File metadata and controls
64 lines (52 loc) · 1.72 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
"""Structured output: constrain decoding to a JSON schema via response_format.
The server's llguidance-backed constrained decoding honors ``response_format``.
This example asks for a JSON object matching a schema and parses the result.
python python/examples/structured_output.py
"""
from __future__ import annotations
import json
import mlxcel
MODEL = "mlx-community/Qwen3-4B-4bit"
SCHEMA = {
"type": "json_schema",
"json_schema": {
"name": "person",
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"city": {"type": "string"},
},
"required": ["name", "age", "city"],
"additionalProperties": False,
},
},
}
def main() -> None:
with mlxcel.LLM(MODEL) as llm:
reply = llm.chat(
[
{
"role": "user",
"content": "Invent a fictional person. Reply as JSON with name, age, city.",
}
],
max_tokens=128,
temperature=0.7,
response_format=SCHEMA,
)
print("raw reply:", reply)
parsed = json.loads(reply)
print("parsed:", parsed)
# The same call via the raw OpenAI client (escape hatch):
oai = llm.openai_client
completion = oai.chat.completions.create(
model=llm.model,
messages=[{"role": "user", "content": "Another fictional person as JSON."}],
response_format=SCHEMA,
max_tokens=128,
)
print("via openai_client:", completion.choices[0].message.content)
if __name__ == "__main__":
main()