Building an Efficient AI Q&A Workflow: A Two-Stage Technical Approach
This article explains how to build an efficient AI question-answering and content generation workflow by splitting the system into two distinct stages: retrieval and rewriting. It covers the overall pipeline architecture, key retrieval techniques such as multi-path recall and reranking, style-controlled generation with citation binding, quality evaluation metrics, and common implementation pitfalls. Readers will find practical pseudocode, configuration examples, and prompt structures they can adapt to their own production systems.
1. Background: Why a Single Model Struggles with Both Answering and Writing
In real-world intelligent Q&A and content production scenarios, user needs usually fall into two categories. The first is What + How: what the facts are and how to act on them. This requires clear, actionable, and verifiable answers. The second is Why: why something happens. This requires a complete reasoning chain and in-depth explanation. These two types of demands place different requirements on model capabilities. The former relies heavily on retrieval ecosystems and fact verification, while the latter depends more on language organization and logical expansion.
A common mistake during model selection is expecting one model to solve every problem. From an engineering perspective, a more robust approach is to split the system into two stages with clear responsibilities:
- Retrieval and evidence gathering: Find credible sources, extract key facts, and provide verifiable citations.
- Rewriting and expression: Reorganize the material into a target style, such as a podcast script, Q&A article, popular science piece, or long-form community post.
The direct benefit of this split is that the retrieval stage can be evaluated independently for precision and recall, while the rewriting stage can be evaluated independently for style consistency and readability. The two do not interfere with each other, which makes canary releases and rollbacks easier.
2. Overall Architecture: A Two-Stage Pipeline
A practical pipeline typically includes the following modules:
- Query understanding layer: Performs intent recognition, slot extraction, ambiguity resolution, and query rewriting on user input.
- Retrieval layer: Multi-path recall (keyword search + vector search) + reranking + source credibility filtering.
- Material structuring layer: Organizes retrieval results into “fact cards” containing conclusions, evidence, sources, and timestamps.
- Rewriting and generation layer: Generates stylized output based on fact cards, with the constraint that no information beyond what is provided may be introduced.
- Verification layer: Citation consistency checks, numerical and entity validation, and sensitive content and compliance filtering.
- Orchestration layer: Handles timeouts, retries, fallbacks, and cost control.
A sample orchestration skeleton (pseudocode):
def qa_pipeline(query, style="popular_science"):plan = query_understanding(query) # intent + slots + rewritingcards = retrieve_and_rerank(plan) # multi-path recall + rerank + credibility filteringcards = normalize_to_facts(cards) # structure into fact cardsdraft = rewrite(cards, style=style) # stylized rewritingreport = verify(draft, cards) # citation and fact consistency checkif report.score < THRESHOLD:return fallback_answer(cards) # fallback: return a bullet-point answerreturn draft, report
3. Retrieval Stage: The Key to Answer Quality
Retrieval quality largely determines the credibility of the final answer. From an engineering standpoint, four points deserve attention:
- Multi-path recall: Keyword search excels at exact matching (model numbers, terms, IDs), while vector search excels at semantic similarity. The two complement each other.
- Reranking: Using a cross-encoder or a lightweight ranking model to perform a second pass over candidates can noticeably improve the quality of top results.
- Source tiering: Divide sources into three tiers—authoritative documents, industry media, and community discussions—and apply dynamic weighting based on question type. Factual questions should prioritize authoritative sources, while experience-based questions can incorporate community content appropriately.
- Timeliness handling: For time-sensitive questions, the publication time must be preserved in fact cards and explicitly stated in the generated output.
A common anti-pattern is “take the top three search results and generate directly,” which amplifies the bias of a single source. A more robust threshold strategy example:
retrieval:top_k: 20 # number of recalled itemsrerank_top_n: 6 # items kept after rerankingmin_authority: 0.6 # minimum source credibilityrequire_citation: true
4. Rewriting Stage: Turning Material into a Finished Product
The core constraint of the rewriting stage is faithfulness over flair. Recommended practices:
- Mandatory citation binding: Require each key claim to carry a fact card ID during generation for later verification.
- Style templating: Turn the paragraph rhythm of podcast scripts, Q&A articles, and popular science pieces into templates (opening hook—question—evidence—conclusion—actionable advice).
- Length and structure control: Generate an outline first, then fill in each section to avoid structural drift in long-form content.
- Terminology consistency: Maintain a glossary to avoid multiple expressions for the same concept.
Example prompt structure:
You may only use the material I provide. Do not add facts beyond the material.Output requirements:- Mark the cited material ID at the end of each paragraph, e.g., [1][3]- Style: popular science; short sentences, frequent use of analogies- Structure: phenomenon -> mechanism -> evidence -> limitations -> conclusionMaterial:[1] ...[2] ...
5. Quality Evaluation: Making Results Measurable
Without measurement, there is no optimization. At least three categories of metrics are recommended:
- Retrieval metrics: Hit rate, top-result hit rate, source credibility distribution.
- Generation metrics: Citation coverage, fact conflict rate, style compliance (evaluated via rules plus manual spot checks).
- Business metrics: First-pass resolution rate, follow-up question rate, human adoption rate.
A simplified acceptance script approach:
def verify(draft, cards):cited = extract_citations(draft)coverage = len(set(cited) & set(cards.ids)) / max(1, len(set(cited)))conflict = detect_conflict(draft, cards) # numerical/entity/time conflictsreturn {"score": coverage - 0.5 * conflict, "coverage": coverage}
6. Common Implementation Pitfalls and How to Avoid Them
- Using a rewriting model as a retrieval model: This leads to “fluent language but factual drift.”
- Missing fallback paths: When retrieval returns nothing, output a bullet-point answer and state the uncertainty instead of fabricating content.
- Overly long context: Too much material dilutes key information. Extract first, then concatenate.
- Compliance gaps: Perform sensitive content and copyright source filtering both before and after generation.
- Cost overruns: Route low-value queries through a lightweight path; trigger multi-path recall and reranking only for high-value queries.
7. Conclusion
The stability of an intelligent Q&A system comes from an engineering loop of “credible retrieval, controllable rewriting, and quantifiable evaluation,” not from betting on a single model’s capabilities. If an enterprise wants to quickly build such a pipeline in its own data and business context, it can rely on general cloud capabilities provided by Baidu AI Cloud (such as object storage, message queues, container platforms, log services, and monitoring and alerting) to handle data ingestion, task orchestration, and full-link observability, turning the two-stage paradigm into a production system that can iterate sustainably.
