feat(rag): add hybrid dense + sparse retrieval pipeline#1283
Open
Avi-47 wants to merge 1 commit intomofa-org:mainfrom
Open
feat(rag): add hybrid dense + sparse retrieval pipeline#1283Avi-47 wants to merge 1 commit intomofa-org:mainfrom
Avi-47 wants to merge 1 commit intomofa-org:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a hybrid retrieval pipeline for MoFA’s RAG system that combines:
The results from both retrievers are fused using Reciprocal Rank Fusion (RRF) to produce a unified ranking.
Hybrid retrieval significantly improves recall and robustness in RAG pipelines by leveraging both semantic similarity and exact keyword matching.
This work builds on the previously introduced BM25 sparse retrieval module and extends the MoFA RAG system toward production-grade hybrid search.
Relates to #305
Depends on: #1266 (BM25 sparse retrieval)
TL;DR
Adds hybrid dense + sparse retrieval to the MoFA RAG pipeline.
Key additions:
• HybridRetriever trait (kernel)
• HybridSearchPipeline implementation (foundation)
• Reciprocal Rank Fusion (RRF)
• Parallel retrieval using tokio::join!
Motivation
The current MoFA RAG pipeline primarily relies on dense vector similarity. While effective for semantic search, dense retrieval may miss:
Sparse retrieval (BM25) complements dense retrieval by providing:
Hybrid retrieval combines both approaches to achieve higher recall and better ranking quality.
Architecture
The implementation follows MoFA’s microkernel architecture:
Hybrid Retrieval Architecture Diagram
Kernel Layer
Adds a new trait:
This defines the abstraction for hybrid retrieval pipelines.
Foundation Layer
New module:
Components:
HybridSearchPipeline
Combines:
VectorStoreRetrieverRuns both retrievers in parallel using
tokio::join!.GenericHybridRetriever
A simpler hybrid retriever that combines any two Retriever implementations.
Reciprocal Rank Fusion (RRF)
Implemented in:
Formula:
Where:
rank= document position in each result listk= smoothing parameter (default 60)RRF is widely used in production hybrid search systems.
Key Features
Files Added
Updated exports:
Example Usage
Testing
Verification steps:
Results:
Impact
This PR enables hybrid retrieval in MoFA, which is a key capability for production RAG systems.
Benefits:
Future Work
Possible follow-ups:
Breaking changes: None
Existing APIs remain unchanged.