openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.16.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/realtime/azure_realtime.py

82lines · 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(
45 websocket_base_url=base_url,
46 api_key=token
47 )
48 async with client.realtime.connect(
49 model=deployment_name,
50 ) as connection:
51 await connection.session.update(
52 session={
53 "output_modalities": ["text"],
54 "model": deployment_name,
55 "type": "realtime",
56 }
57 )
58 while True:
59 user_input = input("Enter a message: ")
60 if user_input == "q":
61 break
62
63 await connection.conversation.item.create(
64 item={
65 "type": "message",
66 "role": "user",
67 "content": [{"type": "input_text", "text": user_input}],
68 }
69 )
70 await connection.response.create()
71 async for event in connection:
72 if event.type == "response.output_text.delta":
73 print(event.delta, flush=True, end="")
74 elif event.type == "response.output_text.done":
75 print()
76 elif event.type == "response.done":
77 break
78
79 await credential.close()
80
81
82asyncio.run(main())
83