A retrieval-augmented generation system that merges retrieved chunks before passing them to the language model. The core idea is that individual retrieved chunks are often weak in isolation — pairing or anchoring them before generation can produce a tighter, more relevant context window.
When a query arrives, MergeRAG embeds it, retrieves the top-N candidates, then decides how to merge them based on the chosen strategy. The merged pool gets re-embedded and reranked against the original query before the final context is handed to the LLM for answer generation.
Retrieval can use ChromaRetriever (dense cosine search only) or HybridRetriever (BM25 sparse + Chroma dense, fused via Reciprocal Rank Fusion). Hybrid retrieval fixes entity-name recall gaps where dense similarity alone fails — e.g. a query for "Scott Derrickson nationality" that the dense arm misses is surfaced at rank 2 by the BM25 arm.
Three merge strategies are supported. top_k is the plain baseline — no merging, just the highest-scoring chunks. symmetric pairs weak chunks from the same document together via concatenation. asymmetric anchors each weak chunk to its nearest strong same-doc chunk and synthesizes it with a dedicated LLM call, capped at one operation by default to bound tail latency. Use --asymmetric-max-ops 3 to reproduce older benchmark settings.
On HotpotQA (n=500, qwen2.5:3b), asymmetric beats the top_k baseline by +1.0 EM and +1.1 F1 at roughly 42% latency overhead. Symmetric slightly underperforms — adjacent same-doc chunks tend to be topically adjacent but not complementary, producing noisier merged context.
flowchart TD
User(["User / Streamlit UI"])
subgraph API["FastAPI — mergerag/api"]
QR["/query endpoint"]
IR["/ingest endpoint"]
RR["/runs endpoint"]
CR["/collections endpoint"]
end
subgraph Pipeline["MergeRAGPipeline — mergerag/pipeline.py"]
E["1. Embed query\nSentenceTransformer\nall-MiniLM-L6-v2"]
R["2. Retrieve top-N\nChromaRetriever (dense)\nor HybridRetriever\n(BM25 + dense, RRF)"]
subgraph Planner["3. Planner — mergerag/merge/planner.py"]
SP["Split chunks\nstrong = top strong_k\nweak = remainder"]
end
subgraph Strategies["4. Strategy"]
TK["top_k\nno merge"]
SYM["symmetric\npair_weak_chunks()\nsame-doc pairs\nconcatenation"]
ASYM["asymmetric\nassign_to_anchors()\nweak → nearest strong anchor\nLLM synthesis\nmax_ops=1 default"]
end
subgraph Executor["5. Executor — mergerag/merge/executor.py"]
SC["symmetric op:\nprimary.text + secondary.text\n(no LLM call)"]
AC["asymmetric op:\nmerge_asymmetric.txt prompt\nllm.complete() → synthesized text"]
end
RE["6. Re-embed merged chunks\nSentenceTransformer"]
RK["7. Rerank pool\nstrong_k raw + merged chunks\ncosine similarity vs query\ntop-k selected"]
GEN["8. Generate answer\nanswer.txt prompt\nllm.complete()\ncitation extraction"]
end
subgraph Adapters["Adapters — mergerag/adapters"]
EMB["SentenceTransformerEmbedder"]
LLM["OllamaLLM\nqwen2.5:7b"]
RET["ChromaRetriever / HybridRetriever\nBM25Index + rrf_fuse"]
RS["SQLiteRunStore\nruns.db"]
end
subgraph Ingestion["Ingestion — mergerag/ingestion"]
LD["Loader\nread file"]
CH["Chunker\nsplit into Chunk objects"]
IG["ingest_document()\nembed + upsert to Chroma"]
end
User --> QR
User --> IR
QR --> E
IR --> LD --> CH --> IG
E --> EMB
E --> R
R --> RET
R --> SP
SP --> TK
SP --> SYM
SP --> ASYM
SYM --> SC
ASYM --> AC
AC --> LLM
SC --> RE
AC --> RE
RE --> EMB
RE --> RK
TK --> GEN
RK --> GEN
GEN --> LLM
GEN --> RS
RS --> RR
RET --> CR
style TK fill:#e8f4e8
style SYM fill:#e8f0fb
style ASYM fill:#fef3e2
style Strategies fill:#fafafa
style Executor fill:#fafafa
style Planner fill:#fafafa
mergerag/
core/ models, ports, utils
ingestion/ loader, chunker, ingest
merge/ planner, executor, strategies/
adapters/ embedder, llm, retriever, sqlite stores
eval/ benchmark orchestrator, scorer
api/ FastAPI app and routes
app/
streamlit_app.py
api_client.py
helpers.py
scripts/
benchmark_cli.py
ingest_cli.py
profile_latency.py
data/
hotpotqa_dev_distractor.json
chroma/
results/
hotpot_dev_500.json
benchmark_findings.md
You need Python 3.11+, uv, and Ollama running locally with qwen2.5:7b pulled (default since v4). qwen2.5:3b also works and was used for benchmark runs v1–v3; switch via OLLAMA_MODEL.
uv sync
ollama pull qwen2.5:7bStart the API server:
uv run uvicorn mergerag.api.app:app --reloadStart the Streamlit UI (runs all three strategies side-by-side):
uv run streamlit run app/streamlit_app.pyIngest a document:
uv run python scripts/ingest_cli.py \
--path path/to/doc.txt \
--collection my_collection \
--persist-path data/chromaRun the benchmark:
uv run python scripts/benchmark_cli.py \
--fixture data/hotpotqa_dev_distractor.json \
--collection hotpot_dev_500 \
--limit 500 \
--persist-path data/chroma \
--output results/hotpot_dev_500.jsonRun a development benchmark only on examples where asymmetric would actually merge:
uv run python scripts/benchmark_cli.py \
--fixture data/hotpotqa_dev_distractor.json \
--collection hotpot_dev_500_active \
--limit 500 \
--persist-path data/chroma \
--retriever hybrid \
--active-asymmetric-only \
--output results/hotpot_dev_500_active.jsonProfile per-stage latency:
uv run python scripts/profile_latency.py --n 100Run tests:
uv run pytestThe API server reads these environment variables, all optional:
CHROMA_PERSIST_PATH — path to a persisted Chroma index (default: ephemeral).
RUN_STORE_PATH — SQLite path for run traces (default: runs.db).
EMBEDDING_MODEL — SentenceTransformer model name (default: all-MiniLM-L6-v2).
OLLAMA_MODEL — Ollama model name (default: qwen2.5:7b; benchmarks v1–v3 used qwen2.5:3b).
DEFAULT_TOP_N — candidates retrieved before merging (default: 20).
DEFAULT_TOP_K — final context window size (default: 5).
DEFAULT_STRONG_K — strong anchor count for merge planning (default: 5).
DEFAULT_TOKEN_BUDGET — soft token cap on final_context after rerank, enforced by the pipeline (default: 2048; set 0 to disable).
DEFAULT_ASYMMETRIC_MAX_OPS — max asymmetric merge LLM calls per query (default: 1).
DEFAULT_RETRIEVER — chroma (dense) or hybrid (BM25 + dense, RRF) — default retriever when the request omits it (default: chroma).
MAX_UPLOAD_BYTES — /ingest upload size cap; oversize uploads return 413 (default: 10_000_000).
The FastAPI app constructs one shared chromadb client in lifespan and exposes it on app.state.chroma_client, so /ingest and /query always see the same collections — critical for the ephemeral (no CHROMA_PERSIST_PATH) case. HybridRetriever instances are cached per (collection, persist_path); /ingest refreshes the cached sparse BM25 index, /collections DELETE clears the cache.
All runs: HotpotQA distractor dev set, n=500, qwen2.5:3b.
Dense retrieval (ChromaRetriever, v2)
| strategy | EM | F1 | latency_ms |
|---|---|---|---|
| top_k | 0.244 | 0.336 | 3646 |
| symmetric† | 0.246 | 0.336 | 3796 |
| asymmetric | 0.252 | 0.346 | 4817 |
† concatenation (no LLM synthesis).
Hybrid retrieval (HybridRetriever: BM25 + dense, RRF k=60, v3)
| strategy | EM | F1 | latency_ms |
|---|---|---|---|
| top_k | 0.240 | 0.320 | 3833 |
| symmetric | 0.240 | 0.320 | 3219 |
| asymmetric | 0.240 | 0.320 | 3495 |
Hybrid EM is flat vs dense-only. The BM25 arm fixes targeted entity recall (verified on "Scott Derrickson nationality") but the RRF pool introduces noise from HotpotQA's topically similar distractor paragraphs that cancels the gain at aggregate level. See results/benchmark_findings.md for full analysis.
Model upgrade (v4, qwen2.5:7b, n=100)
| strategy | EM | F1 | latency_ms |
|---|---|---|---|
| top_k | 0.20 | 0.27 | 4228 |
| symmetric | 0.23 | 0.30 | 3691 |
| asymmetric | 0.22 | 0.26 | 4477 |
Default model switched to qwen2.5:7b after the 3b model contradicted itself on multi-hop comparison questions (e.g. answering "No" to a same-nationality query while citing both subjects as American). 7b reasons correctly on clean 2-chunk contexts but is still derailed by surname-overlap distractors when the correct chunk sits beyond the default top_k=5. Aggregate EM at n=100 is within noise of the 3b baseline. A CoT prompt variant was tried and reverted (caused entity-name regressions on non-comparison questions). See findings doc.
Queries that depend on disambiguating an entity by full name against same-surname distractors (e.g. multiple "Scotts" in the pool) can still fail when the correct chunk is outside the default context window. Workarounds:
- bump
top_kvia the StreamlitPipeline paramssliders (7+ surfaces the chunk on the Scott_Derrickson case) - switch to the
hybridretriever for entity-name queries
All benchmarks were run on the HotpotQA distractor setting — a multi-hop QA dataset where each question comes with ten candidate paragraphs (only two of which are actually supporting), making it a good stress test for retrieval and context selection. We used the first 500 examples from the development set.
The asymmetric strategy is where merging pays off most visibly. These are the kinds of questions it got right when top_k failed:
- Kaiser Ventures corporation was founded by an American industrialist who became known as the father of modern American shipbuilding? — asymmetric correctly returned Henry J. Kaiser by anchoring the Kaiser Ventures chunk to the Kaiser Shipyards chunk. top_k returned the company name.
- The Livesey Hall War Memorial commemorates the fallen of which war, that had over 60 million casualties? — asymmetric returned World War II; top_k returned World War I. The second hop (casualty count) was only recoverable once the two supporting paragraphs were merged.
- What material did a hairdresser from Yorkshire, England invent that was named by his granddaughter? — asymmetric returned Starlite by connecting the inventor biography chunk to the material description chunk. top_k hallucinated "Eaglebeak."
- Which film was released first: Sacred Planet or Oz the Great and Powerful? — a comparison question where asymmetric correctly returned Sacred Planet by synthesizing release date information spread across two chunks.
- The city that contains the Yunnan Provincial Museum is also known by what nickname? — asymmetric returned Spring city after anchoring the museum location chunk to the city overview chunk. top_k could not bridge the two.
Across the 500-example run, asymmetric flipped 13 questions from wrong to right that top_k got incorrect, while the baseline strategy never had access to merged context to make those connections.
Yang, Z., Qi, P., Zhang, S., Bengio, Y., Cohen, W. W., Salakhutdinov, R., & Manning, C. D. (2018). HotpotQA: A Dataset for Diverse, Explainable Multi-hop Question Answering. Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing (EMNLP). https://arxiv.org/abs/1809.09600