-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1037 from parea-ai/PAI-1442-openai-structured-out…
…puts openai-structured-outputs
- Loading branch information
Showing
8 changed files
with
250 additions
and
19 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
cookbook/openai/tracing_with_openai_with_structured_output.py
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,109 @@ | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
from openai import OpenAI | ||
from pydantic import BaseModel | ||
|
||
from parea import Parea | ||
|
||
load_dotenv() | ||
|
||
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) | ||
p = Parea(api_key=os.getenv("PAREA_API_KEY")) | ||
p.wrap_openai_client(client) | ||
|
||
|
||
class CalendarEvent(BaseModel): | ||
name: str | ||
date: str | ||
participants: list[str] | ||
|
||
|
||
def with_pydantic(): | ||
completion = client.beta.chat.completions.parse( | ||
model="gpt-4o-2024-08-06", | ||
messages=[ | ||
{"role": "system", "content": "Extract the event information."}, | ||
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."}, | ||
], | ||
response_format=CalendarEvent, | ||
) | ||
event = completion.choices[0].message.parsed | ||
print(event) | ||
|
||
|
||
def with_json_schema(): | ||
response = client.chat.completions.create( | ||
model="gpt-4o-2024-08-06", | ||
messages=[ | ||
{"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."}, | ||
{"role": "user", "content": "how can I solve 8x + 7 = -23"}, | ||
], | ||
response_format={ | ||
"type": "json_schema", | ||
"json_schema": { | ||
"name": "math_response", | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"steps": { | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"properties": {"explanation": {"type": "string"}, "output": {"type": "string"}}, | ||
"required": ["explanation", "output"], | ||
"additionalProperties": False, | ||
}, | ||
}, | ||
"final_answer": {"type": "string"}, | ||
}, | ||
"required": ["steps", "final_answer"], | ||
"additionalProperties": False, | ||
}, | ||
"strict": True, | ||
}, | ||
}, | ||
) | ||
print(response.choices[0].message.content) | ||
|
||
|
||
def with_tools(): | ||
tools = [ | ||
{ | ||
"type": "function", | ||
"function": { | ||
"name": "get_delivery_date", | ||
"description": "Get the delivery date for a customer's order. Call this whenever you need to know the delivery date, for example when a customer asks 'Where is my package'", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"order_id": { | ||
"type": "string", | ||
"description": "The customer's order ID.", | ||
}, | ||
}, | ||
"required": ["order_id"], | ||
"additionalProperties": False, | ||
}, | ||
}, | ||
"strict": True, | ||
} | ||
] | ||
|
||
messages = [ | ||
{"role": "system", "content": "You are a helpful customer support assistant. Use the supplied tools to assist the user."}, | ||
{"role": "user", "content": "Hi, can you tell me the delivery date for my order with id 5?"}, | ||
] | ||
|
||
response = client.chat.completions.create( | ||
model="gpt-4o-2024-08-06", | ||
messages=messages, | ||
tools=tools, | ||
) | ||
print(response.choices[0].message.tool_calls) | ||
|
||
|
||
if __name__ == "__main__": | ||
with_pydantic() | ||
with_json_schema() | ||
with_tools() |
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
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
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
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
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
Oops, something went wrong.