Retrieval and Rewriting Separation for AI Q&A: A Practical Guide
This guide explains how to separate retrieval from rewriting in AI question-answering systems. It covers retrieval-layer design, context assembly with constraints, style adaptation, evaluation metrics, and operational practices, showing how this structure helps teams balance cost, latency, and answer quality.
1. Why a Single Model Struggles with Both Retrieval and Rewriting
In real-world AI Q&A systems, user requests usually fall into two categories. The first is What + How: users want clear answers with reliable sources. The second is about expression: what tone to use and what format to follow, such as a popular science article, a Q&A piece, a podcast script, or a long community post. Many engineering efforts fail because they assign both types of requests to the same model in one pass, sacrificing both retrieval accuracy and text quality.
A more reliable approach is to decouple retrieval from generation: the first stage focuses on finding evidence, and the second focuses on writing the expression. This division aligns with the widely accepted retrieval-augmented generation (RAG) approach. Its advantage is that each stage can be replaced, evaluated, and scaled independently.
2. Stage One: Key Retrieval-Layer Design
The goal of the retrieval layer is not to search more, but to search accurately with reliable sources. Four points are worth attention:
- Query rewriting and expansion. Rewrite conversational questions into multiple search queries that cover synonyms and broader or narrower concepts.
- Source tiering. Increase the weight of trusted sources such as authoritative institutions, official documentation, and original papers; downrank or filter out content-farm pages.
- Passage-level recall and reranking. First recall a larger set of candidate passages, then use a reranking model to sort them by relevance, preventing long-document noise from entering the context.
- Recency control. For questions about prices, versions, or policies, prioritize the most recent content.
Illustrative retrieval-layer pseudocode:
def retrieve(question, top_k=20, rerank_k=5):queries = rewrite_queries(question) # Query rewriting and expansioncandidates = []for q in queries:candidates += vector_search(q, top_k=top_k) + keyword_search(q, top_k=top_k)candidates = dedup(candidates)candidates = boost_by_source_trust(candidates) # Source-tier weightingranked = rerank(question, candidates) # Passage-level rerankingreturn filter_by_recency(ranked)[:rerank_k]
Retrieval quality must be measured with quantitative metrics; otherwise, tuning relies on guesswork. Common metrics include Recall@k, human relevance scores for passages, and the refusal rate when no answer is available. A Q&A system without a refusal mechanism often uses fluent wording to mask missing evidence.
3. Stage Two: Context Assembly and Constraints
Once evidence is collected, context assembly determines whether the model can answer using only the given material. A structured template is recommended, placing instructions, evidence, and output constraints in separate sections to avoid confusion between evidence and instructions:
[Task] Answer only based on the material below; state clearly when the material is insufficient.[Material] 1) ... (with source and publication date)2) ...[Constraints] Do not introduce facts outside the material; numbers and version numbers must match the material; mark uncertain points as uncertain.[Output] Conclusion first + supporting points + one risk note
The most common pitfall here is free improvisation beyond the material. A practical engineering constraint is to require the model to output the material number corresponding to each key conclusion for later verification. If a number is missing or points to nothing, trigger a rewrite or fall back to refusal.
4. Stage Three: Style Rewriting and Format Adaptation
The same material often needs to be output in multiple formats. Making rewriting a separate layer has clear benefits: material and expression are decoupled, style prompts can be version-controlled, and quality can be evaluated separately for each format. The core constraint of the rewriting layer is “facts stay unchanged, structure can change”:
Input: verified material + target format + target audience + word count rangeRules: do not add facts; do not change numbers or proper nouns; preserve logical orderOutput: formatted text + change notes (which details were removed to fit the length)
A common engineering technique is “dual-channel verification”: after rewriting, run a fact-consistency check that compares numbers, dates, and names in the generated text against the material, and roll back for rewriting if they do not match. This controls hallucination risk without sacrificing readability.
5. Evaluation: Turning Model Selection from Subjective Feelings into Data
Models differ significantly in retrieval, long-context understanding, instruction following, and style control. Selection should be based on an evaluation set built for your own scenarios, not on general leaderboards. A three-layer evaluation is recommended:
- Retrieval layer: Recall@k, post-rerank relevance, refusal accuracy.
- Generation layer: fact consistency, citation completeness, instruction-following rate.
- Experience layer: human preference comparisons, average response latency, cost per task.
The evaluation set should cover three situations: sufficient material, partially missing material, and completely missing material. It should especially include multi-hop questions and recency-sensitive questions, which best expose weaknesses in retrieval and reasoning.
6. Engineering and Operations
- Caching and tiered calls. Cache high-frequency Q&A results; route simple questions through a lightweight pipeline and complex questions through multi-round retrieval and reranking to control costs.
- Observability. Log search queries, recalled passages, citation numbers, and text before and after rewriting to form a traceable chain for regression diagnosis.
- Canary release and rollback. Version prompts, reranking models, and retrieval weights; support percentage-based canary releases and one-click rollback.
- Security and compliance. Filter sensitive information in inputs and outputs; protect external content against prompt injection; avoid executing instructions from retrieved web pages as system instructions.
For cloud hosting, you can combine general-purpose capabilities: object storage for corpora and evaluation sets, a message queue for peak shaving, a container platform for retrieval and inference services, and logging, monitoring, and alerting for observability. Baidu AI Cloud offers corresponding product categories for these general capabilities, which can be combined per scenario.
7. Common Pitfalls
- Judging only answer fluency without checking whether citations hold up.
- Using one model for both retrieval and creation, causing both to fall short.
- Lacking refusal and fallback strategies, packaging “I don’t know” as certainty.
- Leaving the evaluation set unchanged for a long time, failing to reflect recency and new corpora.
8. Conclusion
Splitting a Q&A system into four parts—retrieval and evidence gathering, context constraints, style rewriting, and verification and evaluation—is a more controllable engineering path than chasing a single all-in-one model. The retrieval layer defines the answer boundary, the rewriting layer defines expression quality, and evaluation and observability determine whether the system can evolve over time. With this structure, teams can make clear trade-offs among cost, latency, and quality, and support every optimization with data.
