Retrieval-Augmented Generation tutorial
Learn how AI apps answer questions from your own documents, then build, tune and evaluate a full RAG pipeline in your browser.
Course contents
RAG Foundations
- RAG Basics — What Retrieval-Augmented Generation is and why it matters: retrieve, augment, generate; RAG vs fine-tuning vs long context; the end-to-end architecture; grounding, fewer hallucinations, fresh data, citations and real use cases.
- RAG Pipeline Architecture — The four RAG stages and how data flows between them: chunking, embedding, retrieval and generation; query processing, candidate selection, context packing and citation validation; determinism and the three runtime tiers.
- Corpus Engineering — Choosing and cleaning the documents a RAG system searches: document and ticket corpora, core set vs outer volume, near-duplicates, outdated contradictions, unanswerable questions, frozen snapshots, gold sets and reference chunks.
Document Processing
- Document Ingestion — Turning PDFs, Word files, Markdown and HTML into clean, structured text: parsing, keeping headings, tables and footnotes, multi-column and OCR, cleaning and normalization, boilerplate removal and section trees.
- Metadata Tagging — Extracting and attaching metadata to chunks: title, section path, author, dates, versions and access tags; header injection, filter labels and metadata-aware assembly; indexed fields, path tags and time and place scoping.
- Chunking Strategies — The main ways to split documents for RAG: fixed-length by characters or tokens with sentence fallbacks, sentence, paragraph, section and header-aware chunking, and semantic chunking at topic shifts.
- Chunk Optimization — Tuning chunks for retrieval: sliding-window overlap and ratios, over- vs under-chunking, sizing to queries, standalone completeness, orphans, pronoun resolution, benchmarking, contextual headers and parent-child chunks.
Vector Search
- Vector Embeddings — How text becomes vectors that capture meaning: dense vectors and dimensions, embedding models and how to choose them, L2 normalization, mean vs CLS pooling, and running small quantized models in the browser.
- Similarity Search — Finding the nearest vectors: cosine, dot product and Euclidean distance, top-k selection, a flat in-memory index, and visualizing vector space with PCA, query projection and clusters.
- Vector Indexing — Storing and searching vectors at scale: vector database basics, persistence, flat vs approximate search, recall, IVF and HNSW indexes and their settings, and how to choose an index.
Retrieval Engine
- Top-K Retrieval — Turning a question into the right few chunks: score cutoffs, top-k selection, dynamic thresholds, the search flow and its latency, deduplication, out-of-distribution checks, fallbacks and corrective retrieval.
- Hybrid Search — Combining keyword and vector search: TF-IDF and BM25, dense search, normalizing and weighting scores, Reciprocal Rank Fusion with k=60, alpha calibration, parallel execution, acronym boosts and exact-match overrides.
- Metadata Filtering — Restricting search with metadata: pre-retrieval hard filters, scoping and partitioning, tenant isolation, post-filtering, missing metadata, conflict rules, soft penalties, AND/OR expressions, bitmaps and cardinality.
- Query Transformation — Improving questions before search: disambiguation and paraphrasing, conversational rewriting, query routing, sub-queries, HyDE, context-aware expansion, vague queries and multi-query fusion with RRF.
Context Refinement
- Reranking — Two-stage retrieval and rerankers: high-recall first stage, high-precision second stage, pool sizing and latency, MMR diversity, cross-encoders vs bi-encoders, joint attention, reranker models and score normalization.
- Context Packing — Packing retrieved chunks into the prompt: token budgets, removing overlaps, lost-in-the-middle ordering, source labels and ids, chronological vs relevance order, source enclosures, metadata and visible system instructions.
Grounded Generation
- Grounded Prompting — Writing prompts that keep answers tied to the sources: grounding rules, exact refusals, temperature 0 and seeds, XML delimiters, system vs user placement, and defending against injection hidden in chunks.
- Citation Tracing — Linking every claim to its source and checking it: chunk ids, sections and pages, the citation-first pattern, claim verification, structured citation schemas, fake and missing citations, and streaming with citations.
- Unanswerable Queries — Knowing when not to answer: detecting missing evidence, refusal triggers, surfacing conflicting sources, avoiding speculation and false premises, partly answerable questions, and edge-case test suites.
Advanced RAG
- Multimodal RAG — Retrieving from images, screenshots, tables and scanned pages: captions and OCR, tables as Markdown or CSV, OCR clean-up, layout analysis, shared text-image vector spaces and multi-vector page matching.
- Advanced Retrieval Patterns — Handling questions that need several steps: decomposition, stepwise and multi-hop retrieval, agentic RAG with tool loops and stop rules, Self-RAG reflection, and GraphRAG entity graphs and community summaries.
- RAG Security — Securing RAG systems: RBAC and tenant filters before retrieval, server-side scope, indirect prompt injection, data vs instruction layers and trust levels, PII redaction, retention and deletion, encryption and audit logs.
Production
- RAG Evaluation — Measuring RAG with numbers: Precision@K, Recall@K, hit rate and MRR; faithfulness, context relevance and citation accuracy; gold chunk matching, test suites, the RAG Triad and LLM-as-judge.
- Production Operations — Running RAG in production: full traces, bottlenecks and per-stage latency, re-indexing, eviction and versioning, monitoring and low-similarity alerts, feedback loops, and prompt and response caching.
- RAG Reference — A quick-reference page for RAG: the pipeline at a glance, RAG vs alternatives, metric and scoring formulas, starting settings, a grounded prompt template, troubleshooting, security checklist and glossary.
Development Workflows
- RAG Interface Design — Designing the interface of a RAG assistant: answer states, streaming, citation chips, refusal and error states, reading server-sent events in the frontend, accessibility and feedback that feeds evaluation.
- RAG Service Backend — Building the backend service behind a RAG assistant: API design, access filters from the session, validation and errors, streaming with checked citations, safe caching, and ingestion and deletion as background jobs.
- Engineering Knowledge Assistant — Building an internal RAG assistant over engineering docs for UI/UX, frontend, backend, security and testing: what to index, team-based access, secret redaction, the gold set in CI and end-to-end UI tests.