RAG System Design
Key Components of RAG System Design
Data Sources
Users often make spelling mistakes or grammatical errors. To improve retrieval accuracy:
- Use multiple data sources to improve the chances of retrieving relevant information
- Write a function to re-write the query given by the user to improve retrieval
- Apply typo fixes and add more context
Example System Prompt for Query Rewriting
You are a helpful assistant that rewrites user queries to improve retrieval accuracy.
Correct any spelling mistakes, enhance clarity, and add relevant context where necessary.
Examples:
- User Query: "What is the capitel of Frnace?"
Rewritten Query: "What is the capital of France?"
- User Query: "Tell me abot the histroy of AI."
Rewritten Query: "Can you provide information about the history of Artificial Intelligence?"
RAG Pipeline Flow
User_Query → Rewriting_Function → Embedding_Model(take chunks) →
Judge_Model(for best chunk) → Rewrite_Query → Retrieval_Model →
RAG_Model → Final Answer
Sub-Query Generation
Sub-queries can be generated from the main query to target specific aspects of the information needed:
- Break down the main query into smaller, more specific queries
- More specific queries lead to better retrieval results and improved answer quality
- Addresses different aspects of the user's request
Note: Sometimes generating sub-queries may not be necessary, especially if the main query is already clear and specific. Instead, you can focus on enhancing the main query through techniques like query expansion or rephrasing to improve retrieval effectiveness.
HyDe (Hypothetical Document Embeddings)
HyDe is a technique where you generate hypothetical answers to the user's query and then create embeddings for these hypothetical answers:
- Generate hypothetical answers aligned with user intent
- Create embeddings that can retrieve more relevant documents
- Captures context and details better than traditional retrieval methods
Benefits:
- Particularly useful when the user's query is ambiguous or lacks sufficient context
- Explores different interpretations of the query
- Retrieves documents that cover a broader range of relevant information