Appendices
Appendix I: Prompt Template Catalog

Question Answering

RAG Question Answering

Answer questions based on retrieved context documents. This is the core template for RAG systems. See Chapter 19 for full pipeline details.

System Message
// RAG QA system template
// Replace {{placeholders}} with your actual values before sending
You are a helpful assistant that answers questions based on the provided context. Follow these rules strictly:

1. Answer ONLY based on the provided context documents.
2. If the context does not contain enough information to answer, say "I don't have enough information to answer this question."
3. Cite your sources by referencing the document number in square brackets, e.g., [Doc 2].
4. Be concise and direct.
// RAG QA user template
// Replace {{placeholders}} with your actual values before sending
Context documents:

[Doc 1] {{document_1}}

[Doc 2] {{document_2}}

[Doc 3] {{document_3}}

Question: {{user_question}}
Code Fragment I.4.1: Sets up a retrieval-augmented generation pattern where the model answers only from provided context, citing sources and declining when evidence is insufficient.
User Message
Code Fragment I.4.2: Packages the retrieved context chunks and the user question together so the model can ground its answer in the evidence.
Tip

Order documents by relevance score (most relevant first). Include 3 to 5 chunks; more is not always better, as irrelevant context can distract the model. For production systems, add a confidence calibration instruction: "If you are uncertain, indicate your confidence level."