main
1import asyncio
2from typing import List
3
4import rich
5from pydantic import BaseModel
6
7from openai import AsyncOpenAI
8
9
10class Step(BaseModel):
11 explanation: str
12 output: str
13
14
15class MathResponse(BaseModel):
16 steps: List[Step]
17 final_answer: str
18
19
20async def main() -> None:
21 client = AsyncOpenAI()
22 id = None
23 async with client.responses.stream(
24 input="solve 8x + 31 = 2",
25 model="gpt-4o-2024-08-06",
26 text_format=MathResponse,
27 background=True,
28 ) as stream:
29 async for event in stream:
30 if event.type == "response.created":
31 id = event.response.id
32 if "output_text" in event.type:
33 rich.print(event)
34 if event.sequence_number == 10:
35 break
36
37 print("Interrupted. Continuing...")
38
39 assert id is not None
40 async with client.responses.stream(
41 response_id=id,
42 starting_after=10,
43 text_format=MathResponse,
44 ) as stream:
45 async for event in stream:
46 if "output_text" in event.type:
47 rich.print(event)
48
49 rich.print(stream.get_final_response())
50
51
52if __name__ == "__main__":
53 asyncio.run(main())