TypeScript quickstart for RAG
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
Azure AI Search, Basic pricing tier or higher, with the hotels-sample-index. The sample index must have a default semantic configuration.
Azure OpenAI with a deployment of a chat completion model, such as GPT-4.1 or GPT-4.1-mini.
Visual Studio Code or another IDE.
TypeScript. We recommend a global installation:
npm install -g typescript
. You might also neednpm install @types/node
to support type definitions.
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
Clone or download this sample repository.
Open the folder in Visual Studio Code and open a terminal window for command line operations.
Navigate to the quickstart folder:
cd quickstart-rag-ts
Create a new package for ESM modules in your project directory. You should have
package-lock.json
andpackage.json
when you complete this step.npm init -y npm pkg set type=module
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
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
Open the quickstart-rag-ts folder and find the
tsconfig.json
file. This is the configuration file that specifies the source files.Build the TypeScript code to JavaScript. Both
.ts
files insrc
now have.js
equivalents in thedist
folder.tsc
Open the
dist
folder and notice thequery.js
file. This is the source code for this sample, compiled from TypeScript.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
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.
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.