Gen-AI Developer Classroom notes 29/04/2025

Improving Query Quality

1. Query Rewriting (Query Reformulation)

Idea: Rewrite the user’s vague or incomplete query into a complete, rich query.

How:

  • Use an LLM to rephrase
  • Use a simple prompt: “Rewrite the query to maximize retrieval relevance”

Example:

User Query Rewritten Query
“ai healthcare” “What are the different applications of Artificial Intelligence in the healthcare sector, such as diagnosis, treatment, and patient monitoring?”

2. Context Injection (Conversation Memory)

Idea: Inject previous conversation history into the current query to add missing context.

How:

  • Use ConversationBufferMemory (LangChain)
  • Add last n turns of chat

Example:

Context User Query Final Query
Earlier discussed “AI in education” “And healthcare?” “What are the applications of AI in healthcare, similar to its uses in education?”

3. Query Expansion (Adding Synonyms/Related Terms)

Idea: Expand the query by adding synonyms or related concepts to catch more matches during retrieval.

How:

  • Use ontology (thesaurus/wordnet)
  • Embed expansion manually

Example:

User Query Expanded Query
“cancer diagnosis AI” “cancer diagnosis OR oncology diagnosis OR tumor detection using Artificial Intelligence OR Machine Learning”

4. Multi-Query Generation (Multiple Query Variants)

Idea: Generate multiple rephrased queries and perform retrieval for all of them to maximize coverage.

How:

  • Use LLM to generate 3–5 variations
  • Retrieve for each variant and merge results

Example:

User Query Query Variants
“uses of AI in hospitals” “applications of AI in medical field”, “how AI helps hospitals”, “AI in healthcare diagnostics”

(LangChain has MultiQueryRetriever ready for this.)


5. Semantic Search instead of Keyword Search

Idea: Search by meaning, not words. Even if the query is vague, embedding similarity retrieves relevant content.

How:

  • Use embedding models: OpenAI, HuggingFace, Vertex AI, etc.
  • Store documents in vector DB

Example:

User Query Retrieved
“machine thinking” Document about “Artificial Intelligence” (even if “machine thinking” isn’t explicitly written)

6. Fallback to Generative Answers (if retrieval fails)

Idea: If retrieval gives bad results (empty, irrelevant), fall back to pure LLM generation based on user query.

How:

  • Detect low retrieval scores
  • Trigger direct LLM generation with prompt “Based on your knowledge, answer…”

Example:

User Query Fallback
“How does AI taste food?” (irrelevant or no doc) LLM says: “Currently, AI systems can simulate aspects of taste using chemical sensors but cannot physically taste like humans.”

7. Query Classification and Routing

Idea: Classify user intent (e.g., FAQ, how-to, troubleshooting) and route to specialized retrieval logic.

How:

  • Build intent classifiers (tiny LLM or fine-tuned classifier)
  • Have different retrievers or vector indexes

Example:

User Query Detected Intent Retrieval
“how do I reset password?” Troubleshooting Retrieve from “Help Articles” index
“what is AI?” Definition Retrieve from “Knowledge Base”

Summary Table of Techniques

Technique Purpose Tools/Methods
Query Rewriting Make query more complete LLMs (Prompt Engineering)
Context Injection Bring chat history into current query LangChain Memories
Query Expansion Add synonyms/related terms Ontologies, LLM
Multi-Query Generation Create multiple versions LangChain MultiQueryRetriever
Semantic Search Retrieve by meaning, not exact words Vector Databases
Fallbacks Ensure answers even if retrieval fails Retrieval Confidence + Direct LLM
Query Classification Route to specific pipelines Classifiers (zero-shot or fine-tuned models)

Streamlit

  • Refer Here for streamlit and Refer Here for setup and installation
  • Create a new folder and activate virtual environment
  • install streamlit pip install streamlit and create a requirements.txt pip freeze > requirements.txt
  • Streamlit helps in building simple UI without html or css or javascript
  • Create a file app.py with following content
import streamlit as st

st.title("RAG UI Prototype")

  • Now run the application from terminal using streamlit run app.py Preview
  • Refer Here for widgets and Refer Here for showing progress.
  • Now to debug streamlit lets add the following launch configuration in vscode
{
    "name": "Streamlit Debug",
    "type": "debugpy",
    "request": "launch",
    "module": "streamlit",
    "args": [
        "run",
        "${file}"
    ]
}
  • Complete launch configuration
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Streamlit Debug",
            "type": "debugpy",
            "request": "launch",
            "module": "streamlit",
            "args": [
                "run",
                "${file}"
            ]
        }
    ]
}
  • Now for rest of the widgets or other code look into the classroom video and github code links shared
  • Refer Here for streamlit app linked to textbook rag.

By continuous learner

enthusiastic technology learner

Leave a Reply

Discover more from Direct AI Powered By Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading