openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
release-please--branches--main--changes--next

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/realtime/azure_realtime.py

79lines · modecode

1import os
2import asyncio
3
4from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider
5
6from openai import AsyncOpenAI
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 token_provider = get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
25 token = await token_provider()
26
27 # The endpoint of your Azure OpenAI resource is required. You can set it in the AZURE_OPENAI_ENDPOINT
28 # environment variable.
29 # You can find it in the Microsoft Foundry portal in the Overview page of your Azure OpenAI resource.
30 # Example: https://{your-resource}.openai.azure.com
31 endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
32
33 # The deployment name of the model you want to use is required. You can set it in the AZURE_OPENAI_DEPLOYMENT_NAME
34 # environment variable.
35 # You can find it in the Foundry portal in the "Models + endpoints" page of your Azure OpenAI resource.
36 # Example: gpt-realtime
37 deployment_name = os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"]
38
39 base_url = endpoint.replace("https://", "wss://").rstrip("/") + "/openai/v1"
40
41 # The APIs are compatible with the OpenAI client library.
42 # You can use the OpenAI client library to access the Azure OpenAI APIs.
43 # Make sure to set the baseURL and apiKey to use the Azure OpenAI endpoint and token.
44 client = AsyncOpenAI(websocket_base_url=base_url, api_key=token)
45 async with client.realtime.connect(
46 model=deployment_name,
47 ) as connection:
48 await connection.session.update(
49 session={
50 "output_modalities": ["text"],
51 "model": deployment_name,
52 "type": "realtime",
53 }
54 )
55 while True:
56 user_input = input("Enter a message: ")
57 if user_input == "q":
58 break
59
60 await connection.conversation.item.create(
61 item={
62 "type": "message",
63 "role": "user",
64 "content": [{"type": "input_text", "text": user_input}],
65 }
66 )
67 await connection.response.create()
68 async for event in connection:
69 if event.type == "response.output_text.delta":
70 print(event.delta, flush=True, end="")
71 elif event.type == "response.output_text.done":
72 print()
73 elif event.type == "response.done":
74 break
75
76 await credential.close()
77
78
79asyncio.run(main())
80