-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_chat_completions.py
More file actions
44 lines (34 loc) · 1.07 KB
/
openai_chat_completions.py
File metadata and controls
44 lines (34 loc) · 1.07 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
"""Use the OpenAI-compatible client surface with structured output."""
from typing import Literal
from pydantic import BaseModel, Field
from dottxt import DotTxt as OpenAI
class IncidentSummary(BaseModel):
"""Structured incident summary."""
severity: Literal["low", "medium", "high"]
team: str = Field(max_length=32)
def main() -> None:
"""Run the example."""
client = OpenAI()
completion = client.chat.completions.create(
model="openai/gpt-oss-20b",
messages=[
{
"role": "user",
"content": (
"Summarize this incident: checkout errors are blocking purchases."
),
}
],
response_format={
"type": "json_schema",
"json_schema": {
"name": "incident_summary",
"schema": IncidentSummary.model_json_schema(),
},
},
)
print(completion.choices[0].message.content)
# Example output:
# {"severity":"high","team":"checkout"}
if __name__ == "__main__":
main()