Smarter RAG: Improving FAQ Chatbots with Query Expansion
Retrieval-Augmented Generation (RAG) combines search and generative AI to power smarter FAQ chatbots. By retrieving relevant documents and generating answers, RAG provides accurate, context-aware responses. But even this powerful method has its pitfalls — especially when user queries don’t exactly match the FAQ wording.
The Problem: Missed Retrieval and Strict Prompting
Imagine a user asks:
“How to change my username?”
But your FAQ only contains:
“How do I change my email address?”
“How can I reset my password?”
Since “username” and “email address” are different words, a simple keyword search may miss related FAQs. This is a classic case of missed retrieval.
Query expansion helps here by enriching the user query with synonyms and related terms, so documents mentioning “account settings” or “user profile” get retrieved, increasing recall.
However, even if query expansion fetches related documents, the strict prompting you use to generate answers can limit usefulness.
For example, a strict prompt might say:
Only answer if the exact question is in the FAQs; otherwise, respond with “I don’t have information.”
This causes the assistant to refuse to answer even when related FAQ content is available — leading to poor user experience.
The Solution: Combine Query Expansion with Flexible Prompting
What is Query Expansion?
Query expansion means broadening a user’s query by adding synonyms and related phrases without changing the original intent. For example:
- Original: How to change my username?
- Expanded: How to (change modify update) my (username user-name account-name)?
This helps retrieve documents that don’t have an exact keyword match but are topically relevant.
Why Flexible Prompting Matters
If your generation prompt demands exact matches only, it defeats the purpose of query expansion.
Instead, design prompts that:
- Accept answers based on related FAQs when exact matches are absent
- Clearly communicate when the answer is based on related topics
- Provide concise, useful responses rather than a blunt “no info” message
For example:
If the exact question is not found, provide the best possible answer from related FAQs and mention if the info might be partial.
Before vs After Example
Before (Strict):
- Query: How to change my username?
- Retrieved FAQs: About email and password
- Answer: “I don’t have information about that.”
After (Query Expansion + Friendly Prompt):
- Query expanded with synonyms and related terms
- Retrieved FAQs: Same as above + more
- Answer: “I don’t have exact info on usernames, but here’s related info on account settings and password reset that might help.”
Implementing Query Expansion
You can implement query expansion using simple synonym lists, prompt-based expansion via LLMs, or embedding-based semantic search with expanded queries. Here’s a prompt for LLM-based expansion which I used:
You are an expert in search query expansion. Your task is to enhance the given question by adding relevant synonyms in parentheses, while preserving the exact same intent and action.
Rules:
1. Produce a SINGLE expanded search query.
2. Add relevant synonyms in parentheses immediately after each key word or phrase.
3. Maintain the EXACT SAME intent and action as the original query.
4. DO NOT introduce different actions, topics, or unrelated terms.
5. Return ONLY the expanded query. Do not provide explanations or additional text.
Example input: "How to change my username?"
Example output: How to (change modify update edit revise) my (username user-name display-name account-name)
Example input: "What are the payment methods?"
Example output: What are the (payment billing transaction) (methods options ways means)
Question: {query}
Use the expanded query for document retrieval:
# expand the query to retrieve more relevant FAQs
expanded_query = expansion_chain.invoke({"query": query})
# get the answer based on the expanded query
docs = retriever.invoke(expanded_query, k=2)
Use Flexible prompt to generate the answer based on the retrieved documents:
You are a helpful assistant that provides direct answers from the FAQ database.
Rules:
1. If the exact question is not found in the FAQs, politely inform the user: "I don't have information about that in the FAQs," and then provide the best possible answer strictly based on related FAQ content only.
2. Do NOT mention document IDs, metadata, or any internal references.
3. Keep the answer concise and to the point.
4. Do NOT add any advice, offers, or suggestions beyond what is present in the FAQs.
Query: {query}
FAQs: {docs}
Answer:
Output:
🔗 Check out the full code on GitHub:
👉 https://github.com/deepak786/faq-chatbot
Conclusion
Query expansion helps chatbots find more relevant documents, but you also need prompts that let the chatbot use related info to give helpful answers.
Together, they make FAQ chatbots smarter and friendlier for users.
Happy Coding :)