RAG - Query Transformation and Expansion A developer explains how query transformation and expansion improve retrieval in RAG systems. By transforming vague queries into specific ones and generating multiple variations, the system retrieves more relevant documents from the vector database. The LLM performs both operations, though a rule-based clustering approach is also possible. When a user asks a blended or incomplete query, such as: "How do I deploy it?" the LLM uses the background context it already has to transform the query into a more meaningful one. For example: "How do I deploy a FastAPI application?" By transforming the query into a more specific one, the system is able to retrieve more relevant documents from the vector database. For query transformation to work effectively, the LLM should have some background information about the conversation. When a user query is converted into an embedding, the point obtained in the vector database may not be close to the most relevant documents. By expanding the query into different variations, we can retrieve more relevant documents. Original User Query "How do I deploy a FastAPI application?" Expanded Queries These are different variations of the original user query. For each variation of the user query, the system retrieves a set of relevant contexts from the vector database. During the augmentation phase, the retrieved contexts, along with the original user query, are sent to the LLM so that it can generate more accurate results. Both query expansion and query transformation are performed by the LLM. Instead of calling the LLM for query expansion, we can use a rule-based approach by storing related queries for a user query as a cluster in the vector database. When a user submits a query, the system retrieves the related queries from the cluster and uses them to improve document retrieval.