Generating an Answer
The final step in RAG is generating an answer. The AI model receives the user's question plus the retrieved document chunks and generates a grounded, accurate response.
8 min•By Priygop Team•Updated 2026
The Augmented Prompt
The Augmented Prompt
# How the augmented prompt is structured in a RAG system
def build_rag_prompt(user_question, retrieved_chunks):
"""
Build the final prompt that is sent to the LLM in a RAG system.
The prompt includes both the retrieved context and the user's question.
"""
# Format the retrieved chunks as context
context_text = ""
for i, chunk in enumerate(retrieved_chunks, 1):
context_text += f"Source {i}: {chunk}\n\n"
# Build the full prompt
system_message = """You are a helpful assistant. Answer questions based ONLY on
the provided context. If the answer is not in the context, say 'I don't have
information about that in my knowledge base.' Do not invent information."""
user_message = f"""Context (retrieved from our knowledge base):
{context_text}
Question: {user_question}
Answer based only on the context above:"""
return system_message, user_message
# Example
question = "What is your return policy?"
retrieved = [
"Our return policy allows returns within 30 days of purchase.",
"Items must be in original condition with tags attached.",
"Returns are processed within 5-7 business days.",
]
system, user = build_rag_prompt(question, retrieved)
print("System message:")
print(system)
print()
print("User message (with context):")
print(user)Diagram
Loading diagram…
Deep Learning ⊂ Machine Learning ⊂ Artificial Intelligence