openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
examples/async_demo.py
22lines · modecode
| 1 | #!/usr/bin/env -S poetry run python |
| 2 | |
| 3 | import asyncio |
| 4 | |
| 5 | from openai import AsyncOpenAI |
| 6 | |
| 7 | # gets API Key from environment variable OPENAI_API_KEY |
| 8 | client = AsyncOpenAI() |
| 9 | |
| 10 | |
| 11 | async def main() -> None: |
| 12 | stream = await client.completions.create( |
| 13 | model="gpt-3.5-turbo-instruct", |
| 14 | prompt="Say this is a test", |
| 15 | stream=True, |
| 16 | ) |
| 17 | async for completion in stream: |
| 18 | print(completion.choices[0].text, end="") |
| 19 | print() |
| 20 | |
| 21 | |
| 22 | asyncio.run(main()) |
| 23 | |