Reranking
Reranking is a technique used to improve the relevance and accuracy of search results by re-evaluating and reordering them using a dedicated reranking model.
The search process works in two stages:
- Initial Retrieval: Vector search identifies the top
kmost similar documents from the collection. - Reranking: A reranking model evaluates these
kdocuments based on the relevance between the query and the documents and reorders them to produce the final topnresults (wheren≤k).
This two-stage retrieval approach significantly improves both document relevance and accuracy.
Basic usage
pytidb is the official Python SDK for TiDB, designed to help developers build AI applications efficiently.
pytidb provides the Reranker class that lets you use reranking models from multiple third-party providers.
Create a reranker instance:
from pytidb.rerankers import Reranker reranker = Reranker(model_name="{provider}/{model_name}")Apply the reranker by using the
.rerank()method:table.search("{query}").rerank(reranker, "{field_to_rerank}").limit(3)
Supported providers
The following examples show how to use reranking models from third-party providers.
Jina AI
To use the reranker from Jina AI, go to their website to create an API key.
For example:
jinaai = Reranker(
# Using the `jina-reranker-m0` model
model_name="jina_ai/jina-reranker-m0",
api_key="{your-jinaai-api-key}"
)