讓 AI 讀你的文件Answering from your documents
「請特休要提前幾天?」直接問 AI,它只能用猜的,因為它沒讀過你們的員工手冊。這一章介紹最常見的解法:讓 AI「開書考試」。這個做法有個專有名詞叫 RAG,但概念非常簡單。
Ask an AI "how far ahead must I request annual leave?" and it can only guess, because it has never read your employee handbook. This chapter covers the most common fix: an open-book exam. It has a technical name, RAG, but the idea is very simple.
讀完這章你會知道After this chapter you'll know
- 為什麼 AI 不知道你們公司的資料,以及「開書考試」怎麼解決這個問題。
- RAG 的三個步驟:搜尋、組合、回答。
- 簡單的關鍵字搜尋會在哪裡失敗。
- Why the AI doesn't know your company's data, and how an open-book exam solves that.
- The three steps of RAG: search, assemble, answer.
- Where simple keyword search fails.
開書考試An open-book exam
先幫 AI 翻到相關的那幾頁,再請它「只根據這幾頁」回答。
First turn to the relevant pages for the AI, then ask it to answer "using only these pages".
為什麼不把整本手冊都貼給它?如果手冊很短,其實可以。但公司文件常常有幾百份、幾千頁:全部塞進去太貴、太慢,也可能超過 context window(第 1 章)。而且不同的人能看的文件不一樣。所以實務上會先搜尋,只把相關的段落交給 AI。
Why not paste the whole handbook? If it's short, you can. But company documents often run to hundreds of files and thousands of pages: including everything is too expensive, too slow, and may exceed the context window (chapter B1). Different people may also be allowed to see different documents. So in practice you search first and give the AI only the relevant passages.
- 搜尋Search 從文件中找出和問題最相關的幾段。 Find the few passages most related to the question.
- 組合Assemble 把那幾段和問題組成一個 prompt,並要求「只能根據這些內容回答、標註出處、找不到就說不知道」。 Combine those passages and the question into a prompt that says: answer only from these, cite sources, say so if it's not there.
- 回答Answer 把 prompt 送給 AI(就是第 2 章做的事)。 Send the prompt to the AI (exactly what chapter B2 did).
動手試:員工手冊問答Try it: employee handbook Q&A
搜尋 → 組合 promptSearch → assemble the prompt
員工手冊全文(6 段)Full handbook (6 sections, in Chinese)
搜尋方法很陽春:把文字切成「兩個字一組」,看問題和哪一段有最多相同的片段。最後兩個問題是搜尋失敗的例子,等一下會解釋。
The search is very basic: split text into two-character pieces and see which section shares the most pieces with the question. The last two questions are search failures, explained below.
1. 找到的段落1. Passages found
2. 要送給 AI 的 prompt2. Prompt sent to the AI
搜尋失敗的兩種情況Two ways search fails
「筆電壞掉…」"My laptop (筆電) broke…"
手冊寫的是「筆記型電腦」,問題寫的是「筆電」。意思一樣,但字不一樣,關鍵字搜尋就找不到。
The handbook says 筆記型電腦 (notebook computer); the question says 筆電 (laptop). Same meaning, different characters, so keyword search misses it.
「公司有提供午餐嗎?」"Does the company provide lunch?"
手冊沒寫午餐,但「設備」那段剛好有「公司提供」四個字,就被找出來了。
The handbook says nothing about lunch, but the equipment section happens to contain "the company provides", so it gets matched.
第二種情況就是為什麼 prompt 要寫「找不到答案就說不知道」:就算搜尋給了不相關的段落,AI 也還有機會正確地說「手冊沒有寫到這件事」。第一種情況則需要更聰明的搜尋方法(能理解「筆電」和「筆記型電腦」是同一個東西),進階篇第 5 章會介紹。
The second case is why the prompt says "say so if the answer isn't there": even when search returns an irrelevant passage, the AI can still correctly answer "the handbook doesn't cover this". The first case needs a smarter search that understands 筆電 and 筆記型電腦 are the same thing, covered in core chapter 05.
程式碼The code
def pieces(text):
"""把文字切成「兩個字一組」的小片段,例如「請假」「假要」「要提」。"""
text = "".join(ch for ch in text if ch.strip() and ch not in ",。?!、:;「」")
return {text[i:i + 2] for i in range(len(text) - 1)}
def search(question, top_n=2):
"""計算問題和每一段有幾個相同的片段,取最多的前幾段。"""
q = pieces(question)
scored = []
for doc_id, text in HANDBOOK.items():
overlap = q & pieces(text)
if overlap:
scored.append((len(overlap), doc_id, sorted(overlap)))
scored.sort(reverse=True)
return [(doc_id, score, shared) for score, doc_id, shared in scored[:top_n]]
python -m labs.beginner.handbook_qa "病假超過幾天要附證明?"
練習:把 handbook_qa.py 印出的 prompt,用第 2 章的方式送給 Claude,看看它的回答有沒有標註 [H2]。再試試「公司有提供午餐嗎?」,看它會不會老實說手冊沒寫。
Exercise: send the prompt printed by handbook_qa.py to Claude as in chapter B2 and check that the answer cites [H2]. Then try "公司有提供午餐嗎?" (does the company provide lunch?) and see whether it honestly says the handbook doesn't cover it.