LESSON 44 Β· RETRIEVAL

What is Retrieval?

Learn how a Generative AI system finds the most relevant information before an LLM generates an answer.

30 min readβ€’Intermediateβ€’Generative AI

By the end of this lesson, you will be able to:

  • Explain retrieval and why it is important in RAG systems.
  • Describe how a user query becomes a retrieval request.
  • Understand embeddings, similarity search, top-k, and metadata filtering.
  • See the difference between retrieving evidence and generating an answer.
  • Build a simple Python retrieval mental model.
  • Evaluate whether a retriever returns useful evidence for real questions.
1

What Is Retrieval?

Retrieval is the process of finding relevant information from a collection of stored data in response to a query.

In a RAG system, retrieval happens before generation. The system searches documents, chunks, or other knowledge sources and selects useful pieces of evidence for the language model.

Key idea:Retrieval answers β€œWhich information should I give the model?” Generation answers β€œHow should I use that information to respond?”
EXAMPLE 1Company policy

User asks: β€œHow many days do I have to return a damaged product?” Retrieval finds the relevant returns-policy chunk.

EXAMPLE 2Technical documentation

User asks: β€œHow do I reset an API key?” Retrieval finds the documentation section that describes the reset process.

2

Where Retrieval Fits in RAG

A typical RAG workflow separates knowledge retrieval from answer generation.

User queryβ€œWhat is the refund window?”
β†’
SearchFind relevant chunks
β†’
Top resultsKeep the best evidence
β†’
LLMGenerate an answer

The retriever does not need to write the final answer. Its job is to return useful context with enough relevance and coverage for the next stage.

3

The Retrieval Query

The retrieval process starts with a query. This can be the user's original question, a rewritten question, or another search representation created by the application.

USER QUERY

Natural language

β€œCan I return a damaged item after 20 days?”

β†’
RETRIEVAL QUERY

Search intent

Find policy content about damaged-item return deadlines and conditions.

Important: a good retrieval query should represent the information the system needs to find, not merely contain the exact words that might appear in the source.
4

How Semantic Retrieval Finds Meaning

Modern RAG systems often convert the query and stored chunks into embeddings. Each embedding is a numerical vector representing aspects of the content's meaning.

QUERYβ€œHow long can I return it?”
β†’
EMBEDDING[0.12, -0.44, 0.81, ...]

The retriever then compares the query vector with stored vectors using a similarity or distance measure. Chunks with stronger matches become retrieval candidates.

5

Retrieval From a Vector Index

After documents are chunked and embedded, their vectors can be stored in a vector index or vector database. At query time, the query embedding is compared against those stored vectors.

1ChunkSource text
2EmbeddingVector representation
3MetadataSource, section, date
4IndexFast candidate search

Retrieval is therefore connected to earlier lessons: chunking determines the units being searched, embeddings represent them, and the vector index makes similarity search practical.

6

What Does Top-k Mean?

Top-k means returning the best k retrieval candidates according to the search method.

TOP-1

One result

Focused, but risky if the first result misses important evidence.

TOP-3

Three results

Provides more coverage while keeping the context relatively focused.

TOP-10

Ten results

Can improve recall, but may introduce noise and consume more context.

Trade-off:Increasing k can improve the chance of retrieving useful evidence, but more context is not automatically better.
7

Metadata Filtering

Similarity is not always enough. Metadata can restrict retrieval to documents that satisfy business or application rules.

EXAMPLE 1Product support

Search only documents where product = "Model X" before ranking similar chunks.

EXAMPLE 2Knowledge by date

Search only policy documents where year = 2026 when the current policy matters.

This combination is powerful: semantic similarity finds relevant meaning, while metadata filters enforce known constraints.

8

Simple Python Retrieval Mental Model

You can understand the basic idea without a vector database first. Imagine that each stored item already has a similarity score against the query.

</> Python
results = [
    {"text": "Returns are accepted within 30 days.", "score": 0.92},
    {"text": "Warranty coverage lasts one year.", "score": 0.61},
    {"text": "Shipping usually takes 3 days.", "score": 0.34},
]

top_k = sorted(results, key=lambda item: item["score"], reverse=True)[:2]

for item in top_k:
    print(item["score"], item["text"])

This example skips the embedding and indexing steps. It shows the core retrieval idea: rank candidates by relevance and keep the strongest matches.

9

Retrieval Is Not Generation

Retriever
  • Finds evidence
  • Ranks candidates
  • Applies filters
  • Returns context
Generator
  • Reads retrieved context
  • Follows instructions
  • Produces natural language
  • Forms the final response

A retriever can return excellent evidence while the generator still produces a poor answer. Conversely, a strong language model cannot reliably answer from private or changing information if the relevant evidence was never retrieved.

10

Retrieval and RAG Context

In RAG, retrieved chunks are normally assembled into a context that is passed to the language model.

QueryUser's question
β†’
RetrieveFind relevant chunks
β†’
ContextCombine selected evidence
β†’
GenerateAnswer from context
Failure point:If the correct chunk is not retrieved, the LLM may not have the evidence required for a grounded answer.
11

Exact Search vs Semantic Retrieval

KEYWORD SEARCH

Can miss a relevant document when the user and document use different words for the same concept.

SEMANTIC SEARCH

Can match related meaning even when the wording is different, using embeddings and similarity.

Neither approach is automatically best for every workload. Exact terms, identifiers, product codes, and names can be especially important, while semantic retrieval is valuable for natural-language questions.

12

How to Evaluate Retrieval

Do not evaluate retrieval only by looking at a few impressive examples. Create realistic questions and define what evidence should be retrieved.

HIT RATEWas useful evidence retrieved?

Measure how often the expected evidence appears in the retrieved set.

RANKHow high was it?

Check whether relevant evidence appears near the top of the results.

CONTEXTIs it sufficient?

Verify that the retrieved chunk contains enough information to answer.

ANSWERDid generation succeed?

Finally check whether the retrieved evidence supports a correct answer.

13

Common Retrieval Mistakes

01Retrieving too much

Large result sets can add noise and consume the model's context budget.

02Retrieving too little

Top-1 may miss a second chunk needed to complete the answer.

03Ignoring metadata

Similarity alone cannot enforce every business or time constraint.

04Bad chunks

A retriever cannot recover context that was destroyed during chunking.

05Testing one query

Retrieval quality must be measured across a representative question set.

06Confusing retrieval with truth

A high similarity score means a strong match, not guaranteed factual correctness.

14

What Makes a Good Retriever?

A good retriever consistently brings the right evidence near the top while keeping irrelevant content under control.

RELEVANCEReturned chunks match the user's information need.
COVERAGEMultiple required pieces of evidence can be found.
EFFICIENCYSearch completes within practical latency and cost limits.
System mindset: retrieval quality depends on chunking, embeddings, indexing, query formulation, filters, top-k, and sometimes rerankingβ€”not one setting alone.
15

Practice: Build a Tiny Retriever

Create five short text chunks and assign each a simple relevance score for a sample question. Sort the results and inspect the top three. Then ask whether the top results contain enough evidence to answer the question.

TASK

Inspect the evidence

Try a second question and see whether the same ranking still makes sense. Think about what could go wrong when scores are based on real embeddings.

Quick Quiz

1. What is retrieval?

Finding relevant information from a stored collection in response to a query.

2. What does top-k mean?

Returning the best k candidates according to the retrieval ranking.

3. Why use metadata filters?

To enforce known constraints such as product, source, tenant, language, or date before or during retrieval.

4. Does a high similarity score guarantee a correct answer?

No. It indicates a strong match according to the retrieval measure, but the evidence still needs to be relevant, sufficient, and accurate.

30-Second Recap

1. Retrieve

Find useful evidence for a query.

2. Rank

Order candidates by relevance.

3. Filter

Use metadata when constraints matter.

4. Generate

Give selected context to the LLM.

Key Takeaways

  • Retrieval finds relevant information before generation in a RAG workflow.
  • Semantic retrieval commonly uses embeddings and similarity search.
  • Top-k controls how many candidate results are returned.
  • Metadata filters can enforce constraints that similarity alone cannot express.
  • Retrieval quality depends on chunking, embeddings, indexing, queries, filters, and ranking.
  • Evaluate retrieval with realistic questions and expected evidence, not intuition alone.