Azure Search OpenAI Demo

repository·main·Indexed 27 days ago

https://github.com/azure-samples/azure-search-openai-demo

A full-stack Retrieval Augmented Generation (RAG) chat application featuring a Python backend and React frontend. It enables users to chat with their own documents using Azure OpenAI Service for reasoning and Azure AI Search for indexing and retrieval. Key features include multi-turn chat with citations, support for various document formats, multimodal reasoning, speech input/output, and Microsoft Entra-based authentication.

Tokens
34.6K
Snippets
84
Records
207
Agent score
93%

What's inside azure-search-openai-demo

  1. Overview of RAG chat app with Azure OpenAI and Azure AI Search

    main

    This project is a Retrieval Augmented Generation (RAG) chat application that provides a ChatGPT-like interface over your own documents. It uses Azure OpenAI Service (specifically GPT models) for reasoning and Azure AI Search for indexing and retrieving data.

    Key features include:

    • Multi-turn chat interface with citations and thought process rendering.
    • UI-based settings to tweak model behavior.
    • Support for various document formats and cloud data ingestion.
    • Optional multimodal reasoning (images), speech input/output, and Microsoft Entra-based user login/ACLs.
    • Performance monitoring via Application Insights.
  2. Baseline Evaluation Model Configuration

    main

    The baseline evaluation for this project uses a specific configuration of models and settings. As of June 2025, the default configuration is:

    • Chat completion: gpt-4.1-mini (model version 2025-04-14)
    • Embedding: text-embedding-3-large configured with:
      • Binary quantization
      • 1024 dimension reduction
      • Oversampling
    • Evaluation Model: gpt-4o (model version 2024-08-06)
  3. Understand the SentenceTextSplitter algorithm

    main

    The SentenceTextSplitter (located in ../app/backend/prepdocslib/textsplitter.py) is the primary engine for chunking text in the data ingestion pipeline. It is designed to create semantically coherent chunks for Azure AI Search by following these principles:

    • Sentence Alignment: Chunks align with sentence boundaries using punctuation (. ! ? and CJK equivalents).
    • Token & Character Limits: Enforces a hard limit of 500 tokens per chunk and a soft guideline of 1,000 characters (with a 20% overflow tolerance).
    • Atomic Figures: <figure>...</figure> blocks are treated as atomic units. They are never split and are always attached to preceding text if available.
    • Boundary Repair: Attempts to re-stitch sentences split by page boundaries.
    • Semantic Overlap: Appends a small prefix from the next chunk to the current one to improve retrieval recall.
    • Normalization: Performs minimal whitespace trimming to prevent trivial overflows without altering original formatting.
  4. Understand the RAG Chat Application Architecture

    main

    The application is a Retrieval Augmented Generation (RAG) system that provides a ChatGPT-like experience over user-provided documents. It uses a multi-layered architecture:

    • Frontend: A React/TypeScript application providing a Chat Interface, Settings Panel, and Citation Display.
    • Backend: A Python API (Flask/Quart) implementing the ChatReadRetrieveRead approach for multi-turn conversations.
    • Data Processing: A pipeline that handles document extraction (via Azure Document Intelligence), chunking, embedding generation (via Azure OpenAI), and indexing (via Azure AI Search).
    • Azure Services: Integrates Azure OpenAI (LLM/Embeddings), Azure AI Search (Vector/Semantic search), Azure Blob Storage (Document storage), and Azure Cosmos DB (Optional chat history).
  5. Compare with sample-app-aoai-chatGPT

    main

    The sample-app-aoai-chatGPT repository (https://github.com/Microsoft/sample-app-aoai-chatGPT/) is designed for users setting up via Azure OpenAI Studio and the Azure Portal.

    Key Differences

    • RAG Approach: This repository uses multiple RAG approaches that chain results from multiple API calls (Azure OpenAI and ACS) for greater flexibility. sample-app-aoai-chatGPT uses the built-in data sources option in the Azure OpenAI API.
    • Experimental Nature: This repository is more experimental as it is not tied to the Azure OpenAI Studio.

    Feature Comparison

    Featureazure-search-openai-demosample-app-aoai-chatGPT
    Vector support✅ Yes✅ Yes
    Data ingestion✅ Yes✅ Yes
    Persistent chat history✅ Yes✅ Yes
    User feedback❌ No✅ Yes
    GPT-4-vision✅ Yes❌ No
    Auth + ACL✅ Yes✅ Yes
    User upload✅ Yes❌ No
    Speech I/O✅ Yes❌ No

    Technology Comparison

    Techazure-search-openai-demosample-app-aoai-chatGPT
    FrontendReactReact
    BackendPython (Quart)Python (Quart)
    Vector DBAzure AI SearchAzure AI Search, CosmosDB Mongo vCore, ElasticSearch, Pinecone, AzureML
    DeploymentAzure Developer CLI (azd)Azure Portal, az, azd
  6. Compare with Azure Chat Solution Accelerator (azurechat)

    main

    The Azure Chat Solution Accelerator (https://github.com/microsoft/azurechat) is a popular alternative.

    Key Differences

    • Stack: azurechat uses a full JavaScript/TypeScript stack with a Node.js backend, whereas this repository uses Python (Quart).
    • Focus: azurechat emphasizes user personalization, while this repository focuses on enterprise scenarios like document-level Access Control Lists (ACLs) and evaluation.

    Feature Comparison

    Featureazure-search-openai-demoazurechat
    Vector support✅ Yes✅ Yes
    Data ingestion✅ Yes✅ Yes
    Persistent chat history✅ Yes✅ Yes
    Multimodal✅ Yes✅ Yes
    Voice/Speech I/O✅ Yes✅ Yes
    File upload✅ Yes✅ Yes
    Auth + ACL✅ Yes (Enterprise-focused)✅ Yes (Personal-focused)
    Access control✅ Yes (Document-level)❌ Limited

    Technology Comparison

    Techazure-search-openai-demoazurechat
    FrontendReact (TypeScript)React (TypeScript)
    BackendPython (Quart)Node.js (TypeScript)
    DatabaseAzure AI SearchAzure AI Search
    DeploymentAzure Developer CLI (azd)Azure Developer CLI (azd)
  7. Understand the RAG data ingestion pipeline

    main

    The data ingestion pipeline transforms raw documents into searchable content in Azure AI Search through three main stages:

    1. Document extraction: Extracts text, tables (converted to HTML), and figures (with bounding boxes) from source files. It uses Azure Document Intelligence for complex formats (PDF, HTML, DOCX, PPTX, XLSX, Images) or local parsers (PyPDF, BeautifulSoup, etc.) for simpler ones.
    2. Figure processing (Optional): If multimodal support is enabled, figures are cropped as PNGs, described using GPT-4 Vision or Azure AI Content Understanding, uploaded to Blob Storage, and optionally embedded.
    3. Text processing: Merges figure descriptions back into the text, splits content into semantic chunks (default ~1000 characters with 10% overlap), and computes vector embeddings using Azure OpenAI models.

    Finally, the chunks are indexed into Azure AI Search as individual documents.

  8. Implement document-level access control via Cloud Ingestion

    main

    The recommended way to add document-level access control is using Cloud Ingestion with Azure Data Lake Storage (ADLS) Gen2. This method uses Azure Functions to extract POSIX-style ACLs from files and index them into Azure AI Search.

    Workflow

    1. Store documents in an ADLS Gen2 account with hierarchical namespace enabled.
    2. Set ACLs on files/folders to control user/group access.
    3. An Azure AI Search indexer triggers Azure Functions (Document Extractor, Figure Processor, Text Processor) to parse documents and extract ACLs.
    4. Extracted User IDs (oids) and Group IDs (groups) are stored in the search index.

    Setup Instructions

    1. Enable cloud ingestion and ACLs in your environment:

    azd env set USE_CLOUD_INGESTION true
    azd env set USE_CLOUD_INGESTION_ACLS true
    azd env set AZURE_USE_AUTHENTICATION true
    azd env set AZURE_ENFORCE_ACCESS_CONTROL true

    2. Deploy the application:

    azd up

    3. Upload documents with ACLs: You can use the provided script to upload sample data with ACLs:

    python scripts/adlsgen2setup.py './data/*' --data-access-control './scripts/sampleacls.json' -v

    4. Trigger ingestion:

    ./scripts/setup_cloud_ingestion.sh
    azd env set USE_CLOUD_INGESTION true
    azd env set USE_CLOUD_INGESTION_ACLS true
    azd env set AZURE_USE_AUTHENTICATION true
    azd env set AZURE_ENFORCE_ACCESS_CONTROL true
    
    azd up
    
    python scripts/adlsgen2setup.py './data/*' --data-access-control './scripts/sampleacls.json' -v
    
    ./scripts/setup_cloud_ingestion.sh
  9. Run the development server locally

    main

    You can run a local development server only after a successful azd up deployment.

    1. Ensure you are logged in: azd auth login.
    2. Start the server using the platform-specific script:
      • Windows: ./app/start.ps1
      • Linux/Mac: ./app/start.sh
      • VS Code: Run the "VS Code Task: Start App" task.

    Local application access: http://127.0.0.1:50505

  10. Customize the UI text and localization

    main
    The frontend uses React and Fluent UI. To modify UI elements such as the page title, header text, or example questions, edit the translation files located in app/frontend/src/locales/. The application supports multiple languages; English is the default. Edit the specific JSON file for the target language (e.g., en/translation.json).
  11. Enable CORS for an alternate frontend

    main

    To host a frontend on a different origin than the deployed Azure web app, you must configure CORS.

    1. Set the allowed origin: azd env set ALLOWED_ORIGIN https://<your-domain.com>
    2. Run azd up to apply changes.
    3. In your frontend code, update BACKEND_URI in api.ts to point to the deployed backend URL.
    azd env set ALLOWED_ORIGIN https://<your-domain.com>
    azd up