Skip to main content
Azure AI Search (formerly known as 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 returns documents from an unstructured query. It sends the query as a full-text search request to the 2023-11-01 stable REST API version of Azure AI Search. It does not embed the query or run a vector query. For vector or hybrid retrieval, use AzureSearch.as_retriever() from the vector store integration. This guide shows how to get started with the Azure AI Search retriever. For all AzureAISearchRetriever features and configurations, see the API reference.

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 a searchable text field that matches content_key. The vector store module creates an index that works with this retriever. You can also create an index with the Azure AI Search REST APIs.
  • An API key or Azure AD Token.
Set the search service name, index name, and API key as environment variables, or pass them as arguments to AzureAISearchRetriever. The search index provides the searchable content. With an API Key
With an Azure AD Token
If you want to get automated tracing from individual queries, you can also set your LangSmith API key by uncommenting below:

Installation

This retriever ships in the langchain-azure-ai package, which also installs langchain-openai, azure-search-documents, and azure-identity:

Instantiation

For AzureAISearchRetriever, provide an index_name, content_key, and top_k set to the number of results you’d like to retrieve. If you don’t set top_k, the request omits $top and Azure AI Search applies its default page size.

Usage

Now you can use it to retrieve documents from Azure AI Search. Call invoke to retrieve documents. It returns up to top_k documents that match 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. Because this step creates a vector index, specify a text embedding model to get a vector representation of the text. This example assumes Azure OpenAI with a deployment of text-embedding-3-small. Because this step creates an index, be sure to use an admin API key for your search service.
Use an embedding model from Azure OpenAI to turn the documents into embeddings stored in the Azure AI Search vector store. Setting the index name to langchain-vector-demo creates a new vector store associated with that index name.
Next, load data into the new vector store. This example loads the state_of_the_union.txt file and splits the text into 400-character chunks with no overlap. The chunks are then added to the vector store as embeddings.
Next, create a retriever. The current 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.
Now retrieve the data that is relevant to the query from the uploaded documents.

Use within a chain

API reference

For detailed documentation of all AzureAISearchRetriever features and configurations head to the API reference.