FlashRank

0.2.10 · active · verified Thu Apr 16

FlashRank is an ultra-lite and super-fast Python library designed to add re-ranking capabilities to existing search and retrieval pipelines. Leveraging state-of-the-art LLMs and cross-encoders, it provides both pairwise (cross-encoder based) and listwise (LLM-based) re-ranking. As of version 0.2.10, FlashRank is known for its speed and efficiency, particularly on CPU, making it suitable for cost-effective serverless deployments. The library maintains an active development pace with frequent updates and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize a FlashRank `Ranker` with a specified model and then re-rank a list of passages for a given query. The `RerankRequest` object encapsulates the query and passages. The `rerank` method returns passages sorted by their relevance score.

from flashrank.Ranker import Ranker
from flashrank.RerankRequest import RerankRequest

# Initialize the ranker with a default or specified model
# 'ms-marco-TinyBERT-L-2-v2' (~4MB) is the default and fastest.
# 'ms-marco-MiniLM-L-12-v2' (~34MB) offers better performance.
# For LLM-based rerankers, use 'rank_zephyr_7b_v1_full' (requires flashrank[llm])
ranker = Ranker(model_name="ms-marco-MiniLM-L-12-v2")

query = "What is the capital of France?"
passages = [
    {"id": "1", "text": "Paris is the capital and most populous city of France."},
    {"id": "2", "text": "The Eiffel Tower is in Paris."},
    {"id": "3", "text": "Berlin is the capital of Germany."}
]

# Create a RerankRequest object
rerank_request = RerankRequest(query=query, passages=passages)

# Perform reranking
results = ranker.rerank(rerank_request)

# Print reranked results (sorted by score in descending order)
for result in results:
    print(f"ID: {result['id']}, Text: {result['text']}, Score: {result['score']:.4f}")

view raw JSON →