AIによる翻訳。英語版は人による検証済みである。 原文を読む
教材キット
2026.11.13版(書籍の現行版)より。書籍が更新されると変わる。出典を明記すれば、自由に利用および改変できる。
シラバス
読書時間は章の本文に基づく。学習者はLabまたは自分のマシンでコード例も実行する。
週ごとに一章: 11 週
| 週 | 章 | 読書 | 例 |
|---|---|---|---|
| 1 | 1. 検索をエンドツーエンドで通す | 69分 | 12 |
| 2 | 2. 関連性とは何か | 69分 | 11 |
| 3 | 3. 検索の測定 | 69分 | 13 |
| 4 | 4. 人間と LLM による判定 | 71分 | 11 |
| 5 | 5. 語彙検索 | 78分 | 14 |
| 6 | 6. セマンティック検索 | 74分 | 17 |
| 7 | 7. Hybrid retrieval | 69分 | 14 |
| 8 | 8. クエリの理解 | 74分 | 12 |
| 9 | 9. Ranking | 82分 | 14 |
| 10 | 10. LLMアプリケーションのための検索 | 78分 | 10 |
| 11 | 11. 変更のテスト | 65分 | 13 |
5 週間後
| 週 | 章 | 読書 | 例 |
|---|---|---|---|
| 1 | 1. 検索をエンドツーエンドで通す 2. 関連性とは何か 3. 検索の測定 | 207分 | 36 |
| 2 | 4. 人間と LLM による判定 5. 語彙検索 | 149分 | 25 |
| 3 | 6. セマンティック検索 7. Hybrid retrieval | 143分 | 31 |
| 4 | 8. クエリの理解 9. Ranking | 156分 | 26 |
| 5 | 10. LLMアプリケーションのための検索 11. 変更のテスト | 143分 | 23 |
学習目標と概要
各章の学習目標はその冒頭の一文である。各構成案は章の節に従い、定義される用語、例を実行する前に求められる予測、各節が締めくくる規則を含む。 主要な用語と確認すべき予測は、クイズバンクが出題するとおり英語版のものである。
1. 検索をエンドツーエンドで通す
読了まで約69分。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をベースラインとして使う。BM25は学習データもモデルも必要とせず、うまくいかないときには、どの単語が原因かを確認できる。パラメータは、train queriesで変更の効果を測定できるようになるまで、そのままにしておく。買い物客と商品が同じものに別の言葉を使っている場合は、意味検索(semantic retrieval)を加える。
- 実行
-
判定と照らして結果を確認する
- 実行
judge-first-results.py - 実践ルール 指標を計算する前に、5 件または 10 件のクエリについて、上位の結果を判定と並べて読むこと。付属品、見逃された修飾語、誤ったサイズといったパターンを探す。上位の結果に未判定のものが複数ある場合は、そのクエリのスコアを信頼する前に、それらを判定してもらうこと。
- 実行
-
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 - 実践ルール 融合する前に、各検索器を単独で測定する。RRF は k = 60 を調整なしの既定値として用い、保持したクエリで最良の単独の検索器を上回る場合にのみ、融合したランキングを採用する。一方の検索器がはるかに弱い場合は、同じ票数を受け入れるのではなく、ランキングへの重み付けや、train クエリでの k の調整を試す。第7章では、この店舗のデータで両方を試すが、テストクエリで報告される調整済みブレンドのうち、意味検索を上回るものはない(テストクエリで調整済みブレンドを報告する)。
- 実行
-
Rerank the top 50 with a cross-encoder
- 実行
cross-encoder-rerank.py - 実行
rerank-one-query.py - 実践ルール Rerank a shortlist, here 50 products, because the cost grows with every pair. Adopt a reranker only when it beats the best retrieval alone on held-out queries. This one didn't improve on the hybrid run or on semantic retrieval, so the store's best pipeline so far is semantic retrieval alone. Before you give up on reranking, try a different depth, or a ranker trained on your own judgments. Neither a different depth nor LambdaMART beats semantic retrieval on the test queries in chapter 9 (Set the rerank depth from a budget, Report LambdaMART on the test queries).
- 実行
-
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. 関連性とは何か
読了まで約69分。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 - 実践ルール ペアごとに等級を一つだけ残し、それを選んだ規則を書き留める。サンプルと同様に、低いほうの等級を推奨する。この選択は、その商品を順位付けするランカーに不利な側へ誤るものであり、有利な側へは誤らない。評価者の判定が exact と irrelevant のように二等級にわたって割れた場合は、評価者と一緒にガイドラインを読み直す。両者が定義を共有していないためである。
- 実行
-
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 の線形ゲインを用いる。惜しい結果に買い物客がほとんど価値を見いださない場合は、より低い partial ゲインを選ぶ。何を選ぶにせよ、ランカーを比較する前に固定し、すべてのスコアとともに報告し、異なるゲインで計算した 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. 検索の測定
読了まで約69分。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 - 実践ルール ランカーを比較する前に、主要指標を1つ、そのゲインとカットオフとともに選び、書き留める。判定が等級付きで、買い物客がページを閲覧する場合には、線形ゲインの nDCG@10 を推奨する。適合しなければならない交換部品のように、完全一致が近い一致よりはるかに価値が高い場合は指数ゲインに切り替え、ほとんどのクエリに正解が1つしかない場合は 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検定や符号検定を用いてはならない。
- 実行
-
差の大きさを報告する
- 重要用語 confidence interval, percentile interval
- 実行
bootstrap-interval-by-hand.py - 実行
interval-coverage-check.py - 実践ルール すべての比較について、平均差、そのブートストラップ区間、効果量、p値を示す。区間が0を含むなら、その比較は改善を示していない。区間が0を除外していても、変更のコストに見合う最小の差を下回るなら、差は実在しても、出荷する価値はないかもしれない。区間は、差が存在するかどうかを判定するためではなく、差の大きさを述べるために用いる。
-
何を確立済みとみなすかを事前に決める
- 重要用語 Bonferroni correction
- 実践ルール テストクエリで何かをスコア付けする前に、計画済みの比較の一覧を書いておくこと。一覧は短く保つこと。比較が 1 つ増えるごとに、他の比較の基準が厳しくなるからである。多数のランカーを一度に比較する場合、Bonferroni は厳しくなりすぎる。Sakai は、まず分散分析でランカー間に何らかの差があるかどうかを検定し、続いてランダム化 Tukey HSD 検定を行うことを勧めている。この検定はすべての組を比較しつつ、偽陽性が 1 つでも生じる確率を設定した水準に保つ(Sakai, 2014)。
-
Compare runs in an evaluation harness
- 実行
compare-chapter-one-rankers.py - 実践ルール For every change, report the mean nDCG@10 and judged@10 of each run, then a table like this one, with a column for each comparison and the exploratory ones labelled. A change ships only when it is established against the ranker to beat.
- 実行
-
本番環境で何がうまくいかなくなるか
- 実行
split-changes-the-verdict.py - 実践ルール テストクエリで実行した比較をすべて記録し、境界線上の結果や探索的な結果は新しいクエリで確かめる問いとして扱い、スコアの同点は固定した順序で解消し、nDCG@10 には必ず judged@10 を併記する。
- 実行
- この章で下した決定
4. 人間と LLM による判定
読了まで約71分。11 例。
この章を終えるころには、判定ガイドラインを書き、カッパと重み付きカッパで二人の判定者の一致度を測り、混同行列から LLM 判定者の誤りを読み取り、LLM の判定が人間の判定の代わりになる場面とならない場面を決められるようになる。
講義概要: 13 節
-
Write judging guidelines
- 実行
print-judging-guidelines.py - 実践ルール Write guidelines as definitions, a procedure, a worked example with a reason for each grade, and a rule for each case your judges have disagreed on. Log disagreements and revise the guidelines when one kind keeps coming back. Give an LLM judge the same guidelines as people, plus a rule for what it can't see, and then measure what it does, because a rule you write isn't a rule the judge follows.
- 実行
-
カッパ係数で偶然を超えた一致を測る
- 重要用語 observed agreement
- 実行
kappa-by-hand.py - 実践ルール カッパを報告するときは、観測一致率を併記し、観測一致率だけを示すことはしない。カッパは、train クエリについて人が判定したペアで測り、用いるしきい値には経験則であることを明記する。判定者が3人以上の場合やラベルが欠けている場合は、Krippendorff のアルファを用いる。これは観測された不一致と偶然による不一致を比べ、任意の数の観察者と不完全なデータを扱える(Krippendorff, 2011)。
-
重み付きカッパで惜しい不一致に部分点を与える
- 実行
weighted-kappa-by-hand.py - 実践ルール 順序づけられた評価では、通常のカッパと並べて線形重み付きカッパを報告し、大きく外れた不一致は別に数える。各不一致には、それによって失われるゲインの差に応じた重みを与える。本書の線形ゲインでは、線形の重みがこれに当たる。0、1、3 という指数ゲイン(指標を選ぶ)では、差は 1、2、3 となり、scikit-learn のどちらの選択肢でも得られない。その場合は、例が線形の行列を構築したのと同じ方法で、その行列を構築する。
- 実行
-
kappa はラベルの構成と合わせて読む
- 実行
kappa-under-two-label-mixes.py - 実践ルール kappa を報告するときは、必ずラベルの構成を併記すること。自分で得た kappa であれ公表されたものであれ、構成の異なる標本から得た kappa を互いに比較してはならない。一致率の標本は、判定者を使う方法に合わせて抽出することを勧める。判定者がランカーの上位10件を評価するのであれば、train クエリの上位10件からペアを抽出する。
- 実行
-
LLM の誤りの方向を読む
- 実行
llm-confusion-matrix.py - 実践ルール カッパを読む前に混同行列を読むこと。その際は、判定者を実際に使う方法と同じ仕方で抽出したペアを用い、そのプールについて方向と最大の誤りを明示すること。方向をあるプールから別のプールへ持ち越してはならず、そこからランカーの nDCG@10 を予測してもならない。ランカー同士は同じ評価の下で比較すること。LLM の評価によるスコアと人間の評価によるスコアを比較してはならない。
- 実行
-
LLM が見たものと評価者が見たものを比較する
- 実行
llm-grades-follow-written-words.py - 実践ルール LLM の評価者には、評価者が頼りにした項目のうちテキストにできるもの、たとえば属性や説明の全文を、すべて与える。そして、LLM が見られないものは、プロンプトに書き留めておく。不一致を LLM の誤りとして数える前に、その証拠が LLM の見た内容に含まれていたかを確認する。評価者の証拠が視覚的なものである場合は、この例が両プールで示すように、LLM が exact の商品により低い評価を与えるかを測定する。あるいは、使用するモデルが画像を受け付けるなら、画像を与える。
- 実行
-
プロンプトがラベルをどれだけ変えるかを測る
- 実行
prompt-disagreement.py - 実践ルール プロンプトをジャッジの一部として扱う。すべてのプロンプトを、モデルのバージョン、設定、回答スキーマとともに一語一句記録する。この記録がマニフェストとスクリプトで行っているとおりである。プロンプトの変更は別のジャッジとみなし、その一致度を測り直す。少なくとも 2 つのプロンプトを走らせ、LLM の評価に基づく結論は、両方がその結論を出したときにのみ信頼する。一方のプロンプトを他方より優先して選ぶのは、train のペアに基づく場合に限り、かつ両者の差の区間が 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 は単独で報告せず、入れ替わった対の一覧とともに報告する。ランカーが数個しかなく、1 回の入れ替わりで tau が 0.333 動く場合は、tau を 0.9 と比べて読んではならない。そのうえで、各評価集合のもとでの計画された比較により、入れ替わった対に差があるかどうかを検定する。
- 実行
-
LLM の評点で計画比較をやり直す
- 実行
planned-comparisons-llm-grades.py - 実践ルール LLM の評点による判定のみを根拠に変更を出荷してはならない。LLM の評点は、判定する価値のある候補を選ぶために使い、決定は人手の評点で行う。二つのプロンプトが異なる判定を与える場合、LLM では比較に決着をつけられず、人が決着をつけなければならない。
- 実行
-
人間のサンプルでLLMのスコアを補正する
- 重要用語 Spread
- 実行
correct-llm-scores-with-humans.py - 実践ルール LLMの評価からスコアを報告するときは、必ず無作為に選んだ一部のクエリを人間に判定してもらい、補正後の推定値を報告すること。そのサンプルで、LLMのクエリごとの差が人間のものとどれだけ相関するかを確かめる。相関が弱いときは、予算をより多くのクエリの判定に充てる。
-
本番環境で何がうまくいかなくなるか
- 実践ルール 審査器を固定し、すべての評価に人手のサンプルを残し、何かが変わるたびに一致度をやり直す。そして、調整に使った審査器でランカーを採点してはならない。
- この章で決めたこと
5. 語彙検索
読了まで約78分。14 例。
この章を読み終えるころには、語彙検索をゼロから構築できるようになる。
講義概要: 12 節
-
Split product text into terms
- 実行
tokenize-product-text.py - 実践ルール Print the terms that your queries and products become before you tune any scoring, and run both through the same tokenizer. Start from bm25s's defaults, as the store does. When shoppers type sizes, quantities or model numbers, use a token pattern that keeps single digits and splits a number from its unit, such as the one above. When they type negations, turn the stop list off. Each is a change to search, so measure it on the train queries and check it on test queries before you keep it.
- 実行
-
転置インデックスを構築する
- 実行
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はステミングなしのままとする(決定事項)。改善幅の大きさは不安定なものとして扱うこと。
- 実行
-
訓練クエリで BM25 のパラメータを調整する
- 先に確認する 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 - 実践ルール ライブラリのデフォルトから始める。フィールドやステミングなど、インデックスの対象を変更したときに、訓練クエリで
k1とbを調整する。調整した設定を残すのは、その計画された比較が確立された場合に限る。ストアの調整後の設定は確立されなかったので、ストアはk1=1.5とb=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. セマンティック検索
読了まで約74分。17 例。
本章を終えると、保存済みの埋め込みから、カタログ向けのセマンティック検索を構築できるようになる。
講義概要: 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.
-
NumPyで厳密な最近傍を求める
- 実行
exact-search-numpy.py - 実行
exact-search-memory.py - 実践ルール 厳密探索は、メモリに収まらなくなるか、レイテンシの予算を満たせなくなるまで使うことを勧める。その判断は、自分のカタログを自分のハードウェアで測定して行う。厳密探索には調整すべき設定がなく、近傍を見落とすこともないので、あらゆる近似インデックスの正解基準にもなる。正規化したベクトルを 1 つの 32 ビット行列として保存し、
argpartitionで上位 k 個を取り出し、その k 個だけを並べ替える。
- 実行
-
Trade recall for speed with HNSW
- 実行
hnsw-efsearch-sweep.py - 実践ルール Use an ANN index only when exact search is too slow or too large. Tune it on train queries by its recall against exact search, at the k your pipeline uses, and choose by the worst query, not the mean. We recommend the smallest
efSearchat which every query keeps at least 9 of its true top 10, withM = 32. This chapter tunes onlyefSearch;MandefConstructionchange the graph itself, and deserve the same sweep on a large catalog. Aumüller et al. (2018) find that very different ANN methods reach comparable quality-performance trade-offs, so measure on your own data, not by reputation.
- 実行
-
テストクエリで索引を検証する
- 実行
hnsw-test-recall.py - 実行
hnsw-index-memory.py - 実践ルール 調整した索引は、ホールドアウトしたクエリで厳密検索に対する再現率を調べ、平均スコアより先に、上位 k 件をすべて失うクエリを読む。少数のクエリに依存する nDCG@10 の低下は、対応のある検定で確立されるまでは未証明として扱う。メモリは、ベクトルだけでなくリンクの分も見込む。
- 実行
- Check queries that contain digits
-
型番の重みは小さいと予想する
- 実行
code-probes.py - 実践ルール ログの中から、サイズ、数量、型番を含むクエリを挙げ、どちらのランカーも信頼する前に、それらのクエリで両方を比較する。型番が重要なときは、語彙的に一致させる。正確なサイズが重要なときは、
attributesの幅のような商品フィールドに対して一致させる。トークナイザが数字を保持し、単位の書き方が 1 通りに統一されているなら、語彙的に一致させてもよい(5 章、8 章)。クエリのそれ以外の部分は、意味検索に任せる。両者の組み合わせは、7 章で検証する。
- 実行
-
否定に対する埋め込みを検証する
- 実行
negation-probes.py - 実践ルール 語彙ベースかセマンティックかを問わず、第 1 段階の検索器に否定の処理を任せてはならない。買い物客が、カタログに保存されている形状、素材、肘掛けなどの属性を除外するときは、その除外を検出し、その属性でフィルタリングする。そうでなければ、リランキングを行う。van den Elsen et al. の再現実験では、クロスエンコーダーと LLM リランカーのほうが否定をはるかにうまく扱ったためである。9 章では、ストアのセマンティック検索の結果をクロスエンコーダーと LLM でリランキングするが、否定を検証できないストアの判定付きクエリでは、どのリランカーもセマンティック検索を上回らない。モデルを変更するとき、またはリランカーを追加するときはいつでも、このようなプローブを再実行する確認用として残しておくこと。
- 実行
- 埋め込みモデルを選ぶ
- ベンチマークは候補を絞るための材料として読む
-
Truncate dimensions, then test the loss
- 実行
truncate-embeddings.py - 実行
truncation-paired-test.py - 実践ルール Shortlist models on licence, language, size and domain, and treat benchmark ranks as a hint. Decide between models, or between vector sizes, on your own train queries with a paired test, and read the interval as well as the p-value. Shorten vectors only for a model trained for it, normalize after shortening, and only when memory or search time requires it.
- 実行
-
本番環境で起こる問題
- 実行
similarity-scale.py - 実行
figure-similarity-by-grade.py - 実行
spelling-probes.py - 実践ルール 固定した類似度でフィルタしてはならない。すべてのベクトルとともにモデル、リビジョン、プロンプトを記録し、クエリを埋め込む場所でそれらを確認すること。カタログの変化に合わせてインデックスを再構築し、測定し直すこと。埋め込む前にトークン数を数えること。ログにタイプミスが見られる場合は、クエリを埋め込む前に、各単語の隣接語を確認する修正器で綴りを修正すること(8章)。
- 実行
- この章で下した決定
7. Hybrid retrieval
読了まで約69分。14 例。
学習の終わりには、combine a lexical and a semantic ranking into one, either by adding normalized scores or by reciprocal rank fusion, tune how much each ranking counts on train queries, and decide with a paired test on test queries whether the combination beats the better of its two inputsできるようになる。
講義概要: 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 - 実践ルール 融合の前に、train クエリで各検索器を単独で確認する。確認には judged@10 と対応のある検定を用いる。一方が弱く、勝つクエリも少ない場合は、その検索器に同じ重みの票を与えてはならない。重みを付けて重みを調整するか、使わないようにする。同じ重みの票が適するのは、品質がほぼ同等の 2 つの検索器である。
- 実行
-
スコアは足す前に正規化する
- 実行
raw-scores-versus-min-max.py - 実行
figure-score-scales.py - 実践ルール 生の BM25 スコアとコサインスコアを、推測した重みで足してはならない。どちらのスコア関数についての知識も必要としないため、既定としてはクエリごとの min-max を推奨する。各スコアのとりうる最小値が分かっており、候補によって動かない下限が欲しい場合は、理論的 min-max を用いる。いずれの場合も、α は train クエリで調整する。
- 実行
-
学習クエリで逆順位融合の設定を検証する
- 実行
rrf-k-on-train.py - 実践ルール k = 60 をデフォルトとして保ち、少なくとも深さ 100 のリストを融合する。深いリストは候補を加えるだけだからである。k = 60 から離れるのは、学習データでの最良の k が、並べ替え検定で偶然と区別できるだけの差、すなわち p が 0.05 未満となる差で k = 60 を上回るときに限る。この店のように一方の検索器がより強い場合、変更すべき設定は 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 - 実践ルール train クエリをブートストラップして各標本でチューニングし、さらに、情報を持たない別の順序で同順位を並べてもう一度チューニングする。選ばれた重みがグリッド全体に散らばるなら、より粗いグリッドか、より多くの train クエリを使うか、より単純な設定を選ぶ。同順位の順序だけで重みが動くなら、それを報告するすべての箇所で明記する。2つの確認のどちらでも安定しているなら、その重みをテストクエリに進める。
- 実行
-
チューニング済みブレンドをテストクエリで報告する
- 先に確認する 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値が、計画した比較の数で補正した閾値を下回る場合に限る。そうでなければ、より単純なシステムを維持する。その結果、ストアで打ち負かすべきランカーはセマンティック検索だけとなる。チューニングした重みとコードは保存しておき、リトリーバーやカタログが変わったときにブレンドを再度検証する。
-
Learn sparse term weights with SPLADE
- 実行
exact-products-missing-query-words.py - 実践ルール Consider learned sparse retrieval when BM25's misses are mostly vocabulary misses and you want to keep an inverted index. Check the model's licence first, and measure it like any retriever: alone, per query, on train queries, then on test.
- 実行
-
ColBERTによるトークン単位のマッチング
- 実践ルール シングルベクトル検索が1語に左右される細部を取りこぼし、かつトークンごとにベクトルを持つ余裕がある場合に、レイトインタラクションを検討する。ストアと同規模のカタログでは、まず候補の短いリストに対してリランカーを試すことを勧める(第9章)。リランカーはクエリ時に同じ細部を読み、新しいインデックスを必要としない。ただし、第9章のどのリランカーも、ストアの意味的検索に対する確かな改善を示してはいない。
-
ハイブリッド検索が有効になる場面を見極める
- 実践ルール 2つの検索器の品質が近い場合、埋め込みモデルが学習対象外のドメインで動作する場合、または多くのクエリが埋め込みでは捉えられない正確な語に依存する場合に、ハイブリッドを試すことを勧める。まず重み付きRRFから始める。これは順位だけを必要とし、この店の訓練クエリでは、順列検定によって重み付き和と区別できなかった。両方のエンジンがすべての候補についてスコアを返す場合は、正規化したスコアの重み付き和に移行する。2つの研究が、そのデータでRRFより優れていることを見いだしており(Bruch et al., 2023; Abdallah et al., 2026)、この店の訓練クエリでも、その重みはより安定していた。保持クエリで一方の検索器がより強いと確立され、調整したどの混合もそこでそれを上回らないときは、その検索器を単独で使い、もう一方をベースラインとして残し、正確な制約には字句フィルタを試す。これがこの店の場合である。
-
本番環境で何が問題になるか
- 実践ルール 融合結果と並べて両方の検索器の順位をログに記録し、すべてのエンジンの設定でリストの深さを確認し、検索器が変わるたびに α を再調整する。
- この章で下した決定
8. クエリの理解
読了まで約74分。12 例。
この章を終えると、検索の前に買い物客のクエリを変更できるようになる。
講義概要: 10 節
-
カタログの語彙に基づいてスペルを訂正する
- 実行
catalog-spelling-corrector.py - 実行
spelling-context-counts.py - 実行
measure-spelling-corrections.py - 実践ルール 語彙はカタログから作り、クエリログが得られたらそれも加えて作る。訂正するのは語彙にない語だけにして、ブランド名が残るようにする。語を訂正する前に、その隣接語を確認する。文脈のない訂正器は、異綴りの語を一般的な語に変えてしまい、実在語エラーを見逃す。各訂正は、タイプミスのクエリ自身の判定で採点する。訂正器をテストする前に、ストアにあるよりはるかに多くのタイプミスのクエリを集める。
- 実行
-
Normalize units the way the catalog writes them
- 実行
normalize-units-both-sides.py - 実践ルール Write normalization rules from the catalog's text, not from the queries, by counting every way it writes each unit. Print the terms the tokenizer makes of the normalized text, because a rule that looks right can produce terms that match the wrong products. Apply the same rules to products and queries in one release, and rebuild the index. When the catalog keeps sizes in fields, such as the width in
attributes, match the size against the field instead, as chapter 6 suggests.
- 実行
-
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 - 実践ルール 予測クラスでフィルタしてはならない。train クエリで調整した重みでブーストし、計画済みの比較で改善が確認された場合にのみ、そのブーストを残す。このストアでは確認されていないため、セマンティック検索はブーストなしのままとする。クラスによるフィルタを勧めるのは、たとえばカテゴリのクリックのように、買い物客自身がクラスを選んだ場合に限る。
- 実行
-
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 を試すこと。ストアは HyDE を使っていない。
- 実行
-
フォローアップをスタンドアロンのクエリに変える
- 実行
follow-up-refinements.py - 実践ルール フォローアップだけで検索してはならない。少なくとも前のターンと連結するか、スタンドアロンになるよう書き換える。絞り込みがカタログに記載のある値を指す場合は、その値を抽出してブーストまたはフィルタに使い、残る商品が少なすぎるときはフィルタなしのリストに戻す。いずれも信頼する前に、自分のログの会話で測定する。
- 実行
-
本番環境で何が問題になるか
- 実践ルール 入力されたままのクエリとリライト後のクエリを両方ログに記録し、LLM によるリライトをキャッシュし、モデルとプロンプトを出力と合わせてバージョン管理し、カタログが変わるたびに語彙と分類器を再構築する。
- この章で下した判断
9. Ranking
読了まで約82分。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.
- 実行
-
Rerank semantic retrieval with a cross-encoder
- 実行
cross-encoder-on-semantic.py - 実践ルール Rerank your strongest retrieval, at a depth fixed before you look at test results, and adopt the reranker only on an established gain on held-out queries. The store's general-purpose cross-encoder stays out of the pipeline.
- 実行
-
Score each product with an LLM
- 実行
llm-pointwise-rerank.py - 実践ルール If you rerank pointwise with an LLM, ask for a graded label or a score, count the distinct scores it gives per query, and decide the tie-break in advance: retrieval's order is a safe default. Score the result with human judgments, never with the same LLM's grades.
- 実行
-
1回のLLMプロンプトで20個の商品を並べ替える
- 実行
llm-listwise-rerank.py - 実践ルール リストワイズの答えは、使う前にすべて検査すること。欠落した番号、重複した番号、未知の番号を数え、修復が必要だった答えの数を記録する。実行環境が対応しているなら、スキーマで答えの形を制約する。測定の前に、ウィンドウ、ステップ、商品を見せる順序を固定する。次の検証が示すとおり、順序によって答えが変わるからである。
- 実行
-
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 - 実践ルール 検討する各リランカーについて、深度ごとに1クエリあたりの呼び出し回数と文字数を数え、深度は train クエリで決める。これは測定ではなく経験則だが、リランクするのは train の nDCG@10 が改善しなくなる深度までにとどめる。それを超える商品は、どれも同じコストがかかるのに何も加えないからである。そのうえで、その構成を自分のハードウェアとトラフィックで計測し、レイテンシ予算に収まる場合に限ってその段階を残す。
- 実行
-
クロスエンコーダを小さなランカーに蒸留する
- 実行
distillation-on-train.py - 実行
distilled-ranker-on-test.py - 実践ルール リランカーが確かな向上をもたらすがクエリあたりのコストが高すぎる場合に蒸留する。train クエリのすべての候補に教師でラベルを付け、教師を使わずに計算できる特徴量で生徒を訓練し、保持したテスト用クエリで生徒を自分たちの検索と比較して検定する。生徒のゲインの重要度を読む。検索自身のスコアが支配的であれば、生徒は主に自分たちの検索そのものである。生徒が教師のスケールに合わせられない場合は、ペア間のマージンを用いる。このストアには、セマンティック検索を上回る教師がまだないため、コードは残すが、生徒は出荷しない。
- 実行
-
本番環境で何が問題になるか
- 実践ルール 特徴量とモデルのバージョンを各ランキングとともに記録し、候補は情報を持たない順序に並べ、人手による判定でスコアを付け、judged@10 を確認し、各リランキング段階に制限時間とフォールバックを与える。
- Decisions this chapter made
10. LLMアプリケーションのための検索
読了まで約78分。10 例。
この章を終えると、検索に基づいて回答する LLM の背後にある検索を測定できるようになる。
講義概要: 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 のように、人間のサンプルで較正する。ARES は、少数の人間が注釈を付けたデータ点を予測支援推論(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 とあわせて報告する。1 つの家電に合う部品をすべて挙げるときのように、回答がすべての関連項目を網羅しなければならない場合は、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. 変更のテスト
読了まで約65分。13 例。
この章を読み終えると、検索への変更を本番に出すべきかどうかを判断できるようになる。
講義概要: 12 節
-
2 つの実行をクエリごとに比較する
- 実行
diff-semantic-against-bm25.py - 実践ルール 何に対してもチューニングに使わない判定付きクエリの回帰セットを保持し、すべての候補をそのセット上でベースラインと比較する。計画した比較を先に列挙し、それぞれについて並べ替え検定の p 値、t 検定、区間を報告する。勝ち、負け、引き分けを数え、出荷前にすべての負けを読む。負けに共通のパターンがありそうな場合は、対処する前に訓練クエリでそれを確かめ、そのうえでその種のクエリを修正するか、ベースラインに回す。
- 実行
-
URiskで勝ちより負けを重く扱う
- 実行
urisk-of-two-candidates.py - 実践ルール すべての候補について、平均変化と並べてα = 1のURiskを報告する。平均がどうであれ、URiskが負の候補は出荷せず、その負けを読んで理由を調べる。失敗のコストが高いときはαを上げ、店舗で最も頻度の高いクエリについてはURiskを別に報告する。あるクエリでの負けが別のクエリでの勝ちと同程度でしかない場合に限り、α = 0を用いる。
- 実行
-
新しいランカーが表出させる商品を判定する
- 実行
unjudged-score-range.py - 実践ルール すべての候補について、両方の実行の judged@10 を計算し、二つのシナリオで再採点する。首位が入れ替わる場合は、比較する前に未判定のペアを判定し、新しい判定を回帰セットに恒久的に加える。これにより、回帰セットはテストしたすべてのランカーを網羅する。両方のシナリオで首位が変わらない場合、未判定の商品が比較を覆す可能性は低い。
- 実行
-
Keep the test queries out of every choice
- 実行
tuning-on-test-inflates.py - 実践ルール Choose every setting on train queries, including which candidate to take forward. Look at the test queries once per decision, and log each look: what you compared and what you chose. When a test set has guided many decisions, move it to the train side and judge a fresh set of test queries.
- 実行
-
A/Bテストを実施する
- 重要用語 sample ratio mismatch
- 実行
aa-tests-with-peeking.py - 実行
figure-peeking-false-positives.py - 実践ルール テストの前に、主要指標と悪化してはならないガードレール指標を選び、テストのサイズを決める(検出力とともに)。まず同じセットアップでA/Aテストを実施する。結果を読む前にサンプル比を確認し、結果は計画したサイズで一度だけ読む。実行中のテストを監視せざるを得ない場合は、常時有効なp値のような、継続的な監視のために設計された逐次的手法を使う(Johari et al., 2015)。
-
検出力でテストの規模を決める
- 実行
sample-size-from-power.py - 実行
queries-needed-for-power.py - 実行
figure-power-curve.py - 実践ルール すべての A/B テストは、開始前に規模を決める。ストアが数週間で得られるユーザー数より多くのユーザーが必要な場合は、より大きな変化をテストするか、変動の小さい指標を用いるか、テスト前の各ユーザーのデータで指標を調整する。Deng らは Bing の実験について、この調整が「分散を約50%減らせる」と報告している(Deng et al., 2013)。どれだけ減らせるかは、ユーザーの過去の行動が指標をどれだけ予測できるかによる。オフラインでは、回帰テスト用セットが検出できる最小の変化を計算し、変更がそれより小さい場合はクエリを追加する。数はあらかじめ固定する。検出力の推定値が十分だと示すまでクエリを追加し続けると、有意差を見いだす方向に偏りが生じる(Webber et al.)。
- 実行
-
チームドラフトによる二つのランキングのインターリーブ
- 実行
team-draft-one-query.py - 実践ルール どちらのランカーをユーザーが好むかを知る必要があり、A/B テストより少ない検索数で比較したい場合は、チームドラフトによるインターリーブを使ってオンラインで二つのランカーを比較する。収益のような絶対的な数値が必要な場合や、変更がランキングに関するものでない場合は、代わりに A/B テストを使う。我々の考えでは、インターリーブされたリストでは、遅延、ページレイアウト、結果件数の違いによる影響を示せない。
- 実行
-
模擬ショッパーでインターリービングを予行演習する
- 重要用語 click model
- 実行
one-simulated-shopper.py - 実行
simulated-interleaving-verdict.py - 実践ルール インターリービングの検定は、コードと符号検定を確かめるために模擬で予行演習する。模擬が示唆する検索回数は、仮定したショッパーの妥当性の範囲でしか信頼できないものとして扱い、模擬の判定は、何らかの意味を読み取る前に、同点を逆順にして再実行する。模擬の判定をランカーに関する証拠として報告してはならない。実トラフィックでは、検索回数をあらかじめ固定し、符号検定で判断する。
-
Find the cause when offline and online disagree
- 実行
clicks-per-search-misleads.py - 実践ルール When offline and online results disagree, find the cause before you trust either. Check that the online metric measures success, such as a purchase, and not activity. Check judged@10 on the online winner's results, and judge what is missing. Then decide on the online result of the metric you chose in advance, and add queries or judgments to the regression set so that it predicts better next time.
- 実行
-
出荷するかどうかを決める
- 実践ルール オフラインの項目がすべて通り、事前にサイズを決めたオンラインテストが主要指標で有意な改善を示し、どのガードレールも悪化していないときに出荷する。オフラインの項目が一つでも通らないときは、オンラインに進む前に直す。オンラインテストは買い物客の時間を消費するからである。オンラインテストが計画したサイズで結論に至らないときは、ベースラインを維持する。
-
本番環境で起こる問題
- 実践ルール A/A テスト、サンプル比の確認、固定されたテストサイズは実験プラットフォームで自動化し、テストクエリを参照するたびに記録を残し、同点は ID で解消し、LLM が判定した改善は人間の判定で確認する。
- この章で下した決定
問題バンク
全章の設問と解答を、認証済み教育者向けに提供する。各解答は該当する節に紐づいている。 入手方法
問題集を読み込み中…