由 AI 翻譯。經人工以英文驗證。 閱讀原文
教學套件
版次 2026.11.19 為本書目前的版次,會隨本書更新而變動。可自由使用與改編,但須標示本書出處。
課程大綱
閱讀時間僅計算本章的文字。學生也會在 Lab 或自己的電腦上執行範例。
每週一章:11 週
| 週 | 各章 | 閱讀 | 範例 |
|---|---|---|---|
| 1 | 1. 端到端的資料代理 | 64 分鐘 | 6 |
| 2 | 2. 純文字轉 SQL 為何算錯數字 | 66 分鐘 | 7 |
| 3 | 3. 從你自己的工作建立黃金問題 | 67 分鐘 | 9 |
| 4 | 4. 為代理提供分層的上下文 | 82 分鐘 | 10 |
| 5 | 5. 在語意層中一次定義指標 | 62 分鐘 | 9 |
| 6 | 6. 優先將問題導向受治理的指標 | 77 分鐘 | 7 |
| 7 | 7. 讓代理程式在防護機制內撰寫 SQL | 103 分鐘 | 12 |
| 8 | 8. 能力所不及時要明說 | 64 分鐘 | 6 |
| 9 | 9. 讓代理程式只能看到被允許的內容 | 74 分鐘 | 8 |
| 10 | 10. 評判答案並抓出退步 | 92 分鐘 | 7 |
| 11 | 11. 透過 MCP 提供語意層服務 | 66 分鐘 | 5 |
5 週後
| 週 | 各章 | 閱讀 | 範例 |
|---|---|---|---|
| 1 | 1. 端到端的資料代理 2. 純文字轉 SQL 為何算錯數字 3. 從你自己的工作建立黃金問題 | 197 分鐘 | 22 |
| 2 | 4. 為代理提供分層的上下文 5. 在語意層中一次定義指標 | 144 分鐘 | 19 |
| 3 | 6. 優先將問題導向受治理的指標 7. 讓代理程式在防護機制內撰寫 SQL | 180 分鐘 | 19 |
| 4 | 8. 能力所不及時要明說 9. 讓代理程式只能看到被允許的內容 | 138 分鐘 | 14 |
| 5 | 10. 評判答案並抓出退步 11. 透過 MCP 提供語意層服務 | 158 分鐘 | 12 |
學習目標與大綱
每章的學習目標即為其第一句。每份大綱依循該章各節,列出該節定義的名詞、執行範例前要求的預測,以及每節結尾所歸納的規則。 其關鍵術語與要提問的預測內容,依題庫所列,採用英文版次的內容。
1. 端到端的資料代理
閱讀約需 64 分鐘,含 6 個範例。
讀完本章,你就能建立商店的資料倉儲,讓一個預先錄製好回應的模型針對商業問題撰寫 SQL,執行該 SQL 並抓出它回傳的錯誤數字,再用一個明確定義的指標回答同一個問題,最後以五道黃金問題為兩種做法評分。
課程大綱:8 節
-
認識這家商店及其資料倉儲
- 執行
load-the-warehouse.py - 實用原則 請針對含有您實際會遇到的陷阱的資料倉儲來建置代理程式:測試資料列、已取消的訂單、多種幣別、當地營業日、會計年度曆,以及至少一個名稱誘人的舊版資料表。當您無法分享正式環境資料時,請用種子產生資料,讓任何人都能重建。乾淨的示範綱要,恰好會掩蓋您需要看見的那些失誤。
- 執行
-
提出問題,由模型產生 SQL
- 執行
first-question-to-sql.py - 實用原則 把僅用結構描述的文字轉 SQL 當成要超越的基準,而不是最終產品。在你自己的倉儲上執行一次並保留它的回覆,因為這會讓你看出結構描述沒有承載你的哪些定義。凡是測試所依賴的每一則模型回覆都要錄製下來,並固定模型檔案、提示詞雜湊值與取樣設定,這樣你發現的失敗才能由他人重現。
- 執行
-
執行 SQL 並核對數字
- 執行
run-the-model-sql.py - 實用原則 不要因為代理程式的 SQL 看起來正確,就接受它給出的數字。在任何人依賴該數字之前,先與黃金結果核對;兩者不同時,先找出能解釋差異的資料列,再動手修改。差異出在定義上,例如哪些訂單要計入,通常比出在語法錯誤上更常見。
- 執行
-
Answer from one defined metric
- 執行
one-metric-answer.py - 實用原則 Route a question to a defined metric before you let a model write SQL. Give the model the metric's description and its dimensions, and let it return a request, which your code validates and compiles. Write SQL from a model only for questions no metric covers, and only behind guards.
- 執行
-
Score both on five golden questions
- 執行
score-five-golden.py - 實用原則 Score every change on golden questions written from your own work, and report correct, wrong, error and declined separately. Include questions that should be declined from the start, or an agent that never declines will look perfect. Use a handful of questions as a smoke test while you build, and a larger set with a held-out split before you claim anything.
- 執行
-
閱讀單次執行的追蹤紀錄
- 執行
print-one-trace.py - 實用原則 閱讀追蹤紀錄,而不只是看分數,並且先讀拒答的部分。檢查每次拒答的理由,談的是否為提供給代理程式的內容,例如「沒有任何指標能計算客戶數」,而不是對它從未看過的資料所做的斷言。即使分數將其算作已拒答,只要拒答的理由是錯誤的,就應視為缺陷。
- 執行
-
What goes wrong in production
- 實用原則 Before anyone outside your team uses the agent, make every failure above visible: score on golden questions in CI, show provenance and data freshness with each answer, review declines in traces, replay recorded replies by message hash, and run model-written SQL only on a read-only connection. Start with golden scoring, because it is the only one of these checks that catches a plausible wrong number.
- 本章做出的決定
2. 純文字轉 SQL 為何算錯數字
閱讀約需 66 分鐘,含 7 個範例。
讀完本章後,你能夠說出僅憑結構描述(schema)的文字轉 SQL 在真實資料倉儲上,回傳看似合理卻錯誤數字的五種方式,在商店的資料倉儲上逐一重現,並在一組黃金問題中統計各種情形的次數,還能分辨哪些問題可以靠更好的提示詞修正,哪些則需要結構描述中沒有的定義。
課程大綱:8 節
-
Joins that fan out and inflate a sum
- 執行
tent-orders-fan-out.py - 執行
figure-lines-per-order.py - 實用原則 When a query joins tables of different grains, check the grain of every column it sums. Aggregate the finer table to the coarser grain before joining, or filter with
INorEXISTS, and compare the row count after the join with the count of distinct keys. Expect a model given only the schema to miss this, because the schema names columns, not grains.
- 執行
-
結構描述無法承載的定義
- 執行
gross-or-net-revenue.py - 實用原則 將問題中的每個商業詞彙視為需要查證的定義,而不是要比對的欄位。記下每個定義由誰負責,並將定義本身提供給代理程式,形式為受治理的指標,或至少是詞彙表條目,絕不只提供可用來計算它的欄位。當某個詞沒有書面定義時,應讓代理程式詢問或拒絕回答,而不是自行選擇。
- 執行
-
時區與會計年度曆
- 執行
fiscal-year-and-local-day.py - 實用原則 要讓代理程式掌握你的曆法,而不只是你的欄位:哪個時間戳記是 UTC、哪個日期是營業日,以及會計年度從何時開始、如何命名。每個期間類的問題都要在邊界上測試,例如某個月的第一天,或會計年度的第一個月,因為在這些地方 UTC 與當地日期、日曆年與會計年度會出現分歧。與其讓代理程式自己撰寫日期運算,不如提供一個讓它進行 join 的日期維度表。
- 執行
-
名稱誘人的資料表
- 執行
legacy-table-answer.py - 實用原則 列出代理可以讀取的資料表,即使舊資料表與暫存資料表的名稱再親切,也不要列入清單。模型所寫的每個查詢在執行前,都要依清單檢查其使用的資料表。當代理在你的測試中從未選用誘人的資料表,也仍要保留這項檢查:新的問題、更大的綱要或檢索到的範例,都可能改變這個結果。
- 執行
-
Filters nobody wrote down
- 執行
cancelled-lines-counted.py - 實用原則 Move every rule about which rows count into the marts where you can, so no query can forget it. Where a mart has to keep the rows, as with cancelled orders, write the rule into the definition the agent uses, and add a golden question that fails when the rule is left out. Read the gold for the same rules, because the person who wrote it can miss them as readily as the model can.
- 執行
-
Count the failures by kind
- 執行
classify-schema-only-errors.py - 實用原則 Count failures by kind on your own golden questions before you choose a fix, and keep errors that fail loudly apart from wrong numbers that don't. Label kinds by comparing each query with its golden SQL, with rules you can read, and read the wrong answers no rule labels. Fix the most common kind of wrong number first, and expect it to be a definition, a calendar or a filter more often than a syntax mistake.
- 執行
-
正式環境中會出什麼問題
- 實用原則 在代理的數字送到任何人手上之前,依標準結果為每個答案評分,在期間邊界與每個定義上都保留標準問題,列出代理可以讀取的資料表,並以人工閱讀一份錯誤答案的樣本。依類型統計錯誤數字,並把任何一種類型的計數上升都視為警訊。
- 本章做出的決定
3. 從你自己的工作建立黃金問題
閱讀約需 67 分鐘,含 9 個範例。
讀完本章後,你可以根據公司內部人員實際提出的問題撰寫黃金問題,並為每個問題附上其黃金 SQL、提問者的角色,以及答案所需的類型。
課程大綱:9 節
-
為什麼公開基準測試不等於你自己的測試
- 執行
public-vs-own-score.py - 實用原則 使用公開基準測試,是為了在共同任務上比較模型,絕不能把它當成代理能在你的資料倉儲上運作的證據。引用任何分數時,都要一併註明基準測試的版本,因為標準答案集會被修正。請用依據你自己的工作、採用你自己的定義所撰寫的黃金問題(golden questions)來衡量你自己的代理,並預期兩種分數會不一致。
- 執行
-
Write questions from the work people do
- 執行
load-golden-questions.py - 實用原則 Collect golden questions from real requests, keep the asker's wording, and give each one a role, a kind and a note that names the definition its gold follows. Write the golden SQL against the marts the agent will use, and have the person who owns the definition read the note. Expect to spend more time on the definitions than on the SQL.
- 執行
-
Compare results, not SQL text
- 執行
results-match-rules.py - 實用原則 Score an agent by comparing its result with the golden result, never by comparing SQL text, normalized or not. Keep one comparison function for every score you report, so a change in the score is a change in the agent. When a match surprises you, read the agent's SQL, because a result can match by coincidence.
- 執行
-
順序、四捨五入與欄位名稱
- 執行
tolerant-comparison.py - 實用原則 忽略資料列順序、欄位順序與欄位名稱,允許額外欄位,並要求資料列數量相同,且數值落在較小的相對容許誤差內。每個標準答案都要四捨五入到明訂的位數,並在其上再加該位數的半個單位。只有在問題要求排名或序列時,才設定
order_matters。在問題或其註記中固定單位與尺度,並將使用其他單位的答案視為錯誤,因為閱讀答案的人無法分辨。
- 執行
-
Check the gold before you trust it
- 執行
gold-runs-and-agrees.py - 執行
gold-fan-out-audit.py - 執行
reworded-golden-question.py - 實用原則 Before a golden question scores anything, run its SQL, reject empty and NULL results, compare it with an independent query where you can, and have the definition's owner read its note. Read the agent's wrong answers for unclear questions, and fix the question, not the score. When a question can be read two ways, name the reading in its text, or make it a decline.
- 執行
-
涵蓋拒答與角色,而不只是答案
- 執行
golden-coverage.py - 實用原則 從一開始就納入拒答與禁止的問題,並把拒答分成含糊與無法回答兩類,因為正確答案不同。為每個問題標上它所測試的陷阱,並在評任何分數之前,依切分統計題型、角色與陷阱的數量。要補足缺口,就撰寫新的問題,而不是在看過代理人的表現之後,把既有的問題在切分之間搬動。
- 執行
-
保留一份測試切分
- 執行
baseline-on-both-splits.py - 實用原則 在調校任何東西之前,先將你的黃金問題切分一次,並且只在 dev 上調校。只有在回報結果時才對 test 切分評分,並且並列回報兩個切分。當測試問題被看得太頻繁,以致人們開始針對它們調校時,就撰寫新的測試問題,並將舊的退回 dev。
- 執行
-
正式環境中會出什麼問題
- 實用原則 把黃金問題集視為程式碼:與定義放在一起、在 CI 中執行其檢查、在評分時計算黃金結果、把真實流量中的問題加入開發集,並保護測試分割。分數變動時,請先閱讀有變化的答案,再更改問題集或比對方式。
- 本章的決定
4. 為代理提供分層的上下文
閱讀約需 82 分鐘,含 10 個範例。
讀完本章後,你可以為撰寫 SQL 的模型提供不只是綱要(schema)的資訊,並分層給予:針對名稱容易誤導的欄位所寫的說明、商店業務術語的詞彙表,以及針對每個問題檢索出的已驗證查詢。
課程大綱:9 節
-
結構描述說明了什麼,又沒說明什麼
- 執行
schema-digest.py - 實用原則 結構描述放得下時,就把完整的結構描述保留在情境中,並為它補上意義,而不是移除資料表。在撰寫其他任何一層之前,先取一個真實的問題,列出結構描述中所有能回答它的解讀。結構描述沒有排除的每一種解讀,都是某個其他層必須明訂的規則。
- 執行
-
Describe columns where names mislead
- 執行
column-descriptions.py - 實用原則 Write notes only for tables and columns whose names mislead: deprecated tables, amounts with a sign or unit you wouldn't guess, timestamps in a time zone other than the business's, and columns whose values include a placeholder such as
(none). Write them as facts about the column, not as instructions for one question. Expect a note to fix many questions and occasionally mislead on one, and keep the questions it broke in the golden set so you see it happen.
- 執行
-
加入業務詞彙表
- 執行
glossary-context.py - 實用原則 對於每個其含義是由某人所做選擇的業務用語,都寫一個詞彙表條目,並以資料表與欄位來陳述。每條規則最好只放在一處,不是欄位註解就是詞彙表條目;商店的檔案把排除已取消訂單的規則兩處都寫了,而
gq-002的失誤顯示,說兩次並不會讓它記得牢。在信任詞彙表之前,先用你的開發集問題衡量它,只有在它對開發集有幫助,或下一層依賴它時,才保留它。
- 執行
-
擷取已驗證查詢作為範例
- 執行
retrieve-similar-queries.py - 實用原則 每個問題擷取少數幾筆已驗證查詢,依遮蔽後問題的相似度挑選,並將每筆以其問題與 SQL 呈現。設定最低分數,並用資料倉儲無法回答的問題來檢驗,因為詞彙擷取總是會傳回一些結果。絕不讓黃金問題進入已驗證集合。只有在你自己的測量顯示詞彙擷取遺漏了人員會認為相似的查詢時,才改用嵌入向量。
- 執行
-
讓上下文符合預算
- 執行
context-token-budget.py - 實用原則 將 token 預算設在遠低於模型視窗之處,並依你量測過的優先順序,而不是依你撰寫各層的順序,讓每個問題的上下文符合預算。結構描述放得下時絕不捨棄。無論捨棄了什麼,都將穩定的層放在最前、問題放在最後。將你的 token 估計值與模型的實際計數核對一次,並讓估計值維持在偏高的一側。
- 執行
-
Measure what each layer adds
- 執行
layer-ablation-dev.py - 執行
figure-layers-correct-and-tokens.py - 執行
missing-columns-schema-only.py - 執行
what-stays-wrong.py - 實用原則 Measure context one layer at a time on the dev split, with the same questions, comparison rule and model, and keep the outcomes per question, not only the totals. Treat a difference of one or two questions on a set this size as noise until repeated runs say otherwise. Read the queries that fail, because each one names a layer you don't have yet or a rule the model still skips.
- 執行
-
在測試集上檢查選定的層
- 執行
best-layers-on-test.py - 實用原則 在開發集上選定組合,然後在測試集上與基準線並列評分一次,並回報測試集的數字。請記錄在讀過開發集結果之後,你對情境所做的每一項修改,因為每一項都可能使開發集分數被高估。當開發集與測試集的結果不一致時,先檢查你能測量的事項,例如各資料集與你的範例有多接近,再解釋這個差距。
- 執行
-
What goes wrong in production
- 實用原則 Version the context with the warehouse: keep it beside the marts, checked in CI against the tables it names, scored on golden questions with every change, and logged per answer with the verified queries it used. Move any rule that must always hold out of the context and into a definition the model can't skip.
- Decisions this chapter made
5. 在語意層中一次定義指標
閱讀約需 62 分鐘,含 9 個範例。
讀完本章,你將能以 MetricFlow 的 YAML 規格撰寫商店的語意模型與指標,用本書自己的編譯器將指標請求編譯成 SQL,證明其實體連接不會造成資料列膨脹,依會計年度曆法分組,並確認 MetricFlow 會將相同的定義編譯成相同的結果。
課程大綱:9 節
-
將每個資料表描述為語意模型
- 執行
load-semantic-models.py - 實用原則 每個資料表宣告一個語意模型,只在實體唯一時才將它標為主要實體,並且在資料倉儲或 YAML 變更時,一律對照資料檢查這些宣告。撰寫簡單指標時不使用度量,直接指明其模型與聚合,除非你的 MetricFlow 版本或 dbt 專案仍然要求使用度量。
- 執行
-
Compile a metric request to SQL
- 執行
compile-net-revenue.py - 實用原則 Let the agent, and every other client, send metric requests, never SQL fragments. Resolve every name in a request against the definitions, build the query as a tree with values as literals, and show the SQL with every answer so a person can read what ran. Accept a raw SQL filter only from the people who maintain the layer, never from a model.
- 執行
-
透過實體進行連接,讓加總不會扇出
- 執行
entity-join-no-fanout.py - 執行
figure-entity-graph.py - 實用原則 宣告實體,使語意層進行的每個連接都是多列對一列,其餘的就讓語意層拒絕。當問題需要較細資料表上的數字(例如依商品的營收)時,請在該資料表上定義指標,而不是透過連接去取得。請檢查主要實體在資料中是否唯一,因為錯誤的宣告會讓這項保護失效。
- 執行
-
以簡單指標建立比率指標與衍生指標
- 執行
refund-rate-metric.py - 實用原則 將每個比率都定義為兩個指標之比,使兩邊各自彙總,再依分組相除。寫明每一邊所依據的時間維度,並確保每一邊都套用其篩選條件,不論是在自身的定義中或以輸入篩選條件,而不是仰賴讀者自行得知。當運算式不只一次除法時,使用衍生指標,並讓其輸入維持為可單獨查詢的簡單指標。
- 執行
-
將會計年度曆法放入時間骨幹表
- 執行
fiscal-quarter-grain.py - 實用原則 將每一種會計曆法與營業曆法,以自訂粒度放入時間骨幹表,且每個值都指向單一期間,再讓請求依這些粒度分組。當地營業日要在語意層之前,於資料集市中建立。若需要跨會計期間比較,請每個期間各執行一次請求,再用程式碼比較結果,直到你的語意層支援自訂粒度上的位移為止。
- 執行
-
對照 MetricFlow 檢查定義
- 執行
metricflow-agrees.py - 執行
metricflow-needs-a-date.py - 實用原則 對於你所依賴的每一種請求形式,都要把你的語意層與 MetricFlow(或在正式環境中提供服務的任何工具)做比較,並且在任一方有變動時重新執行比較。請精確固定 MetricFlow 的版本,並把它的引擎限制在單一模組之內,因為它的 Python API 可能在任何一次發行中改變。
- 執行
-
一份定義,適用任何資料倉儲的方言
- 執行
transpile-metric-sql.py - 實用原則 只維護一份定義並加以轉譯,但在每種方言的 SQL 於該資料倉儲上對照已知答案執行之前,都應視為未經測試。請在編譯器中明確轉型日期與除法,讓轉譯時需要猜測的地方更少。
- 執行
-
正式環境中會出什麼問題
- 實用原則 要像測試程式碼一樣測試語意層:對照資料檢查實體的唯一性,在 CI 中執行每個指標的 SQL,每次變更都與 MetricFlow 及黃金問題比對,並且只接受來自維護語意層的人以外的任何來源所送出的結構化請求。
- 本章所做的決定
6. 優先將問題導向受治理的指標
閱讀約需 77 分鐘,含 7 個範例。
讀完本章後,你將能把語意層的指標(而非資料倉儲的資料表)當作工具提供給代理,讓它回傳結構化的指標請求,並在任何查詢執行之前先驗證該請求;當一個問題可能對應多個指標時,讓它向提問的人反問;最後在黃金問題上衡量這些改變帶來的差異。
課程大綱:8 節
-
Give the agent metrics, not tables
- 執行
metric-tools.py - 實用原則 Give the agent the metrics, their descriptions and the dimensions each can use, and keep tables out of its tools until a question needs them. Put the catalog in the prompt while it fits, so a run doesn't spend a call listing metrics, and let a describe tool return dimension values from the warehouse. When the catalog outgrows the prompt, keep names and descriptions there and move the dimensions behind the describe tool.
- 執行
-
Ask for a request, not SQL
- 執行
model-returns-a-request.py - 實用原則 Have the model return a metric request as structured arguments to a tool, and parse it into a typed request before anything else happens. Keep the reply format simple enough for your own code to parse, and keep that parser in one place. Accept SQL from a model only on a separate, guarded path, for questions no metric covers.
- 執行
-
Validate the request against the layer
- 執行
reject-unknown-dimension.py - 實用原則 Validate every request before it compiles: names against the layer, categorical values against the warehouse, dates against their grain. Refuse with a message that names the allowed alternatives, and send it back to the model, which can usually correct itself in one call. Treat any request that would compile into an empty or partial result as invalid, not as an answer of zero.
- 執行
-
Ask back when a question is ambiguous
- 執行
clarify-best-customers.py - 實用原則 Give the agent a tool to ask back, with options that name the layer's metrics, and show the person each option's definition. Ask back only when every option leads to a request the layer can answer; when none does, decline instead. Read clarifications in traces as you read declines, because a score counts a useless question as correct.
- 執行
-
在黃金問題上衡量 metrics-first
- 執行
metrics-first-score.py - 執行
figure-outcomes-by-approach.py - 實用原則 在相同的黃金問題上,將 metrics-first 與你的基準線並列評分,並且只在 dev 上調整。說明調整期間錄製了幾次 dev 切分,並在最後只錄製一次 test 切分。將正確、錯誤答案、錯誤(error)、拒答,以及因 step limit 而結束的執行分開回報,也要計算釐清詢問與被拒絕的請求,因為它們顯示呼叫花在哪裡。
- 執行
-
閱讀未答對的執行紀錄
- 執行
metrics-first-misses.py - 實用原則 逐次呼叫閱讀每一次未答對的執行,並依其需求分類:近似但不正確的指標,表示描述需要更精確,或需要向提問者反問;缺少指標,表示語意層需要新增一個,或該問題需要受保護的 SQL;而答案取決於角色的情況,需要的是存取規則,而不是提示詞。
- 執行
-
正式環境中會出什麼問題
- 實用原則 在擴大代理程式的使用對象之前,讓每個業務術語只對應一個指標,並附上寫明何時不該使用的說明;像審查提示一樣審查驗證訊息;在請求執行時讀取維度值;在代理程式之下強制執行角色;並在分數之外一併計算釐清與拒絕的次數。先從近似重複的指標著手,因為它們會產生有效的請求,卻回傳錯誤的數字。
- 本章的決定
7. 讓代理程式在防護機制內撰寫 SQL
閱讀約需 103 分鐘,含 12 個範例。
讀完本章後,你將能處理沒有任何指標涵蓋的問題:讓代理程式撰寫 SQL,這些 SQL 在唯讀模式下執行,所連的資料庫只含其角色可讀取的列與欄,並且經過剖析、依資料表允許清單檢查、對列數、大小、成本與時間設下上限、在發生錯誤時修復一次,以及轉譯成你的資料倉儲所用的方言。
課程大綱:12 節
-
Give the agent a database with only what it may read
- 執行
agent-warehouse.py - 實用原則 Run the agent's SQL on a connection that can reach only what the agent may query: a role with only those grants, or, in DuckDB, a read-only database file that holds only the allowed tables, with file access and extension loading off and its settings locked. Name the allowed tables one by one. Then list what the database can't hide, such as its catalog, and refuse those reads in the guard.
- 執行
-
為每個角色提供一個由檢視表組成的資料庫
- 執行
role-database.py - 實用原則 以資料庫作為邊界:讓代理程式以唯讀方式連線,並使用只能讀取檢視表的角色,這些檢視表限制該角色可以看到的列與欄;若資料倉儲提供安全檢視表、列存取原則與遮罩原則,就透過它們來實現。在 DuckDB 中,為每個角色提供各自的唯讀資料庫,其內容就是這些檢視表。請依據整個技術堆疊其餘部分所用的同一份原則來產生這些檢視表,並以剖析器會接受的查詢來測試它們。
- 執行
-
在執行前先剖析 SQL
- 執行
parse-agent-sql.py - 實用原則 在任何東西執行之前,先以將執行該查詢之資料倉儲的方言,把代理程式的 SQL 剖析成樹,並且只接受恰好一個陳述式。檢查節點,絕不檢查文字。能被剖析的查詢,只代表其結構現在可以檢視,並不代表它有效;在把查詢送往資料倉儲之前,要先依其可讀取的資料表來解析它的欄位。
- 執行
-
Allow only read-only queries on known tables
- 執行
block-a-delete.py - 執行
block-raw-tables.py - 實用原則 Accept only a query at the root, walk every node for a statement that writes, and allowlist tables by schema and name, with nothing but tables and subqueries in
FROM. Resolve a name without a schema as a CTE only where a CTE of that name is visible, refuse the database's catalog by name, and allow only the functions your analytics need. Check the same list the agent's database holds. When a golden question needs a table off the list, give it a mart or a metric, not a wider list.
- 執行
-
限制資料列數與成本
- 執行
explain-before-running.py - 執行
figure-plan-row-estimates.py - 實用原則 以四種方式限制每個查詢:保留代理程式所寫之較小值的資料列限制、結果大小的限制、在查詢執行前就檢查的
EXPLAIN計畫大小限制,以及涵蓋防護機制每個步驟(包括規劃)的單一期限。結果超過任一限制時應拒絕而非截斷,有步驟沒有估計值的計畫也應拒絕。計畫限制應依據自己已知正確的查詢來設定。切勿使用會執行查詢的EXPLAIN ANALYZE,並且要依賴期限與結果限制,而不是依賴估計值。
- 執行
-
Enforce the deadline by killing a process
- 執行
stop-a-slow-plan.py - 實用原則 Start the deadline when the agent's SQL arrives, and run
EXPLAINunder it as well as the query. Enforce it by killing a separate process with its own read-only connection, not with an interrupt. Check on your own database that a stopped plan and a stopped query really stop.
- 執行
-
對防護層重放攻擊
- 執行
replay-guard-bypasses.py - 實用原則 依賴資料庫自身的限制,並把剖析器視為縱深防禦。把每個被發現的攻擊都保留為測試:分別對代理的資料庫單獨重放,以及經由防護層重放,並確認每一個都由你預期的那一層拒絕。當攻擊繞過剖析器時,先問資料庫為何讓它能觸及任何東西,再去修正剖析器。
- 執行
-
Repair once from the error
- 執行
repair-from-error.py - 實用原則 Repair at most once, and only from an outside signal: the guard's refusal or the warehouse's error, passed to the model with its own SQL and a prompt that doesn't assume the SQL is wrong. Let the model decline in the repair. Score repaired answers on their own, apart from first-time answers, and if your golden questions show they are rarely right, decline on a refusal instead of repairing.
- 執行
-
Transpile to the warehouse's dialect
- 執行
transpile-to-snowflake.py - 實用原則 Let the agent write the dialect your golden questions are checked in, guard that SQL, and transpile the guard's output, not the agent's text, with errors raised for what the transpiler knows it can't translate. Drop the guard's quotes for warehouses that fold unquoted names to upper case, refuse functions the transpiler passed through, and run the golden questions on the target warehouse before you trust a transpiled answer, because a function with the same name can return different values.
- 執行
-
When to prefer a new metric over SQL
- 執行
long-tail-score.py - 實用原則 Send a question to guarded SQL only when no metric covers it, and score that path on its own long tail of golden questions. Promote a question to the semantic layer when it recurs, when its answer goes into a report, or when guarded SQL gets it wrong on a definition, as with what counts as an order. Leave one-off lookups on the guarded path. When a question needs a table off the allowlist, build the mart and its metric rather than widening the list.
- 執行
-
正式環境中會出什麼問題
- 實用原則 將角色的唯讀檢視表資料庫視為控制手段,將防護機制視為把錯誤轉成拒絕並限制成本的一層。檢查代理程式的授權,並測試每個角色的檢視表;在 CI 中讓每個黃金查詢都通過防護機制;在防護機制與倉儲上都設定時間限制;記錄每一次拒絕、估計與修復及其答案;每次變更之後,都在這條路徑自己的黃金問題長尾上為它評分。
- 本章的決定
8. 能力所不及時要明說
閱讀約需 64 分鐘,含 6 個範例。
讀完本章,你可以把「拒答」當成一種正式的回答:分辨資料倉儲答得出與答不出的問題,並以已知類型的理由拒答;檢查所問期間的資料是否已經送達;為每個回答呈現其來源脈絡(provenance);再以精確率(precision)與召回率(recall)衡量拒答,讓代理既不亂猜,也不過度拒絕。
課程大綱:7 節
-
倉儲無法回答的問題類型
- 執行
unanswerable-cases.py - 實用原則 把黃金集中每個標為拒答的問題,歸類為歧義或無法回答,並標明其類別,以便知道它需要哪一種回覆。只有在每種解讀都能回答時,才對歧義問題反問;對於無法回答的問題,則在拒答時說明缺少什麼。對每個無法回答的問題,找出附近可能被模型誤認為答案的欄位或值,因為那正是你要防範的錯誤數字。
- 執行
-
附上理由的拒答
- 執行
cannot-answer-tool.py - 實用原則 把拒答做成一個工具,以固定清單中的種類與一句給使用者看的話來結束執行。讓模型在它能判斷的種類中選擇,例如缺少資料、預測或未定義的詞彙;讓程式碼決定它能檢查的種類,例如過期的資料或被禁止的角色。依種類統計拒答,並在軌跡中閱讀理由,因為種類正確的拒答仍可能給出錯誤的理由。
- 執行
-
回答前先檢查資料新鮮度
- 執行
stale-refunds.py - 執行
figure-refund-settling.py - 實用原則 在每個指明期間的回答之前,以程式碼檢查新鮮度:將回答所讀取的每個饋送的最新載入時間,與期間結束時間加上該指標的沉澱期相比較,當饋送未達該時點時,無論是資料列尚未載入或事件尚未發生,都以 stale 拒答。每個沉澱期都從你自己的資料測量,取能涵蓋你所見事件的延遲。從你自己的載入欄位或載入日誌讀取載入時間,絕不要從資料表中繼資料讀取,並將每個資料表對應到決定其新鮮度的饋送。沒有指定期間的問題可以回答,並在出處中顯示其資料截至時間。
- 執行
-
Show the provenance with every answer
- 執行
answer-with-provenance.py - 實用原則 Show every answer with its provenance, and put the metric's name and definition first, since that's where a near-miss metric shows. Then show the filters, the period, the role and the data as of, and keep the SQL one step away for the people who check it. Show declines the same way, with their kind, reason and role.
- 執行
-
Measure declines, not only accuracy
- 執行
decline-precision-recall.py - 實用原則 Score an agent on declines as well as answers: report decline precision and recall, coverage and selective risk, and a penalty score with a c your stakeholders agree to, stated as a judgement. Compare agents on the same questions, and list the wrong numbers each gave, because a change that removes one wrong number can add another. When the decline questions number in single digits, read the runs, not only the rates.
- 執行
-
正式環境中會出什麼問題
- 實用原則 在更多人使用這個代理之前,請為新鮮度檢查建立載入紀錄,依排程重新量測每個指標的沉澱窗口,檢查每個時間序列的最後一個期間,依類別與來源統計拒答,並閱讀理由與反問的樣本。先從對可回答問題的
not_in_data拒答著手,因為每一筆都指出一個待撰寫的指標。
- 實用原則 在更多人使用這個代理之前,請為新鮮度檢查建立載入紀錄,依排程重新量測每個指標的沉澱窗口,檢查每個時間序列的最後一個期間,依類別與來源統計拒答,並閱讀理由與反問的樣本。先從對可回答問題的
- 本章做出的決定
9. 讓代理程式只能看到被允許的內容
閱讀約需 74 分鐘,含 8 個範例。
讀完本章後,你將能在代理程式之下、於各角色自己的資料庫中,強制執行每個角色的列篩選、欄位遮罩與最小群組規模;語意層中的改寫與防護機制則作為縱深防禦,使任何提示都無法擴大存取範圍。
課程大綱:10 節
-
A rule in the prompt is not a control
- 執行
prompt-rule-bypassed.py - 實用原則 Never enforce access in a prompt. Decide what a role may see in code, from the role your sign-in system gives you, and apply it to the SQL that runs. Keep access rules out of the prompt, so nobody mistakes it for the control, and test the control with questions that try every way around it: other regions by name, totals, rankings, and questions that claim an exception.
- 執行
-
Apply row filters in the compiler
- 執行
regional-lead-revenue.py - 實用原則 Attach row filters to tables, not to questions, and apply them by replacing each table a query reads with a filtered subquery, after the semantic layer compiles and after the guard checks. Write a filter for every table a filtered role can reach, reach the filter's column through a key where a table has none, and refuse tables with no filter. Refuse a filter value outside the role's rows, and record the row filter in every answer's provenance.
- 執行
-
在每一條路徑上遮蔽欄位
- 執行
masked-emails.py - 實用原則 要遮蔽欄位,應替換它所在的資料表:在角色的資料庫中使用檢視,在改寫中使用子查詢,在該欄位的位置回傳遮罩,絕不要改寫查詢所選取的欄位。讓個人資料欄位遠離語意層。角色不應知道其存在的欄位,就拒絕它;角色可以知道其存在但不能讀取的欄位,就遮蔽它。
- 執行
-
隱藏小到足以識別個人的群組
- 執行
small-group-suppressed.py - 實用原則 對每個可以查看個人彙總、但不能查看個人本身的角色,都要設定最小群組規模,並將它記錄為政策選擇及其理由:統計機關對金額表格使用 3,而更大的數字也無法阻止跨答案的相減。用指標自己的篩選條件計算每個群組的相異人數,隱藏所有低於最小值的群組的值,並拒絕提供資料列無法追溯到個人的指標。
- 執行
-
兩個答案相減即可還原一個群組
- 先詢問 Before you run the example, predict whether the policy lets the second answer through, and what subtracting the first answer from it gives.
- 執行
difference-two-answers.py - 實用原則 小群組隱藏只能保護單一答案,不能保護某個人在多個答案之間的資訊。在小群組有影響的情況下,應將代理程式限制在一併規劃好的細分,或擾動數值,並檢視稽核紀錄,找出同一人提出的重疊問題。
-
將資料中的文字視為資料
- 執行
injected-product-name.py - 實用原則 將模型讀取的每個值都視為不受信任,且絕不讓模型的文字敘述成為答案的唯一形式。回傳附有來源資訊的表格,只在程式碼對照表格檢查過之後才顯示摘要,並讓角色與政策遠離模型可以寫入的任何內容。在提示中把資料標示為資料,並不是一種控制措施。
- 執行
-
以黃金問題衡量存取控制
- 執行
role-scope-score.py - 實用原則 每次變更政策,都要以指明提問者角色的黃金問題評分,且兩個資料切分中都要有禁止類問題,並檢視每個問題是如何被拒絕的,而不只看是否被拒絕。要決定應告知角色「有規則適用」,還是只告知「沒有答案」,並讓每一次拒絕都以相同方式表達。
- 執行
-
Log who asked what, and what ran
- 執行
audit-entries.py - 實用原則 Write one audit entry per question, from code, with the principal from your sign-in system, what the agent asked for, the SQL that ran after the policy, and the outcome. Keep values out of the log, protect its questions as personal data, and review it for overlapping questions and for declines a role keeps hitting.
- 執行
-
正式環境中會出什麼問題
- 實用原則 角色要從登入系統綁定,絕不從請求的參數取得;在 CI 中以資料倉儲的資料表測試政策檔;政策只保留一份單一事實來源;維度的值要透過政策列出;每次政策變更之後,都要查閱稽核日誌中的拒絕紀錄、單一群組的回答,以及相互重疊的問題。
- 本章做出的決定
10. 評判答案並抓出退步
閱讀約需 92 分鐘,含 7 個範例。
讀完本章,你將能夠:同時以多項指標為代理程式的一次執行評分;只在結果無法直接比較時,才以模型擔任評判;對照任何人都能重新計算的參考標籤來校準該評判;觀察分數在不同次執行之間的變動幅度;並以錄製下來的執行結果,在 CI 中為提示詞、脈絡或指標的變更設下關卡。
課程大綱:8 節
-
Score a run on more than accuracy
- 執行
scorecard.py - 執行
figure-accuracy-intervals.py - 實用原則 Report every run as a scorecard: correct with a Wilson interval, wrong numbers, declines and their recall and precision, failures by kind, and calls and tokens per question. Compare agents on the same questions in the same table. Score the test split once, when you report a final number, and say that you did; when two agents' intervals overlap, don't call either better from the totals.
- 執行
-
Judge only what can't be compared
- 執行
judge-an-explanation.py - 實用原則 Use a judge only for what can't be compared with a result, such as reasons, explanations and questions back, and keep
results_match()for every number. Judge each pair in both orders and count the verdicts that change, record every verdict, and read a sample of the judge's reasons, not only its labels. Don't let a judge's preference between two texts decide a release until you have measured that judge on cases with known answers.
- 執行
-
Calibrate the judge against reference labels
- 執行
judge-agreement.py - 實用原則 Before you use a judge on text, measure it on cases whose answers a check can label, with labels anyone can recompute. Report observed agreement with Cohen's kappa and Scott's pi, split by the kinds of answer you'll judge, list the errors in each direction, and say how far one case moves kappa when the cases number a few dozen. Thresholds such as 0.8 are conventions, not results (Artstein and Poesio); at a kappa like the one measured here, use the judge to choose what a person reads, never to pass or fail a release on its own.
- 執行
-
Repeat runs to see the variance
- 執行
seed-variance.py - 實用原則 Run each golden question several times at the sampling settings you serve, with seeds pinned in the recording's manifest, and confirm once that a seed replays exactly. Report per-question flips, not only the spread of the total, because a total can stay put while questions trade places. Never lower the temperature to make runs agree, unless that is the setting you will serve.
- 執行
-
在 CI 中把關變更
- 執行
regression-gate.py - 實用原則 對提示詞、上下文或指標的變更進行把關時,應依據多次重複執行下的逐題配對差異,而不是總分之差。只要出現任何穩定破壞,或平均差異的區間低於零,就判定失敗;把這兩條規則寫下來,說明它們是你的選擇,並在你的資料集上算出它們能偵測的最小變化。閘門失敗時,先閱讀有變動的題目,再去動規則。
- 執行
-
Catch a schema change before users do
- 執行
renamed-column-caught.py - 實用原則 Run the golden SQL, every metric and every recorded replay in CI on changes to the warehouse as well as to the agent, and treat
MissingRecordingas a failure, never as a reason to skip a question. When a check fails after a schema change, fix the definition in the semantic layer before you record anything again.
- 執行
-
正式環境中會出什麼問題
- 實用原則 在關卡守護正式環境的代理之前,應把其評審的校準案例與評審提示放在一起,並在任一項變動時重新量測;向評審呈現它能夠查核的內容;在實際提供服務的設定下錄製多次重複執行;與對這些數字負責的人議定關卡的規則;並在每份清單中計算某個分割被錄製的次數。
- 本章做出的決策
11. 透過 MCP 提供語意層服務
閱讀約需 66 分鐘,含 5 個範例。
讀完本章,你將能夠把商店受治理的指標以 MCP 工具的形式提供出來,讓這些工具針對伺服器建置時所設定的角色作答,並以結構化資料回傳每個答案及其來源(provenance);你也能在記憶體內測試該伺服器,並以主機(host)的身分透過它執行本書的代理程式(agent)。
課程大綱:7 節
-
Turn metrics into tools
- 執行
list-metric-tools.py - 實用原則 Serve the tools your agent was measured with, each one a thin call into the same code the in-process agent uses, so that the server can't drift from what you tested. List them in a fixed order and mark them read-only. Let the role change what a tool returns, and shorten the tool list only when a role can't use a tool at all.
- 執行
-
將呼叫者的角色帶入每一次查詢
- 執行
role-bound-at-build.py - 實用原則 在人員通過驗證的地方綁定角色,絕不要放在工具的引數中。對於 stdio 伺服器,由主機在環境中傳入角色,伺服器開啟該角色的唯讀資料庫,並只建置一次其政策。對於 HTTP 伺服器,在每個請求中,從為此伺服器簽發的已驗證權杖取得角色,並且無論權杖的範圍怎麼說,都要在資料庫與政策中強制執行該角色的列。讓
role不出現在任何輸入結構描述中,這樣模型或問題就沒有可填入的東西;並記錄工具忽略的任何引數,優先記錄role,因為這類引數是模型所讀文字正在操控它的跡象(將工單文字視為不可信的資料)。
- 執行
-
回傳附帶出處的答案
- 執行
tool-result-with-provenance.py - 實用原則 讓每個工具都回傳 pydantic 模型,使每個答案都以結構化內容傳遞,並附帶資料列、角色、篩選條件與資料的新鮮度,讓主機端的程式碼可以檢查它們。對於模型能夠修正的請求,拋出工具錯誤,並在訊息中說明如何修正。對於存取上的拒絕,則以拒絕的答案回傳並附上理由,讓主機結束這次執行,而不是讓模型嘗試其他途徑。
- 執行
-
在記憶體中測試伺服器
- 執行
semantic-server-tests.py - 實用原則 透過記憶體內的用戶端測試每個角色的規則,每個角色一台伺服器,並對結構化回答進行斷言,同時加入一項在綁定錯誤時會失敗的檢查。以黃金問題的結果作為預期值,使測試與分數對何謂正確有一致的看法。凡是記憶體內的用戶端無法送出的內容,都要補上透過 HTTP 的測試,權杖優先。
- 執行
-
Answer questions through a host
- 執行
host-asks-the-server.py - 實用原則 Put the server under the host you already measured, and score it on the golden questions before anyone uses it, because the transport can change what the model reads. Compare outcomes question by question with the in-process runs, and read every run that changed. Record what the new text produces rather than editing the server's messages to match old recordings, and show people the structured answer with its provenance.
- 執行
-
正式環境中會出什麼問題
- 實用原則 每個人各執行一個 stdio 伺服器,或使用 HTTP 並依每個請求的權杖決定角色,對政策檔不認得的任何宣告都以封閉方式失敗。查詢時使用該角色專屬的唯讀資料庫,而非共用的資料庫。固定 SDK 版本,並在它變動時透過主機重跑黃金問題集。每個主機中,每個語意層只保留一個伺服器,為每個回答記錄稽核項目,並檢視是否有看似由多人共用的主體。
- 本章所做的決定
測驗題庫
每章皆附習題與解答,供經驗證的教育工作者使用。每個解答都對應其所屬的節。 如何取得
正在載入測驗題庫…