from typing import List
import rich
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()
with client.chat.completions.stream(
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,
) as stream:
for event in stream:
if event.type == "content.delta":
print(event.delta, end="", flush=True)
elif event.type == "content.done":
print("\n")
if event.parsed is not None:
print(f"answer: {event.parsed.final_answer}")
elif event.type == "refusal.delta":
print(event.delta, end="", flush=True)
elif event.type == "refusal.done":
print()
print("---------------")
rich.print(stream.get_final_completion())openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
examples/parsing_stream.py
42lines · modepreview