Context-only prompt shape
A grounded RAG system is only as honest as its prompt. The model will happily invent an answer if you let it. The fix is not a clever prompt. The fix is an explicit rule that says: use only the context, and if the answer is not there, say so.
PROMPT_TEMPLATE = '''You are an enterprise knowledge assistant.
Answer the user's question using ONLY the context snippets below.
If the answer is not in the context, say you don't know.
Context:
{context}
Question: {question}
Answer:'''
def _format_context(docs):
lines = []
for i, d in enumerate(docs, 1):
lines.append(f'[Source {i} | id={d.id}]\n{d.text}')
return '\n\n'.join(lines)Numbered Source headers make citations easier for the model to echo and easier for humans to trace. The ONLY and the explicit I-do-not-know instruction give the model a safe exit when retrieval comes up empty on the real question.
AI prompt: Try it: grounded RAG prompt
Loading practice…
Quiz: Quiz
Loading practice…