如何用检索增强生成(RAG)在没有幻觉的情况下回答问题?

林邦源 · AI 笔记 · 2026年11月3日 · 阅读时间约 8 分钟

English 繁體中文 简体中文

问一个聊天模型问题,它会凭训练中学到的知识生成答案。文字流利,但有些“事实”可能是凭空编造的——这就是幻觉(hallucination)。检索增强生成(Retrieval-Augmented Generation,缩写 RAG)把次序调转:程序先从真实文件找出相关的句子,再请模型只利用那些句子作答。

本文的程序在一次执行中展示这个差异。它把问题 “How many people get headaches at least once a year?” 问 DeepSeek 两次:第一次让 AI 纯粹凭训练所学生成答案,第二次则以 Panadol 头痛网页中最相关的三句作为依据。第二次的答案还会附上它依据的行号,你可以自己打开文件核对——全程 82 行。

一分钟看懂原理

名称本身就说明了三个步骤。检索(Retrieval):把每一句原文变成一个嵌入向量(embedding)——一列数字,捕捉句子的意思(见下方相似句子笔记)——然后选出与问题意思最接近的几句。增强(Augmented):把这几句作为上下文(context)贴进提示字符串,令问题到达模型时,事实已附在旁。生成(Generation):模型作答,但提示已规定它只能用提供的上下文、并给出行号。

先取得密钥

程序的聊天部分要与 DeepSeek 通信,所以需要一条API 密钥——一串像密码的秘密字符。到 https://platform.deepseek.com/ 注册,在 API keys 页面创建一条密钥;少量余额已足够试用。密钥放在同一文件夹的 .env 文件里,一行就够:

DEEPSEEK_API_KEY=paste-your-key-here

下载示例后请自行创建此文件;zip 不会包含它,因为密钥属于个人。下方的 DeepSeek 笔记有更详细的密钥与费用说明。

逐段拆解程序

import os
import re
import requests
from bs4 import BeautifulSoup
from dotenv import load_dotenv
load_dotenv()
from langchain_core.documents import Document
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_openai import ChatOpenAI

前五行引入熟悉的工具:os 读取程序外的设置,re 用模式切开文字,requests 下载网页,BeautifulSoup 挑选可读部分,load_dotenv() 立即载入 .env 密钥文件。langchain_core 的几行引入 LangChain 的积木,用管道符号 | 串在一起:Document 是带有标签的一段文字,InMemoryVectorStore 是存在内存中的语义索引,ChatPromptTemplate 是有待填空位的消息模板,StrOutputParser 把回复解包成纯文字,RunnablePassthrough 把原问题原封不动往下传。最后,HuggingFaceEmbeddings 把文字变成嵌入向量,ChatOpenAI 则是聊天连接器——名字跟 OpenAI 的消息格式一样,因为 DeepSeek 刻意采用同一套规格。

DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
my_model = ChatOpenAI(
    api_key=DEEPSEEK_API_KEY,
    base_url="https://api.deepseek.com",
    model_name="deepseek-chat"
)

embeddings = HuggingFaceEmbeddings(
    model_name="BAAI/bge-m3"
)

先准备两条连线。my_model 把连接器指向 DeepSeek 的网址,带着刚刚载入的密钥;model_name="deepseek-chat" 选用哪个模型作答——名字会随时间改变,有疑问时请查阅官方文档。embeddings 载入 BAAI/bge-m3,与相似句子笔记用的是同一个多语言嵌入模型;首次执行会下载约 2 GB,之后便会储存在电脑上。

url = "https://www.panadol.com/en-gb/adult-symptoms/headache/"
headers = {"User-Agent": "Mozilla/5.0 (compatible; SkillBot/1.0)"}
resp = requests.get(url, headers=headers, timeout=15)
resp.raise_for_status()
resp.encoding = resp.apparent_encoding
soup = BeautifulSoup(resp.text, "html.parser")
for tag in soup(["script", "style", "noscript", "nav", "footer"]):
    tag.decompose()
lines = [" ".join(tag.get_text(" ").split()) for tag in soup.find_all(["h1", "h2", "h3", "p", "li"])]
lines = [line for line in lines if line != ""]

sentences = []
for line in lines:
    for sentence in re.split(r"(?<=[.!?])\s+(?=[A-Z])", line):
        sentences.append(sentence)
lines = sentences

with open("panadol.txt", "w", encoding="utf-8") as outfile:
    for line in lines:
        outfile.write(line + "\n")

这是网页抓取笔记的缩影:下载 Panadol 头痛页(附上客气的 User-Agent 标明程序名称),丢弃 script、style、导航和页脚部分,保留标题、段落和列表项目并整理好空白。然后用模式 (?<=[.!?])\s+(?=[A-Z]) 把每行切成句子——遇到句号、问号或感叹号后紧接着空格和大写字母就切开——与句子切分笔记完全相同。结果存成 panadol.txt,一行一句。文件内样子:

Who gets headaches?
Headaches are very common.
Around seven in 10 people get them at least once a year.
Recurring headaches affect approximately half of adults worldwide.
chunked_text_with_metadata = []
for line_num, line in enumerate(lines, start=1):
    if line.strip() == "":
        continue
    content = f"line number {line_num}\n{line.strip()}"
    chunked_text_with_metadata.append(
        Document(
            page_content=content,
            metadata={"source": url, "line": line_num},
        )
    )

每一句句子都包装成 Document。注意放进 page_content 的内容:上面先写一行 line number 128(或其他编号),下面才是句子本身。metadata 另外记录了来源网址和行号。把行号直接写进模型会读的文字里,是它后面能引用行号的关键——它真的在输入里看得到那个标签。

vectorstore = InMemoryVectorStore.from_documents(chunked_text_with_metadata, embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})

from_documents 把每个 Document 都变成一个嵌入向量,全部存在内存里——in memory 的意思是索引每次执行都重建,程序一结束就消失,对单一网页来说刚好。as_retriever 配合 "k": 3 创建一个检索器:给它一条问题,它就传回三个与问题意思最接近的 Document。

print("Before RAG")
before_rag_template = "{topic}"
before_rag_prompt = ChatPromptTemplate.from_template(before_rag_template)
before_rag_chain = before_rag_prompt | my_model | StrOutputParser()
user_question = "How many people get headaches at least once a year?"
print(before_rag_chain.invoke({"topic": user_question}))

第一次提问刻意简单:模板 "{topic}" 只有问题,所以模型完全凭训练所学作答。管道符号从左读到右:填模板,送给 DeepSeek,解包成纯文字。invoke({"topic": user_question}) 填好空位就执行整条链。回复流利详细——但没有任何来源可以核对。

print("After RAG")
after_rag_template = """Answer the question based only on the following context:
{context} and give line number as well
Question: {question}
"""
after_rag_prompt = ChatPromptTemplate.from_template(after_rag_template)
after_rag_chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | after_rag_prompt
    | my_model
    | StrOutputParser()
)
results = after_rag_chain.invoke(user_question)
print(results)

第二次提问就是 RAG 的版本。模板现在有两个空位,还有一条清楚的指示:只能根据以下上下文回答,并给出行号。在链的开始,{"context": retriever, "question": RunnablePassthrough()} 以不同方式填这两个空位:问题送到检索器,检索器就把三句最相近、带有行号的句子贴进 {context};而 RunnablePassthrough() 把原问题原封不动复制到 {question}。之后同样是模板、模型、纯文字。答案现在来自检索到的句子——还会说明它依据哪一行。

完整程序

import os
import re
import requests
from bs4 import BeautifulSoup
from dotenv import load_dotenv
load_dotenv()
from langchain_core.documents import Document
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_openai import ChatOpenAI

DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
my_model = ChatOpenAI(
    api_key=DEEPSEEK_API_KEY,
    base_url="https://api.deepseek.com",
    model_name="deepseek-chat"
)

embeddings = HuggingFaceEmbeddings(
    model_name="BAAI/bge-m3"
)

url = "https://www.panadol.com/en-gb/adult-symptoms/headache/"
headers = {"User-Agent": "Mozilla/5.0 (compatible; SkillBot/1.0)"}
resp = requests.get(url, headers=headers, timeout=15)
resp.raise_for_status()
resp.encoding = resp.apparent_encoding
soup = BeautifulSoup(resp.text, "html.parser")
for tag in soup(["script", "style", "noscript", "nav", "footer"]):
    tag.decompose()
lines = [" ".join(tag.get_text(" ").split()) for tag in soup.find_all(["h1", "h2", "h3", "p", "li"])]
lines = [line for line in lines if line != ""]

sentences = []
for line in lines:
    for sentence in re.split(r"(?<=[.!?])\s+(?=[A-Z])", line):
        sentences.append(sentence)
lines = sentences

with open("panadol.txt", "w", encoding="utf-8") as outfile:
    for line in lines:
        outfile.write(line + "\n")

chunked_text_with_metadata = []
for line_num, line in enumerate(lines, start=1):
    if line.strip() == "":
        continue
    content = f"line number {line_num}\n{line.strip()}"
    chunked_text_with_metadata.append(
        Document(
            page_content=content,
            metadata={"source": url, "line": line_num},
        )
    )

vectorstore = InMemoryVectorStore.from_documents(chunked_text_with_metadata, embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})

print("Before RAG")
before_rag_template = "{topic}"
before_rag_prompt = ChatPromptTemplate.from_template(before_rag_template)
before_rag_chain = before_rag_prompt | my_model | StrOutputParser()
user_question = "How many people get headaches at least once a year?"
print(before_rag_chain.invoke({"topic": user_question}))

print("After RAG")
after_rag_template = """Answer the question based only on the following context:
{context} and give line number as well
Question: {question}
"""
after_rag_prompt = ChatPromptTemplate.from_template(after_rag_template)
after_rag_chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | after_rag_prompt
    | my_model
    | StrOutputParser()
)
results = after_rag_chain.invoke(user_question)
print(results)

在自己的电脑上执行

下载 zip 文件并解压到一个文件夹。zip 内含程序和 pyproject.toml 项目文件。请自行加入 .env 文件并放入密钥,如上文所示。然后在 Visual Studio Code 中打开文件夹,在终端逐行输入以下三行:

uv python pin 3.12.10
uv sync
uv run rag_sentence01.py

程序会下载网页、写出 panadol.txt、建立语义索引并打印两个答案。首次执行还要下载嵌入模型(约 2 GB),所以第一次需要联网一次;之后模型已储存在电脑上。一次真实执行看起来像这样:

Before RAG
Roughly **half of all adults worldwide** — about **50%** — report having a headache at least once in a given year. That figure comes from global estimates (e.g., WHO and the Global Burden of Disease studies), which put the 1-year prevalence of any headache disorder at around 50% of adults.

Some context on the breakdown:
- **Any headache disorder:** ~50% of adults per year
- **Migraine:** ~14–15% of adults per year
- **Tension-type headache:** ~30–40% of adults per year
- **Chronic daily headache:** ~3–4% of adults

So in absolute terms, with ~8 billion people globally and adults making up roughly 75% of the population, that's on the order of **2.5–3 billion people** experiencing at least one headache per year.
After RAG
Based on the provided context, recurring headaches affect approximately **half of adults worldwide** (line number 128). The context does not specifically state "at least once a year," but this is the closest relevant information available.

对照两个答案。Before RAG 自信而详细,还列出研究名称,但没有任何地方值得打开核对。After RAG 只说了检索到的句子里有的内容,还指向 panadol.txt 第 128 行——打开文件,那句话就在那里——甚至老实承认那些句子没有直接说“at least once a year”。这份诚实就是“根据事实”在发挥作用。你自己执行时用语和行号可能略有不同,因为模型每次都会重新写一段,而实时抓取的网页也可能被更新。

如果你还未设置 uv,请先从 如何安装 Visual Studio Code?开始,然后看 uv 笔记: Windows 或 Mac。

局限

延伸阅读