What are the advanced prompt engineering techniques for RAG generation?
RAG prompt engineering optimizes how retrieved context is presented to the LLM for answer generation. Core prompt structure: system instructions (role definition, answer guidelines, citation requirements), retrieved context (formatted chunks with source metadata), conversation history (for multi-turn), and user question. Context formatting: present chunks with clear source attribution (numbered references: [1] Source: document.pdf, Page 3), order by relevance (most relevant first), and separate chunks with clear delimiters. Instruction design: explicit grounding instructions ("Answer ONLY based on the provided context"), citation requirements ("Cite sources using [n] notation"), uncertainty handling ("If the context does not contain sufficient information, state what is missing"), and format instructions (structured output for specific use cases). Advanced techniques: chain-of-thought retrieval (instruct the model to reason about which context is most relevant before answering), self-reflection (generate answer, then verify each claim against context), and retrieval-augmented chain-of-thought (alternate between retrieval and reasoning steps). Context compression: when retrieved content exceeds token budget, summarize less relevant chunks while keeping highly relevant ones verbatim, or use extractive summarization to pull only the most relevant sentences. Few-shot examples: include 1-2 examples of ideal responses in the system prompt to guide format and quality. Temperature tuning: use low temperature (0.0-0.3) for factual answers, slightly higher (0.3-0.5) for creative synthesis. Output structuring: use JSON mode or structured output for applications that parse the response programmatically, including fields for answer, sources, confidence, and related questions.