Share via

TypeScript quickstart for RAG

Quickstart sample MIT license badge

Demonstrates using TypeScript and the Azure SDK for JavaScript/TypeScript to query an Azure AI Search index and send the results to a chat completion model that provides an answer.

This Node.js console application is featured in Quickstart: Generative search (RAG) using grounding data from Azure AI Search. This sample uses the Azure SDK for JavaScript/TypeScript and runs on a search service using connection information that you provide.

Prerequisites

Set up permissions

We recommend reviewing access configuration for detailed instructions. Otherwise, if you're familiar with role-based access, you must be a member of these roles on Azure AI Search:

  • Search Service Contributor
  • Search Index Data Contributor
  • Search Index Data Reader

On Azure OpenAI, you must be a member of Cognitive Services OpenAI User.

Set up the sample

  1. Clone or download this sample repository.

  2. Open the folder in Visual Studio Code and open a terminal window for command line operations.

  3. Navigate to the quickstart folder:

    cd quickstart-rag-ts
    
  4. Create a new package for ESM modules in your project directory. You should have package-lock.json and package.json when you complete this step.

    npm init -y
    npm pkg set type=module
    
  5. Edit the file sample.env, adding the connection information that's valid for your Azure resources, and then saving is as .env.

     AZURE_SEARCH_ENDPOINT=YOUR-SEARCH-SERVICE-ENDPOINT-GOES-HERE
     AZURE_SEARCH_INDEX_NAME=hotels-sample-index
    
     AZURE_OPENAI_ENDPOINT=YOUR-AZURE-OPENAI-ENDPOINT-GOES-HERE
     AZURE_OPENAI_VERSION=2024-04-01-preview
     AZURE_DEPLOYMENT_MODEL=gpt-4o-mini
    
  6. Install the packages used in this sample. You should see a node_modules folder when you complete this step.

    npm install @azure/identity @azure/search-documents openai dotenv
    

Run the sample

  1. Open the quickstart-rag-ts folder and find the tsconfig.json file. This is the configuration file that specifies the source files.

  2. Build the TypeScript code to JavaScript. Both .ts files in src now have .js equivalents in the dist folder.

    tsc
    
  3. Open the dist folder and notice the query.js file. This is the source code for this sample, compiled from TypeScript.

  4. Run the code from the root folder. The .env is passed into the runtime using the -r dotenv/config.

    node -r dotenv/config dist/query.js
    
  5. You should get output consisting of recommendations for several hotels. This output is generated by the chat completion model, which grounds its answer using the search results from the query.

  6. A second JavaScript file has a more complex prompt that adds more structure to the output. The extra information includes a hotel address, description, tags, and hotel room pricing.

    node -r dotenv/config dist/queryComplex.js
    

Next steps

You can learn more about Azure AI Search on the official documentation site.

You can view additional samples for JavaScript/TypeScript in the azure-sdk-for-js repo or see the documentation.