Retrieval Augmented Generation

Grounding the model in your knowledge, not just its own

Outline

  1. Why retrieval — the problem RAG solves, and the alternatives
  2. Building the index — chunking, embeddings, vector databases
  3. How similarity works — feature spaces, distance, nearest neighbors
  4. Making retrieval better — fine-tuned embeddings and reranking
  5. Measuring quality — precision, recall, and what to hold your team to
  6. From retrieval to answers — grounded generation and the prompt

Why Retrieval?

The problem RAG solves — and the two alternatives

Three ways to give a model your knowledge

1. Put it in the prompt

Paste the documents in every time. Simple, immediate, always current.

Breaks when: the corpus is bigger than the context window, or you pay for the same 200 pages on every question.

2. Fine-tune the model

Continue training on your material so the knowledge is "in the weights."

Breaks when: facts change. Fine-tuning is excellent at teaching form — tone, format, task behaviour — and unreliable at teaching facts. It cannot cite, and it cannot forget on request.

3. Retrieve it on demand

Search your documents at question time and hand the model only the relevant passages.

Why it wins: current, citable, permission-aware, and updated by re-indexing a document rather than retraining a model.

These are not mutually exclusive. Production systems routinely retrieve and fine-tune — retrieval for the facts, fine-tuning for the behaviour.

Building the Index

From a pile of documents to something you can search by meaning

The index is a set of decisions, not a database

Chunk size & overlapToo big retrieves noise, too small loses context. Drive it from document structure.
Embedding modelFixes the geometry of your search space. Changing it means re-embedding everything.
Metadata schemaWhat you can filter on later — source, date, owner, department, sensitivity.
Refresh cadenceHow stale answers are allowed to be, and what happens when a document is withdrawn.
Access controlEnforced at retrieval time, inherited from the source system — not re-invented in the chatbot.
What stays outDrafts, superseded policy, personal data. The index is a publication decision.

How Similarity Works

Feature spaces, distance metrics, and nearest neighbors

Making Retrieval Better

Fine-tuned embeddings and reranking

Does It Work?

Measuring a retrieval system honestly

What to hold the team to

QuestionMetricFailure it catches
Did we find the right passage at all?Recall@kChunking and indexing problems — the answer is not in the shortlist
Did we put it near the top?MAP / MRRRanking problems — fixable with reranking
Is the answer supported by what we retrieved?GroundednessThe model inventing detail the sources do not contain
Do the citations point at the right place?Citation accuracyPlausible-looking references to the wrong document
Does it answer what was asked?Answer relevanceCorrect, well-sourced, off-topic responses
Can we afford it at volume?Latency & cost / queryA pilot that cannot survive its own success

The first two measure retrieval; the next three measure generation. A system can fail either half, and the fixes are completely different — so insist on both being reported separately.

From Retrieval to Answers

Grounded generation and the prompt

Where RAG actually breaks

The passage was never retrievedSplit across a chunk boundary, or phrased so differently from the query that the embedding missed it. The model answers from second-best sources and sounds fine.
The index is staleThe policy changed on Monday; the index refreshed on Friday. Confidently current, factually obsolete.
Conflicting sourcesThree versions of the same document, all retrievable. Similarity has no notion of authority — it cannot tell you which one is in force.
Permission leakageRetrieval that ignores who is asking turns a chatbot into a very efficient document exfiltration tool.
Citations that do not check outThe answer is right, the reference points somewhere else. Erodes trust faster than being wrong.
No graceful "I don't know"When retrieval returns nothing useful, an unconstrained model fills the gap from memory — the single most damaging failure mode.

Six questions for your next design review

  1. What is in the index, who decided, and what was deliberately left out?
  2. How does retrieval know who is asking, and where is that enforced?
  3. How fresh is the index, and what happens when a document is withdrawn?
  4. Show me the golden set — and retrieval and generation scored separately.
  5. What does the system do when it cannot find a good answer?
  6. What is the cost and latency per query at ten times this volume?

Class Discussion

Discussion Question 1

Retrieve, Fine-tune, or Both?

Your firm wants an assistant that answers client questions about your service agreements. The agreements are renegotiated constantly, and the answers must sound like your firm’s house style.

Which knowledge goes in the index and which goes in the model — and how would you justify the split to a skeptical CFO?

Discussion Question 2

The Chunking Decision

A compliance manual contains numbered clauses, many with exceptions that appear two or three paragraphs after the rule itself. Your vendor proposes chunking every 500 tokens with no overlap.

What goes wrong, how would you detect it before go-live, and what would you propose instead?

Discussion Question 3

Similarity Is Not Authority

Your shared drive holds the 2023, 2024 and 2025 travel policies. All three are indexed. A user asks what the mileage reimbursement rate is, and the system returns the 2023 rate with a confident citation.

Whose failure is this, and what are three different places in the architecture where it could be fixed?

Discussion Question 4

Proving It Works

A vendor demos a RAG assistant on your documents. Every question they ask it is answered beautifully. They quote “92% accuracy.”

What do you ask them before signing, and what would you require in a pilot to generate evidence you would actually trust?

Discussion Question 5

Synthesis

You are sponsoring a RAG assistant over HR policies, benefits documents, and the employee handbook, available to all 4,000 employees. Legal is nervous, HR is enthusiastic, and IT has a vector database licence already.

Identify the three biggest risks, name one concrete mitigation for each, and state the single metric you would put in front of the steering committee each month. What would make you stop the project?

Takeaways

RAG is a search problem wearing an AI costumeThe language model is the last and smallest step. Quality is decided in chunking, embedding, and ranking.
"Similar" is a modelling choiceMetric, chunk size, and $k$ all change which evidence reaches the model — and therefore the answer.
A better model cannot fix bad retrievalIt only makes the wrong answer more persuasive.
Without a golden set, you have no evidenceYou have demos. Insist on retrieval and generation measured separately, and re-measured on every change.

Figure Credits

Jay Alammar & Maarten Grootendorst, Hands-On Large Language Models (O'Reilly, 2024) Semantic search and reranking overview; document → chunk → vector-database pipeline; bag-of-words and word2vec; dense retrieval; token, word, sentence and document embeddings; chunking strategies; nearest-neighbor retrieval; embedding fine-tuning; two-stage reranking; mean average precision; the RAG pipeline; and the elements of a prompt.
John D. Kelleher, Brian Mac Namee & Aoife D'Arcy, Fundamentals of Machine Learning for Predictive Data Analytics (MIT Press) The athlete draft dataset; feature space and similarity metrics; Euclidean and Manhattan distance; the nearest-neighbor algorithm; Voronoi tessellation and decision boundaries; k-nearest-neighbors; and feature pre-processing and normalisation.
Cosine similarity worked example Sasi Kumar, “Cosine Similarity vs Euclidean Distance,” Medium — credited on the figure itself.

Figures are reproduced for classroom instruction. Titles and captions were adapted for this deck; please verify each citation against your own copy before redistributing the slides.

◀ Slides