From 0e5111fdb225161851fa28bcf6e063b5e297a69b Mon Sep 17 00:00:00 2001 From: Mukhsin0508 Date: Wed, 21 May 2025 04:24:48 +0500 Subject: [PATCH 1/2] Fix Azure authentication in RTClient examples --- .gitignore | 3 +++ samples/middle-tier/python-fastapi/rt-middle-tier/main.py | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index e2dab94..338c2f0 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,9 @@ # Mono auto generated files mono_crash.* +# Pycharm +.idea + # Build results [Dd]ebug/ [Dd]ebugPublic/ diff --git a/samples/middle-tier/python-fastapi/rt-middle-tier/main.py b/samples/middle-tier/python-fastapi/rt-middle-tier/main.py index b1db119..45334b3 100644 --- a/samples/middle-tier/python-fastapi/rt-middle-tier/main.py +++ b/samples/middle-tier/python-fastapi/rt-middle-tier/main.py @@ -73,12 +73,12 @@ def _initialize_client(self, backend: str | None): if backend == "azure": return RTClient( url=os.getenv("AZURE_OPENAI_ENDPOINT"), - token_credential=DefaultAzureCredential(), - deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT"), + key_credential=AzureKeyCredential(os.getenv("AZURE_OPENAI_API_KEY")), + azure_deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT"), ) return RTClient( key_credential=AzureKeyCredential(os.getenv("OPENAI_API_KEY")), - model=os.getenv("OPENAI_MODEL"), + model=os.getenv("OPENAI_MODEL", "gpt-4o-realtime-preview"), ) async def send(self, message: WSMessage): From 0a6ec59720be97f96fdb5d3aca7c3fc2261b3881 Mon Sep 17 00:00:00 2001 From: Mukhsin0508 Date: Wed, 21 May 2025 04:35:23 +0500 Subject: [PATCH 2/2] Update for AzureKeyCredential --- samples/middle-tier/python-fastapi/README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/samples/middle-tier/python-fastapi/README.md b/samples/middle-tier/python-fastapi/README.md index 6d830ae..c7b8b4b 100644 --- a/samples/middle-tier/python-fastapi/README.md +++ b/samples/middle-tier/python-fastapi/README.md @@ -11,7 +11,7 @@ The service establishes a WebSocket server that communicates with clients using - **Simplified Protocol**: Uses a custom, lightweight communication protocol. - **Backend Support**: Works with both Azure OpenAI and OpenAI Realtime APIs. - **Extendable**: Easily extend the protocol to cover additional functionalities. -- **Secure Authentication**: For Azure, utilizes token credentials through `DefaultAzureCredential`. +- **Secure Authentication**: For Azure, uses API key authentication through `AzureKeyCredential`. - **Async Implementation**: Leverages FastAPI's async capabilities for efficient WebSocket handling. - **Type Safety**: Utilizes Python type hints throughout the codebase. @@ -27,14 +27,15 @@ Set the following environment variables in a `.env` file at the root of the proj ### Using Azure OpenAI Backend - `AZURE_OPENAI_ENDPOINT`: Your Azure OpenAI endpoint URL. +- `AZURE_OPENAI_API_KEY`: Your Azure OpenAI API key. - `AZURE_OPENAI_DEPLOYMENT`: The name of your Azure OpenAI deployment. -Authentication is handled via `DefaultAzureCredential`, supporting environment-based credentials, managed identities, or Azure CLI authentication. +Authentication is done via API key authentication using `AzureKeyCredential`. This provides a more reliable and straightforward approach. ### Using OpenAI Realtime API Backend - `OPENAI_API_KEY`: Your OpenAI API key. -- `OPENAI_MODEL`: The model to use (e.g., `gpt-3.5-turbo`). +- `OPENAI_MODEL`: The model to use (e.g., `gpt-4o-realtime-preview`). ## Setup and Run @@ -113,6 +114,6 @@ class ControlMessage(TypedDict): ## Notes - Ensure that the required environment variables are set correctly for your chosen backend. -- For Azure backend, authentication relies on DefaultAzureCredential, so configure your environment for token-based authentication. +- For Azure backend, authentication uses the API key method with `AzureKeyCredential`. - Logging is configured using Loguru and can be adjusted through its configuration. - The server implements CORS middleware with permissive settings for development. Adjust these settings for production use. \ No newline at end of file