Build your own RAG pipeline
Retrieval & RAG·Core · 4–8h

Build your own RAG pipeline

Chunk, embed, retrieve, assemble — end to end.

What you'll build

You'll build the retrieval and context-assembly half of a RAG pipeline in TypeScript, end to end: chunk a frozen SQuAD corpus three different ways, embed and index the chunks, retrieve top-k with MMR de-duplication, and pack a token-budgeted context block with [doc i] citations. Because SQuAD answers are verbatim spans, you prove the context actually contains the answer with an exact string check — and prove your citations point at the right chunk — with no LLM judge anywhere.

  • Three real chunkers — fixed-size with token overlap, the LangChain recursive splitter, and a LlamaIndex-style sentence-window splitter — emitting token-exact chunk boundaries
  • A retrieval stage over ONNX MiniLM embeddings with MMR de-duplication, plus token-budget context assembly that formats [doc i] citation markers and tracks sources
  • A retrieve→assemble→generate→cite loop where the generator is a deterministic cassette keyed by the exact assembled prompt
  • A CLI — theinference rag ask "<question>" — that prints the exact context block, the grounded answer, its citations, and a flag proving the gold answer was present

What you'll learn

  • How a RAG pipeline really decomposes — and why most failures are retrieval, not the model: the answer never made the top-k, or the citation pointed at the wrong chunk
  • How chunk size and chunk strategy move retrieval quality, measured in a real ablation rather than guessed
  • How to evaluate retrieval deterministically with recall@k, MRR, and answer-span hit-rate@k over gold chunk ids — no LLM grading itself
  • How to enforce citation integrity by checking the cited chunk actually contains the verbatim answer span

Prerequisites

  • Comfortable reading and running TypeScript
  • Familiar with embeddings and cosine similarity — vectors that represent text, compared by dot product (the vector-db / semantic-search basics)
  • No prior RAG experience required — we build chunking, retrieval, and the metrics from scratch

Get started

Everything runs locally — no GPU, no API keys. Clear stage 1 in under ten minutes.

# 1. log in (approve in your browser — required)
npx theinference login

# 2. scaffold the course
npx theinference course start rag-pipeline ./rag-pipeline

# 3. install dependencies
cd rag-pipeline && npm install

# 4. grade the first stage
npx theinference test --stage run-the-grader --watch

Each lesson shows the exact --stage command for that stage — passing it marks the stage complete here automatically.