Skip to content

Instantly share code, notes, and snippets.

@sameer20002
Last active August 9, 2024 06:38
Show Gist options
  • Select an option

  • Save sameer20002/ff79e620a2aa38a8e573bdb38f89a380 to your computer and use it in GitHub Desktop.

Select an option

Save sameer20002/ff79e620a2aa38a8e573bdb38f89a380 to your computer and use it in GitHub Desktop.
This file contains the example code for the blog
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