Skip to main content

🚀 Chroma-Hybrid-RRF

License: MIT Python 3.9+ LangChain Chroma

chroma-hybrid-rrf is a production-grade, highly performant custom LangChain-compatible retriever that merges dense vector semantic search (using ChromaDB) and sparse keyword keyword search (using BM25) using Reciprocal Rank Fusion (RRF).

By combining keyword matching and vector embeddings, this retriever increases query precision and robustness, mitigating issues like synonym misses and context retrieval gaps.


📐 How It Works: Reciprocal Rank Fusion (RRF)

Reciprocal Rank Fusion is a highly reliable algorithm that scores documents solely based on their rank order from different retrievers (rather than comparing raw similarity scores or distances, which vary widely between vector spaces and keyword counters).

The RRF score for a document $d$ across retrieval models $M$ is calculated as:

$$RRF_Score(d \in D) = \sum_{m \in M} \frac{1}{k + r_m(d)}$$

Where:

  • $M$: The set of retrievers (Dense vector search + Sparse BM25 keyword search).
  • $r_m(d)$: The 1-based rank position of document $d$ in the result list returned by retriever $m$.
  • $k$: A constant smoothing parameter (default 60) that prevents low ranks (outliers) from dominating the overall scoring.

🛠️ Key Features

  • Dual-retrieval pipelines: Performs dense search via ChromaDB and sparse keyword search via BM25.
  • Auto-Sync indexing: Dynamically pulls and indexes documents from ChromaDB to construct the BM25 search corpus automatically.
  • Metadata preservation: Retains all original source metadata and appends the calculated rrf_score for debugging and evaluation.
  • LangChain BaseRetriever compliance: Full drop-in integration with LangChain chains (|) and LCEL (LangChain Expression Language).
  • Async-ready: Supports standard async calling conventions (ainvoke).

📦 Installation

To install chroma-hybrid-rrf locally in editable mode for development:

git clone https://github.com/Raj2001A/chroma-hybrid-rrf.git
cd chroma-hybrid-rrf
python -m venv venv
source venv/bin/activate  # On Windows: .\venv\Scripts\activate
pip install -e .[dev]

⚡ Quick Start

from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
from chroma_hybrid_rrf import ChromaHybridRRFRetriever

# 1. Initialize dense Chroma Vector Store
embeddings = OpenAIEmbeddings()
vectorstore = Chroma(
    collection_name="my_docs", 
    embedding_function=embeddings, 
    persist_directory="./chroma_db"
)

# 2. Create the Custom Hybrid RRF Retriever
retriever = ChromaHybridRRFRetriever(
    chroma_vectorstore=vectorstore,
    rrf_k=60,       # RRF constant k
    top_n=4         # Return top 4 fused documents
)

# 3. Retrieve fused documents
query = "Explain LangGraph multi-agent orchestration"
fused_docs = retriever.invoke(query)

for rank, doc in enumerate(fused_docs):
    print(f"Rank {rank + 1} | Score: {doc.metadata['rrf_score']:.6f}")
    print(f"Content: {doc.page_content}\n")

🧪 Evaluation via RAGAS

Evaluating retrieval precision is critical for building production-grade RAG systems. Using the RAGAS framework, you can evaluate the effectiveness of this retriever across key retrieval and generation metrics:

  • Context Precision: Measures how well the retriever ranks relevant documents at the top.
  • Context Recall: Verifies if all relevant ground-truth facts are successfully retrieved.

Setup RAGAS Evaluation:

from ragas import evaluate
from ragas.metrics import context_precision, context_recall
from datasets import Dataset

# Construct your evaluation dataset
eval_data = {
    "question": ["How do you orchestrate agents?"],
    "contexts": [[doc.page_content for doc in fused_docs]],
    "ground_truth": ["LangGraph is used for building stateful, multi-actor applications with LLMs."]
}

dataset = Dataset.from_dict(eval_data)
results = evaluate(dataset, metrics=[context_precision, context_recall])
print(results)

🧪 Testing

To run the test suite and verify calculation correctness:

pytest tests/

🤝 Contributing

Contributions are highly welcome! To contribute:

  1. Fork the repository.
  2. Create a new feature branch: git checkout -b feat/your-feature.
  3. Write your changes and add tests.
  4. Run pytest to make sure all tests pass.
  5. Push to your branch and open a Pull Request.

📜 License

Distributed under the MIT License. See LICENSE for details.

Metadata

Release files for chroma-hybrid-rrf 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chroma-hybrid-rrf 0.1.0
File Size Uploaded Provenance
chroma_hybrid_rrf-0.1.0.tar.gz 8.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for chroma-hybrid-rrf 0.1.0
File Interpreter ABI Platform
chroma_hybrid_rrf-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.6 kB

Release files / chroma_hybrid_rrf-0.1.0.tar.gz

Download URL chroma_hybrid_rrf-0.1.0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
907ed75d4a06baf145169b7df9235c3892087c5df80d04d693d6f800afb373f0
BLAKE2b-256 checksum
How to use checksums
0a155c808702461f61fdb48648808bfb9bebf4ba364dd30ebd7662a084632cc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release files / chroma_hybrid_rrf-0.1.0-py3-none-any.whl

Download URL chroma_hybrid_rrf-0.1.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ed6de9458f7e96b5a682fd05a2b00413b268bcbb9a8ee9df618f0d8523ca42a4
BLAKE2b-256 checksum
How to use checksums
6873327d55911fcdeadb06f10d885322215cdceaf5d21c0554857ad1600e8ece
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Security for chroma-hybrid-rrf 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page