You’ve read a lot about how Azure AI Search performs hybrid searches — now it’s time to see it in action! In this demo, you’ll build an app that uses Azure AI Search to perform a hybrid search on its data.
Open the starter project for this lesson in Visual Studio Code. First, install some dependencies with pip. Run the first cell to install them.
In the next code block, you’ll set the API endpoint locations, keys, and other variables for your app, like the embedding model and the name of your index. These will be the same details you provided in the last demo of the previous lesson, so you can copy them from there. Fill in these values under # TODO: Configure your app and run the cell to continue.
Client Creation
The next cell creates an AzureOpenAI client based on a bearer token obtained from the Default Azure Credential. This client is then used to generate embeddings for the titles and content within the television show information text file. Finally, it saves these embeddings to a file. Run this cell to generate the embeddings.
Search Index Clent
In the next cell, you create a SearchIndexClient and set the field types. Title is a searchable field data type, as are content and category. Next are the title and content vectors. This is your access point to Azure AI Search when you need to perform a search. Run this cell to create the search index client.
Under “Configure the Vector Search,” you’ll now set up your vector search. Uncomment the code under # TODO: Configure the vector search. Here, you create a vectorizer, an object that specifies your embedding model, its name, and the Azure OpenAI service credentials where these models can be found. It’s aptly named “myVectorizer.”
Then, choose the HNSW algorithm for your vector search, naming it myHnsw. Finally, create a profile that contains both configurations and name it myHnswProfile. This is enough to configure your vector search. Execute this cell to create the vector search instance.
Semantic Search
In the cell below “Configure the Semantic Search,” create a semantic configuration object from the selected fields in your data source. Then, use it to create an instance of a semantic search. Run this cell to complete the task.
With the vector and semantic search in place, move to the next cell to create a search index. Uncomment the code beneath the # TODO: Create a search index comment and execute the cell. This will create an index for your data and associate it with the kinds of searches that can be performed on it. Execute the cell to create an index using a name, the fields, the vector search, and the semantic search. In the output, you’ll see “vectest created” when everything goes well.
Push your television data for your index on Azure AI Search in the next cell. You’ll do this by uploading the embedded data you saved to docVectors.json previously. Execute the cell below “Upload to Service” to complete this action.
Finally, you’re ready to do a hybrid search!
First, you’ll try tweaking the weight for your query. To better understand its impact, run a normal vector search with the query “funny entertainment.” This is more of a keyword query, as it’s made up mostly of words that seek to precisely match the indexed data.
Remember that, by default, the weight is a factor of 1 — meaning no bias on the score. Under “Vector Search” in the next cell, uncomment the code below # TODO Perform a hybrid search and execute the cell. Monitor the scores for the results.
The first result here is “Parks and Recreation,” with a score of 0.01667 when rounded up. To apply a weight, move to the next cell and specify weight to be 0.2 when creating an instance of your VectorizableTextQuery. Run this cell and monitor the results.
As you can see, “Parks and Recreation” is still first on the list, but with a lower score than before — exactly 0.2 times less than before.
Move to the next cell to perform a semantic hybrid search. Start by uncommenting the code below # TODO: Perform a weighted vector search.
To perform the hybrid search, specify the query type as semantic, while providing the semantic configuration you created earlier. First, uncomment the code below # TODO: Perform a semantic hybrid search. Update the question to exclude keywords. Instead, use something that’s more unstructured, like a typical chat message, such as “what is friends?”. Given that your indexed television show data has information about a series called “Friends,” you should get relevant results for your query.
This is the power of semantic search — your query now comprises keyword and semantic searches! Run the code and observe the results.
Observing the output, you’ll find that it accurately returned relevant results, with a score as high as 0.96. As always, the last cell allows you to remove the index you created. Execute it to delete the index.
With minor configurations, you’ve now run a hybrid search and a weighted vector search.
That’s all for this demo! Continue to the final segment.