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.
The pipeline
- Retrieve the most relevant chunks for the question.
- Build the prompt: the chunks (labelled with IDs), instructions to answer only from them and cite IDs, then the question.
- Generate the answer.
- Check that cited IDs exist and say "I can't find that" when nothing relevant was retrieved.
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?
Answer from the model's general knowledgeSay it can't find the answer instead of guessingRetry until something is foundReturn 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.