Share via

Quickstart in JavaScript

Quickstart sample MIT license badge

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

Set up the sample

  1. Clone or download this sample repository.

  2. Open the folder in Visual Studio Code and navigate to the samples/cookbook/bring-your-own-data folder:

    cd samples/cookbook/bringYourOwnData
    
  3. Install the dependencies using npm:

    npm install
    
  4. Edit the file sample.env, adding the connection information that's valid for your Azure Cognitive Search service. See

    AZURE_SEARCH_ENDPOINT=https://<search-service-name>.search.windows.net
    AZURE_OPENAI_ENDPOINT=<your_azure_openai_endpoint>
    AZURE_OPENAI_DEPLOYMENT_ID=<name_of_chat_deployment>
    
  5. Rename sample.env to just .env. The quickstart will read the .env file automatically.

Create the Search Index and Load the sample data

  1. 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.