AI RAG and Rewriting: A Practical Guide to Retrieval and Query Transformation
This guide explains why a single model rarely covers both retrieval and rewriting needs in AI question answering, and how to split them into separate layers. It covers the key metrics for retrieval and generation, a two-stage pipeline of retrieve, filter, rewrite, and verify, structured intermediate JSON, practical query preprocessing and multi-source reranking, prompt structure for style-controlled rewriting, and offline plus online quality evaluation. It also lists common anti-patterns and notes that a managed implementation can be assembled from general-purpose cloud service categories while keeping the architecture neutral and observable.
1. Background: Why a Single Model Cannot Cover Every Requirement
In real content production and knowledge service scenarios, what users ask an AI system usually falls into two categories. One is “what + how”, meaning explicit facts, steps, and conclusions. The other is “why”, meaning causes, mechanisms, and reasoning. Most everyday usage belongs to the first category, which means the system must first have high-quality retrieval and answer localization.
But retrieval and text generation are not the same capability. Retrieval emphasizes recall coverage, source credibility, freshness, and completeness of the evidence chain. Rewriting emphasizes semantic fidelity, style transfer, structural reorganization, and readability. Forcing both into one model usually creates a weak spot on one side. A more engineering-oriented approach is to divide the work: the retrieval layer handles “find it and find it accurately”, while the generation layer handles “write it well and write it in the right voice”.
2. Capability Layers: The Boundary Between Retrieval and Generation
2.1 Key Metrics for the Retrieval Layer
- Source coverage: whether it can reach authoritative sites, public documentation, industry reports, and other reliable sources.
- Ranking quality: whether high-credibility, high-relevance content is ranked first.
- Freshness: whether recent content is prioritized to reduce interference from outdated information.
- Traceable evidence: whether verifiable sources are provided for manual review.
2.2 Key Metrics for the Generation Layer
- Semantic fidelity: rewriting does not introduce facts absent from the source.
- Style control: output can follow different styles such as podcast scripts, Q&A scripts, popular science articles, and long community posts.
- Structural capability: scattered material can be reorganized into a general-specific-general structure, a question list, or a timeline.
- Stability: consistency across long text, with little repetition or drift.
3. Recommended Architecture: A Two-Stage Pipeline with Retrieval and Rewriting Decoupled
The overall flow can be designed as four steps: retrieve, filter, rewrite, and verify. Retrieval and rewriting are handled by different capabilities and connected through structured intermediate artifacts, which makes implementation replacement, canary release, and rollback easier.
User question↓Query rewriting (resolve references, split sub-questions)↓Retrieval and reranking (multi-source recall + credibility ranking)↓Material structuring (JSON: claims/evidence/sources/dates)↓Style-based rewriting (generate in the target style)↓Fact verification and citation backfill↓Final answer
The intermediate artifacts should stay structured. Avoid passing large blocks of natural language directly to the generation layer:
{"question": "How to evaluate the reliability of a data migration","claims": [{"text": "Row counts and checksums must be compared before and after migration", "source": "public technical documentation", "date": "2025-11"}],"style": "podcast","constraints": {"max_words": 1200, "no_speculation": true}}
4. Retrieval Layer Practices
- Query preprocessing: resolve references and split sub-questions first. Expand “it” or “this” into explicit entities, and break compound questions into independently searchable sub-queries.
- Multi-source recall and reranking: widen recall first, then rerank by source credibility, freshness, and relevance to avoid the randomness of a single retrieval.
- Evidence first: only put conclusions with sources into the material pool. Mark content without sources as pending confirmation and keep it out of the final draft.
- Deduplication and conflict handling: when multiple sources conflict on the same fact, keep the higher-credibility source and record the conflict reason internally.
- Fallback strategy: when retrieval returns nothing or confidence is low, clearly state that information is insufficient instead of letting the generation layer improvise.
5. Rewriting Layer Practices
The core of rewriting is not swapping words but rebuilding the expression while keeping facts unchanged. The prompt can be split into four parts: role + material + constraints + output format.
Role: You are a senior technical writer.Material: {structured claims}Constraints: Do not add facts; keep sources; mark uncertain points as "to be verified".Output: Follow a podcast speaking rhythm, use short segmented sentences, and include an opening hook and a closing summary.
Differences between common styles can be parameterized:
- Podcast script: short sentences, conversational, driven by rhetorical questions;
- Q&A script: one question and one answer, conclusion first;
- Popular science article: analogy plus layered progression, intuition before details;
- Long community post: a conclusion list first, then point-by-point argumentation.
6. Quality Verification and Evaluation
Build an offline evaluation set covering four metric groups: factual consistency, citation coverage, style match, and readability. Factual consistency can be checked by aligning “claim-evidence” pairs. Citation coverage measures the share of claims that carry sources. Style match is scored by human sampling. Readability focuses on sentence length distribution and paragraph structure. Online, track signals such as “proportion without sources” and “user follow-up rate” for regression monitoring.
7. Common Anti-Patterns
- Mixing retrieval and rewriting in one step, which makes material invisible and errors hard to locate;
- Over-pursuing style, which leads to factual drift;
- Relying on a single retrieval source, with no fallback when coverage gaps appear;
- Lacking structured intermediate artifacts, so the pipeline cannot be replaced or canaried.
8. Conclusion
Separating “find it accurately” from “write it well” is the key to more stable intelligent question answering. The retrieval layer is guided by source credibility and traceable evidence, the generation layer by semantic fidelity and style control, structured material connects the two, and offline evaluation plus online regression support continuous iteration of the content production pipeline. For a managed implementation, you can assemble it yourself from general-purpose capability categories on Baidu AI Cloud, such as object storage, message queues, container platforms, and log service with monitoring and alerting, while keeping the architecture neutral, replaceable, and observable.
