This is quite a common problem with RAG systems in general, and it's why monitoring and observability are so important. In terms of the generation step, setting the temperature to zero, as the previous commenter suggested, is a strong approach. However, that will not completely guarantee identical outputs. Now with rag systems, depending on the complexity, there are lots of different things that can go wrong.
The issue that they have is that those errors can then compound. So you've got to look at your retrieval accuracy, and test that in isolation. And with your retrieval accuracy, obviously, you've also got to consider how you've chunked your documents.
If that's not the issue, if we're saying all of that is held constant, then the problem is likely with your gen step.
What I'd suggest doing there is running a series of experiments in MLFlow using various prompts for your gen stap to see which one produces the most consistent answers.
Specifically, I'd follow these steps:
- Set temperature=0 for deterministic output
- Strengthen the system prompt with explicit boundaries: "Answer only using provided context. Cite sources. If the answer isn't in the context, say 'I don't have that information.'"
- Optimise retrieval: reduce chunk size to 500-800 tokens, add metadata filtering, consider hybrid search (vector + BM25)
- Add reranking or similarity thresholds to filter low-quality retrieved documents
- Log prompts, retrieved context, and responses for debugging; track consistency metrics with MLflow