openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
examples/responses/background_streaming_async.py
53lines · modecode
| 1 | import asyncio |
| 2 | from typing import List |
| 3 | |
| 4 | import rich |
| 5 | from pydantic import BaseModel |
| 6 | |
| 7 | from openai import AsyncOpenAI |
| 8 | |
| 9 | |
| 10 | class Step(BaseModel): |
| 11 | explanation: str |
| 12 | output: str |
| 13 | |
| 14 | |
| 15 | class MathResponse(BaseModel): |
| 16 | steps: List[Step] |
| 17 | final_answer: str |
| 18 | |
| 19 | |
| 20 | async 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 | |
| 52 | if __name__ == "__main__": |
| 53 | asyncio.run(main()) |
| 54 | |