Quickstart in JavaScript
Demonstrates using JavaScript to use the Azure Search SDK for JavaScript/TypeScript to create an Azure Cognitive Search index, load it with documents, and then integrate that search resource with the OpenAI SDK for JavaScript. The index is modeled on a subset of the Hotels dataset, reduced for readability and comprehension. Index definition and documents are included in the code.
Prerequisites
- Node.js.
- NPM should be installed by Node.js.
- Create a search service in the portal or find an existing service under your current subscription. You can use a free service for this quickstart.
- Create and deploy an Azure OpenAI Service resource under your current subscription. You can use a free service for this quickstart.
Set up the sample
Clone or download this sample repository.
Open the folder in Visual Studio Code and navigate to the samples/cookbook/bring-your-own-data folder:
cd samples/cookbook/bringYourOwnData
Install the dependencies using
npm
:npm install
Edit the file
sample.env
, adding the connection information that's valid for your Azure Cognitive Search service. SeeAZURE_SEARCH_ENDPOINT=https://<search-service-name>.search.windows.net AZURE_OPENAI_ENDPOINT=<your_azure_openai_endpoint> AZURE_OPENAI_DEPLOYMENT_ID=<name_of_chat_deployment>
Rename
sample.env
to just.env
. The quickstart will read the.env
file automatically.
Create the Search Index and Load the sample data
Run the following command to create the search index on your azure search resource, load data using that index, and then run an example question with ChatGPT against that search index.
node index.js
You should see a series of messages relating to the creation of the search index, adding documents to it, and, finally, results of a sample ChatGPT question.
If you get a 401 error, make sure the search service is configured for Role-based access control.
Key concepts
The file hotels_quickstart_index.json holds the definition of an index for the data in the file hotels.json. Review those files to see the fields, which ones are searchable, etc.
The file index.js automatically reads the .env file which contains the AZURE_SEARCH_ENDPOINT
needed to create the SearchIndexClient
.
The main
function :
- Checks if the
hotels-quickstart
index exists. - If so, the program deletes the existing index.
- Creates a new
hotels-quickstart
index from the structure in hotels_quickstart_index.json. - Adds the data from hotels.json to the
hotels-quickstart
index. - Sends a question to Chat GPT regarding the loaded search data.
Next steps
You can learn more about Azure OpenAI on the official documentation site.
You can view additional samples for JavaScript/TypeScript in the azure-sdk-for-js repo or see the documentation.