Last active
August 10, 2024 05:15
-
-
Save Kjdragan/42854cfc9d295011e163516707507d20 to your computer and use it in GitHub Desktop.
Structured output by pydantic class in OpenAi
This file contains hidden or 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
| from dotenv import load_dotenv | |
| load_dotenv() | |
| from openai import OpenAI | |
| from pydantic import BaseModel | |
| from typing import List | |
| import openai | |
| # initialize client | |
| client = OpenAI() | |
| # pydantic classes | |
| class Songs(BaseModel): | |
| name: str | |
| class Album(BaseModel): | |
| name: str | |
| songs: List[Songs] | |
| producer: str | |
| release_date: str | |
| # two methods to access the response: using tools or response_format | |
| # 1) using tools: It comes from the tool_calls attribute as function=ParsedFunction(arguments=.... | |
| completion = client.beta.chat.completions.parse( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| {"role": "user", "content": "please provide random famous album details"} | |
| ], | |
| tools=[openai.pydantic_function_tool(Album)], | |
| ) | |
| print(completion) | |
| print(f"\n{completion.choices[0].message.tool_calls[0].function.parsed_arguments}\n") | |
| tool_calls = completion.choices[0].message.tool_calls | |
| for index, tool_call in enumerate(tool_calls): | |
| response = tool_call.function.arguments | |
| print(response) | |
| # 2) using response_format. Set a model parameter 'response_format' to the desired pydantic class eg. response_format=Album | |
| completion = client.beta.chat.completions.parse( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": "please provide random famous album details", | |
| } | |
| ], | |
| response_format=Album, | |
| ) | |
| message = completion.choices[0].message | |
| if message.parsed: | |
| print(completion.choices[0].message.content) | |
| else: | |
| print(message.refusal) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment