问答系统 本节摘要:三种系统塑造了现代问答。抽取式找片段,检索增强把它落到文档上,生成式产出答案。每个现代 AI 助手都是三者的混合。用户输入「第一代 iPhone 何时发布?」期待的是「2007 年 6 月 29 日」,不是「苹果的历史悠久而多元」,也不是孤零零一个「2007」没有句子。要一个直接、有据、正确的答案。过去十年里三种架构主导了问答:抽取式 QA(给定含答案的段落,找起止索引,SQuAD 是经典基准)、开放域 QA(段落不给定,先检索再抽取或生成,是每个 RAG 流水线的基石)、生成式/闭卷 QA(大语言模型从参数记忆答,推理最快、事实最不可靠)。2026 年的趋势是混合:检索最好的几段,再提示生成模型据其作答,这就是 RAG,第 14 节深入检索那一半,本节搭问答那一半。
本节摘要:三种系统塑造了现代问答。抽取式找片段,检索增强把它落到文档上,生成式产出答案。每个现代 AI 助手都是三者的混合。用户输入「第一代 iPhone 何时发布?」期待的是「2007 年 6 月 29 日」,不是「苹果的历史悠久而多元」,也不是孤零零一个「2007」没有句子。要一个直接、有据、正确的答案。过去十年里三种架构主导了问答:抽取式 QA(给定含答案的段落,找起止索引,SQuAD 是经典基准)、开放域 QA(段落不给定,先检索再抽取或生成,是每个 RAG 流水线的基石)、生成式/闭卷 QA(大语言模型从参数记忆答,推理最快、事实最不可靠)。2026 年的趋势是混合:检索最好的几段,再提示生成模型据其作答,这就是 RAG,第 14 节深入检索那一半,本节搭问答那一半。
对应原课程:Phase 5 · Lesson 13 ·
question-answering(原英文phases/05-nlp-foundations-to-advanced/13-question-answering/docs/en.md)。前置依赖:第 11 节(机器翻译)、第 10 节(注意力机制)。
阅读完本节,你应当能够:
用户输入「第一代 iPhone 何时发布?」期待「2007 年 6 月 29 日」。不是「苹果的历史悠久而多元」,不是孤零零一个「2007」没有句子。要一个直接、有据、正确的答案。
过去十年,三种架构主导了问答:
2026 年的趋势是混合:检索最好的几段,再提示生成模型据其作答。这就是 RAG,第 14 节深入检索那一半,本节搭问答那一半。
k 段,阅读器(抽取或生成)用这些段产出答案。检索器-阅读器拆分让各自可独立训练评估,现代 RAG 常在两者间加重排器。from transformers import pipeline qa = pipeline("question-answering", model="deepset/roberta-base-squad2") passage = ( "Apple Inc. released the first iPhone on June 29, 2007. " "The device was announced by Steve Jobs at Macworld in January 2007." ) question = "When was the first iPhone released?" answer = qa(question=question, context=passage) print(answer)
{'score': 0.98, 'start': 57, 'end': 70, 'answer': 'June 29, 2007'}
deepset/roberta-base-squad2 在 SQuAD 2.0 上训练,后者含不可答问题。默认地,question-answering 流水线即便在模型 null 分更高时也返回最高分片段——它不会自动返回空答案。要显式「无答案」行为,给流水线传 handle_impossible_answer=True:仅当 null 分超过每个片段分时才返回空。无论哪种,都要查 score 字段。
from sentence_transformers import SentenceTransformer import numpy as np encoder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") corpus = [ "Apple Inc. released the first iPhone on June 29, 2007.", "Macworld 2007 featured the iPhone announcement by Steve Jobs.", "Android launched in 2008 as Google's mobile operating system.", "The first iPod was released in 2001.", ] corpus_embeddings = encoder.encode(corpus, normalize_embeddings=True) def retrieve(question, top_k=2): q_emb = encoder.encode([question], normalize_embeddings=True) sims = (corpus_embeddings @ q_emb.T).squeeze() order = np.argsort(-sims)[:top_k] return [corpus[i] for i in order] def answer(question): passages = retrieve(question, top_k=2) combined = " ".join(passages) return qa(question=question, context=combined) print(answer("When was the first iPhone released?"))
两阶段流水线。稠密检索器(Sentence-BERT)按语义相似找相关段,抽取式阅读器(RoBERTa-SQuAD)从拼接的前几段抽答案片段。小语料可用,百万文档要用 FAISS 或向量库。
def rag_generate(question, llm): passages = retrieve(question, top_k=3) prompt = f"""Context: {chr(10).join('- ' + p for p in passages)} Question: {question} Answer using only the context above. If the context does not contain the answer, say "I don't know." """ return llm(prompt)
提示模式要紧。显式告诉模型据上下文作答、上下文不够时返回「我不知道」,相比朴素提示把幻觉率砍掉 40~60%。更精巧的模式加引用、置信度、结构化抽取。
SQuAD 用**精确匹配(EM)**和 token 级 F1。EM 是归一化(小写、去标点、去冠词)后的严格匹配——要么完全匹配得 1,要么得 0。F1 在预测与参考的 token 重叠上算,给部分分。两者都低估改写:「June 29, 2007」对「June 29th, 2007」通常 EM 是 0(序数破坏归一化)但 F1 因 token 重叠仍不低。
生产 QA 要测:
k。阅读器救不了缺失的段落。RAGAS 专为 RAG 系统设计,是 2026 的发布默认。它无需金标准参考,打四个维度:
无参考打分让你能在生产实时流量上评估,无需策展金答案。在上面叠加 LLM 评判处理精确匹配指标失效的开放问题。
pip install ragas,接上你的检索器+阅读器,每条查询得四个标量,对回归报警。
2026 年的栈。
| 用例 | 推荐 |
|---|---|
| 给定段落,找答案片段 | deepset/roberta-base-squad2 |
| 固定语料上,闭卷不可接受 | RAG:稠密检索 + LLM 阅读器 |
| 文档库上实时 | RAG 配混合(BM25 + 稠密)检索 + 重排器(第 14 节) |
| 对话式 QA(追问) | LLM 带对话历史 + 每轮 RAG |
| 高度事实、强监管领域 | 在权威语料上抽取式;绝不单独生成式 |
抽取式 QA 在 2026 不时髦,因为配 LLM 的 RAG 覆盖更多情况。它在要求逐字引用的场景仍发布:法律研究、合规、审计工具。
保存为 outputs/skill-qa-architect.md:
--- name: qa-architect description: Choose QA architecture, retrieval strategy, and evaluation plan. version: 1.0.0 phase: 5 lesson: 13 tags: [nlp, qa, rag] --- Given requirements (corpus size, question type, factuality constraint, latency budget), output: 1. Architecture. Extractive, RAG with extractive reader, RAG with generative reader, or closed-book LLM. One-sentence reason. 2. Retriever. None, BM25, dense (name the encoder), or hybrid. 3. Reader. SQuAD-tuned model, LLM by name, or "domain-fine-tuned DistilBERT." 4. Evaluation. EM + F1 for extractive benchmarks; answer accuracy + citation accuracy + refusal calibration for production. Name what you are measuring and how you are measuring it. Refuse closed-book LLM answers for regulatory or compliance-sensitive questions. Refuse any QA system without a retrieval-recall baseline (you cannot evaluate the reader without knowing the retriever surfaced the right passage). Flag questions that require multi-hop reasoning as needing specialized multi-hop retrievers like HotpotQA-trained systems.
handle_impossible_answer=True 才给显式空答案,否则永远返回最高分片段。下一节,我们深入 RAG 的检索那一半——进入「信息检索与搜索」,看 BM25、稠密检索、混合融合如何把对的段落捞出来。