Reranking with Cohere
Reranking is a technique that leverages embeddings as the last stage of a retrieval process, and is especially useful in RAG systems.
We can rerank results from semantic search as well as any other search systems such as lexical search. This means that companies can retain an existing keyword-based (also called “lexical”) or semantic search system for the first-stage retrieval and integrate the Rerank endpoint in the second-stage reranking.
In this tutorial, you’ll learn about:
- Reranking lexical/semantic search results
- Reranking semi-structured data
- Reranking tabular data
- Multilingual reranking
You’ll learn these by building an onboarding assistant for new hires.
Setup
To get started, first we need to install the cohere
library and create a Cohere client.
Reranking lexical/semantic search results
Rerank requires just a single line of code to implement.
Suppose we have a list of search results of an FAQ list, which can come from semantic, lexical, or any other types of search systems. But this list may not be optimally ranked for relevance to the user query.
This is where Rerank can help. We call the endpoint using co.rerank()
and pass the following arguments:
query
: The user querydocuments
: The list of documentstop_n
: The top reranked documents to selectmodel
: We choose Rerank English 3
Further reading:
- Rerank endpoint API reference
- Documentation on Rerank
- Documentation on Rerank fine-tuning
- Documentation on Rerank best practices
- LLM University module on Text Representation
Reranking 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
.
Suppose the new hire now wants to search for any emails about check-in sessions. Let’s pretend we have a list of 5 emails retrieved from the email provider’s API.
To perform reranking over semi-structured data, we add an additional parameter, rank_fields
, which contains the list of available fields.
The model will rerank based on order of the fields passed in. For example, given rank_fields=[‘title’,‘author’,‘text’], the model will rerank using the values in title, author, and text sequentially.
Reranking tabular data
Many enterprises rely on tabular data, such as relational databases, CSVs, and Excel. To perform reranking, you can transform a dataframe into a list of JSON records and use Rerank 3’s JSON capabilities to rank them.
Here’s an example of reranking a CSV file that contains employee information.
Here’s what the table looks like:
Below, we’ll get results from the Rerank endpoint:
Multilingual reranking
The Rerank endpoint also supports multilingual semantic search via the rerank-multilingual-...
models. This means you can perform semantic search on texts in different languages.
In the example below, we repeat the steps of performing reranking with one difference – changing the model type to a multilingual one. Here, we use the rerank-multilingual-v3.0
model. Here, we are reranking the FAQ list using an Arabic query.
Conclusion
In this tutorial, you learned about:
- How to rerank lexical/semantic search results
- How to rerank semi-structured data
- How to rerank tabular data
- How to perform Multilingual reranking
We have now seen two critical components of a powerful search system - semantic search, or dense retrieval (Part 4) and reranking (Part 5). These building blocks are essential for implementing RAG solutions.
In Part 6, you will learn how to implement RAG.