-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathstructured_output.py
More file actions
38 lines (31 loc) · 871 Bytes
/
structured_output.py
File metadata and controls
38 lines (31 loc) · 871 Bytes
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
from openai import OpenAI
from pydantic import BaseModel
client = OpenAI()
class CodeOutput(BaseModel):
function_name: str
code: str
explanation: str
example_usage: str
code_response = client.responses.parse(
model="gpt-5",
input=[
{
"role": "developer",
"content": (
"You are a coding assistant. Generate clean,"
"well-documented Python code."
),
},
{
"role": "user",
"content": "Write a simple Python function to add two numbers",
},
],
text_format=CodeOutput,
)
code_result = code_response.output_parsed
print(f"Function Name: {code_result.function_name}")
print("\nCode:")
print(code_result.code)
print(f"\nExplanation: {code_result.explanation}")
print(f"\nExample Usage:\n{code_result.example_usage}")