Ingest, retrieve, build the prompt, answer
The fake model copies the first numbered source. Real models write their own answer, but the plumbing around them is the same.
A RAG pipeline is four small functions in a row: ingest the documents once, then for each question retrieve the best chunks, build a prompt with numbered sources, and let the model answer. Here the whole thing runs by hand in three languages.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write build_prompt(question, chunks) where chunks is a list of strings. It returns the lines Sources:, then [1] ..., [2] ..., then a blank line and Question: ..., joined with newlines. Print it for two sources.
Write retrieve(scores, k, min_score) where scores is a list of (name, score) pairs. Return the names of the best k with score at least min_score; equal scores are ordered by name.
Explain it without notes
Name the four steps of a RAG pipeline and say which ones run once and which run per question.
Why should you inspect the retrieved chunks before blaming the model for a wrong answer?
What is the purpose of a minimum score in retrieval?
Why number the sources in the prompt?
Expected output
question: how much is express delivery
retrieved: [1] shipping 1.0000, [2] helmets 0.5000
| Answer using only the sources below. Cite them like [1]. If they do not contain the answer, say you do not know.
|
| Sources:
| [1] Standard shipping takes 3 to 5 days. Express shipping takes 1 to 2 days and costs 12 dollars.
| [2] Every bike comes with a free helmet. Extra helmets cost 25 dollars.
|
| Question: how much is express delivery
answer: Standard shipping takes 3 to 5 days. Express shipping takes 1 to 2 days and costs 12 dollars. [1]
question: get my money back
retrieved: [1] returns 1.0000
answer: Unused bikes can be returned within 30 days with the receipt for a full refund. [1]
question: do you sell locks
retrieved: nothing
answer: I do not know.