2026-05-26
Core session 4
How StatsChat chooses which chunks to show the LLM.
A. The LLM reads every PDF
B. The system searches for relevant evidence chunks
C. The system downloads all reports again
D. The answer is generated from model memory
Suggested answer: B — by query time, the searchable index has already been prepared.
Retrieval is a small step in the whole pipeline, but it controls what evidence reaches the LLM.
In a RAG system, retrieval is the step that decides:
Guiding question: what evidence should StatsChat pass to the LLM for this question?
Search usually means:
Retrieval for RAG has an extra job:
In RAG, retrieval is not just finding results. It is selecting the evidence that shapes the answer.
A chunk can be a good search result but weak evidence.
| Chunk type | Search result? | Good evidence? |
|---|---|---|
| Mentions the right topic | Maybe | Not necessarily |
| Comes from the right report | Probably | Not always |
| Contains the exact figure, date and unit | Yes | Usually |
| Explains the definition but not the value | Maybe | Depends on the question |
| Comes from an old release | Maybe | Risky for “latest” questions |
Retrieval needs to ask: does this chunk help answer this specific question safely?
If you built a StatsChat-like tool for your own organisation, retrieval would decide whether users see the right evidence.
Think of one publication or data source from your own organisation.
Retrieval means:
finding the pieces of source material most likely to help answer the user’s question.
A. Whole PDFs
B. Whole websites
C. Text chunks with metadata
D. Only publication titles
Suggested answer: C — the evidence unit is usually a retrieved chunk, carrying metadata such as title, page URL and score.
For official statistics, grounding, traceability and source quality matter.
A. Because the LLM cannot generate text without chunks
B. Because it is usually better to give the LLM a small set of relevant evidence than every PDF
C. Because embeddings always produce perfect answers
D. Because keyword search is never useful
Suggested answer: B — the aim is focused, traceable evidence.
| Method | Similar to | Useful when |
|---|---|---|
| Keyword search | search box / Ctrl+F | exact terms, titles, acronyms |
| Semantic search | search by meaning | natural-language questions |
| Metadata filters | library catalogue filters | date, theme, geography, report type |
| Reranking | reviewing and reordering results | first search is close but imperfect |
| Hybrid retrieval | combining methods | you want strengths of several approaches |
Semantic search needs a way to compare meanings.
This is why embeddings matter: they make chunks searchable by meaning.
For teaching, imagine each text becomes four numbers:
| Dimension | Rough meaning |
|---|---|
| 1 | inflation / prices |
| 2 | population / demographics |
| 3 | current / date-specific |
| 4 | methodology / definition |
Real embeddings have many more dimensions, and the dimensions are not usually human-labelled like this.
Question:
What was the latest inflation rate?
Made-up vector:
| Chunk | Text | Toy vector |
|---|---|---|
| A | Annual inflation was 5.1% in February 2025 | [0.90, 0.00, 0.80, 0.10] |
| B | CPI measures changes in prices over time | [0.70, 0.00, 0.10, 0.90] |
| C | Population growth was 2.3% | [0.00, 0.90, 0.20, 0.00] |
| D | Food prices increased during the quarter | [0.45, 0.00, 0.25, 0.20] |
| E | Annual inflation was 5.3% in January 2025 | [0.85, 0.00, 0.45, 0.10] |
| Rank | Chunk | Toy cosine similarity |
|---|---|---|
| 1 | A: February inflation rate | 1.00 |
| 2 | E: January inflation rate | 0.96 |
| 3 | D: food prices | 0.92 |
| 4 | B: CPI definition | 0.58 |
| 5 | C: population growth | 0.14 |
This is simplified. The point is that numbers allow the system to rank chunks by closeness to the query.
This picture is simplified to two dimensions. Real vector spaces have many more.
Vector search needs a way to measure how close two vectors are.
| Method | Intuition | In retrieval |
|---|---|---|
| L2 / Euclidean distance | straight-line distance between points | lower = closer |
| Cosine similarity | angle / direction between vectors | higher = more similar |
| Inner product | dot product between vectors | higher = more similar |
These are different scoring methods, but they can be closely related.
IndexFlatL2So StatsChat returns L2-style distance scores: lower scores are better.
For unit-normalised vectors, L2 distance and cosine similarity give the same ranking.
[0.85, 0.00, 0.45, 0.10] |A. It is about population
B. It is semantically similar but may be older than the requested “latest” rate
C. It contains no numbers
D. It cannot be embedded
Suggested answer: B — it is relevant to inflation, but may not be the best evidence if the user asked for the latest figure.
FAISS is the first-stage vector search. It is not the whole retrieval method.
At query time, StatsChat roughly does the following:
FAISS finds candidate chunks. The wider retrieval pipeline decides which chunks are useful enough to use.
# 1. Search FAISS for candidate chunks
matches = db.similarity_search_with_score(
query=question,
k=candidate_k,
)
# 2. Keep candidates below a distance threshold
matches = [m for m in matches if m[-1] <= similarity_threshold]
# 3. Rerank and select a smaller result set
retrieved_docs = rerank_results(question, matches)
retrieved_docs = retrieved_docs[:k_docs]
# 4. Select context chunks for generation
context_docs = select_generation_documents(retrieved_docs, k_contexts)Simplified pseudocode. The important pattern is: search → filter → rerank → select context.
| Setting | Simple meaning |
|---|---|
k_docs |
maximum retrieved references returned |
k_contexts |
maximum chunks passed to the LLM |
similarity_threshold |
filter out weak search matches |
answer_threshold |
decide when answer evidence may be too weak |
document_threshold |
decide when no suitable documents were found |
These are design choices: changing them affects quality, cost and user trust.
For official statistics, users often ask for the latest figure.
A system may need semantic search, dates, metadata and reranking to handle this well.
This is especially important when older and newer reports use very similar wording.
A. The word “latest” may require date-aware ranking
B. The latest publication may not contain the latest data point
C. Several publications may mention unemployment
D. All of the above
Suggested answer: D — this is why retrieval for official statistics needs more than simple similarity.
Retrieval is also about deciding when evidence is too weak.
The LLM does not see the whole collection.
Bad retrieval can lead to bad answers even if the LLM itself is strong.
A. Retrieval failure
B. Generation failure
C. Frontend failure
D. Cost failure
Suggested answer: A — the LLM can only work with the evidence it is given.
| Failure type | Example | Why it matters |
|---|---|---|
| Old but similar | inflation report from 2022 for a “latest” question | semantically close but stale |
| Right report, wrong page | methodology page instead of figure/table | source family is right, evidence is wrong |
| Similar but not answer-bearing | text about exports but no export value | related topic is not enough |
These are retrieval failures even if the final generated answer is where we notice the problem.
Question:
What was the annual inflation rate in February 2025?
In the next slide, imagine these are the top retrieved chunks.
Your task: choose which evidence should be passed to the LLM, and which should be excluded.
A. February 2025 CPI release
The annual inflation rate was 5.1 per cent in February 2025...
B. January 2025 CPI release
The annual inflation rate was 5.3 per cent in January 2025...
C. CPI methodology note
The Consumer Price Index measures changes in prices of goods and services...
D. Economic Survey 2020
Inflation remained stable during 2019...
A. February 2025 CPI release
B. January 2025 CPI release
C. CPI methodology note
D. Economic Survey 2020
Suggested answer: A — it directly answers the question and matches the month/year.
This is the central retrieval judgement: not all relevant-looking evidence is useful evidence for the question.
For official statistics, a strong system may need both.
A. Better page-level retrieval or reranking
B. Removing metadata
C. Increasing LLM temperature
D. Ignoring dates
Suggested answer: A — the retrieval system needs to locate the answer-bearing page or chunk, not just the correct publication.
| Choice | Benefit | Risk |
|---|---|---|
| Fewer chunks | cheaper, less noise | may miss evidence |
| More chunks | broader context | more irrelevant/conflicting text |
| Semantic search | handles natural language | similar may not mean useful |
| Keyword search | transparent exact matching | misses different wording |
| Metadata filters | strong control | needs reliable metadata |
| Reranking | better ordering | more complexity |
Retrieval quality should be measured, not guessed.
For your own source, ask:
In the lab, we will practise retrieval judgement.
The aim is not to write code. The aim is to think like a retrieval designer.
Next week we will look at what the LLM does with the retrieved evidence.