Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 8.5 — A Working RAG Pipeline
LLM Engineering · Example 1 of 1

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.

01

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.

02

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

01

Name the four steps of a RAG pipeline and say which ones run once and which run per question.

02

Why should you inspect the retrieved chunks before blaming the model for a wrong answer?

03

What is the purpose of a minimum score in retrieval?

04

Why number the sources in the prompt?

Ingest, retrieve, build the prompt, answer
Sign in to run this example in your browser.

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.