由 AI 翻譯。經人工以英文驗證。 閱讀原文
教學套件
版次 2026.11.13 為本書目前的版次,會隨本書更新而變動。可自由使用與改編,但須標示本書出處。
課程大綱
閱讀時間僅計算本章的文字。學生也會在 Lab 或自己的電腦上執行範例。
每週一章:11 週
| 週 | 各章 | 閱讀 | 範例 |
|---|---|---|---|
| 1 | 1. 搜尋的完整流程 | 82 分鐘 | 12 |
| 2 | 2. 相關性的意義 | 85 分鐘 | 11 |
| 3 | 3. 衡量搜尋 | 76 分鐘 | 13 |
| 4 | 4. 人員與 LLM 的判斷 | 78 分鐘 | 11 |
| 5 | 5. 詞彙檢索 | 100 分鐘 | 14 |
| 6 | 6. 語意檢索 | 86 分鐘 | 17 |
| 7 | 7. 混合檢索 | 74 分鐘 | 14 |
| 8 | 8. 理解查詢 | 85 分鐘 | 12 |
| 9 | 9. Ranking | 98 分鐘 | 14 |
| 10 | 10. LLM 應用的檢索 | 97 分鐘 | 10 |
| 11 | 11. 測試變更 | 64 分鐘 | 13 |
5 週後
| 週 | 各章 | 閱讀 | 範例 |
|---|---|---|---|
| 1 | 1. 搜尋的完整流程 2. 相關性的意義 3. 衡量搜尋 | 243 分鐘 | 36 |
| 2 | 4. 人員與 LLM 的判斷 5. 詞彙檢索 | 178 分鐘 | 25 |
| 3 | 6. 語意檢索 7. 混合檢索 | 160 分鐘 | 31 |
| 4 | 8. 理解查詢 9. Ranking | 183 分鐘 | 26 |
| 5 | 10. LLM 應用的檢索 11. 測試變更 | 161 分鐘 | 23 |
學習目標與大綱
每章的學習目標即為其第一句。每份大綱依循該章各節,列出該節定義的名詞、執行範例前要求的預測,以及每節結尾所歸納的規則。 其關鍵術語與要提問的預測內容,依題庫所列,採用英文版次的內容。
1. 搜尋的完整流程
閱讀約需 82 分鐘,含 12 個範例。
讀完本章,你可以為商品目錄建立一條粗略但完整的搜尋流程,從購物者的查詢一路到排好順序的商品清單,並且能用一個數字衡量流程的某項改動是否有幫助。
課程大綱:10 節
-
Load the store's data
- 關鍵詞 judgment, grade
- 執行
load-the-store.py - 實用原則 Split your queries into train and test before you change anything. Read test results to understand a ranker, but choose and tune every change on train queries, and report on test. Treat unjudged products as unknown. If you have no judgments yet, collect some before you change the ranking.
-
以 BM25 對商品排序
- 執行
first-bm25-search.py - 實用原則 以函式庫預設值的 BM25 作為基準線起步。它不需要訓練資料,也不需要模型,而且出錯時,你能看出是哪些詞彙造成的。在你能於訓練查詢上量測到變化之前,不要更動它的參數。當購物者與商品對同一事物使用不同的詞彙時,再加入語意檢索。
- 執行
-
對照判斷檢查結果
- 執行
judge-first-results.py - 實用原則 在計算任何指標之前,先閱讀五到十個查詢的前幾名結果,並對照其判斷。尋找其中的模式:配件、漏掉的修飾詞、錯誤的尺寸。當前幾名中有數個結果未經判斷時,請先讓它們接受判斷,再相信該查詢的分數。
- 執行
-
Score one ranking with nDCG@10
- 執行
ndcg-for-one-query.py - 實用原則 Use nDCG@10 as your headline metric when your judgments are graded and the first page of results matters most, as it does in a store's search. When you have only yes-or-no labels, or when shoppers look at one result only, another metric fits better. Chapter 3 builds the alternatives and says when to choose each.
- 執行
-
Score every test query
- 執行
bm25-test-scores.py - 執行
figure-bm25-test-query-scores.py - 實用原則 Report the mean nDCG@10 over the test queries with mean judged@10 beside it, and read the lowest-scoring queries every time you score a run. When one ranker's judged@10 is lower than another's, find out why before you compare their nDCG: the gap may come from missing judgments, not worse results.
- 執行
-
Search by meaning with embeddings
- 關鍵詞 embedding
- 執行
first-semantic-search.py - 執行
compare-lexical-semantic.py - 實用原則 Add semantic retrieval when shoppers describe what they want in their own words. Don't assume lexical retrieval wins on exact terms such as sizes, brands and model numbers: before you rely on it there, count which retriever wins the train queries that contain them. On this data, semantic retrieval alone scores highest so far, so it is the ranker to beat.
-
以倒數排名融合法融合兩個排名
- 執行
hybrid-rrf.py - 實用原則 在融合之前,先單獨量測每個檢索器。以 k = 60 的 RRF 作為未經調校的預設做法,並且只有在融合後的排名於保留查詢上勝過最佳的單一檢索器時,才保留它。當其中一個檢索器明顯較弱時,可嘗試為排名加權,或在訓練查詢上調校 k,而不是接受同等的投票權。第 7 章在商店的資料上嘗試了這兩種做法,而它在測試查詢上回報的調校後混合結果,沒有任何一個勝過語意檢索(在測試查詢上回報調校後的混合結果)。
- 執行
-
以交叉編碼器重排前 50 名
- 執行
cross-encoder-rerank.py - 執行
rerank-one-query.py - 實用原則 重排的對象是一份候選清單(這裡是 50 項商品),因為成本會隨每組配對而增加。只有在保留查詢上優於最佳的單獨檢索時,才採用重排器。這個重排器沒有勝過混合結果,也沒有勝過語意檢索,因此到目前為止,商店最佳的管線是單獨使用語意檢索。在放棄重排之前,可以嘗試不同的深度,或以自己的評判結果訓練的排序器。在第 9 章的測試查詢上,不同的深度與 LambdaMART 都沒有勝過語意檢索(依預算設定重排深度、回報 LambdaMART 在測試查詢上的結果)。
- 執行
-
What goes wrong in production
- 執行
tokenizer-drops-terms.py - 實用原則 Check the tokens your queries become, track judged@10, and record the model version next to every vector you store.
- 執行
- 本章的決定
2. 相關性的意義
閱讀約需 85 分鐘,含 11 個範例。
讀完本章後,你能夠批判性地閱讀一組相關性判斷:說明每個等級對商品提出了什麼主張,找出這些判斷的來源與缺漏之處,並決定該在多大程度上信任由它們算出的分數。
課程大綱:11 節
-
Judge relevance against the shopper's need
- 執行
word-match-versus-judgment.py - 實用原則 Treat a judgment as a person's verdict about a need, not a property of the product. Before you blame a ranker for missing an exact product, check whether any field the ranker reads says what the judges saw, and look at the fields it doesn't read, such as attributes. When your judges see more than your ranker does, give the ranker the same evidence where you can, or accept that some exact products will rank low for reasons no change to the ranking can fix.
- 執行
-
預期評審者會在部分配對上意見不一
- 執行
conflicting-judgments.py - 實用原則 每個配對只保留一個等級,並寫下選定該等級的規則。我們建議像樣本那樣採用較低的等級:這樣的誤差不利於替該商品排序的排序器,而不是偏袒它。當評審者的意見相差兩個等級,也就是完全相符對上不相關時,應與他們一起重讀評判準則,因為他們對定義的理解並不相同。
- 執行
-
Read intent as a product and its modifiers
- 關鍵詞 intent, modifiers
- 執行
predicted-class-check.py - 實用原則 Read each query as an entity plus modifiers, and check that your ranker gets the entity right before you tune it for modifiers. Use
query_classas a hint, for a feature or a filter you check against the judgments, and never as a label. When intent is ambiguous, as withcooktop, judge against the guidelines' reading and write that reading down.
-
以分級評定相關性,而非只分是或否
- 關鍵詞 gain
- 執行
binary-labels-hide-partial.py - 實用原則 我們建議商品搜尋採用分級評定,讓指標能對差一點符合的結果給予部分分數,而不會把它誤當成相符的結果。即使同時報告二元指標,也應收集分級標記:分級可以隨時合併為是或否,反過來則不行。當差一點符合的結果對使用者毫無價值時,例如查詢的是型號,而且只有該型號才行,就使用二元標記。
-
刻意選擇增益值
- 執行
gain-choice-changes-ndcg.py - 實用原則 商品搜尋預設採用線性增益值,也就是等級 2、1 與 0。若對你的購物者而言,差一點就符合的商品價值很低,則選擇較低的部分相關增益值。無論如何選擇,都應在比較排序器之前確定,並在每個分數旁一併報告,且絕不要比較以不同增益值計算的 nDCG。
- 執行
-
Read the label mix of each query
- 執行
count-labels-per-query.py - 執行
figure-label-mix-per-query.py - 實用原則 Print the label mix per query before you average any metric, and look at both ends. Read a low score on a query with few exact products as a different problem from a low score on a broad query. Keep per-query scores next to every mean; chapter 3 shows what averages hide.
- 執行
-
將點擊視為有偏誤的證據
- 執行
cascade-clicks.py - 實用原則 離線評估應以判斷而非點擊作為參考標準。將點擊當作相對證據使用,即被點擊的商品優於排在其上方而被略過的商品,並且絕不要把沒有點擊解讀為商品不相關。如果以點擊進行訓練,請對照判斷檢查結果。
- 執行
-
了解判斷從何而來
- 執行
trundle-daybeds-unjudged.py - 實用原則 在信任某項比較之前,先查明你的候選池從何而來。當你評估的排序器與建立候選池的系統不相似時,預期它會呈現出無人判斷過的商品,並計畫對這些商品進行判斷。
- 執行
-
Count unjudged products before you compare scores
- 關鍵詞 Judged@k
- 執行
unjudged-in-bm25-top-10.py - 執行
condensed-ndcg.py - 實用原則 Report judged@10 next to every nDCG@10, and compute condensed scores as a diagnostic, never as the headline. When two rankers' judged@10 differ, have the unjudged products in their top 10s judged before you compare them. That is the dependable cure, and on this sample it is a small job: 22 judgments for BM25's test run. On a full catalog, expect more.
-
正式環境中會出什麼問題
- 實用原則 為判斷標註日期,在正式環境中追蹤 judged@10,並編列重新判斷的預算。
- 本章做出的決定
3. 衡量搜尋
閱讀約需 76 分鐘,含 13 個範例。
讀完本章後,你將能從頭計算標準的排序指標,並以 scikit-learn 檢驗結果,在比較排序器之前選定一項指標,也能判斷兩個排序器在你的測試查詢上的差距是已經確立,還是可能出於偶然。
課程大綱:13 節
-
Count the relevant products in the top k
- 執行
precision-recall-by-hand.py - 實用原則 Use precision@10 when you care how much of the first page is right, and read it next to each query's ceiling. Don't make recall@10 a headline in product search, where a query can have more than a hundred relevant products, as
outdoor light fixtureshas. We recommend recall at a deep cutoff, such as the top 100, for one job only: checking that retrieval passes enough relevant products to a reranker. Even then, read it as an overestimate.
- 執行
-
Score the first relevant result with reciprocal rank
- 執行
reciprocal-rank-by-hand.py - 實用原則 Report MRR as the headline only when shoppers need one right product, as for a model number or a named product such as
kohler whitehaven farmhouse kitchen sink. For queries where shoppers browse a page of options, such aswooden chair outdoor, use nDCG@10 as the headline and report MRR beside it.
- 執行
-
由折減增益建立 nDCG
- 執行
ndcg-from-scratch.py - 實用原則 使用線性增益、log2(i + 1) 折減的形式,截斷點取 10,理想排名由所有已評判商品建立,並且在引用數字時註明這個變體。nDCG 只應在以相同評判評分的結果之間比較:新的評判會改變理想排名,進而改變每一個分數。
- 執行
-
與 scikit-learn 的
ndcg_score相符- 執行
ndcg-matches-scikit-learn.py - 實用原則 報告你自己計算的 nDCG,並以
ndcg_score作為獨立核對,且每個查詢的列中都要包含所有已評判的產品,分數取自名次。比較兩者之前,先檢查每次執行是否至少包含 k 個產品。若改為傳入排序器的原始分數,請先以固定順序打破同分,例如依product_id,否則兩者在分數相同之處會不一致。
- 執行
-
比較之前先選定指標
- 執行
metric-choice-on-train.py - 實用原則 在比較排序器之前,先選定一個主要指標、其增益方式與截斷位置,並記錄下來。當評判為分級式、且購物者是瀏覽整頁時,我們建議使用線性增益的 nDCG@10。當完全符合的價值遠高於近似符合時(例如必須能裝得上的備用零件),請改用指數增益;當大多數查詢只有一個正確答案時,則改用 MRR。請在主要指標之外一併報告其他指標,而且在看到主要指標偏向哪個排序器之後,絕不更改它。
- 執行
-
要看逐查詢的差異,而不只看平均值
- 執行
per-query-hybrid-versus-bm25.py - 實用原則 每次比較時,都要印出差異的平均值與中位數、勝出、落敗與持平的數量,以及損失最大的查詢,然後閱讀這些查詢。在把功勞歸於排序器之前,先檢查變動最大的查詢的 judged@10。報告平均值的絕對差異,而不要報告百分比。
- 執行
-
以排列檢定檢驗差距
- 執行
permutation-test-by-hand.py - 執行
figure-permutation-null.py - 實用原則 對於每個你打算據以行動的差距,都要對各查詢的差值進行成對排列檢定。本書以這項檢定作決定,如其規則所述。若 p 值高於門檻,應解讀為「沒有證據顯示存在差異」,而非「沒有差異」:在查詢這麼少的情況下,檢定可能漏掉真實的差距,而 McKechnie 等人主張應同時衡量這類漏判與偽陽性(McKechnie et al., 2025)。切勿解釋檢定無法與偶然區分的差距。
- 執行
-
以成對 t 檢定取得 p 值
- 執行
paired-t-test-by-hand.py - 實用原則 在排列檢定之外,一併報告成對 t 檢定
scipy.stats.ttest_rel,作為不需重新抽樣的核對。若兩者落在門檻的不同側,請明確說明,並以排列檢定為準。不要使用 Wilcoxon 檢定或符號檢定來比較平均分數(Wilcoxon 符號秩檢定與符號檢定)。
- 執行
-
報告差距有多大
- 關鍵詞 confidence interval, percentile interval
- 執行
bootstrap-interval-by-hand.py - 執行
interval-coverage-check.py - 實用原則 每項比較都應給出其平均差異,並附上自助法區間、效果量與 p 值。如果區間包含 0,該比較就沒有顯示出改善。如果區間排除 0,但低於變更成本所要求的最小差距,這個差異可能是真的,卻仍不值得上線。使用區間來描述差距的大小,而不是用來判斷差距是否存在。
-
事先決定什麼才算成立
- 關鍵詞 Bonferroni correction
- 實用原則 在對測試查詢進行任何評分之前,先寫下計畫比較的清單,並保持簡短,因為每多一項比較,其他比較的門檻就更高。當需要同時比較許多排序器時,Bonferroni 校正會變得過於嚴格。Sakai 建議先做變異數分析,檢驗各排序器之間是否存在任何差異,再做隨機化的 Tukey HSD 檢定,該檢定比較每一對排序器,同時使任何偽陽性出現的機率維持在該顯著水準以內(Sakai, 2014)。
-
在評估框架中比較各次執行
- 執行
compare-chapter-one-rankers.py - 實用原則 每一項改動都要報告各次執行的平均 nDCG@10 與 judged@10,再附上與此處相同的表格,每項比較各占一欄,並標明哪些是探索性比較。唯有相對於要擊敗的排序器已經成立,改動才能上線。
- 執行
-
正式環境中會出什麼問題
- 執行
split-changes-the-verdict.py - 實用原則 記錄你在測試查詢上執行的每一次比較;對於接近門檻或屬探索性的結果,視為需要用新查詢驗證的問題;以固定順序處理分數平手;並在每個 nDCG@10 旁一併報告 judged@10。
- 執行
- 本章做出的決定
4. 人員與 LLM 的判斷
閱讀約需 78 分鐘,含 11 個範例。
讀完本章後,你將能撰寫評判準則,用 kappa 與加權 kappa 衡量兩位評判者的一致程度,從混淆矩陣中解讀 LLM 評判者的錯誤,並判斷 LLM 的判斷在哪些情況下可以取代人員的判斷,在哪些情況下不行。
課程大綱:13 節
-
撰寫評判準則
- 執行
print-judging-guidelines.py - 實用原則 將準則寫成定義、程序、為每個評分附上理由的演練範例,以及針對評判者曾有分歧的每種案例所訂的規則。記錄分歧,當某一類分歧反覆出現時就修訂準則。給 LLM 評判者的準則應與給人的相同,並加上一條針對它看不到的內容的規則,然後衡量它實際的表現,因為你寫下的規則,並不等於評判者會遵循的規則。
- 執行
-
以 kappa 衡量超越機率的一致性
- 關鍵詞 observed agreement
- 執行
kappa-by-hand.py - 實用原則 報告 kappa 時,要並列觀察一致率,不可只報告觀察一致率。請在人工針對訓練查詢評判過的配對上衡量,並將所用的任何門檻標示為經驗法則。若評審超過兩位,或有缺漏的標籤,請使用 Krippendorff's alpha,它比較觀察到的分歧與機率分歧,並能處理任意數量的觀察者與不完整的資料(Krippendorff, 2011)。
-
以加權 kappa 給予接近正確的判斷部分分數
- 執行
weighted-kappa-by-hand.py - 實用原則 對於有順序的等級,在一般 kappa 之外也報告線性加權 kappa,並另外計算嚴重錯誤的數量。每個不一致的權重,應依其造成的增益差距而定。採用本書的線性增益時,就是線性權重。若採用 0、1、3 的指數增益(選擇指標),差距為 1、2、3,這是 scikit-learn 的兩個選項都無法提供的,因此要像範例建立線性矩陣那樣,自行建立該矩陣。
- 執行
-
對照標籤組成來解讀 kappa
- 執行
kappa-under-two-label-mixes.py - 實用原則 每次回報 kappa 時,都要一併回報標籤組成,且不要比較來自不同組成樣本的 kappa,無論是自己的還是已發表的。我們建議以使用評審的方式來抽取一致性樣本:若它將為排序器的前 10 名評分,就從訓練查詢的前 10 名中抽取配對。
- 執行
-
解讀 LLM 錯誤的方向
- 執行
llm-confusion-matrix.py - 實用原則 先看混淆矩陣再看 kappa,且要使用以你將來使用評審的方式抽取的配對,並指出該評估池的方向與最大誤差。不要把一個評估池的方向套用到另一個,也不要據此預測排序器的 nDCG@10。比較排序器時,應在相同的評等下相互比較,切勿拿 LLM 評等下的分數與人工評等下的分數相比。
- 執行
-
比較 LLM 看到的內容與評審看到的內容
- 執行
llm-grades-follow-written-words.py - 實用原則 請將評審所依據、且可以轉成文字的每個欄位都提供給 LLM 評審,例如屬性與完整描述,並寫明它看不到的內容。在將某項分歧算作 LLM 的錯誤之前,先檢查證據是否在它所看到的內容之中。如果評審的證據是視覺性的,請衡量 LLM 是否會給 exact 產品較低的評分,如同本例在兩個資料池中所顯示的;或者,若你的模型接受圖片,就直接提供圖片給它。
- 執行
-
衡量提示詞對標籤的影響程度
- 執行
prompt-disagreement.py - 實用原則 將提示詞視為評審的一部分。逐字記錄每個提示詞,連同模型版本、設定與回答結構,就像這份錄製資料在其清單與腳本中所做的一樣。更換提示詞,就等於換了一個評審,必須重新衡量其一致性。至少執行兩個提示詞,只有在兩者都得出相同結論時,才信任由 LLM 評分得到的結論。選擇某個提示詞而捨棄另一個時,應以訓練配對為依據,而且只在兩者差異的區間不含 0 時才這麼做。
- 執行
-
Fill unjudged pairs with LLM grades
- 執行
fill-unjudged-pairs.py - 實用原則 Fill the unjudged pairs in your rankers' top 10s with LLM grades as a diagnostic, under every prompt you run, and report the filled score beside the human-only one with the number of pairs filled. If filling changes the order of two rankers or a verdict, have people judge those pairs. Never let a filled grade replace a human one.
- 執行
-
檢查 LLM 評分是否以相同方式排序各排序器
- 執行
kendall-tau-four-rankers.py - 實用原則 回報 tau 時,一定要附上被交換的配對清單,不要單獨呈現。排序器只有寥寥數個時,交換一對就會使 tau 變動 0.333,不要拿它與 0.9 相比。接著再以每組評分下的預先規劃比較,檢驗被交換的配對之間是否確有差異。
- 執行
-
以 LLM 評分重跑規劃比較
- 執行
planned-comparisons-llm-grades.py - 實用原則 絕不只憑 LLM 評分得出的判定就上線變更。以 LLM 評分來決定哪些候選方案值得評判,並以人工評分做出決定。當兩種提示詞給出不同的判定時,LLM 無法裁定這項比較,必須由人來裁定。
- 執行
-
以人工樣本校正 LLM 分數
- 關鍵詞 Spread
- 執行
correct-llm-scores-with-humans.py - 實用原則 凡是報告以 LLM 評分得出的分數,都應由人員判斷隨機抽出的一部分查詢,並報告校正後的估計。在該樣本上,檢查 LLM 的各查詢差距與人工差距的相關程度。若相關性偏弱,就把預算用在判斷更多查詢上。
-
正式環境中會出什麼問題
- 實用原則 固定評審、每次評估都保留人工樣本、任何項目變動時重新執行一致性檢查,並且絕不使用調校排序器時所用的評審來為它評分。
- 本章做出的決定
5. 詞彙檢索
閱讀約需 100 分鐘,含 14 個範例。
讀完本章後,你能從頭建立詞彙搜尋:將商品文字轉成詞項、為詞項建立索引,並以 TF-IDF 與 BM25 為商品評分,且分數與 bm25s 函式庫逐筆一致。
課程大綱:12 節
-
將商品文字切分為詞項
- 執行
tokenize-product-text.py - 實用原則 在調整任何評分之前,先印出查詢與商品轉換後的詞項,並讓兩者通過同一個分詞器。如同商店的做法,從 bm25s 的預設值開始。當購物者輸入尺寸、數量或型號時,使用保留單一數字、並將數字與其單位分開的詞元模式,例如上述模式。當他們輸入否定詞時,關閉停用詞表。這兩者都是對搜尋的更動,因此在保留之前,先在訓練查詢上衡量,並以測試查詢加以檢驗。
- 執行
-
建立反向索引
- 執行
inverted-index-by-hand.py - 實用原則 當查詢的結果看起來不對時,先在索引中查詢它的詞項,再決定是否更動評分方式。沒有倒排清單的詞項,例如
7qt,無論評分器如何運作都幫不上任何商品,所以應該修正分詞器或查詢。對於購物者輸入的查詢,應比對包含任一詞項的商品,而非要求全部詞項,因為只要有一個詞無法匹配,結果就會變成空白。正式提供服務時請使用函式庫的索引,並保留像這樣的小型索引,用來檢查某個詞項匹配到哪些商品。
- 執行
-
以 TF-IDF 為詞項加權
- 執行
tf-idf-by-hand.py - 實用原則 在任何詞彙式評分器中,都以 IDF 為詞項加權,因為比對到罕見詞項是較好的證據。不要以原始的 TF-IDF 對商品排序:應限制重複詞項所能增加的分數,並修正過長文字的影響,這正是 BM25 的做法。TF-IDF 則保留用於解釋排序結果,因為可以直接讀出每個詞項的 tf 與 idf。
- 執行
-
Rebuild BM25 by hand to match bm25s
- 執行
figure-bm25-term-saturation.py - 執行
bm25-by-hand.py - 實用原則 Serve BM25 from a library such as bm25s, and keep a small version you wrote yourself to check it, because yours shows where every part of a score comes from. Compare two implementations by their scores first, then by their rankings. Before you compare rankings, break ties the same way in both, by product ID as the book's BM25 does, or tied products alone can make two correct implementations rank differently. When two BM25 systems rank differently, compare their tokenizers before their formulas. The study of variants held tokenization and stop words fixed, and notes that more mundane settings, such as the choice of stop words, often make larger differences than the formula (Kamphuis et al.).
- 執行
-
Stem terms so word forms match
- 執行
stemmer-merges-word-forms.py - 執行
bm25s-with-a-stemmer.py - 實用原則 Stem English product text and queries with the Snowball English stemmer, at indexing time and at query time alike: in bm25s, pass
stemmer=tobm25s.tokenizeon both sides. Read the products that a stem adds, as thefloatscounts do, before you trust a gain. When a stem merges words that your shoppers keep apart, such asmoldingandmold, keep a list of words you don't stem. For text in another language, use that language's stemmer.
- 執行
-
在測試查詢上檢驗詞幹還原
- 執行
measure-stemming.py - 實用原則 以訓練查詢選擇文字處理的變更,且只有在其規劃比較成立時才保留。當排列檢定與 t 檢定意見不一致時,請檢視各查詢中最大的差異,並在平均值旁一併報告排除最大增益後的增益。詞幹還原的增益成立,因此建置詞彙式搜尋時應採用詞幹還原。本書共用的 BM25 仍不做詞幹還原,使後續各章的結果保持可比較(見決策)。請將增益的大小視為不穩固的數字。
- 執行
-
Tune BM25's parameters on train queries
- 先詢問 Before you run it, predict: at the defaults, stemmed BM25 scores 0.774 on the test queries. How much will the best setting on train add?
- 執行
sweep-k1-and-b.py - 實用原則 Start from the library's defaults. Tune
k1andbon train queries when you change what you index, such as fields or stemming, and keep a tuned setting only if its planned comparison is established. The store's tuned setting isn't, so the store keepsk1=1.5andb=0.75.
-
讓商品名稱的權重高於商品描述
- 執行
fields-scored-apart.py - 執行
repeat-the-name-field.py - 實用原則 不要把不同欄位的 BM25 分數相加。要為欄位加權,應在飽和之前先合併詞頻,可使用 BM25F 或重複該欄位。在訓練查詢上調整權重,只有當預先規劃的比較成立時才保留它。就這間商店而言,仍維持使用一個合併的欄位。
- 執行
-
Index product attributes as a field
- 執行
add-attributes-field.py - 實用原則 Keep structured attributes, and read the queries they help, but don't add them to the BM25 text on the strength of a few queries. Index the values without the names, so that a query word such as
sizedoesn't match every product with a size, and keep the field only if its planned comparison is established. For the store, the attributes stay out of the BM25 text. When a modifier names an attribute value exactly, such as a color, a filter or a ranking feature can use it without adding its words to every product's text.
- 執行
-
Match phrases with term positions
- 執行
phrase-positions-by-hand.py - 執行
phrase-bonus-train-and-test.py - 實用原則 Don't add a phrase bonus to every query by default, and keep one only if its planned comparison is established. Build positions when shoppers quote phrases, or when word order changes what they want, as between
table lampandlamp table. Prefer a bonus that you measure on train queries to a filter, because a filter removes products like the hardware kit.
- 執行
-
What goes wrong in production
- 實用原則 Keep a set of probe queries for negations, sizes and codes, and read their results after every change to text processing. Store the tokenizer's and stemmer's settings with the index they built, log the terms that each query becomes, and watch for queries whose terms have no postings.
- Decisions this chapter made
6. 語意檢索
閱讀約需 86 分鐘,含 17 個範例。
讀完本章後,你可以根據儲存的嵌入向量,為商品目錄建立語意檢索:以餘弦相似度比較向量,用 NumPy 精確找出查詢的最近鄰商品,在商品目錄大到無法負擔精確搜尋時改用近似索引,並衡量該索引漏掉了什麼,還能找出嵌入向量出錯的查詢。
課程大綱:13 節
-
Turn text into embeddings
- 執行
inspect-recorded-embeddings.py - 實用原則 Embed products ahead of time and queries at search time, with the same model at the same revision, and follow the model card on prompts: here, a prefix on queries only. Record the model, revision and prompts next to the vectors, as each
manifest.jsoninexamples/data/recorded/does. Check that your longest product fits the model's input limit before you trust its embedding.
- 執行
-
Compare embeddings with cosine similarity
- 關鍵詞 Cosine similarity
- 執行
cosine-by-hand.py - 實用原則 Normalize every vector once, when you store it, and rank by dot product. Normalize again after anything that changes a vector, such as shortening it or averaging several. If a model's documentation says it was trained for the dot product of unnormalized vectors, follow the documentation instead, and never mix the two in one index.
-
Find the exact nearest neighbours with NumPy
- 執行
exact-search-numpy.py - 執行
exact-search-memory.py - 實用原則 We recommend exact search until it no longer fits in memory or meets your latency budget, measured with your own catalog on your own hardware. It has no settings to tune and never misses a neighbour, so it is also the ground truth for any approximate index. Store the normalized vectors as one 32-bit matrix, take the top k with
argpartition, and sort only those.
- 執行
-
以 HNSW 用召回率換取速度
- 執行
hnsw-efsearch-sweep.py - 實用原則 只有在精確搜尋太慢或太大時,才使用 ANN 索引。在訓練查詢上,依其相對於精確搜尋的召回率調校,並使用管線所用的 k,而且要依最差的查詢而非平均值來選擇。我們建議選擇使每個查詢都至少保有其真正前 10 名中 9 項的最小
efSearch,並搭配M = 32。本章只調校efSearch;M與efConstruction會改變圖本身,在大型商品目錄上值得進行同樣的掃描。Aumüller et al. (2018) 發現,差異很大的各種 ANN 方法能達到相當的品質與效能取捨,所以應在自己的資料上衡量,而不是憑名氣。
- 執行
-
在測試查詢上檢查索引
- 執行
hnsw-test-recall.py - 執行
hnsw-index-memory.py - 實用原則 以相對於精確搜尋的召回率,在保留的查詢上檢查調整後的索引,並且先檢視那些完全失去前 k 名結果的查詢,再看平均分數。若 nDCG@10 的下降只建立在少數查詢上,在成對檢定證實之前,都應視為未經證明。編列記憶體預算時,除了向量,也要計入連結。
- 執行
- Check queries that contain digits
-
預期商品代碼的權重很小
- 執行
code-probes.py - 實用原則 列出紀錄中含有尺寸、數量或代碼的查詢,並在信任任一種排序器之前,先用這兩種排序器分別比對這些查詢。代碼重要時,以詞彙比對來匹配。精確的尺寸重要時,則對照商品欄位進行比對,例如
attributes中的寬度;或是在分詞器保留數字、且單位寫法一致之後,改以詞彙比對(第 5 章、第 8 章)。查詢的其餘部分則交給語意檢索。第 7 章會測試如何結合兩者。
- 執行
-
以否定測試嵌入
- 執行
negation-probes.py - 實用原則 不要依賴第一階段的檢索器(無論是詞彙式或語意式)來處理否定。當購物者要排除目錄中已儲存的屬性,例如形狀、材質或扶手時,應偵測該排除條件,並依該屬性過濾。否則就進行重排,因為在 van den Elsen et al. 的重現研究中,交叉編碼器與 LLM 重排器處理否定的表現好得多。第 9 章以交叉編碼器與 LLM 對商店的語意檢索進行重排,而其中沒有任何重排器在商店的已判斷查詢上勝過語意檢索,不過這些查詢無法測試否定。請保留像這樣的探測,在每次更換模型或加入重排器時重新執行,作為檢查。
- 執行
- 選擇嵌入模型
- 將基準測試當作候選清單來讀
-
截短維度,然後檢驗損失
- 執行
truncate-embeddings.py - 執行
truncation-paired-test.py - 實用原則 以授權、語言、大小與領域篩選候選模型,並把基準測試排名視為參考線索。在模型之間,或在向量大小之間做選擇時,要在自己的訓練查詢上以成對檢定來決定,而且除了 p 值之外也要看區間。只對為此訓練的模型截短向量,截短後要正規化,而且只在記憶體或搜尋時間有需要時才這麼做。
- 執行
-
正式環境中會出什麼問題
- 執行
similarity-scale.py - 執行
figure-similarity-by-grade.py - 執行
spelling-probes.py - 實用原則 不要以固定的相似度進行過濾。為每個向量記錄模型、修訂版本與提示詞,並在嵌入查詢的地方檢查這些資訊。隨著商品目錄變動,重建索引並重新測量;若記錄顯示有錯字,就在嵌入查詢之前先修正拼字,並使用會檢查每個詞的相鄰詞的校正器(第 8 章)。
- 執行
- 本章做出的決定
7. 混合檢索
閱讀約需 74 分鐘,含 14 個範例。
讀完本章後,你將能把詞彙排序與語意排序合併為單一排序,做法是加總正規化後的分數,或使用倒數排名融合(reciprocal rank fusion);你也能在訓練查詢上調整兩種排序各自的比重,並在測試查詢上以配對檢定判斷,這個組合是否勝過其兩個輸入中較好的一個。
課程大綱:12 節
-
Find the queries each retriever wins
- 關鍵詞 fusion
- 執行
bm25-semantic-wins-by-query.py - 執行
overlap-and-union-recall.py - 執行
two-queries-two-winners.py - 實用原則 Before you fuse, count per query where each retriever wins on your train queries, and check how many relevant products the union of their lists holds that the better one misses. Consider fusing when each wins a real share of queries and the union holds more relevant products than either list. That is necessary, not sufficient: next, check each retriever alone, and test the tuned blend on held-out queries before you adopt it.
-
融合之前先檢查每個檢索器
- 執行
fusion-gain-by-winning-retriever.py - 實用原則 融合之前,先在訓練查詢上單獨檢查每個檢索器,並使用 judged@10 與成對檢定。當其中一個較弱且勝出的查詢較少時,不要給它相等的票數:改為加權並調整權重,或將它排除。相等的票數適用於品質大致相同的兩個檢索器。
- 執行
-
相加之前先正規化分數
- 執行
raw-scores-versus-min-max.py - 執行
figure-score-scales.py - 實用原則 絕不要以猜測的權重將原始的 BM25 分數與餘弦分數相加。我們建議以逐查詢 min-max 作為預設做法,因為它不需要事先了解任一種評分函數。若你知道各分數可能的最小值,並希望下限不隨候選商品而變動,則可使用理論 min-max。無論採用哪一種,都應在訓練查詢上調校 α。
- 執行
-
以訓練查詢檢驗倒數排名融合的設定
- 執行
rrf-k-on-train.py - 實用原則 預設保留 k = 60,並融合至少 100 名深的清單,因為更深的清單只會增加候選項目。只有在訓練集上的最佳 k 勝過 k = 60,且差距大到排列檢定能將其與機率波動區分開來(p 低於 0.05)時,才改用其他值。當其中一個檢索器是較強的一方時,如商店的情況,該調整的設定不是 k,而是 RRF 的均等投票。請為各排名賦予權重並加以調校。
- 執行
-
以訓練查詢調校混合的權重
- 執行
weighted-rrf-one-product.py - 執行
tune-blend-weights-on-train.py - 執行
figure-blend-weight-curve.py - 實用原則 以粗略的網格在訓練查詢上調校一個權重,並將正規化維持在 min-max,除非另一種正規化勝出的幅度大到訓練查詢上的檢定能將其與偶然區分,即 p 低於 0.05。將訓練查詢上相對於最佳單一檢索器的增益視為偏樂觀,並預期它在測試查詢上會縮小,這是適用於任何從多個選項中挑出最佳者之設定的經驗法則。在信任此設定之前,先檢查它是否穩定,再以測試查詢報告結果。
- 執行
-
確認調校後的權重是否穩定
- 執行
blend-weight-bootstrap.py - 執行
tuning-with-ties-reversed.py - 實用原則 對訓練查詢做自助重抽,並在每個樣本上調校;然後再以另一種不帶任何資訊的順序排列並列項,重新調校一次。如果選出的權重分散在整個網格上,就改用較粗的網格或更多訓練查詢,或者選擇較簡單的設定。如果光是並列項的順序就使它改變,就在報告該權重的每個地方說明這一點。如果它在兩種檢驗下都保持穩定,就帶它到測試查詢上。
- 執行
-
在測試查詢上報告調校後的混合方法
- 先詢問 Predict first: on the test queries, will the weighted sum at α = 0.7 and weighted RRF at α = 0.6 score above semantic retrieval, level with it, or below it?
- 執行
tuned-blends-on-test.py - 實用原則 只有在留存查詢上勝過最佳單一檢索器,且排列檢定 p 值低於依事先規劃的比較次數校正後的門檻時,才採用混合方法。若未達標,就保留較簡單的系統。這使語意檢索單獨成為商店中有待超越的排序器。請保留調校後的權重與程式碼,並在檢索器或商品目錄變動時重新測試這些混合方法。
-
以 SPLADE 學習稀疏詞項權重
- 執行
exact-products-missing-query-words.py - 實用原則 當 BM25 的漏失多半是詞彙不一致,而你又想保留反向索引時,可以考慮學習式稀疏檢索。請先檢查模型的授權,並像對待任何檢索器一樣加以衡量:單獨評估、逐查詢檢視,先在訓練查詢上,再在測試查詢上評估。
- 執行
-
以 ColBERT 逐詞元比對
- 實用原則 當單向量檢索遺漏了取決於單一詞語的細節,而且你負擔得起每個詞元一個向量的成本時,可以考慮延遲互動。對於規模與商店相當的商品目錄,我們建議先在候選短名單上測試重排序器(第 9 章):它在查詢時讀取相同的細節,也不需要新的索引,不過第 9 章的重排序器都尚未顯示出相對於商店語意檢索的確切增益。
-
判斷混合檢索何時值得採用
- 實用原則 當兩個檢索器的品質相近、嵌入模型用在訓練領域之外,或是許多查詢依賴嵌入所漏掉的精確詞彙時,我們建議嘗試混合檢索。先從加權 RRF 開始:它只需要名次,而且在商店的訓練查詢上,排列檢定無法區分它與加權和。當兩個引擎都能為每個候選項目提供分數時,再改用正規化分數的加權和:有兩項研究發現它在其資料上領先 RRF(Bruch et al., 2023;Abdallah et al., 2026),而在商店的訓練查詢上,它的權重較為穩定。當某個檢索器在保留查詢上被確立為較強者,而且沒有任何調校過的混合比例在該處勝過它時,就單獨使用它,把另一個保留作為基準線,並測試針對精確限制的詞彙過濾。這正是商店的情況。
-
正式環境中會出什麼問題
- 實用原則 在融合結果旁記錄兩個檢索器各自的排名,檢查每個引擎設定中的清單深度,並且在任一檢索器變動時重新調校 α。
- 本章做出的決定
8. 理解查詢
閱讀約需 85 分鐘,含 12 個範例。
讀完本章後,你可以在檢索之前修改購物者的查詢:修正拼字、統一單位的寫法、以同義詞擴充查詢、預測查詢的商品類別、以大型語言模型改寫查詢、以該模型想像出的商品進行搜尋,以及把後續追問轉成獨立的查詢。
課程大綱:10 節
-
依據型錄中的詞彙修正拼字
- 執行
catalog-spelling-corrector.py - 執行
spelling-context-counts.py - 執行
measure-spelling-corrections.py - 實用原則 以你的型錄建立詞彙表,等有了查詢紀錄後也納入其中,並且只修正詞彙表中沒有的字,使品牌名稱得以保留。修正一個字之前,先檢查它的相鄰字:沒有上下文的修正器會把變體拼法改成常見的字,也抓不到真詞錯誤。以錯字查詢自身的判斷為每項修正評分,並且在測試修正器之前,蒐集比本商店多得多的錯字查詢。
- 執行
-
依目錄的寫法正規化單位
- 執行
normalize-units-both-sides.py - 實用原則 正規化規則要從目錄的文字寫起,而不是從查詢寫起,並且要計算目錄對每種單位的所有寫法。請印出斷詞器對正規化文字所產生的詞項,因為看起來正確的規則,也可能產生會比對到錯誤商品的詞項。在同一個版本中,對商品與查詢套用相同的規則,並重建索引。當目錄把尺寸存放在欄位中,例如
attributes裡的寬度,就改為以該欄位比對尺寸,如第 6 章的建議。
- 執行
-
Expand queries with a synonym list
- 關鍵詞 query drift
- 執行
synonym-expansion-train-and-test.py - 實用原則 Keep a synonym list for vocabulary gaps you have seen, write each entry from a failing query, and read the products each entry adds before you keep it. Never report a list's gain on the queries you wrote it from, and keep an entry only if a planned comparison on the test queries shows a gain. Test any expansion of semantic retrieval first: the LLM's rewrites, which add synonyms and also broaden and drop words, hurt it. When the gaps are too many to list, try stemming or learned sparse retrieval instead.
-
以商品文字訓練查詢分類器
- 執行
query-classes-by-hand.py - 實用原則 當你沒有已標註的查詢時,請以商品的類別來訓練查詢分類器,並以經過判斷的完全相關商品所屬的類別來檢驗它,而不是拿另一個分類器的猜測來比對。一旦日誌提供了已標註的查詢,就改用這些查詢來訓練。預測出的類別只能當作排序的提示,不可視為標籤。
- 執行
-
以提升預測類別的分數取代過濾
- 執行
class-boost-train-and-test.py - 實用原則 不要依預測類別進行過濾。應以在訓練查詢上調校過的權重來提升該類別,且只有在其預先規劃的比較成立時才保留提升。商店的比較並未成立,因此其語意檢索維持不加提升。我們只建議在購物者自行選定類別時才使用類別過濾,例如點選某個分類。
- 執行
-
Rewrite queries with an LLM
- 執行
llm-rewrites-on-train.py - 執行
llm-rewrites-on-test.py - 執行
rewrite-largest-changes.py - 實用原則 Don't put an LLM rewrite in front of a strong retriever without a planned comparison on the test queries. Read the per-query losses for dropped requirements and broadened products, which the prompt's "keep every requirement" doesn't prevent. The store's semantic retrieval, the ranker to beat, keeps searching the query as typed.
- 執行
-
以假設性商品搜尋(HyDE)
- 執行
hyde-train-and-test.py - 實用原則 嘗試 HyDE 時,務必將查詢本身的嵌入一併平均進去。使用未經標註訓練的編碼器時,預期收益最大;使用微調過的編碼器時,Gao et al. 只在其最大的 LLM 上看到收益,而商店以其模型、提示與編碼器,則完全沒有看到收益。對於像商店這類在這些查詢上已經勝過 BM25的編碼器,在為每個查詢支付一次 LLM 呼叫的費用之前,先測試 HyDE。商店不使用它。
- 執行
-
將後續提問轉成獨立查詢
- 執行
follow-up-refinements.py - 實用原則 絕不要單獨搜尋後續提問。至少要把它與前面的對話串接,或改寫成獨立查詢。當細化條件指出目錄中有明確記載的值時,就擷取該值,並據以加權或篩選;剩下的商品太少時,則退回未篩選的清單。在信任上述任何做法之前,先用自己日誌中的對話來衡量。
- 執行
-
正式環境中會出什麼問題
- 實用原則 記錄使用者輸入的原始查詢與改寫後的查詢,快取 LLM 的改寫結果,將模型與提示連同輸出一起納入版本管理,並在商品目錄每次變動時重建詞彙表與分類器。
- 本章的決定
9. Ranking
閱讀約需 98 分鐘,含 14 個範例。
學習結束時,學員能夠describe each product that retrieval returns with features, train a learning-to-rank model with LightGBM on your own judgments, rerank the top of the list with a cross-encoder or an LLM, measure an LLM reranker's positional bias, set the rerank depth by counting its costs, and distil a slow reranker into a fast one。
課程大綱:12 節
-
以特徵描述每個候選商品
- 執行
ranking-features-by-grade.py - 實用原則 從你已經在計算的特徵著手:每個檢索器的分數、若有重排序器則加入其分數、名稱等欄位上的比對結果,以及熱門度訊號。訓練之前,先在訓練查詢上依等級比較每個特徵,分數用平均值,偏態的計數則用中位數。不隨等級變動的特徵,能讓模型學到的東西很少。訓練與上線服務時,要用相同的程式碼計算特徵。
- 執行
-
Learn a ranking from pairs of products
- 執行
lambdas-for-one-query.py - 執行
lambdas-match-lightgbm.py - 實用原則 Train rankers with a gradient that weighs each pair by the metric you report, as LambdaMART does, rather than regressing on grades. Know which defaults of your library change that gradient, and check on train queries that it beats a pointwise model on your data, because the families' order in a benchmark doesn't transfer by itself.
- 執行
-
以 LightGBM 訓練 LambdaMART
- 執行
lambdamart-tree-size-on-train.py - 執行
lambdamart-choices-on-train.py - 實用原則 將資料列依查詢排序並傳入
group,將label_gain設為你的指標所使用的增益,透過eval_X與eval_y以保留的訓練查詢進行提前停止,並固定亂數種子與執行緒數。如第 3 章所建議,將每個查詢的候選項目依產品 ID 排序,分數相同時也以同樣方式決定先後。除非訓練查詢上的交叉驗證另有結論,否則將未判定的候選項目視為等級 0。已判定的查詢很少時,可嘗試較小的樹。
- 執行
-
Report LambdaMART on the test queries
- 執行
lambdamart-on-test.py - 實用原則 Adopt a learned ranker only when its planned test comparison is established. Read its gain importance before you trust it, because the features it leans on are the ones you'll pay for at query time. Early stopping on a few held-out queries keeps very different numbers of trees from one split to the next, from 99, 20, 8, 43, 38 across the cross-validation folds to 213 here. A rule of thumb, not a measurement: with this few queries, judge more queries before you tune further, and treat any one stopping point as noise.
- 執行
-
以 cross-encoder 重排語意檢索結果
- 執行
cross-encoder-on-semantic.py - 實用原則 重排你最強的檢索結果,並在查看測試結果之前先固定深度;只有在保留查詢上確立了改善時,才採用該重排器。本商店的通用 cross-encoder 不納入管線。
- 執行
-
用 LLM 為每項商品評分
- 執行
llm-pointwise-rerank.py - 實用原則 若以 LLM 進行逐點式重新排序,請要求它給出分級標籤或分數,統計每個查詢給出的不同分數數量,並事先決定同分時的處理規則:沿用檢索的順序是安全的預設做法。評估結果時,請使用人工判斷,切勿使用同一個 LLM 的評分。
- 執行
-
用一個 LLM 提示詞排序 20 項商品
- 執行
llm-listwise-rerank.py - 實用原則 使用每個 listwise 答案之前都要先檢查:計算缺漏、重複與未知的數字,並記錄有多少答案需要修復。若執行環境支援結構描述,就用它限制答案的形式。在測量之前,先固定視窗、步幅與呈現商品的順序,因為順序會改變答案,下一項檢查即說明這一點。
- 執行
-
Reverse the input to measure positional bias
- 執行
listwise-position-bias.py - 實用原則 Before you trust a listwise LLM reranker, run each query in at least two orders and report how much the answers agree. When agreement is low, aggregate several orders or ask each pair in both orders, and count those calls against your budget. In production, show the products in retrieval's order every time, so that the same list at least gets the same answer.
- 執行
-
依預算設定重排深度
- 執行
reranker-prompt-sizes.py - 執行
rerank-depth-on-train.py - 實用原則 對你考慮的每一種重排器,計算各個深度下每次查詢的呼叫次數與字元數,並以訓練查詢設定深度。這是經驗法則,而非量測結果:重排深度只要到訓練集 nDCG@10 不再提升的深度即可,因為超過該深度的每項商品都要付出相同成本,卻沒有任何增益。接著在你自己的硬體與流量下為該設定計時,只有在符合延遲預算時才保留這個階段。
- 執行
-
將交叉編碼器蒸餾為小型排序器
- 執行
distillation-on-train.py - 執行
distilled-ranker-on-test.py - 實用原則 當重排序器已確立能帶來增益、但每次查詢的成本過高時,才進行蒸餾:以教師模型標註訓練查詢的每個候選項目,用不需要教師模型即可計算的特徵訓練學生模型,並在保留的查詢上,檢定學生模型是否勝過你的檢索方式。請閱讀學生模型的增益重要性:當檢索自身的分數佔主導時,學生模型基本上就是你的檢索。當學生模型無法對上教師模型的尺度時,請改用成對商品之間的差距(margin)。商店目前還沒有能勝過語意檢索的教師模型,因此保留程式碼,但不上線學生模型。
- 執行
-
正式環境中會出什麼問題
- 實用原則 每次排序都要記錄特徵與模型版本,將候選項目放在不帶任何資訊的順序中,依人工判斷評分,檢查 judged@10,並為每個重排序階段設定時間限制與備援方案。
- Decisions this chapter made
10. LLM 應用的檢索
閱讀約需 97 分鐘,含 10 個範例。
讀完本章後,你能夠衡量 LLM 回答問題時所依據的檢索:依答案引用的商品為答案評分,將每筆引用與檢索實際傳回的結果核對,衡量 LLM 讀到的商品,選擇以什麼作為單一可檢索單位來建立索引,並從代理(agent)的工具呼叫紀錄中判讀其搜尋行為。
課程大綱:10 節
-
將檢索到的產品放進提示
- 關鍵詞 context window
- 執行
catalog-against-the-context.py - 實用原則 提示中放入少量檢索到的項目,而不是整個資料集,並透過在訓練查詢上以數種數量量測答案來決定放幾個,因為更多上下文可能有幫助,也可能有害。商店的錄製檔只回答了測試查詢,每次五個產品,因此這裡的五是固定設定,而非量測結果。只有在整個資料集能夠輕鬆放進視窗時,才考慮整份送入,而且要在相同查詢上將它與檢索做比較測試。
-
Score each answer by the products it cites
- 執行
rag-answers-by-context.py - 實用原則 Score every answer by its cited products' human grades, and report beside it how many answers recommend nothing, split by whether the context held an exact product. Keep a random context as a floor whenever you change the model or the prompt: an answer that recommends something from random products has invented a fit. Test a retriever's gain on the answers too, because a gain in ranking needn't carry through.
- 執行
-
對照脈絡檢查每一則引用
- 關鍵詞 Grounding
- 執行
citations-inside-the-context.py - 實用原則 對每一則答案執行 ID 檢查,在離線階段、購物者看到之前完成,並把脈絡之外的 ID 視為失敗的答案:捨棄或重試,絕不顯示。把引用要求放在獨立欄位中,讓檢查不必解析內文,並檢查文字與欄位是否一致。統計答案違反了哪些指示,例如推薦的數量,並在程式碼中強制執行其中重要的那些。
-
檢查被引用的商品支持什麼
- 關鍵詞 Faithfulness
- 執行
faithful-but-partial.py - 實用原則 先以你的判斷標註為回答評分,因為它們說明推薦是否合適。再針對回答對每項商品所做的主張加上支持檢查,可用 NLI 模型或 LLM 評審。以人工樣本校準該評審,如第 4 章所做,也如 ARES 所做:它使用一小組人工標註的資料點進行預測驅動推論(prediction-powered inference,PPI)以減輕預測誤差,即以人工標籤修正評審的估計(Saad-Falcon et al., 2024)。對於沒有判斷標註的查詢,你只有支持檢查可用,因此在報告結果時要明確說明這一點。
-
先衡量情境,再衡量答案
- 關鍵詞 context precision, context recall, Spearman's rank correlation
- 執行
context-measures-by-hand.py - 執行
context-measures-against-answers.py - 實用原則 對於大小固定的情境,請根據你的判定回報該情境的 precision@k 或 context precision,並在旁列出 recall@k。當答案必須涵蓋每一個相關項目時,例如適用於某一電器的所有零件,就使用 recall。沒有判定時,Ragas 由 LLM 評判的版本可估計類似的量;在信任它們之前,請先以人工樣本加以校準。選擇 k 的方法,是在訓練查詢上以數個 k 值衡量答案,這需要為那些查詢記錄的答案。
-
Choose the retrievable unit
- 關鍵詞 retrievable unit, chunking
- 執行
retrievable-unit-on-train.py - 實用原則 When your items are short, index each whole item as one unit, as the store does. When they are long, chunk them, put the item's title in front of every chunk, and choose the chunk size on train queries by the context measures of the previous section. Pass the LLM the whole item, or the chunk with its title, so it knows what it is reading. Before you trust a small gap, count the queries whose top k changes when ties break the other way.
-
Read an agent's searches from its trace
- 關鍵詞 tool, tool call, trace
- 執行
agent-searches-in-traces.py - 執行
agent-later-searches.py - 實用原則 Read the traces before you score an agent. Count its searches per task, compare each search with the shopper's words, and find where added words come from. Add the agent's searches to the queries you test your retriever on, scored with the judgments for the shopper's request or judged afresh, because a retriever tuned on people's queries hasn't been tested on an agent's. Expect an agent that mostly passes the shopper's words through to be bounded by the same retrieval as a RAG answer.
-
比較代理人工具背後的檢索器
- 執行
agent-picks-by-tool.py - 實用原則 測試代理人工具背後的檢索器,要比照為人設計的檢索器來測試:使用代理人自己的搜尋與最終挑選,並以人工判斷評分。檢查每一項挑選是否都來自工具的結果。計算工具從未回傳相關商品的任務數,因為這些任務沒有任何代理人能夠補救,只有檢索能夠解決。
- 執行
-
What goes wrong in production
- 實用原則 Version the model, prompt and context settings with the answers, check citations on every answer, track abstentions, and keep agents' searches in a test set of their own.
- 本章所做的決定
11. 測試變更
閱讀約需 64 分鐘,含 13 個範例。
讀完本章後,你能判斷一項搜尋變更是否應該上線:逐一查詢比對兩個排序器的結果、避免測試查詢讓變更看起來比實際更好、估算 A/B 測試的規模,並以交錯(interleaving)方式比較兩個排序器的結果。
課程大綱:12 節
-
逐一查詢比對兩次執行結果
- 執行
diff-semantic-against-bm25.py - 實用原則 保留一組經過相關性判斷、且絕不用來調校的回歸查詢集,並讓每個候選都與基準在其上比較。先列出計畫中的比較項目,再為每一項報告其排列檢定 p 值、t 檢定與區間。統計勝、敗與平手的數量,並在上線前逐一檢視每個失敗案例。當這些失敗案例似乎有共同模式時,先在訓練查詢上檢驗,再採取行動,然後修正該類查詢,或將它們交給基準處理。
- 執行
-
以 URisk 讓損失的權重高於獲益
- 執行
urisk-of-two-candidates.py - 實用原則 對每個候選系統,在平均變化量旁一併報告 α = 1 的 URisk。無論平均值多高,URisk 為負的候選系統都不要上線,並閱讀其損失以找出原因。失敗的代價高時,應提高 α,並另外報告商店最常見查詢上的 URisk。只有在某個查詢上的損失不比另一個查詢上的獲益更糟時,才使用 α = 0。
- 執行
-
評判新排序器浮現的商品
- 執行
unjudged-score-range.py - 實用原則 對每個候選,計算兩次執行的 judged@10,並在兩種情境下重新評分。當領先者更換時,先評判這些未評判的配對再進行比較,並將新的評判永久加入迴歸測試集,使它涵蓋你測試過的每一個排序器。當領先者在兩種情境下都不變時,未評判的商品不太可能推翻這項比較。
- 執行
-
讓測試查詢遠離每一項選擇
- 執行
tuning-on-test-inflates.py - 實用原則 每一項設定都在訓練查詢上選擇,包括決定帶往下一步的候選方案。每項決策只查看測試查詢一次,並記錄每次查看:比較了什麼、選了什麼。當一組測試集已經引導過許多決策時,應將它移到訓練端,另以一組全新的測試查詢來評判。
- 執行
-
執行 A/B 測試
- 關鍵詞 sample ratio mismatch
- 執行
aa-tests-with-peeking.py - 執行
figure-peeking-false-positives.py - 實用原則 測試前,先選定主要指標與不得變差的護欄指標,並決定測試的規模(含檢定力)。先在相同的設置上執行 A/A 測試。在讀取任何結果之前先檢查樣本比例,並只在規劃的樣本數時讀取一次結果。若必須在測試進行時持續觀察,請使用專為連續監控而設計的序貫方法,例如隨時有效的 p 值(always-valid p-values)(Johari et al., 2015)。
-
以檢定力決定測試規模
- 執行
sample-size-from-power.py - 執行
queries-needed-for-power.py - 執行
figure-power-curve.py - 實用原則 每個 A/B 測試都應在開始前決定規模。若所需的使用者多於商店數週內的流量,就測試更大的變化、改用變異較小的指標,或以每位使用者在測試前的資料來調整指標。針對 Bing 的實驗,Deng et al. 指出這做法「可將變異降低約 50%」(Deng et al., 2013);能節省多少,取決於使用者過去的行為能多準確地預測該指標。在離線情況下,應計算迴歸測試集可偵測的最小變化,當你的改動幅度更小時,就增加查詢。要事先固定數量:不斷增加查詢,直到檢定力估計值顯示足夠為止,會使你偏向找到顯著性(Webber et al.)。
- 執行
-
以 team draft 交錯兩個排名
- 執行
team-draft-one-query.py - 實用原則 當你需要知道使用者偏好哪一個排序器,並希望以比 A/B 測試更少的搜尋次數線上比較兩者時,請使用 team draft 交錯。當你需要絕對數值(例如營收),或變動並不在排名本身時,請改用 A/B 測試。依我們的推論,交錯後的清單無法呈現延遲、頁面版面或結果數量不同所造成的影響。
- 執行
-
以模擬購物者演練交錯測試
- 關鍵詞 click model
- 執行
one-simulated-shopper.py - 執行
simulated-interleaving-verdict.py - 實用原則 在模擬中演練交錯測試,以檢查你的程式碼與符號檢定。對於模擬所建議的搜尋次數,其可信度不會高於所假設的購物者;在解讀模擬的判定結果之前,先以相反的平手處理方式重新執行一次。絕不要把模擬的判定結果當成關於排序器的證據。在真實流量上,應事先固定搜尋次數,並以符號檢定做決定。
-
離線與線上結果不一致時,找出原因
- 執行
clicks-per-search-misleads.py - 實用原則 當離線與線上結果不一致時,先找出原因,再決定相信哪一方。檢查線上指標衡量的是成功(例如購買),而非單純的活動量。檢查線上勝出者結果的 judged@10,並對缺漏的部分進行判斷。接著,根據事先選定的指標之線上結果做出決定,並將查詢或判斷加入迴歸測試集,讓它下次的預測更準確。
- 執行
-
決定是否上線
- 實用原則 當所有離線項目都通過,且事先決定樣本量的線上測試在主要指標上顯示顯著增益、同時沒有任何護欄指標變差時,才上線。當某個離線項目未通過,請先修正再進行線上測試,因為線上測試會耗用購物者的時間。當線上測試在計畫樣本量下仍無定論時,請保留基準線。
-
正式環境中會出什麼問題
- 實用原則 在實驗平台中將 A/A 測試、樣本比例檢查與固定測試樣本數自動化,記錄對測試查詢的每一次查看,以 ID 打破同分,並以人工判斷確認 LLM 評審的改善。
- 本章做出的決定
測驗題庫
每章皆附習題與解答,供經驗證的教育工作者使用。每個解答都對應其所屬的節。 如何取得
正在載入測驗題庫…