Skip to main content

Overview

The Hybrid RRF module implements an advanced hybrid RAG pipeline that combines lexical retrieval (BM25) and semantic retrieval (ChromaDB), fuses both ranked lists using Reciprocal Rank Fusion (RRF), and then applies Maximal Marginal Relevance (MMR) for diversity. Module: src.rag.hybrid_rrf Source: src/rag/hybrid_rrf.py

Configuration

Retrieval Settings

Default Models

Retrievers

Core Functions

reciprocal_rank_fusion

Fuses multiple ranked lists using Reciprocal Rank Fusion (RRF) algorithm.
List[List[Document]]
required
List of ranked document lists from different retrievers
int
default:"60"
RRF constant. Higher values give less weight to rank position
int
default:"5"
Number of top documents to return after fusion
List[Document]
Fused and re-ranked list of documents
RRF Formula: score = sum(1 / (k_constant + rank)) for each document across all rankings.

mmr_select

Selects top-k documents using Maximal Marginal Relevance (MMR) to balance relevance and diversity.
str
required
The original user query
List[Document]
required
Pool of candidate documents to select from
int
required
Number of documents to select
float
default:"0.7"
Lambda parameter controlling relevance vs diversity tradeoff.
  • 1.0 = pure relevance
  • 0.0 = pure diversity
  • 0.7 = 70% relevance, 30% diversity
List[Document]
Selected documents balancing relevance and diversity
MMR Formula: MMR = λ * relevance(query, doc) - (1-λ) * max_similarity(doc, selected_docs)

retrieve_hybrid_rrf

Retrieves candidates from both BM25 and semantic retrievers, fuses with RRF, then applies MMR diversification.
str
required
The user’s query
List[Document]
Final list of k_final documents (default: 5)

format_docs

Formats retrieved documents for the answer prompt.
List[Document]
required
List of documents to format
str
Formatted string with document contents and metadata

process_hybrid_rrf_query

Processes a query with Hybrid RAG + RRF fusion.
str
required
The user’s question
BaseChatModel
default:"None"
Custom language model for answer generation
Dict[str, Any]
Dictionary containing:
  • answer (str): Generated answer
  • contexts (List[str]): Document contents
  • retrieved_documents (List[Document]): Full documents
  • metrics (dict): Token usage and cost metrics

query_for_evaluation

Wrapper function for RAGAS-compatible evaluation.
str
required
The question to process
str
default:"None"
Model name to use. If None, uses default “gpt-4o”
BaseChatModel
default:"None"
Pre-configured language model. Takes precedence over llm_model
dict
Dictionary containing:
  • question (str): Original question
  • answer (str): Generated answer
  • contexts (List[str]): Retrieved document contents
  • source_documents (List[Document]): Full retrieved documents
  • metadata (dict): Comprehensive metadata including:
    • num_contexts (int): Number of contexts
    • retrieval_method (str): “hybrid_bm25_semantic_rrf_mmr”
    • rrf_k (int): RRF constant used
    • k_bm25_candidates (int): 15
    • k_semantic_candidates (int): 15
    • k_rrf_pool (int): 10
    • k_final (int): 5
    • mmr_lambda (float): 0.7
    • llm_model (str): Model name
    • provider (str): Provider name
    • model_id (str): Full model ID
    • embedding_model (str): “text-embedding-3-small”
    • execution_time (float): Execution time in seconds
    • input_tokens (int): Input tokens used
    • output_tokens (int): Output tokens generated
    • total_cost (float): Total cost in USD
    • tokens_used (int): Total tokens
    • usage_source (str): Usage data source
    • cost_source (str): Cost calculation source

Usage Example

Pipeline Flow

  1. BM25 Retrieval: Retrieves top 15 candidates using lexical search
  2. Semantic Retrieval: Retrieves top 15 candidates using vector similarity
  3. RRF Fusion: Fuses both ranked lists using Reciprocal Rank Fusion, creating a pool of 10 documents
  4. MMR Selection: Applies Maximal Marginal Relevance to select final 5 documents balancing relevance and diversity
  5. Format: Formats documents with metadata
  6. Generate: Uses LLM to generate answer
  7. Track: Captures comprehensive metrics

Key Features

  • Advanced Fusion: Uses Reciprocal Rank Fusion for better ranking
  • Diversity: MMR ensures diverse document selection
  • Deduplication: Automatically removes duplicate documents
  • Tunable Parameters: Configurable k values and lambda for fine-tuning
  • High Recall: Retrieves 15 candidates from each method
  • Balanced Results: 70% relevance + 30% diversity by default