Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Introduction to AI Engineering  ›  Capstone Project

Retrieval-Augmented Generation (RAG)

RAGs 15 minRetrieval-augmented generation first finds the documents relevant to a question, then gives them to the language model so it answers from real, current facts.

Retrieval-Augmented Generation (RAG)

You now have the two halves of Pedal Point's assistant. The model writes fluent answers but invents facts it was not given. Embeddings can find the right help page for any question. Put them together and you get the most common pattern in AI engineering: retrieval-augmented generation, or RAG.

The rule

Retrieve the right facts first, then let the model write.

RAG has two phases.

Before any question (indexing):

  1. Chunk. Split each document into pieces small enough to be specific, often a few paragraphs.
  2. Embed. Turn each chunk into a vector.
  3. Store. Save vectors, text and labels (page, country, date) in a vector database.

When a question arrives (answering):

  1. Embed the question with the same model.
  2. Retrieve the few nearest chunks.
  3. Generate. Put those chunks into the prompt with an instruction like "Answer using only the reference text below. If it does not contain the answer, say so."
Reference text:
[Returns policy] Unused items can be returned within 30 days...
[Helmets] For hygiene reasons, helmets can only be returned sealed...

Question: Can I return a helmet I've worn once?

RAG gives the model facts that are private, fresh, or too specific to be in its training. Updating knowledge means editing documents, not retraining. The model can also cite which chunk it used.

Dynamic filters make retrieval sharper. Before comparing meanings, restrict the search by labels: only chunks for the customer's country, or only pages for their bike model. Similar meaning is not enough if the page is about another region's rules.

RAG lowers the rate of invented answers but does not remove it. The model can still misread a chunk or blend it with what it learned in training. That is why the instruction to admit gaps matters, and why you check answers against their sources.

The shortcut

Say the pipeline as two lines: "Chunk, embed, store" once; "embed, retrieve, generate" per question. When an answer is wrong, walk the steps and find where it broke.

Debugging a wrong helmet answerStep 1 of 4
  1. Check retrieval first

    Did the helmet chunk come back? It did not: only the general returns chunk did.

The trap is blaming the model for a retrieval miss. If the right text never reached the prompt, no model can use it. Test retrieval on its own.

Drills

Answer fast — trust the rule.

quick drill

In RAG, which step happens once, ahead of time, rather than for every question?

quick drill

A Canadian customer gets the US shipping policy. Which fix targets the cause?

quick drill

The right chunk was never retrieved, and the answer was wrong. What should you fix first?

quick drill

The returns window changes to 45 days. With RAG, what is the update?

quick drill

Why tell the model "if the reference text does not contain the answer, say so"?

Remember

  • Retrieve the right facts first, then let the model write.
  • Chunk, embed, store once; embed, retrieve, generate per question.
  • Wrong answer? Check retrieval before blaming the model.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainDesign Pedal Point's RAG pipeline

Design the retrieval-augmented pipeline that lets the Pedal Point assistant answer from the shop's help pages. Describe what happens before any customer asks, what happens when a question arrives, and one thing you would add so the assistant does not invent answers when retrieval finds nothing useful.