Files
rag-lab/README.md
T
2026-10-28 20:44:22 +07:00

3.8 KiB

rag-lab

Hybrid retrieval with measured quality: lexical search, dense vectors and rank fusion in PostgreSQL, with an honest evaluation on a public benchmark instead of a demo that looks nice.

The point of this repository is not "I built a RAG". It is "I can tell you what each retrieval decision is worth in recall and latency, and I can show the code that produced the numbers".

What is measured

Benchmark: BEIR SciFact — 5,183 scientific documents, 339 test queries with graded relevance judgements. Same corpus, same queries, four modes:

mode recall@1 recall@10 MRR@10 nDCG@10 p50, ms p95, ms
lexical (BM25, k1=1.2 b=0.75) 0.517 0.759 0.614 0.645 66.2 87.0
dense (all-MiniLM-L6-v2, cosine, HNSW) 0.484 0.817 0.618 0.662 39.8 62.8
hybrid (reciprocal rank fusion) 0.540 0.830 0.654 0.691 114.1 264.9
hybrid + cross-encoder rerank running running running running running running

Numbers are produced by scripts/evaluate.py and stored in results/eval.json. Nothing in the table is written by hand. 300 of 339 test queries were used (the rest have no corpus document to retrieve). Metrics are computed per document: chunks of one document are collapsed in rank order before scoring, otherwise a document with many chunks would inflate recall and nDCG.

Reference point: the published BM25 baseline for SciFact in BEIR is nDCG@10 about 0.66, so the lexical column is in the right ballpark instead of looking artificially weak.

Architecture

queries ──┬── lexical:  BM25 in Python over the same chunk table
          ├── dense:    bge-small-en -> vector(384)  (HNSW, cosine)
          └── fusion:   reciprocal rank fusion, k=60
                          └── optional cross-encoder rerank (top-N -> top-k)
  • chunks table keeps doc_id, heading trail, text, tsvector and vector(384).
  • A tsvector column and GIN index are kept by a trigger as well; BM25 was chosen for the measured numbers, tsvector remains available as a mode for comparison.
  • HNSW index uses m=16, ef_construction=64.
  • Chunking is markdown-aware: heading trail is preserved, so a retrieved fragment is readable on its own.

Why these choices

Lexical is not a legacy fallback. In scientific text exact terms and identifiers matter, and BM25-style matching often beats dense retrieval there. Dense wins on paraphrases. Fusion keeps both without tuning a weight per query.

RRF instead of a weighted sum. Rank fusion needs no score calibration between two incomparable scales (cosine similarity and ts_rank_cd).

Reranker is optional and degradable. Cross-encoders are expensive; if the model cannot be loaded, the service returns the fused order instead of failing the request.

Run locally

python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"

createdb rag_lab
psql -d rag_lab -c "CREATE EXTENSION IF NOT EXISTS vector;"

make data      # download BEIR SciFact
make ingest    # chunk + embed + index
make eval      # four modes, metrics, latency
make api       # http://127.0.0.1:8088/docs

Environment: RAGLAB_DSN, RAGLAB_EMBED_MODEL, RAGLAB_RERANK_MODEL, RAGLAB_LEXICAL_CONFIG.

API

GET /health
GET /search?q=...&mode=hybrid|dense|lexical|tsvector&k=10&rerank=false

Tests

pytest -q

Pure functions only: metric formulas, fusion order, chunk boundaries. No database needed.

Limitations

  • SciFact is English and scientific; numbers do not transfer to chat logs or Russian support text.
  • Chunking is markdown-aware, not layout-aware: PDFs and HTML need a parser in front.
  • No query rewriting, no HyDE, no multi-vector retrieval yet. Those are separate experiments with their own measurements, not guesses.