-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathstructured_outputs_example.py
135 lines (108 loc) · 3.69 KB
/
structured_outputs_example.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# https://openai.com/index/introducing-structured-outputs-in-the-api/
from enum import Enum
from typing import Union
from pydantic import BaseModel
import openai
from openai import OpenAI
NEW_GPT_4o_AUG = "gpt-4o-2024-08-06"
def structured_output_tool_call():
class Table(str, Enum):
orders = "orders"
customers = "customers"
products = "products"
class Column(str, Enum):
id = "id"
status = "status"
expected_delivery_date = "expected_delivery_date"
delivered_at = "delivered_at"
shipped_at = "shipped_at"
ordered_at = "ordered_at"
canceled_at = "canceled_at"
class Operator(str, Enum):
eq = "="
gt = ">"
lt = "<"
le = "<="
ge = ">="
ne = "!="
class OrderBy(str, Enum):
asc = "asc"
desc = "desc"
class DynamicValue(BaseModel):
column_name: str
class Condition(BaseModel):
column: str
operator: Operator
value: Union[str, int, DynamicValue]
class Query(BaseModel):
table_name: Table
columns: list[Column]
conditions: list[Condition]
order_by: OrderBy
client = OpenAI()
completion = client.beta.chat.completions.parse(
model=NEW_GPT_4o_AUG,
messages=[
{
"role": "system",
"content": "You are a helpful assistant. The current date is August 6, 2024. You help users query for the data they are looking for by calling the query function.",
},
{
"role": "user",
"content": "Find all the orders that were cancelled in the first quarter of 2022",
},
],
tools=[
openai.pydantic_function_tool(Query),
],
)
def mock_query_function(query: Query):
print(f"Table Name: {query.table_name}")
print("Columns:")
for column in query.columns:
print(f" - {column}")
print("Conditions:")
for condition in query.conditions:
print(
f" - Column: {condition.column}, Operator: {condition.operator}, Value: {condition.value}"
)
print(f"Order By: {query.order_by}")
print(
"completion.choices and completion.choices[0].message",
completion.choices and completion.choices[0].message,
)
# Parse the completion result and pass it to the mock function if available
if completion.choices and completion.choices[0].message.tool_calls:
if completion.choices[0].message.tool_calls[0].function.name == "Query ":
query_result = (
completion.choices[0].message.tool_calls[0].function.parsed_arguments
)
mock_query_function(query_result)
else:
print(f"{completion.choices and completion.choices[0].message.content}")
else:
print(f"{completion.choices and completion.choices[0].message.content}")
def structured_output_minimal():
class Step(BaseModel):
explanation: str
output: str
class MathResponse(BaseModel):
steps: list[Step]
final_answer: str
client = OpenAI()
completion = client.beta.chat.completions.parse(
model=NEW_GPT_4o_AUG,
messages=[
{"role": "system", "content": "You are a helpful math tutor."},
{"role": "user", "content": "solve 8x + 31 = 2"},
],
response_format=MathResponse,
)
message = completion.choices[0].message
if message.parsed:
print(message.parsed.steps)
print(message.parsed.final_answer)
else:
print(message.refusal)
structured_output_minimal()
structured_output_tool_call()