openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.35.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/realtime/azure_realtime.py

79lines · modeblame

82ccc985Krista Pratico1 years ago1import os
2import asyncio
3
4from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider
5
d167d145Andreas Lachenmann6 months ago6from openai import AsyncOpenAI
82ccc985Krista Pratico1 years ago7
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.
17For an audio example, see push_to_talk_app.py and update the client and model parameter accordingly.
18
19When prompted for user input, type a message and hit enter to send it to the model.
20Enter "q" to quit the conversation.
21"""
22
23credential = DefaultAzureCredential()
d167d145Andreas Lachenmann6 months ago24token_provider = get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
25token = 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
31endpoint = 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
37deployment_name = os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"]
38
39base_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.
a7a60166stainless-app[bot]6 months ago44client = AsyncOpenAI(websocket_base_url=base_url, api_key=token)
3d3d16abstainless-app[bot]11 months ago45async with client.realtime.connect(
d167d145Andreas Lachenmann6 months ago46model=deployment_name,
82ccc985Krista Pratico1 years ago47) as connection:
3d3d16abstainless-app[bot]11 months ago48await connection.session.update(
49session={
50"output_modalities": ["text"],
d167d145Andreas Lachenmann6 months ago51"model": deployment_name,
3d3d16abstainless-app[bot]11 months ago52"type": "realtime",
53}
54)
82ccc985Krista Pratico1 years ago55while True:
56user_input = input("Enter a message: ")
57if user_input == "q":
58break
59
60await connection.conversation.item.create(
61item={
62"type": "message",
63"role": "user",
64"content": [{"type": "input_text", "text": user_input}],
65}
66)
67await connection.response.create()
68async for event in connection:
3d3d16abstainless-app[bot]11 months ago69if event.type == "response.output_text.delta":
82ccc985Krista Pratico1 years ago70print(event.delta, flush=True, end="")
3d3d16abstainless-app[bot]11 months ago71elif event.type == "response.output_text.done":
82ccc985Krista Pratico1 years ago72print()
73elif event.type == "response.done":
74break
75
76await credential.close()
77
78
79asyncio.run(main())