User query thường ngắn, colloquial, khác phong cách với tài liệu trong knowledge base → retrieval kém. Query transformation biến query gốc thành dạng phù hợp với retrieval.
1. HyDE (Hypothetical Document Embeddings) (Gao et al. 2022)
- Ý tưởng: thay vì embed query, yêu cầu LLM sinh một answer giả định cho query, rồi embed answer đó.
- Lý do: answer (prose, full sentences) có distribution giống tài liệu hơn query (ngắn, câu hỏi). Cosine similarity giữa embedding answer và embedding chunks tốt hơn.
- Code flow: user_query → LLM prompt "viết đoạn văn trả lời câu hỏi này" → hypo_answer → embed → vector search.
- Hiệu quả nhất khi corpus toàn prose dài; kém hiệu quả với corpus code hoặc structured data.
2. Query Decomposition / Multi-query
- Câu hỏi phức tạp (multi-hop, comparison) chia thành các sub-query đơn giản.
- "So sánh pricing của Stripe và PayPal với transaction $10K/month" → ["Stripe pricing tier nào cho $10K/month?", "PayPal pricing tier nào cho $10K/month?"].
- Retrieve từng sub-query → aggregate context → generate answer.
- Pattern: RAG-Fusion dùng LLM generate N variant queries, retrieve mỗi variant, fuse kết quả bằng RRF.
3. Step-back prompting (Zheng et al. 2023)
- Trước khi answer, LLM tự đặt một câu hỏi "step-back" tổng quát hơn để retrieve background context.
- "Einstein đã nghiên cứu gì năm 1905?" → step-back: "Einstein đã làm gì ở Thụy Sĩ?" → retrieve tiểu sử → context rộng hơn cho answer cụ thể.
- Tốt cho câu hỏi cần knowledge nền và reasoning.
4. Query rewriting — LLM viết lại query: expand từ viết tắt, correct typo, thêm context ("thread ở câu trước là về payment", rewrite query có prefix đó), phù hợp với hội thoại đa lượt.
5. Query routing — classifier chọn knowledge base phù hợp (docs, FAQ, policy, codebase) thay vì search toàn bộ.
Trade-off:
- Thêm LLM call → latency +0.5-2s, cost tăng.
- Chỉ dùng khi retrieval baseline kém; nếu hybrid search + rerank đã tốt, query transform thêm 1-2% recall không bù chi phí.
- Combine: query decomposition cho câu hỏi complex + HyDE cho simple conversational query + routing cho multi-KB.
Implementation: LlamaIndex QueryTransform, LangChain MultiQueryRetriever, hoặc tự viết trong pipeline.
User queries are often short, colloquial, stylistically different from KB documents → poor retrieval. Query transformation converts the raw query into a retrieval-friendly form.
1. HyDE (Hypothetical Document Embeddings) (Gao et al. 2022)
- Idea: instead of embedding the query, ask an LLM to generate a hypothetical answer to it, then embed that.
- Why: answers (prose, full sentences) share a distribution closer to documents than queries (short, interrogative). Cosine similarity between answer and chunk embeddings improves.
- Flow: user_query → LLM prompt "write a passage answering this question" → hypo_answer → embed → vector search.
- Best when the corpus is long prose; weaker on code or structured data.
2. Query Decomposition / Multi-query
- Complex questions (multi-hop, comparison) are split into simpler sub-queries.
- "Compare Stripe and PayPal pricing at $10K/month" → ["What Stripe tier applies at $10K/month?", "What PayPal tier applies at $10K/month?"].
- Retrieve per sub-query → aggregate context → generate answer.
- Pattern: RAG-Fusion asks the LLM for N query variants, retrieves each, fuses results via RRF.
3. Step-back prompting (Zheng et al. 2023)
- Before answering, the LLM poses a more abstract "step-back" question to retrieve background context.
- "What did Einstein study in 1905?" → step-back: "What did Einstein do in Switzerland?" → retrieves biography → broader context for the specific answer.
- Strong for questions needing background + reasoning.
4. Query rewriting — the LLM rewrites: expand acronyms, fix typos, add context ("last turn was about payments", rewrite with that prefix), for multi-turn conversations.
5. Query routing — a classifier picks the right KB (docs, FAQ, policy, codebase) instead of searching everything.
Trade-offs:
- Adds an LLM call → +0.5–2s latency, +cost.
- Only use when baseline retrieval is weak; if hybrid search + reranker is already good, +1–2% recall may not justify the cost.
- Combine: decomposition for complex questions + HyDE for simple conversational ones + routing for multi-KB.
Implementations: LlamaIndex QueryTransform, LangChain MultiQueryRetriever, or hand-rolled.