openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.43.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/async_demo.py

30lines · modeblame

08b8179aDavid Schnurr2 years ago1#!/usr/bin/env -S poetry run python
2
3import asyncio
4
5from openai import AsyncOpenAI
6
7# gets API Key from environment variable OPENAI_API_KEY
8client = AsyncOpenAI()
9
10
11async def main() -> None:
d64d811eJustin Beckwith2 weeks ago12stream = await client.chat.completions.create(
13model="gpt-5.5",
14messages=[
15{
16"role": "user",
17"content": "Say this is a test",
18},
19],
08b8179aDavid Schnurr2 years ago20stream=True,
21)
d64d811eJustin Beckwith2 weeks ago22async for chunk in stream:
23if not chunk.choices:
24continue
25
26print(chunk.choices[0].delta.content, end="")
08b8179aDavid Schnurr2 years ago27print()
28
29
30asyncio.run(main())