Skip to content

feat(retrieval): BM25 hybrid retrieval with reciprocal rank fusion#11

Open
AyushAnand413 wants to merge 2 commits into
mainfrom
feat/bm25-hybrid
Open

feat(retrieval): BM25 hybrid retrieval with reciprocal rank fusion#11
AyushAnand413 wants to merge 2 commits into
mainfrom
feat/bm25-hybrid

Conversation

@AyushAnand413

Copy link
Copy Markdown
Owner

Summary

  • Adds BM25Okapi (sparse) retrieval alongside existing FAISS (dense) in retrieval/retriever.py
  • Both retrievers independently fetch initial_top_k candidates; results merged with Reciprocal Rank Fusion (k=60) — parameter-free, robust to score-scale differences between sparse and dense signals
  • Final merged list is capped at initial_top_k and passed to Reranker unchanged — zero breaking changes downstream
  • rank_bm25==0.2.2 added to requirements.txt

How it works

query
 ├── FAISS (cosine sim)  → top-25 ranked list
 └── BM25Okapi           → top-25 ranked list
          ↓
   Reciprocal Rank Fusion  (score = Σ 1/(60 + rank))
          ↓
   merged top-25 → Reranker → final answer

Tests

10 new unit tests in tests/test_hybrid_retrieval.py covering:

  • _tokenize edge cases
  • _reciprocal_rank_fusion (single list, agreement boost, exclusive chunks, empty input, custom k)
  • BM25 scoring correctness (relevant chunk ranked higher, zero score for unrelated query)

All 10 pass without requiring faiss or sentence_transformers in the test environment.

Test plan

  • pip install rank_bm25==0.2.2
  • Upload a PDF and run a query — verify chunks returned without errors
  • Keyword-exact queries should surface the matching chunk higher than before
  • pytest tests/test_hybrid_retrieval.py → 10 passed

🤖 Generated with Claude Code

AyushAnand413 and others added 2 commits May 5, 2026 15:28
Replace single-pass FAISS dense retrieval with a hybrid pipeline:
- FAISS (dense) + BM25Okapi (sparse) each fetch `initial_top_k` candidates
- Results are merged via Reciprocal Rank Fusion (k=60)
- Final ranked list preserves the same output schema consumed by Reranker
- rank_bm25==0.2.2 added to requirements.txt

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Covers _tokenize, _reciprocal_rank_fusion edge cases, and BM25 scoring
without requiring faiss or sentence_transformers in the test environment.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vercel

vercel Bot commented May 5, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
ragify-ai Ready Ready Preview, Comment May 5, 2026 10:08am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant