Retrieval with memory. Handling follow-up questions.
User: "Who is the CEO of Apple?" AI: "Tim Cook." User: "How old is he?"
If you search for "How old is he?", the vector store has no idea who "he" is. It retrieves random documents about men. This is why naïve RAG fails in chat.
We need to rewrite the prompt to be Standalone. Pass the Chat History + New Question to an LLM.
Now, we search for "How old is Tim Cook?" and we get the correct answer.
from langchain.chains import create_history_aware_retriever
history_aware_retriever = create_history_aware_retriever(
llm=llm,
retriever=retriever,
prompt=rephrase_prompt
)
Even with all these tools, systems fail.
In RAG systems, the model answers only what retrieval allows it to see. If the retriever is blind, the model is hallucinating. Retrieval is not a support component—it is the system’s epistemology.
Key Intuition: "Garbage in, Hallucination out."