A Python toolkit for Retrieval-Augmented Generation that stores and searches documents in DuckDB or PostgreSQL.
Raudaschl/rag-fusion
Python reference implementation of RAG-Fusion, combining LLM query rewrites, BM25 and vector search, and Reciprocal Rank Fusion to improve retrieval, with an NFCorpus evaluation harness.
- Stars
- 960
- Forks
- 113
- Open issues
- 0
- Last commit
- 28 days ago
What is it?
RAG-Fusion is a retrieval method for retrieval-augmented generation. An LLM rewrites the user's query into several variants from different angles, each variant is searched with both BM25 keyword search and vector search, and the ranked lists are merged with Reciprocal Rank Fusion so that documents appearing across many lists rise to the top. It targets cases where a single phrasing of a query misses relevant material, such as when user vocabulary differs from how the corpus is indexed.
The repository contains a basic pipeline in main.py that uses vector search with ChromaDB and OpenAI, plus an evaluation harness in eval/ that compares baseline, hybrid, RAG-Fusion and hybrid-diverse variants on the NFCorpus dataset from BEIR. It also includes cross-encoder reranking stages and experiment write-ups measuring NDCG, precision, recall and MRR, along with cost and latency measurements per configuration.
The repo's measurements suggest that adding a reranker after hybrid fusion yields small but statistically significant gains, while vector-only fusion gives little benefit once a strong reranker is in place.
Key features
- 01Generates multiple LLM rewrites of a user query to cover different phrasings.
- 02Combines BM25 keyword search and vector search for each query variant.
- 03Merges ranked results with Reciprocal Rank Fusion.
- 04Supports cross-encoder and other reranker stages on the fused candidate pool.
- 05Evaluates retrieval variants on the NFCorpus dataset with IR metrics and bootstrap confidence intervals.
- 06Caches LLM query rewrites on disk and persists ChromaDB embeddings locally.
Use cases
- Searching specialist literature such as medical or scientific papers where terminology varies.
- Improving recall in retrieval pipelines where missing a relevant document is costly.
- Experimenting with query rewriting and rank fusion before adopting them in a RAG system.
Quick start
- 1
Install the core dependencies
pip install openai chromadb python-dotenv tqdm tabulate rank_bm25 - 2
Create the .env file from the template
cp .env.example .env - 3
Add your OpenAI API key to .env, then run the demo
python main.py
Things to know
- Running the demo and rewrites requires a paid OpenAI API key.
- The demo in main.py uses vector search only; the best-performing hybrid-plus-rerank setup lives in eval/ and needs extra packages.
- Experiments rely on specific LLM and decision models, some of which may be retired or unavailable.
Topics
FAQ
- What is rag-fusion?
- Python reference implementation of RAG-Fusion, combining LLM query rewrites, BM25 and vector search, and Reciprocal Rank Fusion to improve retrieval, with an NFCorpus evaluation harness. RAG-Fusion is a retrieval method for retrieval-augmented generation. An LLM rewrites the user's query into several variants from different angles, each variant is searched with both BM25 keyword search and vector search, and the ranked lists are merged with Reciprocal Rank Fusion so that documents appearing across many lists rise to the top. It targets cases where a single phrasing of a query misses relevant material, such as when user vocabulary differs from how the corpus is indexed.
- Is rag-fusion free to use? What license does it have?
- rag-fusion is open source under the MIT license. Check the license terms before using it in commercial products.
- Is rag-fusion still maintained?
- Actively maintained. The last commit to rag-fusion was on Oct 10, 2026.
- What can I use rag-fusion for?
- Searching specialist literature such as medical or scientific papers where terminology varies. Improving recall in retrieval pipelines where missing a relevant document is costly. Experimenting with query rewriting and rank fusion before adopting them in a RAG system.
- How do I install rag-fusion?
- Install the core dependencies: pip install openai chromadb python-dotenv tqdm tabulate rank_bm25 Create the .env file from the template: cp .env.example .env Add your OpenAI API key to .env, then run the demo: python main.py
- Where can I get rag-fusion?
- The source code and installation instructions are on GitHub: https://github.com/Raudaschl/rag-fusion
Summary generated by AI from the project's README. GitHub stats refresh daily.
Related projects
Embeddable vector database for Go with a Chroma-like interface, no third-party dependencies, and optional on-disk persistence.
An Azure-hosted RAG accelerator that grounds a chat assistant in your own documents, returning answers with inline citations to sources.
A RAG chat app that answers natural-language questions about rows in a PostgreSQL table, using pgvector search and OpenAI models, deployable to Azure.
Local-first AI memory system that stores conversation history verbatim and retrieves it with semantic search, exposed as an MCP server and CLI.
A no-code RAG server that indexes a folder of documents into one of 8 vector databases and keeps answers in sync as files change.