-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
from enum import Enum | ||
from typing import Union | ||
|
||
from pydantic import BaseModel | ||
|
||
import openai | ||
from openai import OpenAI | ||
|
||
|
||
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="gpt-4o-2024-08-06", | ||
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": "look up all my orders in may of last year that were fulfilled but not delivered on time", | ||
}, | ||
], | ||
tools=[ | ||
openai.pydantic_function_tool(Query), | ||
], | ||
) | ||
|
||
print(completion.choices[0].message.tool_calls[0].function.parsed_arguments) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
# https://openai.com/index/introducing-structured-outputs-in-the-api/ | ||
|
||
from pydantic import BaseModel | ||
|
||
from openai import OpenAI | ||
|
||
|
||
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="gpt-4o-2024-08-06", | ||
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) |