Reranking
In this tutorial, we’ll explore reranking using Cohere’s Rerank model on Azure AI Foundry.
Reranking is a crucial technique used in information retrieval systems, particularly for large-scale search applications. The process involves taking an initial set of retrieved documents and reordering them based on how relevant they are to the user’s search query.
One of the most compelling aspects of reranking is its ease of implementation - despite providing substantial improvements to search results, Cohere’s Rerank models can be integrated into any existing search system with just a single line of code, regardless of whether it uses semantic or traditional keyword-based search approaches.
In this tutorial, we’ll cover:
- Setting up the Cohere client
- Retrieving documents
- Reranking documents
- Reranking semi structured data
We’ll use Cohere’s Embed model deployed on Azure to demonstrate these capabilities and help you understand how to effectively implement semantic search in your applications.
Setup
First, you will need to deploy the Rerank model on Azure via Azure AI Foundry. The deployment will create a serverless API with pay-as-you-go token based billing. You can find more information on how to deploy models in the Azure documentation.
In the example below, we are deploying the Rerank Multilingual v3 model.
Once the model is deployed, you can access it via Cohere’s Python SDK. Let’s now install the Cohere SDK and set up our client.
To create a client, you need to provide the API key and the model’s base URL for the Azure endpoint. You can get these information from the Azure AI Foundry platform where you deployed the model.
Retrieve documents
For this example, we’ll work with documents that have already been retrieved through an initial search stage (which could be semantic search, keyword matching, or another retrieval method).
Below is a list of nine documents representing the initial search results. Each document contains email data structured as a dictionary with two fields - Title and Content. This semi-structured format allows the Rerank endpoint to effectively process and reorder the results based on relevance.
Rerank documents
Adding a reranking component is simple with Cohere Rerank. It takes just one line of code to implement.
Calling the Rerank endpoint requires the following arguments:
documents
: The list of documents, which we defined in the previous sectionquery
: The user query; we’ll use ‘What emails have been about refunds?’ as an exampletop_n
: The number of documents we want to be returned, sorted from the most to the least relevant document
When passing documents that contain multiple fields like in this case, for best performance we recommend formatting them as YAML strings.
Since we set top_n=3
, the response will return the three documents most relevant to our query. Each result includes both the document’s original position (index) in our input list and a score indicating how well it matches the query.
Let’s examine the reranked results below.
The search query was looking for emails about refunds. But none of the documents mention the word “refunds” specifically.
However, the Rerank model was able to retrieve the right documents. Some of the documents mentioned the word “return”, which has a very similar meaning to “refunds.”
Rerank semi structured data
The Rerank 3 model supports multi-aspect and semi-structured data like emails, invoices, JSON documents, code, and tables. By setting the rank fields, you can select which fields the model should consider for reranking.
In the following example, we’ll use an email data example. It is a semi-stuctured data that contains a number of fields – from, to, date, subject, and text.
The model will rerank based on order of the fields passed.
Summary
In this tutorial, we learned about:
- How to set up the Cohere client to use the Rerank model deployed on Azure AI Foundry
- How to retrieve documents
- How to rerank documents
- How to rerank semi structured data
In the next tutorial, we’ll learn how to build RAG applications by leveraging the models that we’ve looked at in the previous tutorials - Command, Embed, and Rerank.