openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
examples/async_demo.py
22lines · modeblame
08b8179aDavid Schnurr2 years ago | 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( | |
a045ef84Stainless Bot2 years ago | 13 | model="gpt-3.5-turbo-instruct", |
08b8179aDavid Schnurr2 years ago | 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()) |