Vector Databases and RAG Tutorial
Learn Vector Databases and RAG step by step: 36 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Retrieval-Augmented Generation? Learn what retrieval-augmented generation (RAG) is, why language models need it, how the retrieve-then-generate pipeline works, and build a tiny one in Python.
- What Is a Vector Database? Understand what a vector database stores, how it differs from SQL and keyword search engines, and build a minimal in-memory vector store with NumPy.
- Your First RAG Pipeline Build a complete retrieve-and-answer RAG pipeline in Python: chunk documents, embed with TF-IDF vectors, search by cosine similarity, build the prompt and cite sources.
- The Vector Database Landscape Compare vector search libraries, database extensions and dedicated vector databases such as FAISS, pgvector, Qdrant, Milvus, Weaviate, Chroma and Pinecone, and learn how to choose.
Embeddings and Similarity
- What Are Embeddings? Understand embeddings as points in space where similar things sit close together, then build word vectors from scratch with co-occurrence counts and SVD in NumPy.
- Similarity Metrics Compare cosine similarity, dot product and Euclidean distance, see when they agree and when they differ, and learn which metric to configure in a vector database.
- Embedding Models and Dimensions Learn how text embedding models differ, what dimensionality costs in memory and speed, how truncation works, and how to choose and test a model for your RAG data.
- TF-IDF and LSA: Embeddings Without a Neural Network Build sparse TF-IDF vectors and dense LSA embeddings with scikit-learn, see how dense vectors bridge vocabulary gaps, and learn why this still matters for RAG.
- Embedding Geometry and the Curse of Dimensionality See why distances behave strangely in high dimensions, what hubness and anisotropy do to nearest-neighbor search, and how centering and thresholds keep scores meaningful.
Indexing and ANN Search
- Exact Search, ANN and the Recall Tradeoff Learn brute-force vector search, why it stops scaling, what approximate nearest neighbor (ANN) search trades away, and how to measure recall against exact results.
- HNSW: Navigating a Graph of Neighbors Understand how HNSW indexes work: greedy graph search, layers, and the M, efConstruction and efSearch settings, with a runnable graph index you build yourself.
- IVF: Searching Only the Nearest Clusters Learn how inverted file (IVF) vector indexes use k-means clusters and the nprobe setting to search a fraction of the data, with a complete Python implementation.
- Product Quantization and Vector Compression Shrink vector indexes 4x to 64x with scalar, binary and product quantization. Build each in NumPy, measure recall, and learn when rescoring recovers accuracy.
- Locality-Sensitive Hashing Learn how random-hyperplane LSH hashes similar vectors into the same buckets, how bits and tables trade recall for speed, and when LSH beats or loses to HNSW.
- Tuning and Benchmarking Vector Indexes Learn a repeatable method to benchmark ANN indexes: ground truth, recall-versus-cost curves, picking the cheapest setting for a target recall, and checking worst-case queries.
Chunking and Ingestion
- Loading and Cleaning Documents Turn HTML, PDF text and mixed files into clean documents for RAG: extract text, strip boilerplate, fix hyphenation and Unicode, and add quality checks before chunking.
- Chunking Strategies Learn how to split documents into chunks for RAG: fixed-size, overlap, sentence and recursive splitting, how chunk size changes retrieval, and how to choose and test a strategy.
- Structure-Aware and Semantic Chunking Split by headings, code blocks and tables, add heading breadcrumbs, detect topic shifts with embeddings, and retrieve small chunks while returning their larger parents.
- Metadata and Ingestion Pipelines Design a reliable RAG ingestion pipeline: chunk metadata, stable ids and content hashes, exact and near-duplicate removal with MinHash, incremental sync, batching and error handling.
Retrieval Strategies
- Retrieval Basics, Top-k and Score Thresholds Learn how a RAG retriever turns a question into ranked chunks, how to choose top-k, why score thresholds matter, and how to detect questions with no answer.
- BM25 and Keyword Search for RAG Build BM25 from scratch in Python, learn what k1 and b do, and see why exact keyword matching still beats embeddings for codes, names and rare terms.
- Hybrid Search and Reciprocal Rank Fusion Learn hybrid search for RAG: run keyword and vector retrieval together and merge the rankings with reciprocal rank fusion or weighted score fusion, in Python.
- Metadata Filtering and Query Transformation Learn pre-filter vs post-filter search, how to extract filters from a question (self-query), and how query expansion and multi-query retrieval raise recall.
- Re-ranking and Maximal Marginal Relevance Learn the two-stage retrieve-then-rerank pattern, how cross-encoders differ from bi-encoders, and how MMR removes near-duplicate chunks from RAG context.
Generation and Prompting
- RAG Prompt Templates and Context Assembly Learn how to write a RAG prompt, label sources, fit chunks into a token budget, order them to avoid the lost-in-the-middle effect, and handle empty context.
- Citations and Grounded Answers in RAG Make RAG answers verifiable: number sources, ask for structured citations, validate them in code, detect unsupported claims and refuse when evidence is missing.
- Conversational RAG and Query Rewriting Handle follow-up questions in a RAG chatbot: rewrite each turn into a standalone query, manage chat history within a budget, and decide when to retrieve again.
- Advanced RAG: HyDE, Parent Documents, Corrective and Agentic RAG Go beyond basic RAG: hypothetical document embeddings, parent-document retrieval, query decomposition, corrective retrieval loops and agentic RAG, with code.
Evaluation
- Retrieval Metrics: Recall, Precision, MRR and nDCG Measure retrieval quality with hit rate, recall@k, precision@k, MRR and nDCG. Implement each in Python and compare two retrievers on the same labelled queries.
- Evaluating Generation: Faithfulness and Answer Quality Evaluate the answer half of RAG: faithfulness, answer relevance, context precision and correctness, how LLM-as-judge works, and cheap automatic checks.
- Building an Evaluation Set for RAG Create a RAG evaluation set from real queries and synthetic questions, include unanswerable cases, split dev and test, and report results with confidence intervals.
Production Patterns
- Caching, Latency and Cost in RAG Systems Budget RAG latency stage by stage, cache embeddings and answers (exact and semantic), estimate token cost per query, and apply cheap optimizations safely.
- Updates, Deletes and Index Versioning Keep a RAG index fresh: sync by content hash, delete chunks for removed documents, handle stale data, and migrate embedding models with a blue-green index swap.
- Security, Access Control and Prompt Injection in RAG Secure a RAG system: enforce document permissions at retrieval time, isolate tenants, defend against prompt injection in retrieved text, and redact personal data.
- Observability and Common RAG Failure Modes Trace every RAG request, monitor retrieval scores and refusal rate, collect feedback, and use a checklist to diagnose the ten most common RAG failure modes.