如何用檢索增強生成(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。

限制

延伸閱讀