openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
examples/streaming.py
56lines · modecode
| 1 | #!/usr/bin/env -S poetry run python |
| 2 | |
| 3 | import asyncio |
| 4 | |
| 5 | from openai import OpenAI, AsyncOpenAI |
| 6 | |
| 7 | # This script assumes you have the OPENAI_API_KEY environment variable set to a valid OpenAI API key. |
| 8 | # |
| 9 | # You can run this script from the root directory like so: |
| 10 | # `python examples/streaming.py` |
| 11 | |
| 12 | |
| 13 | def sync_main() -> None: |
| 14 | client = OpenAI() |
| 15 | response = client.completions.create( |
| 16 | model="gpt-3.5-turbo-instruct", |
| 17 | prompt="1,2,3,", |
| 18 | max_tokens=5, |
| 19 | temperature=0, |
| 20 | stream=True, |
| 21 | ) |
| 22 | |
| 23 | # You can manually control iteration over the response |
| 24 | first = next(response) |
| 25 | print(f"got response data: {first.model_dump_json(indent=2)}") |
| 26 | |
| 27 | # Or you could automatically iterate through all of data. |
| 28 | # Note that the for loop will not exit until *all* of the data has been processed. |
| 29 | for data in response: |
| 30 | print(data.model_dump_json()) |
| 31 | |
| 32 | |
| 33 | async def async_main() -> None: |
| 34 | client = AsyncOpenAI() |
| 35 | response = await client.completions.create( |
| 36 | model="gpt-3.5-turbo-instruct", |
| 37 | prompt="1,2,3,", |
| 38 | max_tokens=5, |
| 39 | temperature=0, |
| 40 | stream=True, |
| 41 | ) |
| 42 | |
| 43 | # You can manually control iteration over the response. |
| 44 | # In Python 3.10+ you can also use the `await anext(response)` builtin instead |
| 45 | first = await response.__anext__() |
| 46 | print(f"got response data: {first.model_dump_json(indent=2)}") |
| 47 | |
| 48 | # Or you could automatically iterate through all of data. |
| 49 | # Note that the for loop will not exit until *all* of the data has been processed. |
| 50 | async for data in response: |
| 51 | print(data.model_dump_json()) |
| 52 | |
| 53 | |
| 54 | sync_main() |
| 55 | |
| 56 | asyncio.run(async_main()) |
| 57 | |