openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
examples/parsing_stream.py
42lines · modecode
| 1 | from typing import List |
| 2 | |
| 3 | import rich |
| 4 | from pydantic import BaseModel |
| 5 | |
| 6 | from openai import OpenAI |
| 7 | |
| 8 | |
| 9 | class Step(BaseModel): |
| 10 | explanation: str |
| 11 | output: str |
| 12 | |
| 13 | |
| 14 | class MathResponse(BaseModel): |
| 15 | steps: List[Step] |
| 16 | final_answer: str |
| 17 | |
| 18 | |
| 19 | client = OpenAI() |
| 20 | |
| 21 | with client.beta.chat.completions.stream( |
| 22 | model="gpt-4o-2024-08-06", |
| 23 | messages=[ |
| 24 | {"role": "system", "content": "You are a helpful math tutor."}, |
| 25 | {"role": "user", "content": "solve 8x + 31 = 2"}, |
| 26 | ], |
| 27 | response_format=MathResponse, |
| 28 | ) as stream: |
| 29 | for event in stream: |
| 30 | if event.type == "content.delta": |
| 31 | print(event.delta, end="", flush=True) |
| 32 | elif event.type == "content.done": |
| 33 | print("\n") |
| 34 | if event.parsed is not None: |
| 35 | print(f"answer: {event.parsed.final_answer}") |
| 36 | elif event.type == "refusal.delta": |
| 37 | print(event.delta, end="", flush=True) |
| 38 | elif event.type == "refusal.done": |
| 39 | print() |
| 40 | |
| 41 | print("---------------") |
| 42 | rich.print(stream.get_final_completion()) |
| 43 | |