From retrieval to context
Retrieval returns documents. Context is the string you paste into the prompt. Between the two you pick how many results to use, how to label them, and what to do when nothing matches. Small choices, big impact on answer quality.
def chat(self, role: str, query: str) -> str:
docs = self.doc_store.search_documents(role, query, top_k=3)
if not docs:
return "I couldn't find any relevant documents to help answer your question."
context = "\n\n".join(
f"Document: {doc['title']}\n{doc['content']}" for doc in docs
)
system_prompt = (
f"You are a helpful assistant with access to {role} documents.\n"
f"Answer the user's question based only on the retrieved documents.\n"
f"If the documents don't contain the information needed, say so clearly.\n"
f"Use a professional, helpful tone appropriate for a {role} professional."
)
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": f"Retrieved documents:\n{context}\n\nQuestion: {query}"},
]
return self.provider.chat(messages, temperature=0.2, max_tokens=800)
Role is a required parameter. It picks the collection for retrieval and it shapes the system prompt. The two uses of role reinforce each other.
Retrieve, label, ground, answer
Each retrieval becomes a labelled context block. The model sees role, context, and question in one well-shaped prompt.
Fill in the blanks: Shape the context block
Loading practice…
Quiz: Quiz
Loading practice…