Skip to main content
In this guide we will use a reranker model to improve the results produced from a simple semantic search workflow. To get a better understanding of how semantic search works please refer to the Cookbook here. A reranker model operates by looking at the query and the retrieved results from the semantic search pipeline one by one and assesses how relevant the returned result is to the query. Because the reranker model can spend compute assessing the query with the returned result at the same time it can better judge how relevant the words and meanings in the query are to individual documents. This also means that rerankers are computationally expensive and slower - thus they cannot be used to rank every document in our database. We run a semantic search process to obtain a list of 15-25 candidate objects that are similar “enough” to the query and then use the reranker as a fine-toothed comb to pick the top 5-10 objects that are actually closest to our query. We will be using the Mxbai Rerank reranker model.
Rerank models like Mxbai-Rerank-Large-V2 are only available with dedicated model inference. You can bring up a dedicated endpoint to use reranking in your applications.
How to improve search with rerankers

Download and View the Dataset

Shell
Python
Our dataset contains information about popular movies:

Implement Semantic Search Pipeline

Below we implement a simple semantic search pipeline:
  1. Embed movie documents + query
  2. Obtain a list of movies ranked based on cosine similarities between the query and movie vectors.
Python
Next we implement a function that when given the above embeddings and a test query will return indices of most semantically similar data objects:
Python
We will use the above function to retrieve 25 movies most similar to our query:
Python
This will give us the following movie indices and movie titles:
Python
Notice here that not all movies in our top 25 have to do with our query - super hero mystery action movie about bats. This is because semantic search captures the “approximate” meaning of the query and movies. The reranker can more closely determine the similarity between these 25 candidates and rerank which ones deserve to be atop our list.

Use Llama Rank to Rerank Top 25 Movies

Treating the top 25 matching movies as good candidate matches, potentially with irrelevant false positives, that might have snuck in we want to have the reranker model look and rerank each based on similarity to the query.
Python
This will give us a reranked list of movies as shown below:
Here we can see that the reranker was able to improve the list by demoting irrelevant movies like Watchmen, Predator, Despicable Me 2, Night at the Museum: Secret of the Tomb, Penguins of Madagascar, further down the list and promoting Batman Returns, Batman Begins, Batman & Robin, Batman v Superman: Dawn of Justice to the top of the list! The multilingual-e5-large-instruct embedding model gives us a fuzzy match to concepts mentioned in the query, the Llama-Rank-V1 reranker then improves the quality of our list further by spending more compute to resort the list of movies. Learn more about how to use reranker models in the docs here!