RAG question parsing starts at the route

Most RAG builds treat the chat box as special input. The user’s string goes verbatim into the embedder, or to an agent that’s supposed to sort everything out on its own. Angela Shi’s article on question parsing in RAG over at Towards Data Science argues the opposite, and I agree with it: RAG question parsing deserves the same care as document parsing. The parsed question splits into a retrieval brief and a generation brief before either stage runs. For WordPress developers, none of this is new machinery. You already write it every time you register a REST route and describe the arguments.

Her series opens with document parsing, and content indexing in WordPress starts with the parser too. The question was the last untyped input.

What the embedder does with a negation

The example that convinced me is the insurance question: “What is the limit per claim, not the deductible?” The string carries a topic, an answer shape, and a negative cue, and the cue is there because limits and deductibles get listed together. Hand the whole string to retrieval and the negation lands nowhere useful. Embeddings don’t do exclusion. The vector for “limit per claim NOT deductible” sits almost on top of the vector for “limit per claim deductible”. You haven’t excluded anything. You’ve added a word that pulls deductible-bearing lines closer, and the user’s warning becomes the retrieval bug.

The wrong fix and why it fails

The reflex is to filter the excluded term out of the candidate set before ranking, and it fails at every granularity you can try. Drop lines that mention the deductible and you lose the answer itself:

The limit per claim is €1,500,000, with a deductible of €1,000.

Drop the page and you lose it again, because the limit and the deductible share a table. The section is named Limits and Deductibles, so dropping the whole section loses it a third time. The terms co-occur because they’re related, which is why the user warned you. Keyword search doesn’t rescue you either, since limit AND NOT deductible removes every passage where both words appear, and those passages hold the answer. The WooCommerce version shows up all the time. A shopper asks what something costs “not the sale price”, and both prices live in the same product record, so a pre-filter that removes one removes the product. If you’re grounding a chatbot in store data, that’s the failure to design against.

RAG question parsing starts at the route

If your chat endpoint accepts one raw question string and nothing else, the parsing has quietly become someone else’s problem. The fix in WordPress terms is to make the parsed question the shape of the endpoint. register_rest_route already has the machinery: an args map with types, defaults, enums, and sanitize and validate callbacks. Your question brief is that map.

add_action( 'rest_api_init', function () {
    register_rest_route( 'bbioon/v1', '/ask', array(
        'methods'             => 'POST',
        'callback'            => 'bbioon_ask',
        'permission_callback' => function () {
            return current_user_can( 'read' );
        },
        'args'                => array(
            'question'      => array(
                'type'              => 'string',
                'required'          => true,
                'sanitize_callback' => 'sanitize_text_field',
            ),
            'topic'         => array(
                'type'              => 'string',
                'default'           => '',
                'sanitize_callback' => 'sanitize_text_field',
            ),
            'scope'         => array(
                'type'    => 'string',
                'default' => 'all',
                'enum'    => array( 'all', 'pages', 'products' ),
            ),
            'answer_format' => array(
                'type'    => 'string',
                'default' => 'text',
                'enum'    => array( 'text', 'amount', 'date' ),
            ),
            'exclude_terms' => array(
                'type'              => 'array',
                'default'           => array(),
                'sanitize_callback' => 'bbioon_sanitize_terms',
                // generation only: never append to the retrieval query
            ),
        ),
    ) );
} );

function bbioon_sanitize_terms( $value ) {
    return array_map( 'sanitize_text_field', (array) $value );
}

A cheap first pass fills most of this with no model call. A currency or date pattern sets answer_format. A SKU or policy code pattern sets the anchor keyword the embedder would miss anyway. Then your bbioon_ask callback builds two views from one validated request: retrieval gets the topic, the scope, and the anchors, while generation gets the original wording, the answer format, and exclude_terms.

What each stage is allowed to act on

Retrieval is similarity matching. It’s good at finding what’s close to the query and bad at rejecting precisely, because it has no way to act on a negation. Generation reads, distinguishes, and excludes, and it’s the only stage in the pipeline that can. So I’d adopt the article’s rule as written: retrieve broadly, exclude at generation. “The user asked for the limit, not the deductible” goes into the prompt, never into the query string. The same routing covers the related cases, so “not from the previous version” means retrieve all versions and let the model pick.

Where the split earns its keep

My caveat is that this isn’t free. On a small corpus, if retrieval over a few hundred posts already returns everything relevant, generation sees it all and the brief is mostly bookkeeping, and I’m not sure the split pays for itself there. It earns its keep once the candidate space is big enough that retrieval genuinely excludes things. And if your parser is another LLM call, you’ve doubled the model calls per question, which hurts on shared hosting. That’s why I fill every field a regex can fill first and only send the leftovers to a model.

If you’re building a question endpoint like this and the answers keep coming back confident and wrong, the fix usually lives in the parsing layer before it lives in the model. I do this work on WordPress and WooCommerce sites and I’m glad to look at your pipeline.

The check I run before shipping one of these is grep: search the retrieval call and the prompt build for the same field. If exclude_terms shows up in both, the split leaked, and the next “not the X” question will find it.

author avatar
Ahmad Wael
I'm a WordPress and WooCommerce developer with 15+ years of experience building custom e-commerce solutions and plugins. I specialize in PHP development, following WordPress coding standards to deliver clean, maintainable code. Currently, I'm exploring AI and e-commerce by building multi-agent systems and SaaS products that integrate technologies like Google Gemini API with WordPress platforms, approaching every project with a commitment to performance, security, and exceptional user experience.

Leave a Comment