Azure Cognitive Search
) is a Microsoft cloud search service that gives developers infrastructure, APIs, and tools for information retrieval of vector, keyword, and hybrid queries at scale.
AzureAISearchRetriever
is an integration module that returns documents from an unstructured query. It’s based on the BaseRetriever class and it targets the 2023-11-01 stable REST API version of Azure AI Search, which means it supports vector indexing and queries.
This guide will help you get started with the Azure AI Search retriever. For detailed documentation of all AzureAISearchRetriever
features and configurations head to the API reference.
AzureAISearchRetriever
replaces AzureCognitiveSearchRetriever
, which will soon be deprecated. We recommend switching to the newer version that’s based on the most recent stable version of the search APIs.
Integration details
Setup
To use this module, you need:- An Azure AI Search service. You can create one for free if you sign up for the Azure trial. A free service has lower quotas, but it’s sufficient for running the code in this notebook.
- An existing index with vector fields. There are several ways to create one, including using the vector store module. Or, try the Azure AI Search REST APIs.
-
An API key or Azure AD Token.
- API keys are generated when you create the search service. If you’re just querying an index, you can use the query API key, otherwise use an admin API key. See Find your API keys for details.
- Azure AD Token can be used with Azure Managed Identity. See Connect your app to Azure AI Search using identities for details.
AzureAISearchRetriever
). The search index provides the searchable content.
With an API Key
Installation
This retriever lives in thelangchain-community
package. We will need some additional dependencies as well:
Instantiation
ForAzureAISearchRetriever
, provide an index_name
, content_key
, and top_k
set to the number of number of results you’d like to retrieve. Setting top_k
to zero (the default) returns all results.
Usage
Now you can use it to retrieve documents from Azure AI Search. This is the method you would call to do so. It will return all documents relevant to the query.Example
This section demonstrates using the retriever over built-in sample data. You can skip this step if you already have a vector index on your search service. Start by providing the endpoints and keys. Since we’re creating a vector index in this step, specify a text embedding model to get a vector representation of the text. This example assumes Azure OpenAI with a deployment of text-embedding-ada-002. Because this step creates an index, be sure to use an admin API key for your search service.langchain-vector-demo
. This will create a new vector store associated with that index name.
state_of_the_union.txt
file. We’ll split the text in 400 token chunks with no overlap. Finally, the documents are added to our vector store as emeddings.
index_name
variable is langchain-vector-demo
from the last step. If you skipped vector store creation, provide your index name in the parameter. In this query, the top result is returned.
Use within a chain
API reference
For detailed documentation of allAzureAISearchRetriever
features and configurations head to the API reference.