The grounding problem Welcome back. In this lecture, we're diving into RAG: How Retrieval-Augmented Generation Grounds an LLM. Let's get started. Picture this: a customer asks your brand new support bot about refunds, and it cheerfully promises thirty days. Your policy page says five. That model isn't broken; it simply relies on parametric memory—static weights frozen during pre-training that never saw your private docs. It cannot recall facts it was never shown, so it guesses based on generic patterns from the public web. RAG bridges this gap with non-parametric memory, dynamically pairing external knowledge stores with the language model so answers stay grounded in verified facts. It splits into two phases. Index offline, retrieve online First, offline indexing chops company documents into chunks, converts them into numerical embedding vectors, and stores them in a searchable index so raw text becomes mathematically searchable long before any user arrives. Then, online querying retrieves the relevant chunks on the fly and stuffs them straight into the prompt, giving the model exact citations to answer with instead of forcing it to guess from raw memory. Semantic space and cosine similarity To search across text automatically, we translate meaning into geometry. An embedding model maps text passages into a multi-dimensional semantic space. Nearby points share concepts, so our refund-policy chunk and bank-timing chunk cluster tightly together, while a shipping chunk sits way out on its own. When a user asks 'How do refunds work?', that question vector lands right inside the refund cluster. We measure this closeness using cosine similarity—the dot product of the vectors divided by their lengths. A tiny angle means a score close to one, instantly identifying our most relevant context before we ever touch an LLM. The pipeline in code Let's connect this geometry directly to code. In our query function, we first pass the raw question to embed, producing our dense vector. Next, we search the vector index for the top three nearest hits. Watch the pipeline calculate scores: refund-policy scores zero point eight two, bank timing gets zero point seven eight, and policy exceptions gets zero point six four, while shipping drops out at zero point one one. We then format those winning hits into numbered passages and wrap them in a strict prompt instruction: 'Answer using only the context.' Finally, the language model generates an accurate response, citing chunks one and two with confidence. Debug retrieval first When a RAG system answers poorly, developers often rush to tweak model prompts. Most failures are actually retrieval failures, happening long before generation starts. If your gold chunk was stuck at rank seven when top-k was three, the model never had a chance because the necessary facts were omitted entirely from the prompt context. Always debug retrieval first by checking recall at k to ensure your ground truth appears in the retrieved candidates. Next, diagnose your chunk size: chunks that are too tiny lose surrounding context, while giant chunks dilute vector embeddings and flood your token budget. Also watch out for stale data—if last year's policy lingers in the index, you need metadata version filters to discard deprecated documents before ranking. If the model still ignores context, enforce explicit negative constraints. Production patterns, trade-offs and recap In real-world production, these components power engineering assistants searching codebases, customer support portals linking help articles, and legal engines citing specific contract clauses. In system design interviews, expect this classic trade-off: when do you fine-tune instead of using RAG? Remember the golden rule: RAG provides dynamic, verifiable knowledge with citations; fine-tuning changes behavior, style, and tone. To evaluate production RAG, track recall at k and Mean Reciprocal Rank for your retriever, and faithfulness for your generator. Retrieve first, generate second, verify the chunks, and always cite your sources. And that's a wrap on this lecture. Thanks for watching, and I'll see you in the next one.