0Pricing
AI Agents · Lesson

Sub-Question Decomposition Strategy

For complex multi-hop questions, split into sub-questions, answer each, then synthesize a final answer.

When Single-Hop Fails

Some questions need multiple lookups:

"Compare the revenue of Apple in 2022 and Microsoft in 2023."

One similarity search will not retrieve both facts; you need TWO sub-queries.

Sub-Question Decomposition

LlamaIndex SubQuestionQueryEngine splits a question into sub-questions, answers each, then synthesises:

from llama_index.core.query_engine import SubQuestionQueryEngine
from llama_index.core.tools import QueryEngineTool, ToolMetadata

query_engine_tools = [
    QueryEngineTool(
        query_engine=apple_index.as_query_engine(),
        metadata=ToolMetadata(name='apple', description='Financial data for Apple Inc.')
    ),
    QueryEngineTool(
        query_engine=msft_index.as_query_engine(),
        metadata=ToolMetadata(name='microsoft', description='Financial data for Microsoft Corp.')
    )
]

engine = SubQuestionQueryEngine.from_defaults(query_engine_tools=query_engine_tools)
response = engine.query('Compare 2022 revenue of Apple vs 2023 revenue of Microsoft')

All lessons in this course

  1. Document Loaders and Parsers
  2. The Index Hierarchy: Vector, Tree, Keyword
  3. Query Engines and Response Synthesis
  4. Sub-Question Decomposition Strategy
← Back to AI Agents