liveBook · Manning

Atomic query

As explained in Build an Advanced RAG Application (From Scratch)

An atomic sub-query is an individual query representing one information need extracted from a compound query.

An atomic query represents one information need extracted from a compound question. Decomposition gives each need its own embedding, route classification, and retrieval call, avoiding a blended embedding that returns partially relevant chunks. section 7.4.3

Compound question decomposed

The question “What was Uber's revenue in 2021, and how does its gross bookings growth compare to Lyft's?” becomes three independently answerable queries: Uber's 2021 revenue, Uber's 2021 gross bookings growth, and Lyft's 2021 gross bookings growth. section 7.4.3

A decomposer preserves entities and time periods, avoids overlap, and returns two to four focused queries for compound input. Simple input passes through unchanged as a one-element array. The resulting answers are synthesized after focused retrieval. section 7.4.3

def decompose_query(user_query: str) -> list:
    decompose_prompt = f'''
    Analyze the following query and determine if it contains
    multiple distinct information needs. If it does, break it
    into 2-4 focused atomic sub-queries. If it is already a
    single focused question, return it unchanged.
 
    Rules:
    - Each sub-query must be independently answerable
    - Sub-queries should not overlap or repeat each other
    - Preserve specific entities (company names, time periods)
    - Return ONLY a JSON array of strings
 
    Query: {user_query}
    '''
    response = openai_client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "user", "content": decompose_prompt}
        ],
        temperature=0,
    )
    try:
        content = response.choices[0].message.content.strip()
        json_match = re.search(                                 #A
            r"\[.*\]", content, re.DOTALL
        )
        return json.loads(json_match.group())
    except (json.JSONDecodeError, AttributeError):
        return [user_query]                                     #B

Use the Python implementation to build a JSON-returning decomposer with independently answerable outputs and unchanged pass-through for simple questions. section 7.4.3

Within the enterprise RAG pipeline, decomposition follows cache filtering, routing, and query rewriting, then sends each atomic query to the selected collection or live search before response synthesis. section 7.5

How Build an Advanced RAG Application (From Scratch) covers it

How deep it goes
Defines decomposition, gives prompt rules and outputs, and stops short of detailing synthesis internals.
Examples
Uses an Uber–Lyft financial comparison and a simple Uber revenue question to demonstrate splitting and pass-through.
What you can do after
Build it from scratch as a Python decomposer that emits focused JSON sub-queries for downstream retrieval.
How it connects
Connects query decomposition with query rewriting, semantic caching, routing, retrieval, and response synthesis.

Also explained in

Compare how 2 books explain atomic query →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an Advanced RAG Application (From Scratch). The section links point to the parts of the book it draws on.