-
Notifications
You must be signed in to change notification settings - Fork 3.8k
docs(examples/azure): example script with realtime API #1967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RobertCraigie
merged 1 commit into
openai:next
from
kristapratico:azure-realtime-example
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
import asyncio | ||
|
||
from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider | ||
|
||
from openai import AsyncAzureOpenAI | ||
|
||
# Azure OpenAI Realtime Docs | ||
|
||
# How-to: https://learn.microsoft.com/azure/ai-services/openai/how-to/realtime-audio | ||
# Supported models and API versions: https://learn.microsoft.com/azure/ai-services/openai/how-to/realtime-audio#supported-models | ||
# Entra ID auth: https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity | ||
|
||
|
||
async def main() -> None: | ||
"""The following example demonstrates how to configure Azure OpenAI to use the Realtime API. | ||
For an audio example, see push_to_talk_app.py and update the client and model parameter accordingly. | ||
|
||
When prompted for user input, type a message and hit enter to send it to the model. | ||
Enter "q" to quit the conversation. | ||
""" | ||
|
||
credential = DefaultAzureCredential() | ||
client = AsyncAzureOpenAI( | ||
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"], | ||
azure_ad_token_provider=get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default"), | ||
api_version="2024-10-01-preview", | ||
) | ||
async with client.beta.realtime.connect( | ||
model="gpt-4o-realtime-preview", # deployment name for your model | ||
) as connection: | ||
await connection.session.update(session={"modalities": ["text"]}) # type: ignore | ||
while True: | ||
user_input = input("Enter a message: ") | ||
if user_input == "q": | ||
break | ||
|
||
await connection.conversation.item.create( | ||
item={ | ||
"type": "message", | ||
"role": "user", | ||
"content": [{"type": "input_text", "text": user_input}], | ||
} | ||
) | ||
await connection.response.create() | ||
async for event in connection: | ||
if event.type == "response.text.delta": | ||
print(event.delta, flush=True, end="") | ||
elif event.type == "response.text.done": | ||
print() | ||
elif event.type == "response.done": | ||
break | ||
|
||
await credential.close() | ||
|
||
|
||
asyncio.run(main()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignoring typing because Azure doesn't require the model to be updated. It doesn't seem like OpenAI requires it either - is this a spec bug?
https://platform.openai.com/docs/api-reference/realtime-client-events/session
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @kwhinnery-openai