Overview

This exercise demonstrates how to implement Retrieval Augmented Generation (RAG) using Azure OpenAI Service and Azure AI Search. The goal is to enhance AI-generated responses by grounding them in custom data sources.


Steps & Configuration Details

1. Provision Azure Resources

To complete this exercise, you need:

  • Azure OpenAI resource
  • Azure AI Search resource
  • Azure Storage Account resource

Configuration Items:

  • Azure OpenAI Resource:

    • Subscription: Select an approved Azure subscription.
    • Resource Group: Choose or create a resource group.
    • Region: Choose from:
      • East US
      • East US 2
      • North Central US
      • South Central US
      • Sweden Central
      • West US
      • West US 3
    • Name: A unique name.
    • Pricing Tier: Standard S0
  • Azure AI Search Resource:

    • Service Name: A unique name.
    • Location: Same as OpenAI resource.
    • Pricing Tier: Basic
  • Azure Storage Account Resource:

    • Storage Account Name: A unique name.
    • Region: Same as OpenAI resource.
    • Primary Service: Azure Blob Storage or Azure Data Lake Storage Gen 2
    • Performance: Standard
    • Redundancy: Locally Redundant Storage (LRS)

After provisioning, gather:

  • Azure OpenAI Endpoint & Key
  • Azure AI Search Endpoint & Admin Key

2. Upload Your Data

  • Download the dataset:
    https://aka.ms/own-data-brochures
    
  • Extract the brochures and upload them to Azure Blob Storage:
    • Create a Blob Container named margies-travel.
    • Upload .pdf brochures to the root folder.

3. Deploy AI Models

Two models are required:

  1. Text Embedding Model → Converts text into vector format.
  2. GPT Model → Generates responses based on indexed data.

Deployment Commands (Azure Cloud Shell - Bash):

  • Text Embedding Model:
    az cognitiveservices account deployment create \
      -g <your_resource_group> \
      -n <your_OpenAI_resource> \
      --deployment-name text-embedding-ada-002 \
      --model-name text-embedding-ada-002 \
      --model-version "2" \
      --model-format OpenAI \
      --sku-name "Standard" \
      --sku-capacity 5
    
  • GPT Model:
    az cognitiveservices account deployment create \
      -g <your_resource_group> \
      -n <your_OpenAI_resource> \
      --deployment-name gpt-4o \
      --model-name gpt-4o \
      --model-version "2024-05-13" \
      --model-format OpenAI \
      --sku-name "Standard" \
      --sku-capacity 5
    

  • Navigate to Azure AI SearchImport and Vectorize Data.
  • Configuration Items:
    • Data Source: Azure Blob Storage
    • Blob Container: margies-travel
    • Vectorization Model: text-embedding-ada-002
    • Authentication Type: API Key
    • Index Name: margies-index
    • Enable Semantic Ranking: Yes
    • Schedule Indexer: Run Once

5. Configure Your Application

  • Clone the repository:
    git clone https://github.com/MicrosoftLearning/mslearn-openai
    
  • Open the folder in Visual Studio Code.
  • Install dependencies:
    • C#:
      dotnet add package Azure.AI.OpenAI --version 2.1.0
      dotnet add package Azure.Search.Documents --version 11.6.0
      
    • Python:
      pip install openai==1.65.2
      
  • Open the configuration file:
    • C#: appsettings.json
    • Python: .env
  • Update Configuration Values:
    • Azure OpenAI Endpoint
    • API Key
    • Deployment Name (gpt-4o)
    • Azure AI Search Endpoint
    • Search API Key
    • Search Index Name (margies-index)
  • Save the configuration file.

6. Add Code to Use Azure OpenAI

  • Open the code file:
    • C#: ownData.cs
    • Python: ownData.py
  • Configure the Azure OpenAI client:
    • C#:
      ChatCompletionOptions chatCompletionsOptions = new ChatCompletionOptions() {
          MaxOutputTokenCount = 600,
          Temperature = 0.9f,
      };
      chatCompletionsOptions.AddDataSource(
          new AzureSearchChatDataSource() {
              Endpoint = new Uri(azureSearchEndpoint),
              IndexName = azureSearchIndex,
              Authentication = DataSourceAuthentication.FromApiKey(azureSearchKey),
          }
      );
      
    • Python:
      text = input('\\nEnter a question:\\n')
      completion = client.chat.completions.create(
          model=deployment,
          messages=[{"role": "user", "content": text}],
          extra_body={
              "data_sources": [
                  {
                      "type": "azure_search",
                      "parameters": {
                          "endpoint": os.environ["AZURE_SEARCH_ENDPOINT"],
                          "index_name": os.environ["AZURE_SEARCH_INDEX"],
                          "authentication": {
                              "type": "api_key",
                              "key": os.environ["AZURE_SEARCH_KEY"],
                          }
                      }
                  }
              ]
          }
      )
      

7. Run Your Application

  • Open Visual Studio Code.
  • Run the application:
    • C#:
      dotnet run
      
    • Python:
      python ownData.py
      
  • Example prompt:
    Tell me about London.
    
  • The response should include AI-generated text grounded in indexed data.

8. Clean Up

  • Delete Azure resources to avoid unnecessary costs:

Related Posts