Skip to content

Commit d167d14

Browse files
andreasjlstainless-app[bot]
authored andcommitted
docs(examples): update Azure Realtime sample to use v1 API (#2829)
1 parent 8be9907 commit d167d14

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

examples/realtime/azure_realtime.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider
55

6-
from openai import AsyncAzureOpenAI
6+
from openai import AsyncOpenAI
77

88
# Azure OpenAI Realtime Docs
99

@@ -21,18 +21,37 @@ async def main() -> None:
2121
"""
2222

2323
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",
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
2847
)
2948
async with client.realtime.connect(
30-
model="gpt-realtime", # deployment name for your model
49+
model=deployment_name,
3150
) as connection:
3251
await connection.session.update(
3352
session={
3453
"output_modalities": ["text"],
35-
"model": "gpt-realtime",
54+
"model": deployment_name,
3655
"type": "realtime",
3756
}
3857
)

0 commit comments

Comments
 (0)