End-to-end RAG pipeline#
One query: Pipeline.query#
Tasks in code#
| Task | Code |
|---|
| Fixed window chunks | fixed_window(doc_id, text, size=400, overlap=80) |
|---|
| Chunk at headings | structural_markdown(doc_id, text) |
|---|
| Score a chunker | eval_recall(chunker, fixture, ks=(1, 3, 5)) |
|---|
| Fuse ranked lists | rrf(rankings, k=60, weights=[1.0, 1.0]) |
|---|
| Retrieve through a rewriter | retrieve_with_rewriter(query, rewriter, retriever) |
|---|
| Rerank candidates | rerank(model, query, candidates, top_k) |
|---|
| Graded ranking score | ndcg_at_k(retrieved, graded, k) |
|---|
| Run the gated demo | python3 code/main.py exits 1 on a FAIL |
|---|
Hybrid retrieval with RRF#
retriever = HybridRetriever()BM25Index + DenseIndex
for d in CORPUS:
retriever.add(d)indexes both modalities
result = retriever.search(q, k_each=5, k_out=5)
result["bm25"], result["dense"], result["fused"]three ranked lists
bm25 = BM25Index(k1=1.5, b=0.75)title tokens count 3 times
idf = math.log((n - df + 0.5) / (df + 0.5) + 1.0)stays positive
score[doc.doc_id] += w * (1.0 / (k + rank + 1))rank starts at 0
Assemble and query the pipeline#
p = Pipeline()chunker, index, rewriter, reranker
p.ingest(CORPUS)recursive split, target 400
p.train_reranker_on(TRAIN_TRIPLES)fits the small cross-encoder
result = p.query("<question>")
result.answersentences plus anchors
result.citationslist of doc_id:chunk_index
result.rewriter_strategyhyde, multiquery or decompose
result.latency_msrewrite, retrieve, rerank, generate
Tips#
- Start with recursive 512 tokens. Measure recall@5 on a 50-query eval set.
- Tell the model to use only the context and to say "I don't know."
- Pin the bi-encoder, cross-encoder and N together.
- Judge with a different model family than the generator.
- Slice qrels by query class: literal, paraphrased, multi-topic.
- Tier the eval: 20 queries in CI, 200 nightly, 2000 weekly.