|
|
RAG with Query Expansion using Synonyms and LLM Paraphrasing
Author: Venkata Sudhakar
Query expansion improves RAG retrieval by augmenting the original customer query with synonyms, related terms, and paraphrases before searching the document index. ShopMax India customers use inconsistent terminology - 'earphones', 'earbuds', 'in-ear headphones', and 'TWS' all refer to the same product category. Without query expansion, a BM25 index built with 'earbuds' in the product titles misses queries that use 'earphones'. Query expansion bridges this vocabulary mismatch without requiring multiple retrieval passes.
Two approaches implement query expansion: dictionary-based expansion uses a predefined synonym map for known product terms (fast, deterministic, zero cost), and LLM-based paraphrasing generates query variants on the fly using a small model (flexible, handles novel queries but adds latency). For e-commerce, a hybrid works best: maintain a product vocabulary synonym dictionary for common category terms, and fall back to LLM paraphrasing for queries that match no dictionary entries.
The following example implements hybrid query expansion for ShopMax India. It first checks a product synonym dictionary, then expands with LLM paraphrasing if needed, and merges all expanded terms before retrieval.
It gives the following output,
Q: best earphones under Rs 20000
A: The Sony WF-1000XM5 earbuds at Rs 19,990 are an excellent choice. They offer 8-hour battery, ANC, and IPX4 water resistance. The Samsung Galaxy Buds2 Pro at Rs 17,990 is also great value with IPX7 protection.
Q: noise cancelling TWS under Rs 25000
A: The Samsung Galaxy Buds2 Pro (Rs 17,990, IPX7) and Sony WF-1000XM5 (Rs 19,990, IPX4) are both noise-cancelling true wireless options available under Rs 25,000 across India.
For ShopMax India, maintain the synonym dictionary as a YAML file in your repository so product and marketing teams can update it without code changes. Include common misspellings for top-selling brands (e.g. 'samsng', 'appel', 'soni') in the synonym map - these show up frequently in mobile-typed queries. Run monthly analysis on queries that returned zero results to identify missing synonym entries. The LLM paraphrasing fallback should use your smallest, fastest model to keep latency under 200ms for the expansion step.
|
|