Last active
August 9, 2024 06:38
-
-
Save sameer20002/ff79e620a2aa38a8e573bdb38f89a380 to your computer and use it in GitHub Desktop.
This file contains the example code for the blog
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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment