rerankers

repository·main·Indexed 23 days ago

https://github.com/answerdotai/rerankers

A lightweight, unified Python API for various document re-ranking models, including cross-encoders, T5, ColBERT, LLM-based (RankGPT/RankLLM), and API-based rerankers (Cohere, Jina, MixedBread, Pinecone, Isaacus). It provides a consistent interface via the Reranker class to rank queries against documents and includes integration for LangChain pipelines through the as_langchain_compressor method.

Tokens
6.1K
Snippets
14
Records
30
Agent score
82%

What's inside rerankers

  1. Access results from RankedResults

    main

    The .rank() method returns a RankedResults object. This object contains a list of Result objects.

    Key methods and properties:

    • top_k(k): Returns the top k Result objects.
    • results: The full list of Result objects.
    • query: The original query used.

    Each Result object contains:

    • document: A Document object containing the text, doc_id, and metadata.
    • score: The reranking score.
    • rank: The integer rank.

    You can access document properties directly from a Result object (e.g., result.text or result.metadata).

  2. Get Started with rerankers

    main

    The rerankers library provides a unified interface for various reranking strategies. Depending on your needs, you can use different categories of rerankers:

    • Transformers-based: Cross-encoders, T5, and ColBERT.
    • RankGPT: Listwise reranking.
    • API-based: Cohere, Jina, MixedBread, Pinecone, Isaacus, and HuggingFace's Text-Embedding-Server (TEI).
    • FlashRank: ONNX-optimised rerankers that are very fast on CPU.
    • RankLLM: Improved RankGPT with support for local models like RankZephyr and RankVicuna (requires Python 3.10+).
    • Multi-Modal: Support for models like MonoQwen2-VL (requires flash-attention, peft, accelerate, and recent transformers).
  3. Install rerankers

    main

    The rerankers core package is lightweight and ships with no dependencies by default. You can install specific extras to support different model architectures:

    • Core only: pip install rerankers
    • Transformers-based (cross-encoders, t5, colbert): pip install "rerankers[transformers]"
    • RankGPT: pip install "rerankers[gpt]"
    • API-based (Cohere, Jina, MixedBread, Pinecone, Isaacus): pip install "rerankers[api]"
    • FlashRank (ONNX-optimised, fast on CPU): pip install "rerankers[flashrank]"
    • RankLLM (Requires Python 3.10+): pip install "rerankers[rankllm]"
    • Multi-Modal (MonoVLM models): pip install "rerankers[monovlm]"
    • LLM-Layerwise: pip install "rerankers[llmlayerwise]"
    • All extras: pip install "rerankers[all]"
    pip install rerankers
    pip install "rerankers[transformers]"
    pip install "rerankers[gpt]"
    pip install "rerankers[api]"
    pip install "rerankers[flashrank]"
    pip install "rerankers[rankllm]"
    pip install "rerankers[monovlm]"
    pip install "rerankers[llmlayerwise]"
    pip install "rerankers[all]"
  4. Process and access results with RankedResults

    main

    The .rank() method returns a RankedResults object, which is a collection of Result objects.

    Key features of RankedResults:

    • top_k(k): Returns the top k results, sorted by rank.
    • has_scores: A boolean attribute indicating if the model provided numerical scores.
    • get_score_by_docid(doc_id): Retrieves the score for a specific document ID.
    • query: Contains the original query used for the ranking.

    Key features of Result:

    • text: The original document text.
    • doc_id: The identifier for the document.
    • rank: The 1-indexed rank of the document.
    • score: The numerical score returned by the model (if available).
  5. Install dependencies for specific reranker types

    main

    The rerankers package uses optional dependencies to keep the core installation lightweight. If you encounter a KeyError when initializing a model, you likely need to install the corresponding dependency group.

    Use the following command pattern:

    # Install all dependencies for all reranker types
    pip install "rerankers[all]"
    
    # Install dependencies for a specific type (e.g., transformers)
    pip install "rerankers[transformers]"

    Common dependency mappings include:

    • TransformerRanker -> transformers
    • T5Ranker -> transformers
    • LiT5Ranker -> lit5
    • RankGPTRanker -> gpt
    • APIRanker -> api
    • ColBERTRanker -> transformers
    • FlashRankRanker -> flashrank
    • RankLLMRanker -> rankllm
    • LLMLayerWiseRanker -> transformers
    • MonoVLMRanker -> transformers
    • LLMRelevanceFilter -> litellm
    • UPRRanker -> transformers
    • MxBaiV2Ranker -> transformers
    • PyLateRanker -> pylate
    pip install "rerankers[all]"
  6. Use Single-Label Cross-Encoders

    main

    Cross-encoders take both the query and document as input to capture subtle interactions. rerankers provides balanced defaults for this model family.

    To load a specific model from the Hugging Face Hub, provide the model path and specify model_type="cross-encoder" to avoid warnings.

    Available Arguments for Cross-Encoders:

    • model_type: Set to "cross-encoder" when loading specific Hub models.
    • lang: Set to a 2-letter ISO language code (e.g., 'fr') to attempt loading a language-specific default model.
    • verbose: Controls logging (0 to suppress most messages).
    • dtype: The data type for the model (e.g., None for auto-detection of fp16/fp32).
    • device: The device to use (cuda, mps, or cpu).
    • batch_size: Number of documents to process in a batch (default: 16).
  7. Use T5-Based Rerankers

    main

    T5-based rerankers use a SequenceToSequence approach, predicting relevance tokens.

    Arguments for T5 Models:

    • model_type: Set to "t5" when loading specific T5 models.
    • token_false: The token representing non-relevance (default: "auto").
    • token_true: The token representing relevance (default: "auto").
    • return_logits: Whether to return a normalized score or the raw logit for token_true (default: False).
    # Load default T5
    ranker = Reranker('t5')
    
    # Load specific T5 model
    ranker = Reranker("unicamp-dl/ptt5-base-pt-msmarco-10k-v2", model_type='t5', verbose=0)
  8. Use RankGPT and RankLLM

    main

    These models use Large Language Models (LLMs) for listwise reranking.

    RankGPT:

    • Uses LLMs (like GPT-4) to compare documents against each other.
    • Supports rankgpt, rankgpt3 (GPT-3.5), and rankgpt4 (GPT-4).
    • Uses LiteLLM as a backend, allowing you to use different providers by setting model_type="rankgpt" and providing the appropriate environment variables.

    RankLLM:

    • A refinement of RankGPT that supports non-GPT models (e.g., RankVicuna, RankZephyr).
    • In newer versions, gpt-* models will default to RankLLM. To ensure RankLLM usage with GPT models, specify model_type="rankllm".
    # RankGPT with OpenAI
    ranker = Reranker("rankgpt", api_key=os.environ['OPENAI_API_KEY'])
    
    # RankGPT with Azure (via LiteLLM)
    # Set AZURE_API_KEY, AZURE_API_BASE, AZURE_API_VERSION env vars
    ranker = Reranker("azure/my-deployment", model_type="rankgpt", api_key=os.environ['AZURE_API_KEY'])
    
    # RankLLM
    ranker = Reranker('rankllm', api_key=os.environ['OPENAI_API_KEY'])
    from rerankers import Reranker
    import os
    
    # RankGPT
    ranker = Reranker("rankgpt", api_key=os.environ['OPENAI_API_KEY'])
    
    # RankLLM
    ranker = Reranker('rankllm', api_key=os.environ['OPENAI_API_KEY'])
    
    # RankLLM with a specific GPT model
    ranker = Reranker('gpt-4-turbo', model_type='rankllm', api_key=os.environ['OPENAI_API_KEY'])
  9. Use ColBERT Rerankers

    main

    ColBERT uses a late-interaction approach. While it behaves like a bi-encoder during document encoding, it acts as a powerful reranker during inference.

    Arguments for ColBERT Models:

    • model_type: Set to "colbert" when loading specific ColBERT models.
    • query_token: The token prepended to queries (e.g., "[unused0]").
    • document_token: The token prepended to documents (e.g., "[unused1]").
    # Load default ColBERT
    ranker = Reranker('colbert')
    
    # Load specific ColBERT model
    ranker = Reranker('antoinelouis/colbertv2-camembert-L4-mmarcoFR', model_type="colbert")
  10. Use API-based Rerankers (Jina, Cohere, Isaacus)

    main

    You can use powerful hosted reranking models via API. You must provide an api_key for the respective provider.

    Supported Providers:

    • jina
    • cohere (supports multilingual via lang argument and fine-tuned models via api_provider="cohere")
    • isaacus

    Example Usage:

    # Jina
    ranker = Reranker("jina", api_key=os.environ['JINA_API_KEY'])
    
    # Cohere (Multilingual)
    ranker = Reranker("cohere", lang="en", api_key=os.environ['COHERE_API_KEY'])
    
    # Cohere (Fine-tuned model)
    ranker = Reranker("my-finetuned-model-name", api_provider="cohere", api_key=os.environ['COHERE_API_KEY'])
    
    # Isaacus
    ranker = Reranker("isaacus", api_key=os.environ['ISAACUS_API_KEY'])
    from rerankers import Reranker
    import os
    
    # Jina
    ranker = Reranker("jina", api_key=os.environ['JINA_API_KEY'])
    results = ranker.rank(query=query, docs=docs)
    
    # Cohere
    ranker = Reranker("cohere", api_key=os.environ['COHERE_API_KEY'])
    
    # Isaacus
    ranker = Reranker("isaacus", api_key=os.environ['ISAACUS_API_KEY'])
  11. Use FlashRank Rerankers for fast CPU inference

    main

    FlashRank provides ONNX-optimized weights designed for very fast inference on CPUs.

    # Load default FlashRank model (MiniLM-L12-v2)
    ranker = Reranker('flashrank')
    
    # Load a specific FlashRank model
    ranker = Reranker('ms-marco-TinyBERT-L-2-v2', model_type='flashrank')
    from rerankers import Reranker
    
    # Load default FlashRank model
    ranker = Reranker('flashrank')
    
    # Load a specific FlashRank model
    ranker = Reranker('ms-marco-TinyBERT-L-2-v2', model_type='flashrank')
  12. Load a multi-modal reranker

    main

    To use multi-modal reranking (e.g., for images), use the monovlm model type. It is highly recommended to run this on a GPU.

    To install the necessary dependencies for multi-modal support, use:

    pip install rerankers[monovlm]

    By default, flash-attention is enabled to manage high memory consumption. If you need to use a different attention implementation, you can specify it via the attention_implementation parameter.

    from rerankers import Reranker
    
    ranker = Reranker("monovlm", device='cuda')