Skip to main content

Command Palette

Search for a command to run...

๐Ÿ” Zoom In or Out: Query Decomposition for Smarter Retrieval

Learn how Query Decomposition refines vague or complex prompts by breaking them down or expanding them.

Updated
โ€ข4 min read
๐Ÿ” Zoom In or Out: Query Decomposition for Smarter Retrieval

๐Ÿง  What Is Query Decomposition?

Query Decomposition is a smart method that either breaks down complex queries into simpler parts (less abstraction) or expands narrow queries into broader topics (more abstraction). This helps AI systems retrieve the most relevant information โ€” especially in multi-turn or research-heavy tasks.


๐Ÿ”„ Two Modes of Decomposition

๐Ÿ”น 1. Less Abstraction (Zooming In)

Breaks a complex or broad query into smaller, focused sub-questions.

Example:
User asks: โ€œWhat is machine learning?โ€

It becomes:

  • What is a machine?

  • What is learning?

  • How do machines learn?

Helps fetch foundational context.

๐Ÿ”ธ 2. More Abstraction (Zooming Out)

Expands a specific query into a more generalized one to capture broader context.

Example:
User asks: โ€œWhere was Elon Musk born?โ€
Transformed into: โ€œWhat is the life history of Elon Musk?โ€

Now, the model can retrieve a richer narrative, not just a fact.


๐ŸŽฏ Why Use Query Decomposition?

  • Handle vague, broad, or overloaded questions.

  • Improve grounding of answers.

  • Fetch facts and context.

  • Great for education and chatbot use cases.


๐Ÿ’ป Python Code: Query Decomposition with LangChain + Qdrant

from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Qdrant
from qdrant_client import QdrantClient
from collections import defaultdict

# Initialize components
llm = ChatOpenAI()
qdrant_client = QdrantClient(url="http://localhost:6333")
embedding_model = OpenAIEmbeddings()
vector_store = Qdrant(client=qdrant_client, collection_name="my_collection", embeddings=embedding_model)

# === PROMPTS ===

# Prompt for LESS Abstraction (zoom in)
less_abstraction_prompt = PromptTemplate(
    input_variables=["query"],
    template="Break the following query into 3 simpler sub-questions to increase clarity: {query}"
)

# Prompt for MORE Abstraction (zoom out)
more_abstraction_prompt = PromptTemplate(
    input_variables=["query"],
    template="Reframe the following specific query into a broader context-based question or topic: {query}"
)

# === CHAINS ===
less_chain = LLMChain(llm=llm, prompt=less_abstraction_prompt)
more_chain = LLMChain(llm=llm, prompt=more_abstraction_prompt)

# === INPUT ===
user_query = "Where was Elon Musk born?"

# === Decompose Queries ===
less_sub_queries = less_chain.run(user_query).split('\n')
more_sub_query = more_chain.run(user_query)  # One broader query

print("๐Ÿ”น LESS Abstraction Queries (Zoomed In):")
for q in less_sub_queries:
    print(f"- {q}")

print("\n๐Ÿ”ธ MORE Abstraction Query (Zoomed Out):")
print(f"- {more_sub_query}")

# === RETRIEVE CHUNKS ===
all_queries = less_sub_queries + [more_sub_query]
retrieved_chunks = []

for query in all_queries:
    docs = vector_store.similarity_search(query, k=5)
    retrieved_chunks.extend(docs)

# === DEDUPLICATE ===
unique_chunks = list({doc.page_content: doc for doc in retrieved_chunks}.values())

# === DISPLAY RESULTS ===
print("\n๐Ÿ“„ Retrieved Chunks from Combined Decomposition:")
for doc in unique_chunks:
    print(doc.page_content)

๐Ÿ“ˆ Pros and Cons of Query Decomposition

Pros ๐Ÿ‘Cons ๐Ÿ‘Ž
Helps resolve vague or overloaded queriesLLM might over-decompose or misinterpret intent
Great for educational or research-style questionsSlight increase in processing time
Captures both high-level and low-level informationNeeds deduplication logic
Works well in chain-of-thought reasoning modelsMay return overly generic chunks if not carefully prompted

โœ… Best Practices

  • Use prompt tuning to control whether you want more or less abstraction.

  • Limit to 3โ€“4 sub-queries to avoid excessive noise.

  • Combine with RRF if merging chunks from multiple decompositions.


๐Ÿ” Decomposition vs. Fan-Out: What's the Difference?

FeatureFan-OutDecomposition
PurposeSimilar phrasing of same querySimplifying or expanding the query
FocusSemantic varietyConceptual breakdown
Ideal ForBroad coverageDepth and clarity

Use them together for maximum retrieval power.


๐Ÿ”œ Next Up: Blog 5 โ€“ No Data? No Problem. Meet HYDE โ€” The AI That Writes Before It Retrieves


Thank you for reading our article! We appreciate your support and encourage you to follow us for more engaging content. Stay tuned for exciting updates and valuable insights in the future. Don't miss out on our upcoming articlesโ€”stay connected and be part of our community!

YouTube : youtube.com/@mycodingjourney2245

LinkedIn : linkedin.com/in/nidhi-jagga-149b24278

GitHub : github.com/nidhijagga

HashNode : https://mycodingjourney.hashnode.dev/


A big shoutout to Piyush Garg Hitesh Choudhary for kickstarting the GenAI Cohort and breaking down the world of Generative AI in such a simple, relatable, and impactful way! ๐Ÿš€
Your efforts are truly appreciated โ€” learning GenAI has never felt this fun and accessible. ๐Ÿ™Œ


#ChaiCode #ChaiAndCode #GenAI #ChaiAndCode #GenAI #QueryDecomposition #ChainOfThought #LLMDesign #ChaiCode #ChaiAndCode #GenAI #PromptDesign #LangChain

Gen AI

Part 6 of 11

Explore the future of technology with our Generative AI seriesโ€”your go-to guide for mastering AI tools, prompt engineering, LLMs, and practical applications. From beginner tips to advanced techniques.

Up next

๐Ÿงช No Data? No Problem. Meet HYDE โ€” The AI That Writes Before It Retrieves

Discover how the HYDE method improves AI search by generating hypothetical documents based on user queries.