AI Engineering Tutorial · Retrieval (RAG) · Lesson 19 of 25

A Minimal RAG Pipeline

Retrieval-augmented generation end to end: retrieve chunks, put them in the prompt, ask for a grounded answer with citations.

Create a free account to save this page, take notes and track your progress. Sign in

The pipeline

  1. Retrieve the most relevant chunks for the question.
  2. Build the prompt: the chunks (labelled with IDs), instructions to answer only from them and cite IDs, then the question.
  3. Generate the answer.
  4. Check that cited IDs exist and say "I can't find that" when nothing relevant was retrieved.
Retrieve, then build a grounded promptTry it yourself
chunks = {"kb-12": "Refunds are issued within 5 business days to the original payment method.",
          "kb-19": "Orders can be cancelled free of charge until they ship.",
          "kb-31": "Gift cards are not refundable."}

def retrieve(question, k=2):
    words = {w for w in question.lower().replace("?", "").split() if len(w) > 3}   # skip "do", "you"...
    score = lambda text: len(words & set(text.lower().replace(".", "").split()))
    ranked = sorted(chunks, key=lambda cid: -score(chunks[cid]))
    return [cid for cid in ranked[:k] if score(chunks[cid]) > 0]

def build_prompt(question):
    ids = retrieve(question)
    if not ids:
        return None
    docs = "\n".join(f'<doc id="{cid}">{chunks[cid]}</doc>' for cid in ids)
    return (f"{docs}\nAnswer using only the documents above and cite doc ids in brackets. "
            f"If they don't contain the answer, say you can't find it.\nQuestion: {question}")

print(build_prompt("How long do refunds take?"))
print(build_prompt("Do you deliver to Mars?") or "nothing relevant: answer 'I can't find that'")

Output

<doc id="kb-12">Refunds are issued within 5 business days to the original payment method.</doc>
Answer using only the documents above and cite doc ids in brackets. If they don't contain the answer, say you can't find it.
Question: How long do refunds take?
nothing relevant: answer 'I can't find that'

For production concerns (hybrid search, reranking, permissions, freshness, evaluation), see the course RAG in Production.

Test yourself

What should a RAG system do when retrieval finds nothing relevant?

  1. Answer from the model's general knowledge
  2. Say it can't find the answer instead of guessing
  3. Retry until something is found
  4. Return the first document anyway
Show answer

Answer: Say it can't find the answer instead of guessing

Grounded systems should prefer 'not found' to a confident guess.

Was this page helpful?