Building a Dual-Engine Architecture for AI Q&A Applications: Retrieval Plus Rewriting
This article explains a dual-engine architecture for AI question-answering applications that separates retrieval from generation. It covers request routing by intent, retrieval pipeline design with query expansion, multi-source recall, and reranking, style-controlled rewriting with strict fact constraints, end-to-end orchestration, quantitative evaluation across both pipelines, and common pitfalls to avoid.
1. The Problem: Why a Single Model Struggles to Be Both Accurate and Well-Written
When building a real-world AI Q&A system, teams often run into a structural conflict: the component that finds answers and the component that writes answers have fundamentally different capability requirements. The former needs broad recall, trustworthy sources, and accurate ranking. The latter needs coherent language, appropriate style, and structural stability over long output. Force both goals into one model, and one of them usually gets sacrificed.
The industry commonly frames this conflict as two relatively independent capability domains:
- Retrieval: whether the system can find reliable, citable, and sufficiently current material.
- Generation: whether the system can turn that material into finished text users can use directly.
Decoupling the two into a dual-engine architecture, retrieval engine plus rewriting engine, is currently the more robust engineering approach. This article does not benchmark any specific product; it discusses the general design method behind this architecture.
2. Capability Layering: Separating “What + How” from “Why”
Most users want a clear answer and actionable steps, the “what” and the “how.” The “why” is an explanatory need: less frequent, but more demanding in depth. Requests can therefore be routed into three classes:
- Factual requests: need high-confidence sources; answers should be short, direct, and traceable.
- Procedural requests: need step-by-step output, with emphasis on executability and boundary conditions.
- Explanatory requests: need longer context and a reasoning chain, and allow a larger generation budget.
For the routing layer, prefer a lightweight classifier or rule-first logic so that not every request goes through the most expensive path. Example:
def route(query: str) -> str:if any(k in query for k in ("步骤", "怎么", "如何", "配置")):return "how"if any(k in query for k in ("为什么", "原理", "对比原因")):return "why"return "what"
3. Retrieval Engine Design: Precision Comes from Query Rewriting, Multi-Source Recall, and Reranking
Retrieval precision is not a single-model capability; it is the product of pipeline quality. A practical design breaks it into four steps:
- Query rewriting: expand a conversational question into several search queries, adding synonyms, hypernyms and hyponyms, and constraints.
- Multi-source recall: query web search, structured knowledge bases, internal document stores, and other sources in parallel, normalizing everything into an intermediate representation.
- Reranking: score candidate passages for relevance with a cross-encoder and cut off low-scoring content.
- Context assembly: order by a weighted combination of credibility, freshness, and relevance, and control the injected length to fit the context window.
A simplified recall and assembly flow:
def build_context(query: str, top_k: int = 6) -> list[dict]:variants = expand_query(query) # Query expansioncandidates = parallel_recall(variants) # Parallel multi-source recallranked = rerank(query, candidates)[:top_k] # Rerank and truncatereturn dedup_by_source(ranked) # Deduplicate and tag sources
Building the retrieval ecosystem matters more to final precision than model choice: source allowlists, site quality tiers, main-text extraction quality, and freshness decay factors are the engineering details that determine whether recalled material is actually trustworthy.
4. Rewriting Engine Design: Controllable Style Is the Core Metric
Once material is in hand, the rewriting stage is not about generating all over again; it is style transfer and structural reorganization. Define style as an explicit parameter instead of relying on the model’s default tendencies:
STYLE_PRESETS = {"podcast": {"sentence_len": "short", "persona": "host", "keep_numbers": True},"qa": {"sentence_len": "medium", "persona": "expert", "keep_numbers": True},"science": {"sentence_len": "long", "persona": "narrator", "keep_numbers": True},"forum": {"sentence_len": "medium", "persona": "peer", "keep_numbers": False},}def rewrite(material: list[dict], style: str) -> str:cfg = STYLE_PRESETS[style]return llm_rewrite(material, cfg, forbid_new_facts=True)
The critical constraint is forbid_new_facts: the rewriting stage must not introduce facts absent from the material. Otherwise you get content that reads smoothly but cannot be traced back to a source, which is the most dangerous failure mode for a Q&A system.
5. Orchestration: The Full Lifecycle of One Request
- Receive the question and route by intent.
- The retrieval engine produces a material set annotated with sources.
- If material confidence falls below the threshold, trigger a second retrieval or state the uncertainty explicitly.
- The rewriting engine generates a first draft in the target style.
- A validation layer checks that numbers, entities, and citations are consistent.
- Output the answer plus a source list and optional follow-up suggestions.
Keep the threshold policy conservative: it is better to answer “no reliable source found” than to output an unfounded confident conclusion.
6. Quality Evaluation: Replace Subjective Feelings with Quantifiable Metrics
Evaluation should cover both pipelines, not just the final answer:
- Retrieval side: hit rate, distribution of source credibility, Top-K relevance, freshness compliance rate.
- Generation side: factual consistency (whether new facts were introduced), style adherence, structural completeness, human readability score.
Maintain a fixed evaluation set and a regression pipeline, and run it after every prompt or parameter change, so tuning is not based on gut feeling.
7. Common Pitfalls
- Handing retrieval and generation to the same model, which degrades recall quality under long context.
- Optimizing prompts only, without governing the corpus and main-text extraction.
- Letting the model improvise during rewriting, causing fact drift.
- No expression of uncertainty, producing overconfident output.
- No regression evaluation, making iteration effects impossible to reproduce.
8. Conclusion
The dual-engine idea, retrieval owns accuracy and rewriting owns quality, is essentially about splitting a complex objective into engineering problems that can be optimized independently. If developers keep investing in query rewriting, source governance, reranking, style parameterization, and consistency validation, they can build Q&A applications on top of general model capabilities that deliver clear answers, controllable style, and traceable sources. To handle large document volumes and concurrent access, deployment and operations can build on general cloud capabilities such as object storage, message queues, container platforms, and log monitoring.
