main
1import os
2import asyncio
3
4from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider
5
6from openai import AsyncAzureOpenAI
7
8# Azure OpenAI Realtime Docs
9
10# How-to: https://learn.microsoft.com/azure/ai-services/openai/how-to/realtime-audio
11# Supported models and API versions: https://learn.microsoft.com/azure/ai-services/openai/how-to/realtime-audio#supported-models
12# Entra ID auth: https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity
13
14
15async def main() -> None:
16 """The following example demonstrates how to configure Azure OpenAI to use the Realtime API.
17 For an audio example, see push_to_talk_app.py and update the client and model parameter accordingly.
18
19 When prompted for user input, type a message and hit enter to send it to the model.
20 Enter "q" to quit the conversation.
21 """
22
23 credential = DefaultAzureCredential()
24 client = AsyncAzureOpenAI(
25 azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
26 azure_ad_token_provider=get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default"),
27 api_version="2024-10-01-preview",
28 )
29 async with client.realtime.connect(
30 model="gpt-realtime", # deployment name for your model
31 ) as connection:
32 await connection.session.update(
33 session={
34 "output_modalities": ["text"],
35 "model": "gpt-realtime",
36 "type": "realtime",
37 }
38 )
39 while True:
40 user_input = input("Enter a message: ")
41 if user_input == "q":
42 break
43
44 await connection.conversation.item.create(
45 item={
46 "type": "message",
47 "role": "user",
48 "content": [{"type": "input_text", "text": user_input}],
49 }
50 )
51 await connection.response.create()
52 async for event in connection:
53 if event.type == "response.output_text.delta":
54 print(event.delta, flush=True, end="")
55 elif event.type == "response.output_text.done":
56 print()
57 elif event.type == "response.done":
58 break
59
60 await credential.close()
61
62
63asyncio.run(main())