### Install Flashrank - Lightweight Pairwise Rerankers Source: https://github.com/prithivirajdamodaran/flashrank/blob/main/README.md Installs the core flashrank package for lightweight pairwise reranking. This is the default installation and does not require additional dependencies like Torch or Transformers. ```bash pip install flashrank ``` -------------------------------- ### Install Flashrank - LLM Based Listwise Rerankers Source: https://github.com/prithivirajdamodaran/flashrank/blob/main/README.md Installs flashrank with support for LLM-based listwise rerankers. This installation includes additional dependencies required for running larger language models. ```bash pip install flashrank[listwise] ``` -------------------------------- ### Initialize FlashRank Ranker Source: https://github.com/prithivirajdamodaran/flashrank/blob/main/README.md Demonstrates how to instantiate the Ranker class with different model configurations, including specifying model names and cache directories. These models vary in size, performance, and language support. ```python from flashrank import Ranker # Initialize with specific model and cache directory ranker = Ranker(model_name="rank-T5-flan", cache_dir="/opt") ranker = Ranker(model_name="ms-marco-MultiBERT-L-12", cache_dir="/opt") ranker = Ranker(model_name="rank_zephyr_7b_v1_full", max_length=1024) ``` -------------------------------- ### Initialize Ranker class Source: https://context7.com/prithivirajdamodaran/flashrank/llms.txt Demonstrates how to instantiate the Ranker class with various pre-trained models, ranging from nano-sized cross-encoders to large-scale LLM-based listwise models. ```python from flashrank import Ranker # Default: Nano model (~4MB) ranker = Ranker(max_length=128) # Small model (~34MB) ranker = Ranker(model_name="ms-marco-MiniLM-L-12-v2", cache_dir="/opt") # Medium model (~110MB) ranker = Ranker(model_name="rank-T5-flan", cache_dir="/opt") # Multi-lingual model (~150MB) ranker = Ranker(model_name="ms-marco-MultiBERT-L-12", cache_dir="/opt") # LLM-based listwise reranker (~4GB) ranker = Ranker(model_name="rank_zephyr_7b_v1_full", max_length=1024) # Arabic-specific reranker ranker = Ranker(model_name="miniReranker_arabic_v1", cache_dir="/opt") ``` -------------------------------- ### Initialize Ranker with Cache Directory in Python Source: https://github.com/prithivirajdamodaran/flashrank/blob/main/README.md Demonstrates how to initialize the Ranker class from the flashrank library, specifying a custom cache directory. This is particularly useful in serverless environments like AWS Lambda where the filesystem might be read-only. ```python ranker = Ranker(model_name="ms-marco-MiniLM-L-12-v2", cache_dir="/opt") ``` -------------------------------- ### Initialize Flashrank Ranker with Specific Model and Cache Directory Source: https://github.com/prithivirajdamodaran/flashrank/blob/main/README.md Initializes the Ranker object with a specific model, 'ms-marco-MiniLM-L-12-v2' (~34MB), known for its slightly slower speed but best performance in ranking precision. It also specifies a custom cache directory for downloaded models. ```python from flashrank import Ranker # Small (~34MB), slightly slower & best performance (ranking precision). ranker = Ranker(model_name="ms-marco-MiniLM-L-12-v2", cache_dir="/opt") ``` -------------------------------- ### Configure Listwise LLM Reranker Source: https://context7.com/prithivirajdamodaran/flashrank/llms.txt Demonstrates the initialization of the listwise reranker using the rank_zephyr_7b model. This approach supports larger context windows and holistic passage comparison. ```python from flashrank import Ranker, RerankRequest # Initialize listwise LLM reranker ranker = Ranker( model_name="rank_zephyr_7b_v1_full", max_length=1024 ) ``` -------------------------------- ### Initialize Flashrank Ranker with Default Model Source: https://github.com/prithivirajdamodaran/flashrank/blob/main/README.md Initializes the Ranker object using the default lightweight model (~4MB). This model offers a balance of speed and competitive ranking precision. The max_length parameter should be set to accommodate the longest passage plus query tokens. ```python from flashrank import Ranker # Nano (~4MB), blazing fast model & competitive performance (ranking precision). ranker = Ranker(max_length=128) ``` -------------------------------- ### FlashRank Supported Models Reference Source: https://context7.com/prithivirajdamodaran/flashrank/llms.txt Provides a reference for the various models supported by FlashRank, detailing their size, type (pairwise/listwise), and characteristics. It shows how to initialize the Ranker with a specific model name. ```python from flashrank import Ranker # Available models with their characteristics: models = { # Default - Ultra lightweight, blazing fast "ms-marco-TinyBERT-L-2-v2": {"size": "~4MB", "type": "pairwise"}, # Best cross-encoder accuracy "ms-marco-MiniLM-L-12-v2": {"size": "~34MB", "type": "pairwise"}, # Best zero-shot performance on out-of-domain data "rank-T5-flan": {"size": "~110MB", "type": "pairwise"}, # Multi-lingual support (100+ languages, not for English) "ms-marco-MultiBERT-L-12": {"size": "~150MB", "type": "pairwise"}, # Fine-tuned on Amazon ESCI e-commerce dataset "ce-esci-MiniLM-L12-v2": {"size": "~34MB", "type": "pairwise"}, # LLM-based listwise reranker with large context "rank_zephyr_7b_v1_full": {"size": "~4GB", "type": "listwise"}, # Dedicated Arabic language reranker "miniReranker_arabic_v1": {"size": "~34MB", "type": "pairwise"}, } # Initialize any model by name for model_name in ["ms-marco-TinyBERT-L-2-v2", "ms-marco-MiniLM-L-12-v2"]: ranker = Ranker(model_name=model_name) print(f"Loaded: {model_name}") ``` -------------------------------- ### Deploy FlashRank to AWS Lambda Source: https://context7.com/prithivirajdamodaran/flashrank/llms.txt Provides a template for deploying FlashRank in a serverless environment. It highlights the use of the /tmp directory for model caching and efficient cold-start configurations. ```python from flashrank import Ranker, RerankRequest def lambda_handler(event, context): # Use /tmp for model caching in Lambda ranker = Ranker( model_name="ms-marco-TinyBERT-L-2-v2", cache_dir="/tmp", max_length=128 ) query = event.get("query", "") passages = event.get("passages", []) if not query or not passages: return {"statusCode": 400, "body": "Missing query or passages"} request = RerankRequest(query=query, passages=passages) results = ranker.rerank(request) return { "statusCode": 200, "body": { "query": query, "results": results } } ``` -------------------------------- ### Rerank Lexical Search Results with FlashRank Source: https://context7.com/prithivirajdamodaran/flashrank/llms.txt Demonstrates how to integrate FlashRank with traditional full-text search systems. It initializes a Ranker, processes search results, and returns the top-k most relevant passages based on a user query. ```python from flashrank import Ranker, RerankRequest # Initialize ranker ranker = Ranker(model_name="ms-marco-MiniLM-L-12-v2") # Simulated results from a lexical search lexical_search_results = [ {"id": "doc_001", "text": "Python is a programming language with dynamic typing and automatic memory management."}, {"id": "doc_002", "text": "Python snakes are non-venomous constrictors found in Africa, Asia, and Australia."}, {"id": "doc_003", "text": "Learn Python programming fundamentals including variables, loops, and functions."}, {"id": "doc_004", "text": "The Python package index hosts thousands of third-party modules for Python programming."}, ] # Rerank based on user query query = "Python programming tutorial for beginners" request = RerankRequest(query=query, passages=lexical_search_results) reranked = ranker.rerank(request) # Return top-k most relevant results top_k = 3 for passage in reranked[:top_k]: print(f"[{passage['id']}] Score: {passage['score']:.4f} - {passage['text'][:80]}...") ``` -------------------------------- ### Create RerankRequest object Source: https://context7.com/prithivirajdamodaran/flashrank/llms.txt Shows how to structure the query and passages into a RerankRequest object, including optional metadata for tracking. ```python from flashrank import RerankRequest query = "How to speedup LLMs?" passages = [ {"id": 1, "text": "Introduce lookahead decoding...", "meta": {"source": "paper1"}}, {"id": 2, "text": "LLM inference efficiency...", "meta": {"source": "blog1"}}, {"id": 3, "text": "There are many ways to increase...", "meta": {"source": "tutorial1"}} ] rerank_request = RerankRequest(query=query, passages=passages) ``` -------------------------------- ### Optimizing FlashRank Performance with max_length Source: https://context7.com/prithivirajdamodaran/flashrank/llms.txt Illustrates how to configure the `max_length` parameter in the Ranker for different passage lengths to optimize performance. Shorter `max_length` values lead to faster processing, suitable for short texts like titles or snippets. ```python from flashrank import Ranker, RerankRequest # For short passages (e.g., titles, snippets < 100 tokens) ranker_fast = Ranker(max_length=128) # For medium passages (e.g., paragraphs, 100-300 tokens) ranker_medium = Ranker(max_length=256) # For long passages (e.g., full documents, 300-500 tokens) ranker_full = Ranker(max_length=512) # Example: Short snippet reranking short_passages = [ {"id": 1, "text": "Quick Python tutorial"}, {"id": 2, "text": "Python programming basics"}, {"id": 3, "text": "Learn Python in 10 minutes"}, ] request = RerankRequest(query="beginner Python guide", passages=short_passages) results = ranker_fast.rerank(request) # Fastest for short text print("Reranking complete for short passages.") ``` -------------------------------- ### Listwise Reranking with FlashRank Source: https://context7.com/prithivirajdamodaran/flashrank/llms.txt Demonstrates how to perform listwise reranking using the Ranker and RerankRequest objects. It takes a query and a list of passages, returning the passages in a ranked order based on relevance to the query. This method does not explicitly return scores. ```python from flashrank import Ranker, RerankRequest passages = [ {"id": 1, "text": "Climate change is causing rising sea levels and extreme weather events worldwide."}, {"id": 2, "text": "The stock market experienced significant volatility during the economic downturn."}, {"id": 3, "text": "Renewable energy sources like solar and wind are becoming more cost-effective."}, {"id": 4, "text": "Global warming affects biodiversity and threatens many species with extinction."}, ] query = "Environmental impact of climate change" request = RerankRequest(query=query, passages=passages) # Initialize Ranker (default model) ranker = Ranker() # Listwise reranking - returns passages in ranked order (no explicit scores) results = ranker.rerank(request) for i, passage in enumerate(results, 1): print(f"Rank {i}: [{passage['id']}] {passage['text'][:60]}...") ``` -------------------------------- ### Perform Reranking Request Source: https://github.com/prithivirajdamodaran/flashrank/blob/main/README.md Shows how to structure a reranking request by defining a query and a list of passages with associated metadata. The Ranker object then processes these inputs to return ranked results. ```python from flashrank import RerankRequest query = "How to speedup LLMs?" passages = [ {"id": 1, "text": "Introduce lookahead decoding...", "meta": {"additional": "info1"}}, {"id": 2, "text": "LLM inference efficiency...", "meta": {"additional": "info2"}} ] rerankrequest = RerankRequest(query=query, passages=passages) results = ranker.rerank(rerankrequest) print(results) ``` -------------------------------- ### Execute Rerank method Source: https://context7.com/prithivirajdamodaran/flashrank/llms.txt Performs the reranking process on a request and iterates through the sorted results, which are ordered by relevance score. ```python from flashrank import Ranker, RerankRequest ranker = Ranker(max_length=128) # ... (passages defined as above) rerank_request = RerankRequest(query=query, passages=passages) results = ranker.rerank(rerank_request) for result in results: print(f"ID: {result['id']}, Score: {result['score']:.6f}") print(f"Text: {result['text'][:100]}...") ``` -------------------------------- ### Rerank Vector Search Results for RAG Source: https://context7.com/prithivirajdamodaran/flashrank/llms.txt Shows how to enhance RAG pipelines by reranking semantically similar results from vector databases. This improves the quality of context provided to the LLM. ```python from flashrank import Ranker, RerankRequest # Initialize ranker with appropriate max_length for your passage sizes ranker = Ranker(max_length=256) # Simulated vector search results vector_search_results = [ {"id": "chunk_1", "text": "Machine learning models require large amounts of training data to achieve good performance.", "meta": {"source": "ml_guide.pdf", "page": 12}}, {"id": "chunk_2", "text": "Deep learning is a subset of machine learning using neural networks with multiple layers.", "meta": {"source": "dl_intro.pdf", "page": 3}}, {"id": "chunk_3", "text": "Transfer learning allows models to leverage knowledge from pre-trained models.", "meta": {"source": "transfer_learning.pdf", "page": 1}}, {"id": "chunk_4", "text": "Fine-tuning involves training a pre-trained model on a specific downstream task.", "meta": {"source": "fine_tuning.pdf", "page": 5}}, ] # User query for RAG query = "How can I train a model with limited data?" # Rerank to get most relevant chunks for LLM context request = RerankRequest(query=query, passages=vector_search_results) reranked = ranker.rerank(request) # Use top results as context for LLM context_for_llm = "\n\n".join([p["text"] for p in reranked[:2]]) print(f"Context for LLM:\n{context_for_llm}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.