Reranking with Cohere
Reranking is a technique that provides a semantic boost to the search quality of any keyword or vector search system, 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 serialize the documents to YAML format, which prepares the data in the format required for reranking. Then, we pass the YAML formatted documents to the Rerank endpoint.
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. We follow the same steps in the previous example, where we convert the data into YAML format before passing it to the Rerank endpoint.
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 models (rerank-v3.5
and rerank-multilingual-v3.0
) support 100+ languages. 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-v3.5
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.