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.
pip install flashrankVerified import paths — ran on the pinned version, not inferred.
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.
Ensure the user has write permissions to the cache directory (default is an OS-specific temp directory). For persistent storage or specific control, initialize the Ranker with `cache_dir=path/to/cache`: `ranker = Ranker(model_name='ms-marco-MiniLM-L-12-v2', cache_dir='/path/to/models')`.
Ensure `from flashrank.Ranker import Ranker` is imported *before* `from langchain.retrievers.document_compressors import FlashrankRerank`. The correct import order is crucial for Pydantic to resolve type annotations. No manual `model_rebuild()` is necessary if the order is correct.
Explicitly specify a currently supported model using the `model_name` parameter during `Ranker` initialization. Refer to the official FlashRank GitHub repository for the latest list of recommended models. For example: `ranker = Ranker(model_name='ms-marco-MiniLM-L-12-v2')`.
Ensure your `model_name` is correct and supported. If using a custom `cache_dir`, verify its path and permissions. Updating to a newer, explicitly named model (e.g., `model_name='ms-marco-MiniLM-L-12-v2'`) can often resolve this by forcing a fresh download.
Move `from flashrank.Ranker import Ranker` to appear *before* any imports or instantiations of `FlashrankRerank` (e.g., `from langchain.retrievers.document_compressors import FlashrankRerank`).
Specify a valid, existing directory on your Windows system for caching models using the `cache_dir` parameter during `Ranker` initialization. For example: `ranker = Ranker(model_name="ms-marco-MiniLM-L-12-v2", cache_dir='C:/Users/YourUser/FlashRankModels')`.