VoiceRAG Reference Implementation

repository·main·Indexed 20 days ago

https://github.com/azure-samples/aisearch-openai-rag-audio

A reference implementation for building voice-driven applications using Retrieval Augmented Generation (RAG). VoiceRAG combines the GPT-4o Realtime API for low-latency audio interaction with Azure AI Search for knowledge retrieval, featuring a Python backend and a frontend that captures browser microphone input and plays generated audio responses.

Tokens
10.1K
Snippets
29
Records
43
Agent score
69%

What's inside VoiceRAG

  1. Overview of VoiceRAG pattern

    main

    VoiceRAG is an application pattern for implementing Retrieval Augmented Generation (RAG) with a voice user interface. It uses the GPT-4o Realtime API for audio to process voice input and generate audio responses, while leveraging Azure AI Search to retrieve relevant documents from a knowledge base to ground the model's responses.

    Key features include:

    • Voice interface: Captures browser microphone input for processing by Azure OpenAI.
    • RAG: Uses Azure AI Search to provide context to the GPT-4o Realtime API.
    • Audio output: Plays generated responses via browser audio capabilities.
    • Citations: Displays the search results used to generate the response.
  2. How the VoiceRAG architecture works

    main

    The VoiceRAG pattern follows a specific flow between the frontend and backend:

    1. The frontend RTClient receives audio input from the user's microphone.
    2. The audio is sent to a Python backend.
    3. The backend uses an RTMiddleTier object to interface with the Azure OpenAI real-time API.
    4. The RTMiddleTier includes a tool for searching Azure AI Search to perform RAG.

    This architecture can be deployed to Azure Container Apps using the provided Infrastructure as Code (IaC) and Dockerfile, or run locally if Azure AI Search and Azure OpenAI services are configured.

  3. Cost management and security guidance

    main

    Cost Management

    Resources created by this project incur costs. To avoid unnecessary spending:

    • Use azd down to tear down resources when not in use.
    • Consider switching to free SKUs for services where possible (though this introduces limitations).
    • Monitor usage for Azure OpenAI (per 1K tokens), Azure AI Search (per hour), and Azure Container Apps (Consumption plan).

    Security

    • The template uses Managed Identity to eliminate the need for managing credentials in deployed environments.
    • For local development, you can use Entra ID by omitting API keys in the .env file.
    • It is recommended to enable GitHub secret scanning in your repositories to prevent credential leaks.
  4. Use an existing Azure AI Search index

    main

    You can use an existing Azure AI Search index directly.

    • If the index was created using the Import and vectorize data option in the Azure Portal, no code changes are required.
    • If the index was created manually, you must update the field names in app/backend/ragtools.py to match your specific text and vector field names.
  5. Update local environment for voice preview

    main

    To sync your azd environment changes to your local .env file (for example, to test a new voice choice with the local development server), run the appropriate script for your shell:

    • Bash: ./scripts/write_env.sh
    • PowerShell: pwsh ./scripts/write_env.ps1

    After running the script, restart your development server to pick up the changes.

    ./scripts/write_env.sh
    # OR
    pwsh ./scripts/write_env.ps1
  6. Run the development server locally

    main

    You can run the application locally by pointing it at Azure services (provisioned via azd up) or existing services.

    1. Configure Environment Variables

    If you did not use azd up, you must create an app/backend/.env file with the following variables:

    AZURE_OPENAI_ENDPOINT=wss://<your instance name>.openai.azure.com
    AZURE_OPENAI_REALTIME_DEPLOYMENT=gpt-4o-realtime-preview
    AZURE_OPENAI_REALTIME_VOICE_CHOICE=<choose one: echo, alloy, shimmer>
    AZURE_OPENAI_API_KEY=<your api key>
    AZURE_SEARCH_ENDPOINT=https://<your service name>.search.windows.net
    AZURE_SEARCH_INDEX=<your index name>
    AZURE_SEARCH_API_KEY=<your api key>

    Note: To use Entra ID (your local user) instead of API keys, simply omit the _KEY variables.

    2. Start the App

    Windows (PowerShell):

    pwsh .\scripts\start.ps1

    Linux/Mac:

    ./scripts/start.sh

    3. Access the App

    Once running, the app is available at http://localhost:8765.

    # Example .env configuration
    AZURE_OPENAI_ENDPOINT=wss://<your instance name>.openai.azure.com
    AZURE_OPENAI_REALTIME_DEPLOYMENT=gpt-4o-realtime-preview
    AZURE_OPENAI_REALTIME_VOICE_CHOICE=alloy
    AZURE_OPENAI_API_KEY=<your api key>
    AZURE_SEARCH_ENDPOINT=https://<your service name>.search.windows.net
    AZURE_SEARCH_INDEX=<your index name>
    AZURE_SEARCH_API_KEY=<your api key>
  7. Customize Azure OpenAI deployment version and capacity

    main

    You can modify the Azure OpenAI deployment settings using azd env set. After setting these values, you must run azd up to apply the changes to the Azure resources.

    Deployment Version

    Set AZURE_OPENAI_REALTIME_DEPLOYMENT_VERSION to specify a different API version (e.g., 2024-12-17).

    Deployment Capacity

    Set AZURE_OPENAI_REALTIME_DEPLOYMENT_CAPACITY to specify the capacity. The default is 1.

    # Set deployment version
    azd env set AZURE_OPENAI_REALTIME_DEPLOYMENT_VERSION 2024-12-17
    
    # Set deployment capacity
    azd env set AZURE_OPENAI_REALTIME_DEPLOYMENT_CAPACITY 2
    
    # Apply changes
    azd up
  8. Configure local development environment with .env

    main

    To test VoiceRAG locally using an existing azure-search-openai-demo index, create a .env file in the app/backend directory. This file allows the local development server to connect to your existing Azure services without using azd environment variables.

    # Create app/backend/.env with the following structure:
    AZURE_TENANT_ID=<YOUR-TENANT-ID>
    AZURE_OPENAI_ENDPOINT=https://<YOUR_OPENAI_ENDPOINT>.openai.azure.com
    AZURE_OPENAI_REALTIME_DEPLOYMENT=gpt-4o-realtime-preview
    AZURE_OPENAI_REALTIME_VOICE_CHOICE=<choose one: echo, alloy, shimmer>
    AZURE_SEARCH_ENDPOINT=https://<YOUR_SEARCH_SERVICE>.search.windows.net
    AZURE_SEARCH_INDEX=gptkbindex
    AZURE_SEARCH_SEMANTIC_CONFIGURATION=default
    AZURE_SEARCH_IDENTIFIER_FIELD=id
    AZURE_SEARCH_CONTENT_FIELD=content
    AZURE_SEARCH_TITLE_FIELD=sourcepage
    AZURE_SEARCH_EMBEDDING_FIELD=embedding
  9. Reuse an existing Azure AI Search index from azure-search-openai-demo

    main

    If you are already using the azure-search-openai-demo solution, you can connect VoiceRAG to your existing Azure AI Search service and index. Run these commands before running azd up to configure the infrastructure and application settings.

    Note on Vector Search: If your search index has a built-in vectorizer (available in azure-search-openai-demo releases from October 17, 2024, onwards), you do not need to manually disable vector search. Otherwise, you may need to set AZURE_SEARCH_USE_VECTOR_QUERY to false.

    # 1. Prevent creation of a new search service
    azd env set AZURE_SEARCH_REUSE_EXISTING true
    
    # 2. Assign RBAC roles for the existing resource
    azd env set AZURE_SEARCH_SERVICE_RESOURCE_GROUP <YOUR_RESOURCE_GROUP>
    
    # 3. Point app code to the search service endpoint
    azd env set AZURE_SEARCH_ENDPOINT https://<YOUR_SEARCH_SERVICE>.search.windows.net
    
    # 4. Configure index and field mappings
    azd env set AZURE_SEARCH_SEMANTIC_CONFIGURATION default
    azd env set AZURE_SEARCH_IDENTIFIER_FIELD id
    azd env set AZURE_SEARCH_CONTENT_FIELD content
    azd env set AZURE_SEARCH_TITLE_FIELD sourcepage
    azd env set AZURE_SEARCH_EMBEDDING_FIELD embedding
    azd env set AZURE_SEARCH_REUSE_EXISTING true
    azd env set AZURE_SEARCH_INDEX gptkbindex
    
    # 5. (Optional) Disable vector search if no built-in vectorizer exists
    azd env set AZURE_SEARCH_USE_VECTOR_QUERY false
  10. Required Azure Services for VoiceRAG

    main

    To run the VoiceRAG application, you must provision the following Azure services:

    1. Azure OpenAI: Requires two model deployments:
      • One deployment of the gpt-4o-realtime-preview model.
      • One deployment for embeddings (e.g., text-embedding-3-large, text-embedding-3-small, or text-embedding-ada-002).
    2. Azure AI Search: Any tier from Basic or above is required. It is recommended to have Semantic Search enabled.
    3. Azure Blob Storage: A container containing the content that serves as your knowledge base (e.g., PDFs, Office documents).
  11. Customize the real-time voice choice

    main

    You can change the voice used by the real-time deployment by setting the AZURE_OPENAI_REALTIME_VOICE_CHOICE environment variable.

    Available options are:

    • alloy (default)
    • echo
    • shimmer

    To apply changes to the deployed app, run azd up after setting the variable.

    If you want to preview the voice locally using the development server before deploying, update your local .env file by running the provided script, then restart the development server.

    azd env set AZURE_OPENAI_REALTIME_VOICE_CHOICE <echo, alloy, or shimmer>
  12. Deploy the application to Azure using azd

    main

    Use the Azure Developer CLI (azd) to provision Azure resources and deploy the application to Azure Container Apps. This process includes setting up integrated vectorization for sample data.

    Deployment Steps

    1. Login to Azure:

      azd auth login

      Note: If using GitHub Codespaces and the above fails, use azd auth login --use-device-code.

    2. Create an environment:

      azd env new

      Enter a name for the resource group. This creates a new folder in .azure and sets it as the active environment.

    3. (Optional) Customize deployment: Set azd environment variables to use existing services or customize the voice choice before proceeding.

    4. Provision and Deploy:

      azd up
      • Warning: This command incurs immediate costs, primarily from Azure AI Search. If interrupted, resources may still accrue costs. Use azd down to tear down resources.
      • You will be prompted to select two locations: one for general resources and one for the OpenAI resource (based on model availability).
    5. Access the app: Once complete, a URL will be printed to the console. Navigate to this URL to interact with the app.

    azd auth login
    azd env new
    azd up