<aside> ๐
Date: Tuesday, October 6, 2026 ยท Week 2, Day 9 ยท Type: Build Day
</aside>
9.1 The RAG Architecture โ Retrieval-Augmented Generation = Search Engine + Context Injector + Generative Model.
9.2 The Prompt Template โ structuring prompts to instruct the LLM: "Answer the question ONLY using the facts provided below."
9.3 Grounding & Hallucination Mitigation โ forcing factual attribution to retrieved context passages.
Build a complete end-to-end RAG script: load a multi-page document, chunk it (Day 8), embed and store it in TinyVectorDB (Day 5), retrieve top 3 chunks for a user question, inject them into a prompt, and generate a factual answer.
[User Question] --------+
| (Query)
v
[TinyVectorDB Index] ---> [Top 3 Chunks Retrieved]
|
v
[LLM Prompt: Question + Retrieved Chunks] ---> [Grounded Answer Generated]
Problem 09. Write a Python prompt builder function build_rag_prompt(user_query: str, retrieved_chunks: list[str]) -> str that formats the retrieved context into a clean, delimited system prompt with explicit guardrail instructions against outside speculation.
Data Structure: Inverted String Buffer (Prompt Context Assembly).
Mechanics: Dynamic string joining with delimiters to construct structured context payloads within strict token bounds.
Real-World Production Use: Deployed inside all enterprise RAG orchestrators to assemble dynamic LLM prompts.