-
Notifications
You must be signed in to change notification settings - Fork 3
/
streaming-aoai.py
34 lines (27 loc) · 1010 Bytes
/
streaming-aoai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Streaming completions with Azure OpenAI service, using the 1.x version of the openai Python package.
# Define Azure OpenAI API endpoint, deployment, key, and version in a .env file.
import os
import asyncio
from openai import AsyncAzureOpenAI
from dotenv import load_dotenv
load_dotenv()
endpoint = os.environ["AZURE_OPENAI_API_ENDPOINT"]
deployment = os.environ["AZURE_OPENAI_API_MODEL"]
key = os.environ["AZURE_OPENAI_API_KEY"]
version = os.environ["AZURE_OPENAI_API_VERSION"]
client = AsyncAzureOpenAI(
azure_endpoint = endpoint,
api_key = key,
api_version = version
)
async def main() -> None:
stream = await client.chat.completions.create(
model=deployment,
messages = [ {"role": "user", "content": "What is chatgpt?"} ],
stream=True,
)
async for chunk in stream:
if chunk.choices:
content = chunk.choices[0].delta.content
print(content, end="", flush=True)
asyncio.run(main())