Retrieval and Style Rewriting for Production Q&A Systems
A practical look at splitting intelligent Q&A into retrieval, generation, and style rewriting so factual accuracy, traceability, and tone can be measured and tuned independently—plus hybrid recall, grounded prompting, and sample pipeline code.
Retrieval and Style Rewriting for Production Q&A Systems
Why a single model is not enough for real Q&A
In production question answering, teams often start by betting on one large model. Two limits show up quickly. Factuality: without external knowledge, models can sound confident while being wrong. Style: the same facts may need a conversational podcast script, a rigorous explainer, or a short FAQ answer, and one model rarely holds a stable voice for every use case.
A more reliable engineering pattern is to split Q&A into three stages you can optimize and evaluate separately: retrieval, generation, and rewriting—retrieval-augmented generation (RAG) plus a post-processing rewrite. This article walks through that architecture and how to land each stage.
Architecture: retrieve, generate, rewrite
A practical pipeline is:
- Retrieval: recall high-trust material from a knowledge base for the user question.
- Generation: produce a clear, source-backed answer (what + how) using only that material.
- Rewriting: move the answer into the target voice without changing facts.
Separation of concerns matters: recall, factual consistency, and style fit can be scored and tuned on their own instead of dumping every failure into one model call. When answers are wrong in production, you can tell whether retrieval missed the evidence or generation ignored it, rather than blindly swapping or scaling the model.
Retrieval: precision and source trust
Most users want one clear answer, not a pile of candidates. Core metrics are Top-K hit rate and source credibility, not raw recall volume.
- Hybrid recall: dense (vector) search handles paraphrases; keyword search handles proper nouns and numbers. Reciprocal rank fusion (RRF) merges them and cuts misses.
- Source weighting: boost official manuals and authoritative docs; down-weight forums and aggregators so unreliable text is suppressed early.
- Reranking: a light ranking model reorders the fused set so the most relevant snippets sit first—this often sets the ceiling for generation quality.
Generation: explicit what + how answers
Generation should stay faithful to the material and stay structured. Useful prompt constraints: answer only from the given sources, cite snippet IDs for traceability, and put the conclusion before the steps. That matches the demand for a definite answer while remaining checkable. If the material is insufficient, the model should say so instead of inventing—this is the first control on hallucination.
Rewriting: controlled style transfer
Rewriting is about sounding right. Decouple facts from voice: the rewrite prompt must keep facts, numbers, and citation IDs unchanged and only adjust tone, sentence shape, and structure. One authoritative answer can then become a podcast script, an explainer, or a FAQ without repeating retrieval and generation, saving compute and keeping a single source of truth.
Programming vs. conversation vs. knowledge Q&A
Different products stress different capabilities; configure, do not fork the whole stack:
- Coding help: recall over code corpora and long context; emphasize runnable answers and edge cases.
- Everyday chat: dialogue state and natural tone; emphasize multi-turn context.
- Knowledge Q&A: retrieval precision and citations; emphasize factual consistency.
Different retrieval settings, prompt templates, and rewrite-style parameters can cover these cases while the backbone stays the same.
Illustrative pipeline code
The following sketch shows the three-stage path (llm and index are generic and can wrap any model and vector search service):
from dataclasses import dataclassfrom typing import List@dataclassclass Document:doc_id: strsource: str # source type, used for trust weightingcontent: strscore: float = 0.0def retrieve(query: str, index, top_k: int = 5) -> List[Document]:"""Retrieve: dense + keyword search, RRF fusion, then source-trust weighting."""dense = index.vector_search(query, top_k=top_k)sparse = index.keyword_search(query, top_k=top_k)merged = fuse_rrf(dense, sparse)for d in merged:d.score *= source_trust(d.source) # boost authoritative sourcesreturn sorted(merged, key=lambda x: x.score, reverse=True)[:top_k]def generate(query: str, docs: List[Document]) -> str:"""Generate: conclusion + steps from sources only, with citation IDs."""context = "\n".join(f"[{d.doc_id}] {d.content}" for d in docs)prompt = ("Answer using only the following material. Give the conclusion first, ""then the steps, and include citation IDs. If the material is insufficient, say so clearly:\n"f"{context}\n\nQuestion: {query}")return llm.chat(prompt)def rewrite(answer: str, style: str) -> str:"""Rewrite: keep facts and citations; change only style."""prompt = (f"Keeping facts, numbers, and citation IDs unchanged, "f"rewrite the following in a 「{style}」 style:\n{answer}")return llm.chat(prompt)# Main pathdocs = retrieve(query, index)answer = generate(query, docs)final = rewrite(answer, style="科普稿")
Evaluation, monitoring, and iteration
After launch, close the loop: retrieval monitors Top-K hit rate and source mix; generation monitors factual consistency and citation coverage; rewriting monitors style fit and fact retention. Logging and alerts can instrument the full path. Offline “wrong answer” samples become an eval set that drives retrieval and prompt iteration. For large corpora and concurrency, object storage for documents, queues for peak smoothing, and container autoscaling keep the path stable.
Summary
Accuracy comes from retrieval plus generation that stays on the sources. Voice comes from a rewrite that does not touch facts. Splitting Q&A into retrieve–generate–rewrite lets you measure and tune each piece and reuse the same backbone for coding, chat, and knowledge Q&A. With this method you can build production Q&A that is high-precision, traceable, and style-customizable at a controlled cost.
