Repository navigation
Expand file tree
/
Copy pathstructured_output.py
More file actions
53 lines (47 loc) · 1.78 KB
/
Copy pathstructured_output.py
File metadata and controls
53 lines (47 loc) · 1.78 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
"""JSON output: `json_object` (any model with the json capability) and a strict
`json_schema` (use an OpenAI model for the schema form).
Docs: https://netarz.ir/docs/ai/chat-completions
"""
import json
import os
from openai import OpenAI
client = OpenAI(
base_url=os.getenv("NETARZ_BASE_URL", "https://netarz.ir/api/ai/v1"),
api_key=os.environ["NETARZ_API_KEY"],
)
# 1) Free-form JSON object.
loose = client.chat.completions.create(
model="gpt-4o-mini",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": "فقط یک شیء JSON معتبر برگردانید."},
{"role": "user", "content": "برای محصول «کفش دویدن» سه ویژگی کوتاه با کلید features بنویسید."},
],
max_tokens=200,
)
print(json.loads(loose.choices[0].message.content))
# 2) Strict schema: the answer must match this shape exactly.
schema = {
"name": "product_card",
"strict": True,
"schema": {
"type": "object",
"properties": {
"title": {"type": "string"},
"category": {"type": "string", "enum": ["gift_card", "subscription", "software", "other"]},
"tags": {"type": "array", "items": {"type": "string"}},
},
"required": ["title", "category", "tags"],
"additionalProperties": False,
},
}
strict = client.chat.completions.create(
model="gpt-4o-mini",
response_format={"type": "json_schema", "json_schema": schema},
messages=[
{"role": "user", "content": "از این متن یک کارت محصول بسازید: «گیفت کارت ۵۰ دلاری اپل برای ریجن آمریکا»"},
],
max_tokens=200,
)
card = json.loads(strict.choices[0].message.content)
print(card["title"], card["category"], card["tags"])