RAG with Azure AI Search

Nov 15 2024 · Python 3.12, Microsoft Azure, JupyterLab

Lesson 02: Vector Search in Azure AI Search

Demo 02

Episode complete

Play next episode

Next
Transcript

In this demo, you’ll create an app, embed and index textual data, and search through it using vector search.

Project Setup

Open the starter project in Visual Studio Code to get started.

Execute the first cell to install the listed Python modules.

Under Set API Keys, provide the same data for the Azure Search service URL, Azure OpenAI service URL, and Azure OpenAI service keys, as you did in Lesson 1. Having already created a text embedding model and acquired the Azure OpenAI key, provide them in their respective variables. Execute this cell to populate the variables and load them into your current session.

Data Preparation

In the next cell, under Generate Embeddings, you initialize an Azure OpenAI instance with your credentials — this will enable you to manage your Azure OpenAI resource from your app.

In the code that follows, import television data from the television-data.json file in your starter project. A quick look at this file reveals that it’s simply an array of movies, series, and shows with ID, title, content, and category attributes.

You then create a slice (or a collection) of the titles and content only, storing them in titles and content variables.

Embedding Creation

Your data is now ready for embedding! This is a necessary step to vectorize your data prior to indexing.

To create embeddings, use the client.embeddings.create API from the AzureOpenAI object you created earlier. Uncomment the code under TODO: Create embeddings. This code will create embeddings for the titles and content you created earlier.

Finally, in this cell, you’ll store these embeddings in a JSON file named docVectors.json, so you can see how they look. Execute this cell.

After it’s done, look within your project directory for the docVectors.json file. You can see how the vectorization is represented — mainly a multidimensional array of numbers.

Search Client Configuration

Now, you can go ahead and create a search client. This is what you’ll use to perform your search on Azure AI Search. You’ll create one by initializing an instance of SearchIndexClient. In the next cell, under Setup Fields, that’s exactly what your app is doing. To initialize this client, you provide the fields you want your client to search. Execute this cell to create the search client.

Vector Search Setup

It’s now time to configure a vector search! To do this, you’ll create an instance of VectorSearch. In the next cell, you’re configuring a vector search to use the Hierarchical Navigable Small World (HNSW) algorithm. The name argument is simply a name you’ll use to identify your algorithm.

You then provide a vector search profile with a name, while specifying your algorithm for the vector search. You can use this profile to configure things, like the distance metric, nearest neighbor algorithm, a compression method, and other parameters that tune the relevance and accuracy of your vector search. In this instance, you’re only specifying a name for your profile, the algorithms you’re using, and the vectorizer. You’re using your Azure OpenAI resource as your vectorizer. Run this cell to create the vector search instance.

Semantic Search Configuration

To enhance your search even further, you’ll configure a semantic search in the next cell — this will give you the opportunity to specify specific fields to apply semantic search to your data. Uncomment the code under # TODO: Configure semantic search in the next cell, and execute it to create a configuration for the semantic search. Name it my-semantic-config and specify the title, category, and content fields as the prioritized fields to search.

Indexing and Querying

With all configurations properly set up, you’re ready to index your data. You’ve named your index vectest. Uncomment the code in the next cell, execute it, and wait for your index to be created. Check the output as it displays “vectest created” — this shows that your index was created successfully.

At this point, you’ve embedded your data, configured a vector search, and indexed it with the name vectest, but you haven’t used your embedded data yet. So, in the cell under Upload to service, you’ll read the embedded data into memory and upload it into the index you created earlier. Run this cell to complete the upload process. When it’s successful, you’ll see “Uploaded 50 documents” in the output for the cell.

And now, the moment of truth! :]

In the cell that follows, you’ll execute a query on the index. Here is where you put all the major pieces together — your query is simply “intergalactic”. (You can change it to whatever you want, but keep an eye out for your quota as you execute vector searches.)

First, embed your query, so you can use it to retrieve data from your index. The data you’re requesting is embedded, so you need to have your query in the same format, preferably with the same embedding model.

Then, use a familiar API, client.embeddings.create, to embed the query, and proceed to perform the search by using the search API from the search client you created earlier.

You can then store the results in results and print its content to the output.

Ready? Execute this cell and monitor the output. The returned documents in the results have a score attached to give you a good idea of their relevance to your query.

The score ranges from 0 to 1, with 1 indicating the highest relevance to the search query.

Cleanup

You can try out a few more queries if you want, but don’t forget to clean up after yourself when you’re done! That’s precisely what the last cell does — it deletes your index to preserve resources and avoid incurring unnecessary costs.

That’s it for this demo. Continue to the concluding segment for this lesson.

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 02 Next: Conclusion