Skip to content

Commit f629e18

Browse files
committed
Structured outputs demos
1 parent 0a4e74f commit f629e18

5 files changed

Lines changed: 297 additions & 0 deletions

structured_outputs_basic.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import os
2+
3+
import azure.identity
4+
import openai
5+
import rich
6+
from dotenv import load_dotenv
7+
from pydantic import BaseModel
8+
9+
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
10+
load_dotenv(override=True)
11+
API_HOST = os.getenv("API_HOST", "github")
12+
13+
if API_HOST == "azure":
14+
token_provider = azure.identity.get_bearer_token_provider(
15+
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
16+
)
17+
client = openai.AzureOpenAI(
18+
api_version=os.environ["AZURE_OPENAI_VERSION"],
19+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
20+
azure_ad_token_provider=token_provider,
21+
)
22+
MODEL_NAME = os.environ["AZURE_OPENAI_DEPLOYMENT"]
23+
24+
elif API_HOST == "ollama":
25+
client = openai.OpenAI(base_url=os.environ["OLLAMA_ENDPOINT"], api_key="nokeyneeded")
26+
MODEL_NAME = os.environ["OLLAMA_MODEL"]
27+
28+
elif API_HOST == "github":
29+
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.environ["GITHUB_TOKEN"])
30+
MODEL_NAME = os.getenv("GITHUB_MODEL", "gpt-4o")
31+
32+
else:
33+
client = openai.OpenAI(api_key=os.environ["OPENAI_KEY"])
34+
MODEL_NAME = os.environ["OPENAI_MODEL"]
35+
36+
37+
38+
class CalendarEvent(BaseModel):
39+
name: str
40+
date: str
41+
participants: list[str]
42+
43+
44+
completion = client.beta.chat.completions.parse(
45+
model=MODEL_NAME,
46+
messages=[
47+
{"role": "system", "content": "Extract the event information."},
48+
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
49+
],
50+
response_format=CalendarEvent,
51+
)
52+
53+
54+
message = completion.choices[0].message
55+
if (message.refusal):
56+
rich.print(message.refusal)
57+
else:
58+
event = message.parsed
59+
rich.print(event)

structured_outputs_description.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import os
2+
3+
import azure.identity
4+
import openai
5+
import rich
6+
from dotenv import load_dotenv
7+
from pydantic import BaseModel, Field
8+
9+
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
10+
load_dotenv(override=True)
11+
API_HOST = os.getenv("API_HOST", "github")
12+
13+
if API_HOST == "azure":
14+
token_provider = azure.identity.get_bearer_token_provider(
15+
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
16+
)
17+
client = openai.AzureOpenAI(
18+
api_version=os.environ["AZURE_OPENAI_VERSION"],
19+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
20+
azure_ad_token_provider=token_provider,
21+
)
22+
MODEL_NAME = os.environ["AZURE_OPENAI_DEPLOYMENT"]
23+
24+
elif API_HOST == "ollama":
25+
client = openai.OpenAI(base_url=os.environ["OLLAMA_ENDPOINT"], api_key="nokeyneeded")
26+
MODEL_NAME = os.environ["OLLAMA_MODEL"]
27+
28+
elif API_HOST == "github":
29+
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.environ["GITHUB_TOKEN"])
30+
MODEL_NAME = os.getenv("GITHUB_MODEL", "gpt-4o")
31+
32+
else:
33+
client = openai.OpenAI(api_key=os.environ["OPENAI_KEY"])
34+
MODEL_NAME = os.environ["OPENAI_MODEL"]
35+
36+
37+
38+
class CalendarEvent(BaseModel):
39+
name: str
40+
date: str = Field(..., description="A date in the format YYYY-MM-DD")
41+
participants: list[str]
42+
43+
44+
completion = client.beta.chat.completions.parse(
45+
model=MODEL_NAME,
46+
messages=[
47+
{"role": "system", "content": "Extract the event information. If no year is specified, assume the current year (2025)."},
48+
{"role": "user", "content": "Alice and Bob are going to a science fair on the 1st of april."},
49+
],
50+
response_format=CalendarEvent,
51+
)
52+
CalendarEvent(name='Science Fair', date='2025-04-01', participants=['Alice', 'Bob'])
53+
54+
message = completion.choices[0].message
55+
if (message.refusal):
56+
rich.print(message.refusal)
57+
else:
58+
event = message.parsed
59+
rich.print(event)

structured_outputs_enum.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import os
2+
from enum import Enum
3+
4+
import azure.identity
5+
import openai
6+
import rich
7+
from dotenv import load_dotenv
8+
from pydantic import BaseModel
9+
10+
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
11+
load_dotenv(override=True)
12+
API_HOST = os.getenv("API_HOST", "github")
13+
14+
if API_HOST == "azure":
15+
token_provider = azure.identity.get_bearer_token_provider(
16+
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
17+
)
18+
client = openai.AzureOpenAI(
19+
api_version=os.environ["AZURE_OPENAI_VERSION"],
20+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
21+
azure_ad_token_provider=token_provider,
22+
)
23+
MODEL_NAME = os.environ["AZURE_OPENAI_DEPLOYMENT"]
24+
25+
elif API_HOST == "ollama":
26+
client = openai.OpenAI(base_url=os.environ["OLLAMA_ENDPOINT"], api_key="nokeyneeded")
27+
MODEL_NAME = os.environ["OLLAMA_MODEL"]
28+
29+
elif API_HOST == "github":
30+
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.environ["GITHUB_TOKEN"])
31+
MODEL_NAME = os.getenv("GITHUB_MODEL", "gpt-4o")
32+
33+
else:
34+
client = openai.OpenAI(api_key=os.environ["OPENAI_KEY"])
35+
MODEL_NAME = os.environ["OPENAI_MODEL"]
36+
37+
38+
39+
class DayOfWeek(str, Enum):
40+
SUNDAY = "Sunday"
41+
MONDAY = "Monday"
42+
TUESDAY = "Tuesday"
43+
WEDNESDAY = "Wednesday"
44+
THURSDAY = "Thursday"
45+
FRIDAY = "Friday"
46+
SATURDAY = "Saturday"
47+
48+
class CalendarEvent(BaseModel):
49+
name: str
50+
date: DayOfWeek
51+
participants: list[str]
52+
53+
completion = client.beta.chat.completions.parse(
54+
model=MODEL_NAME,
55+
messages=[
56+
{"role": "system", "content": "Extract the event information."},
57+
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
58+
],
59+
response_format=CalendarEvent,
60+
)
61+
62+
63+
message = completion.choices[0].message
64+
if (message.refusal):
65+
rich.print(message.refusal)
66+
else:
67+
event = message.parsed
68+
rich.print(event)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
3+
import azure.identity
4+
import openai
5+
import rich
6+
from dotenv import load_dotenv
7+
from pydantic import BaseModel
8+
9+
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
10+
load_dotenv(override=True)
11+
API_HOST = os.getenv("API_HOST", "github")
12+
13+
if API_HOST == "azure":
14+
token_provider = azure.identity.get_bearer_token_provider(
15+
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
16+
)
17+
client = openai.AzureOpenAI(
18+
api_version=os.environ["AZURE_OPENAI_VERSION"],
19+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
20+
azure_ad_token_provider=token_provider,
21+
)
22+
MODEL_NAME = os.environ["AZURE_OPENAI_DEPLOYMENT"]
23+
24+
elif API_HOST == "ollama":
25+
client = openai.OpenAI(base_url=os.environ["OLLAMA_ENDPOINT"], api_key="nokeyneeded")
26+
MODEL_NAME = os.environ["OLLAMA_MODEL"]
27+
28+
elif API_HOST == "github":
29+
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.environ["GITHUB_TOKEN"])
30+
MODEL_NAME = os.getenv("GITHUB_MODEL", "gpt-4o")
31+
32+
else:
33+
client = openai.OpenAI(api_key=os.environ["OPENAI_KEY"])
34+
MODEL_NAME = os.environ["OPENAI_MODEL"]
35+
36+
37+
class GetDeliveryDate(BaseModel):
38+
order_id: str
39+
40+
response = client.chat.completions.create(
41+
model=MODEL_NAME,
42+
messages=[
43+
{"role": "system",
44+
"content": "You're a customer support bot. Use the tools to assist the user."},
45+
{"role": "user",
46+
"content": "Hi, can you tell me the delivery date for my order #12345?"}],
47+
tools=[openai.pydantic_function_tool(GetDeliveryDate)])
48+
49+
rich.print(response.choices[0].message.tool_calls[0].function)

structured_outputs_nested.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import os
2+
3+
import azure.identity
4+
import openai
5+
import rich
6+
from dotenv import load_dotenv
7+
from pydantic import BaseModel
8+
9+
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
10+
load_dotenv(override=True)
11+
API_HOST = os.getenv("API_HOST", "github")
12+
13+
if API_HOST == "azure":
14+
token_provider = azure.identity.get_bearer_token_provider(
15+
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
16+
)
17+
client = openai.AzureOpenAI(
18+
api_version=os.environ["AZURE_OPENAI_VERSION"],
19+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
20+
azure_ad_token_provider=token_provider,
21+
)
22+
MODEL_NAME = os.environ["AZURE_OPENAI_DEPLOYMENT"]
23+
24+
elif API_HOST == "ollama":
25+
client = openai.OpenAI(base_url=os.environ["OLLAMA_ENDPOINT"], api_key="nokeyneeded")
26+
MODEL_NAME = os.environ["OLLAMA_MODEL"]
27+
28+
elif API_HOST == "github":
29+
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.environ["GITHUB_TOKEN"])
30+
MODEL_NAME = os.getenv("GITHUB_MODEL", "gpt-4o")
31+
32+
else:
33+
client = openai.OpenAI(api_key=os.environ["OPENAI_KEY"])
34+
MODEL_NAME = os.environ["OPENAI_MODEL"]
35+
36+
37+
38+
class Participant(BaseModel):
39+
name: str
40+
job_title: str
41+
42+
class CalendarEvent(BaseModel):
43+
name: str
44+
date: str
45+
participants: list[Participant]
46+
47+
completion = client.beta.chat.completions.parse(
48+
model=MODEL_NAME,
49+
messages=[
50+
{"role": "system", "content": "Extract the event information."},
51+
{"role": "user", "content": "Alice the carpenter and Bob the plumber are going to a science fair on Friday."},
52+
],
53+
response_format=CalendarEvent,
54+
)
55+
56+
57+
message = completion.choices[0].message
58+
if (message.refusal):
59+
rich.print(message.refusal)
60+
else:
61+
event = message.parsed
62+
rich.print(event)

0 commit comments

Comments
 (0)