Tutorial Pt5 V2
Reranking
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
id='a0f8c264-63d4-4965-9123-a49f19789ce4' results=[V2RerankResponseResultsItem(document=None, index=2, relevance_score=0.11591089)] meta=ApiMeta(api_version=ApiMetaApiVersion(version='2', is_deprecated=None, is_experimental=None), billed_units=ApiMetaBilledUnits(images=None, input_tokens=None, output_tokens=None, search_units=1.0, classifications=None), tokens=None, warnings=None)
Rank: 1 Score: 0.11591089 Document: Health and Wellness Benefits: We care about your well-being and offer gym memberships, on-site yoga classes, and comprehensive health insurance.
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.
Rank: 1
Score: 0.73868835
Document: {'from': 'john@co1t.com', 'to': 'david@co1t.com', 'date': '2024-06-24', 'subject': 'First Week Check-In', 'text': "Hello! I hope you're settling in well. Let's connect briefly tomorrow to discuss how your first week has been going. Also, make sure to join us for a welcoming lunch this Thursday at noon—it's a great opportunity to get to know your colleagues!"}
Rank: 2
Score: 0.11693554
Document: {'from': 'hr@co1t.com', 'to': 'david@co1t.com', 'date': '2024-06-24', 'subject': 'A Warm Welcome to Co1t!', 'text': "We are delighted to welcome you to the team! As you embark on your journey with us, you'll find attached an agenda to guide you through your first week."}
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.
Rank: 1
Score: 0.81875163
Document: {'name': 'Emma Williams', 'role': 'Product Designer', 'join_date': '2024-06-15', 'email': 'emma@co1t.com', 'status': 'Full-time'}
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.
Rank: 1 Score: 0.22882831 Document: Health and Wellness Benefits: We care about your well-being and offer gym memberships, on-site yoga classes, and comprehensive health insurance. Rank: 2 Score: 0.01610483 Document: Working from Abroad: Working remotely from another country is possible. Simply coordinate with your manager and ensure your availability during core hours.
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.