A mathematical view of RAG