phycomp commited on
Commit
de4cd47
·
verified ·
1 Parent(s): a48c190

deploy: AI/ML 應用工程主題 (2026-11-12 10:55)

Browse files
00_主題總覽.html ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
2
+ <title>00 · AI/ML 應用工程主題總覽 — AI/ML 應用工程主題</title>
3
+ <style>
4
+ :root{--bg:#0b1020;--panel:#121a2e;--panel2:#0f1628;--text:#e6edf7;--muted:#9fb0c8;--accent:#7aa2ff;--accent2:#4fd1c5;--border:#22304d;--code:#0a1222}
5
+ *{box-sizing:border-box} html,body{margin:0;padding:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Noto Sans TC","PingFang TC","Microsoft JhengHei",sans-serif;line-height:1.7}
6
+ a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
7
+ .layout{display:flex;min-height:100vh}
8
+ nav{width:290px;min-width:240px;background:var(--panel2);border-right:1px solid var(--border);padding:18px 14px;position:sticky;top:0;height:100vh;overflow:auto}
9
+ nav h2{font-size:15px;margin:0 0 10px 0;color:var(--muted);font-weight:600;letter-spacing:.02em}
10
+ nav .brand{font-size:17px;font-weight:700;color:var(--text);margin-bottom:14px;display:block}
11
+ nav ul{list-style:none;padding:0;margin:0}
12
+ nav li{margin:4px 0}
13
+ nav a{display:block;padding:6px 8px;border-radius:8px;color:var(--text);font-size:14px}
14
+ nav a:hover{background:rgba(122,162,255,.12);text-decoration:none}
15
+ nav a.active{background:rgba(122,162,255,.18);color:#fff}
16
+ main{flex:1;max-width:1100px;padding:28px 34px 60px}
17
+ h1{font-size:30px;margin:0 0 8px} h2{font-size:22px;margin:28px 0 10px;border-bottom:1px solid var(--border);padding-bottom:4px}
18
+ h3{font-size:18px;margin:22px 0 8px} p{margin:10px 0}
19
+ blockquote{margin:10px 0;padding:8px 14px;border-left:4px solid var(--accent);background:rgba(122,162,255,.08);color:var(--muted);border-radius:6px}
20
+ code{background:var(--code);padding:1px 6px;border-radius:5px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.92em}
21
+ pre{background:var(--code);border:1px solid var(--border);border-radius:10px;padding:14px;overflow:auto}
22
+ pre code{background:none;padding:0}
23
+ .table-wrap{overflow-x:auto;margin:14px 0;border:1px solid var(--border);border-radius:10px}
24
+ table{border-collapse:collapse;width:100%;min-width:520px;font-size:14px}
25
+ th,td{border-bottom:1px solid var(--border);padding:9px 12px;text-align:left;vertical-align:top}
26
+ th{background:#16223a;color:#dfe8f5;font-weight:600;position:sticky;top:0}
27
+ tr:nth-child(even) td{background:rgba(255,255,255,.02)}
28
+ tr:hover td{background:rgba(122,162,255,.06)}
29
+ ul,ol{padding-left:22px} li{margin:4px 0}
30
+ .cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:14px;margin-top:18px}
31
+ .card{background:var(--panel);border:1px solid var(--border);border-radius:12px;padding:14px}
32
+ .card h3{margin:0 0 6px;font-size:16px;border:0} .card p{margin:0;color:var(--muted);font-size:14px}
33
+ .badge{display:inline-block;background:rgba(79,209,197,.15);color:var(--accent2);padding:2px 8px;border-radius:999px;font-size:12px;margin-bottom:14px}
34
+ @media(max-width:900px){.layout{flex-direction:column} nav{position:relative;width:auto;height:auto;border-right:0;border-bottom:1px solid var(--border)} main{padding:18px}}
35
+ </style></head><body><div class="layout"><nav><a class="brand" href="index.html">AI/ML 應用工程主題</a><h2>文檔導覽</h2><ul><li><a href="index.html" class="">首頁</a></li><li><a href="00_主題總覽.html" class="active">00 · AI/ML 應用工程主題總覽</a></li><li><a href="01_本地LLM部署與推理優化.html" class="">01 · 本地 LLM 部署與推理優化</a></li><li><a href="02_模型選型與成本算力.html" class="">02 · 模型選型與成本算力</a></li><li><a href="03_RAG與向量資料庫.html" class="">03 · RAG 與向量資料庫</a></li><li><a href="04_Agent框架與工具呼叫.html" class="">04 · Agent 框架與工具呼叫</a></li><li><a href="05_微調與對齊流程.html" class="">05 · 微調與對齊流程</a></li><li><a href="06_ML工具鏈與實驗管理.html" class="">06 · ML 工具鏈與實驗管理</a></li><li><a href="07_評估與可觀測性.html" class="">07 · 評估與可觀測性</a></li><li><a href="08_上線架構與維運成本.html" class="">08 · 上線架構與維運成本</a></li></ul></nav><main><article><h1 id="00-aiml">00 · AI/ML 應用工程主題總覽</h1>
36
+ <blockquote>
37
+ <p>來源:本機技能沉澱(無單一原始文件;以官方文件與實務經驗綜合)
38
+ 網路充實:2026-11-12(LLM 推理引擎、MCP 規格、可觀測性 OTel GenAI、微調/部署工具鏈現況)</p>
39
+ </blockquote>
40
+ <hr />
41
+ <h2 id="llm-rag-agent">一、子主題地圖(應用工程面:LLM / RAG / Agent)</h2>
42
+ <p>本主題聚焦「把模型用起來」而非訓練理論:從本地推理、成本選型、檢索增強、代理編排、微調對齊、實驗工具、評估監控到上線維運,形成一條可落地的工程鏈。</p>
43
+ <table>
44
+ <thead>
45
+ <tr>
46
+ <th>編號</th>
47
+ <th>子主題</th>
48
+ <th>核心問題</th>
49
+ <th>對應檔案</th>
50
+ </tr>
51
+ </thead>
52
+ <tbody>
53
+ <tr>
54
+ <td>01</td>
55
+ <td>本地 LLM 部署與推理優化</td>
56
+ <td>怎麼在自有算力上跑得快、省、穩?</td>
57
+ <td><code>01_本地LLM部署與推理優化.md</code></td>
58
+ </tr>
59
+ <tr>
60
+ <td>02</td>
61
+ <td>模型選型與成本算力</td>
62
+ <td>選開源/閉源、API/自架、長 context 的真實成本?</td>
63
+ <td><code>02_模型選型與成本算力.md</code></td>
64
+ </tr>
65
+ <tr>
66
+ <td>03</td>
67
+ <td>RAG 與向量資料庫</td>
68
+ <td>如何讓回答有據可查、檢索品質可量測?</td>
69
+ <td><code>03_RAG與向量資料庫.md</code></td>
70
+ </tr>
71
+ <tr>
72
+ <td>04</td>
73
+ <td>Agent 框架與工具呼叫</td>
74
+ <td>何時需要 agent、如何控步數與可靠度?</td>
75
+ <td><code>04_Agent框架與工具呼叫.md</code></td>
76
+ </tr>
77
+ <tr>
78
+ <td>05</td>
79
+ <td>微調與對齊流程</td>
80
+ <td>什麼情況才該微調、LoRA/DPO 怎麼做?</td>
81
+ <td><code>05_微調與對齊流程.md</code></td>
82
+ </tr>
83
+ <tr>
84
+ <td>06</td>
85
+ <td>ML 工具鏈與實驗管理</td>
86
+ <td>環境、追蹤、版本、服務框架怎麼選?</td>
87
+ <td><code>06_ML工具鏈與實驗管理.md</code></td>
88
+ </tr>
89
+ <tr>
90
+ <td>07</td>
91
+ <td>評估與可觀測性</td>
92
+ <td>如何用 golden set + trace 證明「真的變好」?</td>
93
+ <td><code>07_評估與可觀測性.md</code></td>
94
+ </tr>
95
+ <tr>
96
+ <td>08</td>
97
+ <td>上線架構與維運成本</td>
98
+ <td>部署拓撲、快取、限流、安全與 GPU 經濟學</td>
99
+ <td><code>08_上線架構與維運成本.md</code></td>
100
+ </tr>
101
+ </tbody>
102
+ </table>
103
+ <h2 id="_1">二、快速決策路由</h2>
104
+ <table>
105
+ <thead>
106
+ <tr>
107
+ <th>你遇到的情境</th>
108
+ <th>優先讀</th>
109
+ <th>關鍵決策點</th>
110
+ </tr>
111
+ </thead>
112
+ <tbody>
113
+ <tr>
114
+ <td>想在 4090 / Mac 上跑 7B–70B</td>
115
+ <td>01, 02</td>
116
+ <td>量化格式、KV cache/VRAM 估算、吞吐 vs 延遲</td>
117
+ </tr>
118
+ <tr>
119
+ <td>API 帳單暴增或要估預算</td>
120
+ <td>02, 08</td>
121
+ <td>token 經濟學、快取折扣、agent 多輪放大、語意快取</td>
122
+ </tr>
123
+ <tr>
124
+ <td>回答常幻覺、需要引用來源</td>
125
+ <td>03, 07</td>
126
+ <td>chunking + hybrid search + rerank、recall@k 與回歸測試</td>
127
+ </tr>
128
+ <tr>
129
+ <td>想讓模型會查資料/操作工具</td>
130
+ <td>04, 08</td>
131
+ <td>先單 agent + 工具,再談多 agent;步數/成本上限、權限最小化</td>
132
+ </tr>
133
+ <tr>
134
+ <td>模型格式或語氣不穩</td>
135
+ <td>05, 03</td>
136
+ <td>先 prompt/RAG,確認是行為問題再 SFT/LoRA/DPO</td>
137
+ </tr>
138
+ <tr>
139
+ <td>線上品質忽好忽壞</td>
140
+ <td>07, 08</td>
141
+ <td>trace、TTFT/TPOT、漂移偵測、fallback 與降級</td>
142
+ </tr>
143
+ <tr>
144
+ <td>要把原型變成服務</td>
145
+ <td>06, 08</td>
146
+ <td>FastAPI+推理服務分離、容器、CI smoke test、監控告警</td>
147
+ </tr>
148
+ </tbody>
149
+ </table>
150
+ <h2 id="_2">三、子主題摘要</h2>
151
+ <h3 id="01-llm">01 本地 LLM 部署與推理優化</h3>
152
+ <p>比較 llama.cpp/Ollama 與 vLLM/SGLang/LMDeploy 的吞吐與易用性取捨;整理 GGUF/AWQ/GPTQ/FP8 量化、KV cache 估算、7B–70B VRAM 需求與投機解碼。<strong>結論:互動低併發偏 llama.cpp/Ollama,高併發服務偏 vLLM 類引擎;先量好瓶頸再換硬體。</strong></p>
153
+ <h3 id="02">02 模型選型與成本算力</h3>
154
+ <p>建立開源 vs 閉源選型矩陣與 token 成本模型(含快取、batch、長 context 非線性)。<strong>結論:優先便宜手段(prompt/RAG)再微調/換大模型;長 context 不保證有效 recall。</strong></p>
155
+ <h3 id="03-rag">03 RAG 與向量資料庫</h3>
156
+ <p>覆蓋解析→chunking→embedding→索引→檢索→rerank 全管線,比較 pgvector/Qdrant/Milvus/Chroma 等與 HNSW/IVF。<strong>結論:混合檢索+重排通常比單純換 embedding 更有效;評估要有 golden set。</strong></p>
157
+ <h3 id="04-agent">04 Agent 框架與工具呼叫</h3>
158
+ <p>說明 function calling、ReAct/plan-and-execute、記憶與 MCP(tools/resources/prompts)。<strong>結論:能不用框架就不用;先控步數、工具描述、驗證與人機協作,再談多 agent。</strong></p>
159
+ <h3 id="05">05 微調與對齊流程</h3>
160
+ <p>釐清 SFT/DPO/ORPO/KTO 適用邊界、LoRA/QLoRA 實務超參數、資料品質與蒸餾風險。<strong>結論:知識缺口先 RAG,格式/風格再 SFT,偏好對齊再 DPO;資料品質重於數量。</strong></p>
161
+ <h3 id="06-ml">06 ML 工具鏈與實驗管理</h3>
162
+ <p>整理 uv/conda、W&amp;B/MLflow、DVC/HF、FastAPI+vLLM/Triton/BentoML、Docker 與 ML CI/CD。<strong>結論:先求可重現(環境+資料+實驗紀錄),再追求花俏平台。</strong></p>
163
+ <h3 id="07">07 評估與可觀測性</h3>
164
+ <p>建立 golden set、LLM-as-a-judge 偏誤認知、trace 與 TTFT/TPOT/成本監控、漂移與 A/B 測試。<strong>結論:沒有離線回歸+線上觀測,就沒有「品質提升」這回事。</strong></p>
165
+ <h3 id="08">08 上線架構與維運成本</h3>
166
+ <p>討論部署拓撲、連續批次、快取/限流/降級、安全(prompt injection/PII/最小權限)與 GPU 租買經濟學。<strong>結論:可靠性來自隔離、上限、回滾與可觀測,不來自單一最強模型。</strong></p>
167
+ <h2 id="_3">四、跨子主題共識</h2>
168
+ <table>
169
+ <thead>
170
+ <tr>
171
+ <th>共識</th>
172
+ <th>說明</th>
173
+ </tr>
174
+ </thead>
175
+ <tbody>
176
+ <tr>
177
+ <td>先量測再優化</td>
178
+ <td>沒有 trace/eval 的調參多半是錯覺(07、08)</td>
179
+ </tr>
180
+ <tr>
181
+ <td>先便宜後昂貴</td>
182
+ <td>prompt/RAG/快取 → 微調 → 換大模型 → 自架大叢集(02、03、05)</td>
183
+ </tr>
184
+ <tr>
185
+ <td>簡單優先</td>
186
+ <td>單次呼叫 &gt; 單 agent &gt; 多 agent;單機可解不先上 K8s(04、08)</td>
187
+ </tr>
188
+ <tr>
189
+ <td>資料與評測是資產</td>
190
+ <td>golden set、資料版本、失敗案例庫比換一次模型更保值(03、06、07)</td>
191
+ </tr>
192
+ <tr>
193
+ <td>安全與成本是功能</td>
194
+ <td>權限、配額、注入防護、成本告警要當一等公民設計(04、07、08)</td>
195
+ </tr>
196
+ </tbody>
197
+ </table>
198
+ <h2 id="_4">五、使用建議</h2>
199
+ <ul>
200
+ <li><strong>落地順序建議</strong>:02(選型/預算)→ 03 或 01(RAG 或本地推理)→ 04(工具化)→ 07(評估監控)→ 08(上線硬化);05 僅在 03/04 無法滿足時介入。</li>
201
+ <li><strong>本主題邊界</strong>:偏應用工程與維運決策,不深入訓練數學(Transformer 推導、RLHF 理論細節)。</li>
202
+ <li><strong>時效提醒</strong>:模型牌價、GPU 租金、框架版本與 MCP/OTel 規範變動快;各檔「📅 網路充實」標 2026-11-12,引用前請重查官方來源。</li>
203
+ <li><strong>免責</strong>:文中成本與效能數字多為量級估算或特定條件示例,實際結果依工作負載、模型版本與地區而異,不構成採購或容量承諾。</li>
204
+ </ul></article><p style="margin-top:28px;color:var(--muted);font-size:13px"><a href="index.html">← 回首頁</a></p></main></div></body></html>
01_本地LLM部署與推理優化.html ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
2
+ <title>01 · 本地 LLM 部署與推理優化 — AI/ML 應用工程主題</title>
3
+ <style>
4
+ :root{--bg:#0b1020;--panel:#121a2e;--panel2:#0f1628;--text:#e6edf7;--muted:#9fb0c8;--accent:#7aa2ff;--accent2:#4fd1c5;--border:#22304d;--code:#0a1222}
5
+ *{box-sizing:border-box} html,body{margin:0;padding:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Noto Sans TC","PingFang TC","Microsoft JhengHei",sans-serif;line-height:1.7}
6
+ a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
7
+ .layout{display:flex;min-height:100vh}
8
+ nav{width:290px;min-width:240px;background:var(--panel2);border-right:1px solid var(--border);padding:18px 14px;position:sticky;top:0;height:100vh;overflow:auto}
9
+ nav h2{font-size:15px;margin:0 0 10px 0;color:var(--muted);font-weight:600;letter-spacing:.02em}
10
+ nav .brand{font-size:17px;font-weight:700;color:var(--text);margin-bottom:14px;display:block}
11
+ nav ul{list-style:none;padding:0;margin:0}
12
+ nav li{margin:4px 0}
13
+ nav a{display:block;padding:6px 8px;border-radius:8px;color:var(--text);font-size:14px}
14
+ nav a:hover{background:rgba(122,162,255,.12);text-decoration:none}
15
+ nav a.active{background:rgba(122,162,255,.18);color:#fff}
16
+ main{flex:1;max-width:1100px;padding:28px 34px 60px}
17
+ h1{font-size:30px;margin:0 0 8px} h2{font-size:22px;margin:28px 0 10px;border-bottom:1px solid var(--border);padding-bottom:4px}
18
+ h3{font-size:18px;margin:22px 0 8px} p{margin:10px 0}
19
+ blockquote{margin:10px 0;padding:8px 14px;border-left:4px solid var(--accent);background:rgba(122,162,255,.08);color:var(--muted);border-radius:6px}
20
+ code{background:var(--code);padding:1px 6px;border-radius:5px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.92em}
21
+ pre{background:var(--code);border:1px solid var(--border);border-radius:10px;padding:14px;overflow:auto}
22
+ pre code{background:none;padding:0}
23
+ .table-wrap{overflow-x:auto;margin:14px 0;border:1px solid var(--border);border-radius:10px}
24
+ table{border-collapse:collapse;width:100%;min-width:520px;font-size:14px}
25
+ th,td{border-bottom:1px solid var(--border);padding:9px 12px;text-align:left;vertical-align:top}
26
+ th{background:#16223a;color:#dfe8f5;font-weight:600;position:sticky;top:0}
27
+ tr:nth-child(even) td{background:rgba(255,255,255,.02)}
28
+ tr:hover td{background:rgba(122,162,255,.06)}
29
+ ul,ol{padding-left:22px} li{margin:4px 0}
30
+ .cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:14px;margin-top:18px}
31
+ .card{background:var(--panel);border:1px solid var(--border);border-radius:12px;padding:14px}
32
+ .card h3{margin:0 0 6px;font-size:16px;border:0} .card p{margin:0;color:var(--muted);font-size:14px}
33
+ .badge{display:inline-block;background:rgba(79,209,197,.15);color:var(--accent2);padding:2px 8px;border-radius:999px;font-size:12px;margin-bottom:14px}
34
+ @media(max-width:900px){.layout{flex-direction:column} nav{position:relative;width:auto;height:auto;border-right:0;border-bottom:1px solid var(--border)} main{padding:18px}}
35
+ </style></head><body><div class="layout"><nav><a class="brand" href="index.html">AI/ML 應用工程主題</a><h2>文檔導覽</h2><ul><li><a href="index.html" class="">首頁</a></li><li><a href="00_主題總覽.html" class="">00 · AI/ML 應用工程主題總覽</a></li><li><a href="01_本地LLM部署與推理優化.html" class="active">01 · 本地 LLM 部署與推理優化</a></li><li><a href="02_模型選型與成本算力.html" class="">02 · 模型選型與成本算力</a></li><li><a href="03_RAG與向量資料庫.html" class="">03 · RAG 與向量資料庫</a></li><li><a href="04_Agent框架與工具呼叫.html" class="">04 · Agent 框架與工具呼叫</a></li><li><a href="05_微調與對齊流程.html" class="">05 · 微調與對齊流程</a></li><li><a href="06_ML工具鏈與實驗管理.html" class="">06 · ML 工具鏈與實驗管理</a></li><li><a href="07_評估與可觀測性.html" class="">07 · 評估與可觀測性</a></li><li><a href="08_上線架構與維運成本.html" class="">08 · 上線架構與維運成本</a></li></ul></nav><main><article><h1 id="01-llm">01 · 本地 LLM 部署與推理優化</h1>
36
+ <blockquote>
37
+ <p>來源:本機技能沉澱(無單一原始文件;以官方文件與實務經驗綜合)
38
+ 網路充實:2026-11-12(vLLM 0.33 / SGLang 0.5.23 / LMDeploy 0.18 的功能邊界、TGI 封存時點、GGUF K-quant 實測 bpw 與困惑度)</p>
39
+ </blockquote>
40
+ <h2 id="_1">一、先分「單人用」與「多人用」</h2>
41
+ <table>
42
+ <thead>
43
+ <tr>
44
+ <th>引擎</th>
45
+ <th>殺手級功能</th>
46
+ <th>適用</th>
47
+ </tr>
48
+ </thead>
49
+ <tbody>
50
+ <tr>
51
+ <td>llama.cpp</td>
52
+ <td>GGUF、CPU/Metal、部分卸載</td>
53
+ <td>單機、Mac、無獨顯</td>
54
+ </tr>
55
+ <tr>
56
+ <td>Ollama</td>
57
+ <td>llama.cpp 包裝+模型管理</td>
58
+ <td>原型、內部小工具</td>
59
+ </tr>
60
+ <tr>
61
+ <td>vLLM</td>
62
+ <td>PagedAttention、continuous batching、APC 前綴快取</td>
63
+ <td>生產預設,模型支援最廣</td>
64
+ </tr>
65
+ <tr>
66
+ <td>SGLang</td>
67
+ <td>RadixAttention(token 級基數樹共享前綴)</td>
68
+ <td>多輪、RAG、agent 共用長 system prompt</td>
69
+ </tr>
70
+ <tr>
71
+ <td>LMDeploy</td>
72
+ <td>TurboMind C++、INT4 KV cache</td>
73
+ <td>量化單卡、Qwen 系列</td>
74
+ </tr>
75
+ <tr>
76
+ <td>TensorRT-LLM</td>
77
+ <td>NVIDIA 原生 kernel</td>
78
+ <td>願付編譯成本換峰值</td>
79
+ </tr>
80
+ </tbody>
81
+ </table>
82
+ <p>TGI 已於 2025-12 轉維護模式、2026-03 封存,新專案直接跳過。選型關鍵不是「誰最快」,而是「你的流量是否共享前綴」:獨立 prompt 下 vLLM 與 SGLang 在 70B 級差距只有幾個百分點;共享前綴重(多輪、RAG、工具定義很長)時 SGLang 的命中率明顯較高。</p>
83
+ <h2 id="_2">二、量化:位元數決定一切</h2>
84
+ <table>
85
+ <thead>
86
+ <tr>
87
+ <th>格式</th>
88
+ <th>實際 bpw</th>
89
+ <th>8B 檔案</th>
90
+ <th>困惑度增幅(8B 參考)</th>
91
+ </tr>
92
+ </thead>
93
+ <tbody>
94
+ <tr>
95
+ <td>Q4_K_M</td>
96
+ <td>4.89</td>
97
+ <td>4.6 GiB</td>
98
+ <td>+3% 左右,一般任務無感</td>
99
+ </tr>
100
+ <tr>
101
+ <td>Q5_K_M</td>
102
+ <td>5.70</td>
103
+ <td>5.3 GiB</td>
104
+ <td>+1% 左右,寫程式/JSON 較穩</td>
105
+ </tr>
106
+ <tr>
107
+ <td>Q6_K</td>
108
+ <td>6.57</td>
109
+ <td>6.1 GiB</td>
110
+ <td>+0.4%</td>
111
+ </tr>
112
+ <tr>
113
+ <td>Q8_0</td>
114
+ <td>8.50</td>
115
+ <td>8.0 GiB</td>
116
+ <td>幾乎無損,但慢、佔記憶體</td>
117
+ </tr>
118
+ <tr>
119
+ <td>AWQ / GPTQ 4-bit</td>
120
+ <td>~4.25</td>
121
+ <td>—</td>
122
+ <td>需 GPU;AWQ 在指令跟隨略優於 GPTQ</td>
123
+ </tr>
124
+ <tr>
125
+ <td>FP8</td>
126
+ <td>8</td>
127
+ <td>—</td>
128
+ <td>H100 級原生支援,品質接近 FP16</td>
129
+ </tr>
130
+ </tbody>
131
+ </table>
132
+ <p>結論:<strong>預設 Q4_K_M,寫程式或結構化輸出升 Q5_K_M,低於 3 bit 只在「跑得動比跑得好重要」時用</strong>。K-quant 的 M 版會把部分 attention/FFN 張量留在 6 bit,這是同檔案大小下品質較好的原因。模型越大,量化容忍度越高:70B Q4 通常比 32B Q5 更值得。</p>
133
+ <h2 id="kv-cache">三、KV cache 是記憶體殺手</h2>
134
+ <p>每 token 的 KV 大小=<code>2 × 層數 × KV head 數 × head_dim × 位元組數</code>。</p>
135
+ <ul>
136
+ <li>Llama-3-8B(32 層、8 KV head、128):FP16 下約 <strong>128 KiB/token</strong>,8K context 約 1 GiB。</li>
137
+ <li>70B(80 層):約 <strong>320 KiB/token</strong>,8K 約 2.5 GiB;32K 就吃掉 10 GiB。</li>
138
+ <li>KV cache 量化成 FP8/INT8 可再砍半,品質損失通常小於權重 4-bit。</li>
139
+ </ul>
140
+ <p>vLLM 兩個旋鈕:<code>gpu_memory_utilization</code>(0.85–0.92,留空間給 CUDA graph 與碎片)、<code>max_model_len</code>(别直接設模型上限,會拖慢啟動並浪費 KV 區)。<code>max_num_seqs</code> 換吞吐也換 KV;併發低時別開大。</p>
141
+ <h2 id="4-bit-8k-context">四、硬體估算(4-bit 權重+8K context)</h2>
142
+ <table>
143
+ <thead>
144
+ <tr>
145
+ <th>模型</th>
146
+ <th>權重</th>
147
+ <th>實務 VRAM</th>
148
+ <th>可跑硬體</th>
149
+ </tr>
150
+ </thead>
151
+ <tbody>
152
+ <tr>
153
+ <td>7–8B</td>
154
+ <td>~4.6 GB</td>
155
+ <td>6–7 GB</td>
156
+ <td>RTX 4060 Ti 16G、4070</td>
157
+ </tr>
158
+ <tr>
159
+ <td>14B</td>
160
+ <td>~9 GB</td>
161
+ <td>11–12 GB</td>
162
+ <td>4080、4090(游刃有餘)</td>
163
+ </tr>
164
+ <tr>
165
+ <td>32B</td>
166
+ <td>~20 GB</td>
167
+ <td>22–24 GB</td>
168
+ <td>4090 24G 勉強,context 別開滿</td>
169
+ </tr>
170
+ <tr>
171
+ <td>70B</td>
172
+ <td>~40 GB</td>
173
+ <td>44–48 GB</td>
174
+ <td>2×4090、48G+ Mac unified memory</td>
175
+ </tr>
176
+ </tbody>
177
+ </table>
178
+ <p>Mac 的 unified memory 頻寬(M4 Max 約 500 GB/s 級)決定了 decode 速度上限,好處是「大模型塞得下」,壞處是高併發吞吐遠不如資料中心 GPU。</p>
179
+ <h2 id="vs">五、吞吐 vs 延遲:兩個不同瓶頸</h2>
180
+ <ul>
181
+ <li><strong>Prefill 是算力瓶頸</strong>:長 prompt 一次算完,batch 越大越划算;chunked prefill 可讓長 prompt 不阻塞短請求。</li>
182
+ <li><strong>Decode 是記憶體頻寬瓶頸</strong>:每 token 要把權重掃一遍,所以 <code>tok/s ≈ 頻寬 ÷ 權重位元組</code>。4090(~1 TB/s)跑 8B Q4 的理論上限約 200 tok/s,實測常見 70–120。</li>
183
+ <li><strong>投機解碼</strong>(draft model、n-gram、EAGLE-3)在低 batch、memory-bound 時可加速約 1.5–3 倍;batch 大、GPU 已吃滿時收益趨近零,甚至變慢。</li>
184
+ </ul>
185
+ <h2 id="api">六、決策樹:本地還是 API</h2>
186
+ <ol>
187
+ <li>資料不能出境/有法規或合約限制 → 本地。</li>
188
+ <li>每月 token 量大且穩定(尖峰可預測)→ 本地攤提才划算。</li>
189
+ <li>需求是「偶爾跑、偶爾大模型」→ API,別為了省錢買卡。</li>
190
+ <li>需要最新 frontier 能力、長 context、多模態 → API 通常仍領先。</li>
191
+ <li>延遲要求 &lt;100 ms 且呼叫量高 → 本地+前綴快取,或 edge 小模型。</li>
192
+ <li>只是做 PoC/驗證想法 → 先用 API,別先架 GPU。</li>
193
+ </ol>
194
+ <p>混合模式最實際:<strong>本地跑高頻、低難度(分類、抽取、改寫),API 只留給難例</strong>。</p>
195
+ <h2 id="_3">實務檢查清單</h2>
196
+ <ul>
197
+ <li>[ ] 先量測真實 prompt 長度分佈,再決定 <code>max_model_len</code></li>
198
+ <li>[ ] 確認 tokenizer 與模型版本一致(換版重算 token 數)</li>
199
+ <li>[ ] 壓測看 p95 TTFT 與 ITL,不只看平均 tok/s</li>
200
+ <li>[ ] 開啟前綴快取,並確認 system prompt 放在最前面且固定</li>
201
+ <li>[ ] 多租戶情境加 <code>cache_salt</code>,避免以時間差推理他人文件</li>
202
+ <li>[ ] 量化後跑自己的 golden set,不要只看困惑度</li>
203
+ <li>[ ] 設好 OOM 前的降級路徑(縮 context/降併發)</li>
204
+ </ul>
205
+ <h2 id="_4">常見誤解</h2>
206
+ <ul>
207
+ <li>「4-bit 一定掉很多分」:Q4_K_M 在一般任務幾乎無感,真正崩的是 3 bit 以下。</li>
208
+ <li>「Q8_0 最好」:接近無損,但 decode 速度與記憶體成本都最差,多數場景不值得。</li>
209
+ <li>「context 開越大越好」:KV 線性成長,且長 context 的有效召回會下降。</li>
210
+ <li>「GPU 利用率 100% 就是快」:decode 階段本來就無法吃滿算力,那是頻寬限制。</li>
211
+ <li>「本地一定比 API 便宜」:低使用率下,折舊與電費遠高於 API 帳單。</li>
212
+ </ul></article><p style="margin-top:28px;color:var(--muted);font-size:13px"><a href="index.html">← 回首頁</a></p></main></div></body></html>
02_模型選型與成本算力.html ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
2
+ <title>02 · 模型選型與成本算力 — AI/ML 應用工程主題</title>
3
+ <style>
4
+ :root{--bg:#0b1020;--panel:#121a2e;--panel2:#0f1628;--text:#e6edf7;--muted:#9fb0c8;--accent:#7aa2ff;--accent2:#4fd1c5;--border:#22304d;--code:#0a1222}
5
+ *{box-sizing:border-box} html,body{margin:0;padding:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Noto Sans TC","PingFang TC","Microsoft JhengHei",sans-serif;line-height:1.7}
6
+ a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
7
+ .layout{display:flex;min-height:100vh}
8
+ nav{width:290px;min-width:240px;background:var(--panel2);border-right:1px solid var(--border);padding:18px 14px;position:sticky;top:0;height:100vh;overflow:auto}
9
+ nav h2{font-size:15px;margin:0 0 10px 0;color:var(--muted);font-weight:600;letter-spacing:.02em}
10
+ nav .brand{font-size:17px;font-weight:700;color:var(--text);margin-bottom:14px;display:block}
11
+ nav ul{list-style:none;padding:0;margin:0}
12
+ nav li{margin:4px 0}
13
+ nav a{display:block;padding:6px 8px;border-radius:8px;color:var(--text);font-size:14px}
14
+ nav a:hover{background:rgba(122,162,255,.12);text-decoration:none}
15
+ nav a.active{background:rgba(122,162,255,.18);color:#fff}
16
+ main{flex:1;max-width:1100px;padding:28px 34px 60px}
17
+ h1{font-size:30px;margin:0 0 8px} h2{font-size:22px;margin:28px 0 10px;border-bottom:1px solid var(--border);padding-bottom:4px}
18
+ h3{font-size:18px;margin:22px 0 8px} p{margin:10px 0}
19
+ blockquote{margin:10px 0;padding:8px 14px;border-left:4px solid var(--accent);background:rgba(122,162,255,.08);color:var(--muted);border-radius:6px}
20
+ code{background:var(--code);padding:1px 6px;border-radius:5px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.92em}
21
+ pre{background:var(--code);border:1px solid var(--border);border-radius:10px;padding:14px;overflow:auto}
22
+ pre code{background:none;padding:0}
23
+ .table-wrap{overflow-x:auto;margin:14px 0;border:1px solid var(--border);border-radius:10px}
24
+ table{border-collapse:collapse;width:100%;min-width:520px;font-size:14px}
25
+ th,td{border-bottom:1px solid var(--border);padding:9px 12px;text-align:left;vertical-align:top}
26
+ th{background:#16223a;color:#dfe8f5;font-weight:600;position:sticky;top:0}
27
+ tr:nth-child(even) td{background:rgba(255,255,255,.02)}
28
+ tr:hover td{background:rgba(122,162,255,.06)}
29
+ ul,ol{padding-left:22px} li{margin:4px 0}
30
+ .cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:14px;margin-top:18px}
31
+ .card{background:var(--panel);border:1px solid var(--border);border-radius:12px;padding:14px}
32
+ .card h3{margin:0 0 6px;font-size:16px;border:0} .card p{margin:0;color:var(--muted);font-size:14px}
33
+ .badge{display:inline-block;background:rgba(79,209,197,.15);color:var(--accent2);padding:2px 8px;border-radius:999px;font-size:12px;margin-bottom:14px}
34
+ @media(max-width:900px){.layout{flex-direction:column} nav{position:relative;width:auto;height:auto;border-right:0;border-bottom:1px solid var(--border)} main{padding:18px}}
35
+ </style></head><body><div class="layout"><nav><a class="brand" href="index.html">AI/ML 應用工程主題</a><h2>文檔導覽</h2><ul><li><a href="index.html" class="">首頁</a></li><li><a href="00_主題總覽.html" class="">00 · AI/ML 應用工程主題總覽</a></li><li><a href="01_本地LLM部署與推理優化.html" class="">01 · 本地 LLM 部署與推理優化</a></li><li><a href="02_模型選型與成本算力.html" class="active">02 · 模型選型與成本算力</a></li><li><a href="03_RAG與向量資料庫.html" class="">03 · RAG 與向量資料庫</a></li><li><a href="04_Agent框架與工具呼叫.html" class="">04 · Agent 框架與工具呼叫</a></li><li><a href="05_微調與對齊流程.html" class="">05 · 微調與對齊流程</a></li><li><a href="06_ML工具鏈與實驗管理.html" class="">06 · ML 工具鏈與實驗管理</a></li><li><a href="07_評估與可觀測性.html" class="">07 · 評估與可觀測性</a></li><li><a href="08_上線架構與維運成本.html" class="">08 · 上線架構與維運成本</a></li></ul></nav><main><article><h1 id="02">02 · 模型選型與成本算力</h1>
36
+ <blockquote>
37
+ <p>來源:本機技能沉澱(無單一原始文件;以官方文件與實務經驗綜合)
38
+ 網路充實:2026-11-12(OpenAI 牌價結構:cached input 0.1x、cache write 1.25x/2.5x、Batch 0.5x、&gt;272K 長 context 2x;開源 embedding 維度與授權)</p>
39
+ </blockquote>
40
+ <h2 id="vs">一、開源 vs 閉源:不要只看能力</h2>
41
+ <table>
42
+ <thead>
43
+ <tr>
44
+ <th>維度</th>
45
+ <th>閉源 API</th>
46
+ <th>自架開源</th>
47
+ </tr>
48
+ </thead>
49
+ <tbody>
50
+ <tr>
51
+ <td>前段能力</td>
52
+ <td>通常領先 6–12 個月</td>
53
+ <td>落後,但快追上</td>
54
+ </tr>
55
+ <tr>
56
+ <td>單位成本</td>
57
+ <td>按量計價,可預測</td>
58
+ <td>前期資本+維運人力</td>
59
+ </tr>
60
+ <tr>
61
+ <td>低使用率成本</td>
62
+ <td>極低</td>
63
+ <td>很差(折舊攤不回來)</td>
64
+ </tr>
65
+ <tr>
66
+ <td>高使用率成本</td>
67
+ <td>線性成長</td>
68
+ <td>攤提後大幅下降</td>
69
+ </tr>
70
+ <tr>
71
+ <td>資料落地</td>
72
+ <td>需合約/區域端點</td>
73
+ <td>完全自控</td>
74
+ </tr>
75
+ <tr>
76
+ <td>維運負擔</td>
77
+ <td>幾乎為零</td>
78
+ <td>GPU、驅動、升級、on-call</td>
79
+ </tr>
80
+ <tr>
81
+ <td>鎖定風險</td>
82
+ <td>高(prompt/tool 格式綁定)</td>
83
+ <td>低</td>
84
+ </tr>
85
+ </tbody>
86
+ </table>
87
+ <p>實務結論:<strong>能够用「一個抽象層+可換模型」寫的應用,就先這樣寫</strong>。把 model id、temperature、tool schema 收斂到一處設定,換模型是改一行而不是重寫。</p>
88
+ <h2 id="token">二、Token 經濟學:四個乘數決定帳單</h2>
89
+ <p>2026-11 的主流牌價結構(比率比絕對價格穩定,絕對值請查當日官網):</p>
90
+ <table>
91
+ <thead>
92
+ <tr>
93
+ <th>項目</th>
94
+ <th>相對倍率</th>
95
+ <th>說明</th>
96
+ </tr>
97
+ </thead>
98
+ <tbody>
99
+ <tr>
100
+ <td>Input(未命中快取)</td>
101
+ <td>1.0x</td>
102
+ <td>基準</td>
103
+ </tr>
104
+ <tr>
105
+ <td>Cached input(讀)</td>
106
+ <td>0.1x(部分模型 0.05x)</td>
107
+ <td>前綴完全相同才命中</td>
108
+ </tr>
109
+ <tr>
110
+ <td>Cache write(30 分)</td>
111
+ <td>1.25x</td>
112
+ <td>寫一次、讀一次即回本</td>
113
+ </tr>
114
+ <tr>
115
+ <td>Cache write(12 小時)</td>
116
+ <td>2.5x</td>
117
+ <td>低頻但長期重複的參考資料</td>
118
+ </tr>
119
+ <tr>
120
+ <td>Batch API</td>
121
+ <td>0.5x</td>
122
+ <td>24 小時內完成,可與快取疊加</td>
123
+ </tr>
124
+ <tr>
125
+ <td>長 context(&gt;272K)</td>
126
+ <td>input 2x、output 1.5x</td>
127
+ <td>非線性懲罰</td>
128
+ </tr>
129
+ </tbody>
130
+ </table>
131
+ <p>三個推論:
132
+ 1. <strong>快取要「前綴對齊」</strong>。system prompt、工具定義、檢索文件必須放前面且逐字固定;中間插一個時間戳就會全部 miss。
133
+ 2. <strong>輸出通常貴 5 倍</strong>(input : output 常見 1:5)。要求模型「長篇解釋」比「給 JSON」貴得多。
134
+ 3. <strong>Batch 與快取可疊加</strong>:離線分類、回填、 nightly 報告用 Batch+快取,可壓到牌價的一成以下。</p>
135
+ <h2 id="_1">三、四種手段的正確順序(先便宜後貴)</h2>
136
+ <ol>
137
+ <li><strong>Prompt engineering</strong>:一天內可做,改指令、給 2–3 個範例、要求結構化輸出。先做這個。</li>
138
+ <li><strong>RAG</strong>:模型不知道的私有知識。成本是索引與檢索,不動權重,可即時更新。</li>
139
+ <li><strong>Fine-tune</strong>:當「格式/語氣/分類邊界」不穩定,且你已有數百至數千筆乾淨樣本。它教「怎麼回答」,不教「新事實」。</li>
140
+ <li><strong>換模型</strong>:前四項都做完仍不夠,才升級模型或換供應商。</li>
141
+ </ol>
142
+ <p>常見錯誤是倒著做:一開始就 fine-tune,結果資料沒清乾淨,模型學會了錯誤答案的語氣。</p>
143
+ <h2 id="context">四、長 context 的迷思</h2>
144
+ <ul>
145
+ <li>「lost in the middle」:關鍵資訊放在超長 context 的中段,召回明顯低於開頭與結尾。這是架構與訓練分佈的問題,不是 prompt 寫不好。</li>
146
+ <li>塞滿 200K 的代價:input 進入長 context 倍率,且 TTFT 變長;每次呼叫都重算(除非命中快取)。</li>
147
+ <li><strong>有效 recall ≠ 名目 window</strong>。多數任務在 8K–32K 內、把資料整理好,效果勝過硬塞 200K。</li>
148
+ <li>需要「全部掃過」的任務(比對法規全文、跨文件統計)才真的該用長 context,而且要要求模型引用段落編號。</li>
149
+ </ul>
150
+ <h2 id="vs_1">五、小模型+好檢索 vs 大模型+爛檢索</h2>
151
+ <p>實務反覆驗證的結論:<strong>檢索品質的邊際報酬遠高於模型規模</strong>。喂錯資料時,大模型只會把錯誤講得更流暢、更難被發現。經驗法則:</p>
152
+ <ul>
153
+ <li>檢索 recall 低於 0.7 時,先修 chunking 與 hybrid search,別急著換模型。</li>
154
+ <li>小模型(8B–30B 級)+rerank+嚴格「不確定就說不知道」的指令,在封閉域 QA 常勝過 frontier 模型+naive top-k=3。</li>
155
+ <li>大模型的價值在開放域推理、多步工具使用、長尾語言與格式;不在「記住你的內部文件」。</li>
156
+ </ul>
157
+ <h2 id="_2">六、成本估算工作流</h2>
158
+ <ol>
159
+ <li>估每月<strong>請求數</strong>,不是 token 數(先有請求才推 token)。</li>
160
+ <li>每請求平均 input/output token(用實際日誌抽樣,別用感覺)。</li>
161
+ <li>乘上 <strong>agent 放大係數</strong>:單輪 chat 約 1.2–2x;工具型 agent 常 3–8x(每步都重送歷史);長任務可到 10x 以上。</li>
162
+ <li>乘上 <strong>retry 係數</strong>:JSON 解析失敗、驗證失敗、429 重試,實務 1.15–1.5x。</li>
163
+ <li>套牌價(含快取命中率假設,通常樂觀值 0.5–0.7)。</li>
164
+ <li>加 20% buffer,並設<strong>每日/每使用者硬上限</strong>。</li>
165
+ </ol>
166
+ <p>沒有第 3、4 項的估算,通常低估 3–5 倍。</p>
167
+ <h2 id="_3">實務檢查清單</h2>
168
+ <ul>
169
+ <li>[ ] 模型 id 與 pricing 集中在一個 config,不散落在程式各處</li>
170
+ <li>[ ] 記錄每次呼叫的 input/output/cached token,能事後歸因</li>
171
+ <li>[ ] system prompt 與���具定義放在最前且逐字固定(吃快取)</li>
172
+ <li>[ ] 離線工作一律走 Batch,不要用互動價格跑回填</li>
173
+ <li>[ ] 對「輸出長度」設上限,避免模型自言自語把帳單放大</li>
174
+ <li>[ ] 建 golden set,換模型/改 prompt 前後都跑一次</li>
175
+ <li>[ ] 設定成本告警與 hard cap,避免迴圈爆帳</li>
176
+ </ul>
177
+ <h2 id="_4">常見誤解</h2>
178
+ <ul>
179
+ <li>「context 越長越強」:長 context 的召回會衰减,成本卻是倍率跳升。</li>
180
+ <li>「fine-tune 可以注入知識」:知識會過期,且難以更新;RAG 才是知識層。</li>
181
+ <li>「大模型就不需要檢索」:錯資料餵給大模型,只會得到更有說服力的錯答案。</li>
182
+ <li>「快取是自動的,不用設計」:前綴不固定等於沒有快取。</li>
183
+ <li>「單價低就便宜」:真正的成本是 token 量 × 輪數 × 重試率。</li>
184
+ </ul></article><p style="margin-top:28px;color:var(--muted);font-size:13px"><a href="index.html">← 回首頁</a></p></main></div></body></html>
03_RAG與向量資料庫.html ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
2
+ <title>03 · RAG 與向量資料庫 — AI/ML 應用工程主題</title>
3
+ <style>
4
+ :root{--bg:#0b1020;--panel:#121a2e;--panel2:#0f1628;--text:#e6edf7;--muted:#9fb0c8;--accent:#7aa2ff;--accent2:#4fd1c5;--border:#22304d;--code:#0a1222}
5
+ *{box-sizing:border-box} html,body{margin:0;padding:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Noto Sans TC","PingFang TC","Microsoft JhengHei",sans-serif;line-height:1.7}
6
+ a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
7
+ .layout{display:flex;min-height:100vh}
8
+ nav{width:290px;min-width:240px;background:var(--panel2);border-right:1px solid var(--border);padding:18px 14px;position:sticky;top:0;height:100vh;overflow:auto}
9
+ nav h2{font-size:15px;margin:0 0 10px 0;color:var(--muted);font-weight:600;letter-spacing:.02em}
10
+ nav .brand{font-size:17px;font-weight:700;color:var(--text);margin-bottom:14px;display:block}
11
+ nav ul{list-style:none;padding:0;margin:0}
12
+ nav li{margin:4px 0}
13
+ nav a{display:block;padding:6px 8px;border-radius:8px;color:var(--text);font-size:14px}
14
+ nav a:hover{background:rgba(122,162,255,.12);text-decoration:none}
15
+ nav a.active{background:rgba(122,162,255,.18);color:#fff}
16
+ main{flex:1;max-width:1100px;padding:28px 34px 60px}
17
+ h1{font-size:30px;margin:0 0 8px} h2{font-size:22px;margin:28px 0 10px;border-bottom:1px solid var(--border);padding-bottom:4px}
18
+ h3{font-size:18px;margin:22px 0 8px} p{margin:10px 0}
19
+ blockquote{margin:10px 0;padding:8px 14px;border-left:4px solid var(--accent);background:rgba(122,162,255,.08);color:var(--muted);border-radius:6px}
20
+ code{background:var(--code);padding:1px 6px;border-radius:5px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.92em}
21
+ pre{background:var(--code);border:1px solid var(--border);border-radius:10px;padding:14px;overflow:auto}
22
+ pre code{background:none;padding:0}
23
+ .table-wrap{overflow-x:auto;margin:14px 0;border:1px solid var(--border);border-radius:10px}
24
+ table{border-collapse:collapse;width:100%;min-width:520px;font-size:14px}
25
+ th,td{border-bottom:1px solid var(--border);padding:9px 12px;text-align:left;vertical-align:top}
26
+ th{background:#16223a;color:#dfe8f5;font-weight:600;position:sticky;top:0}
27
+ tr:nth-child(even) td{background:rgba(255,255,255,.02)}
28
+ tr:hover td{background:rgba(122,162,255,.06)}
29
+ ul,ol{padding-left:22px} li{margin:4px 0}
30
+ .cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:14px;margin-top:18px}
31
+ .card{background:var(--panel);border:1px solid var(--border);border-radius:12px;padding:14px}
32
+ .card h3{margin:0 0 6px;font-size:16px;border:0} .card p{margin:0;color:var(--muted);font-size:14px}
33
+ .badge{display:inline-block;background:rgba(79,209,197,.15);color:var(--accent2);padding:2px 8px;border-radius:999px;font-size:12px;margin-bottom:14px}
34
+ @media(max-width:900px){.layout{flex-direction:column} nav{position:relative;width:auto;height:auto;border-right:0;border-bottom:1px solid var(--border)} main{padding:18px}}
35
+ </style></head><body><div class="layout"><nav><a class="brand" href="index.html">AI/ML 應用工程主題</a><h2>文檔導覽</h2><ul><li><a href="index.html" class="">首頁</a></li><li><a href="00_主題總覽.html" class="">00 · AI/ML 應用工程主題總覽</a></li><li><a href="01_本地LLM部署與推理優化.html" class="">01 · 本地 LLM 部署與推理優化</a></li><li><a href="02_模型選型與成本算力.html" class="">02 · 模型選型與成本算力</a></li><li><a href="03_RAG與向量資料庫.html" class="active">03 · RAG 與向量資料庫</a></li><li><a href="04_Agent框架與工具呼叫.html" class="">04 · Agent 框架與工具呼叫</a></li><li><a href="05_微調與對齊流程.html" class="">05 · 微調與對齊流程</a></li><li><a href="06_ML工具鏈與實驗管理.html" class="">06 · ML 工具鏈與實驗管理</a></li><li><a href="07_評估與可觀測性.html" class="">07 · 評估與可觀測性</a></li><li><a href="08_上線架構與維運成本.html" class="">08 · 上線架構與維運成本</a></li></ul></nav><main><article><h1 id="03-rag">03 · RAG 與向量資料庫</h1>
36
+ <blockquote>
37
+ <p>來源:本機技能沉澱(無單一原始文件;以官方文件與實務經驗綜合)
38
+ 網路充實:2026-11-12(pgvector 0.8.6/Qdrant 1.18/Milvus 2.6-3.0/LanceDB 0.34 版本與索引支援、Qwen3-Embedding 與 BGE-M3 規格、pgvector 2000 維索引上限)</p>
39
+ </blockquote>
40
+ <h2 id="_1">一、索引管線:八成問題出在解析</h2>
41
+ <p><code>解析 → chunk → embedding → 索引 → 檢索 → rerank → 組 prompt</code></p>
42
+ <table>
43
+ <thead>
44
+ <tr>
45
+ <th>階段</th>
46
+ <th>實務要點</th>
47
+ </tr>
48
+ </thead>
49
+ <tbody>
50
+ <tr>
51
+ <td>PDF 解析</td>
52
+ <td>先判斷是文字層還是掃描;掃描走 OCR,別直接丟 embedding</td>
53
+ </tr>
54
+ <tr>
55
+ <td>表格</td>
56
+ <td>轉成 markdown 或「欄名: 值」的句子,保留表頭;切勿讓表格變成斷行亂碼</td>
57
+ </tr>
58
+ <tr>
59
+ <td>��題階層</td>
60
+ <td>把 <code>h1 &gt; h2 &gt; h3</code> 併入 chunk 前綴,等於免費的語意提示</td>
61
+ </tr>
62
+ <tr>
63
+ <td>中繼資料</td>
64
+ <td>來源、日期、權限、章節,之後 filter 與引用都靠它</td>
65
+ </tr>
66
+ </tbody>
67
+ </table>
68
+ <h2 id="chunking">二、Chunking:沒有萬用答案,只有取捨</h2>
69
+ <table>
70
+ <thead>
71
+ <tr>
72
+ <th>策略</th>
73
+ <th>適用</th>
74
+ <th>缺點</th>
75
+ </tr>
76
+ </thead>
77
+ <tbody>
78
+ <tr>
79
+ <td>固定長度(512 token、overlap 50–80)</td>
80
+ <td>基準線、均質文本</td>
81
+ <td>切斷語意、句子斷頭</td>
82
+ </tr>
83
+ <tr>
84
+ <td>Recursive(按段落→句子遞迴切)</td>
85
+ <td>多數文件預設</td>
86
+ <td>長表格仍會爆</td>
87
+ </tr>
88
+ <tr>
89
+ <td>Semantic(相鄰句向量相似度斷點)</td>
90
+ <td>主題跳躍明顯的長文</td>
91
+ <td>成本高、結果不穩定</td>
92
+ </tr>
93
+ <tr>
94
+ <td>結構導向(按標題/條文/Q&amp;A 對)</td>
95
+ <td>法規、手冊、FAQ</td>
96
+ <td>需先有乾淨結構</td>
97
+ </tr>
98
+ </tbody>
99
+ </table>
100
+ <p>實務值:中文 chunk 300–600 字、overlap 10–15%;英文 400–800 token。<strong>先量測再調</strong>。表格與程式碼必須整塊保留,寧可超長也不要切斷。</p>
101
+ <h2 id="embedding">三、Embedding 選型</h2>
102
+ <table>
103
+ <thead>
104
+ <tr>
105
+ <th>模型</th>
106
+ <th>維度</th>
107
+ <th>最大長度</th>
108
+ <th>授權</th>
109
+ <th>備註</th>
110
+ </tr>
111
+ </thead>
112
+ <tbody>
113
+ <tr>
114
+ <td>Qwen3-Embedding-0.6B</td>
115
+ <td>1024(MRL 可截)</td>
116
+ <td>32K</td>
117
+ <td>Apache 2.0</td>
118
+ <td>多語,query 需 instruction</td>
119
+ </tr>
120
+ <tr>
121
+ <td>Qwen3-Embedding-4B / 8B</td>
122
+ <td>2560 / 4096</td>
123
+ <td>32K</td>
124
+ <td>Apache 2.0</td>
125
+ <td>品質優先,索引成本高</td>
126
+ </tr>
127
+ <tr>
128
+ <td>BGE-M3</td>
129
+ <td>1024</td>
130
+ <td>8192</td>
131
+ <td>MIT</td>
132
+ <td>同時出 dense/sparse/multi-vector,hybrid 首選</td>
133
+ </tr>
134
+ <tr>
135
+ <td>multilingual-e5-large</td>
136
+ <td>1024</td>
137
+ <td>512</td>
138
+ <td>MIT</td>
139
+ <td>需 <code>query:</code>/<code>passage:</code> 前綴</td>
140
+ </tr>
141
+ <tr>
142
+ <td>nomic-embed-text v1.5</td>
143
+ <td>768(MRL)</td>
144
+ <td>8192</td>
145
+ <td>Apache 2.0</td>
146
+ <td>CPU 友善</td>
147
+ </tr>
148
+ </tbody>
149
+ </table>
150
+ <p>三個坑:
151
+ 1. <strong>前綴不對齊</strong>:e5 要 <code>query:</code>/<code>passage:</code>,Qwen3 要 query 端 instruction、document 端不加。少寫不報錯,只會悄悄掉 1–5%。
152
+ 2. <strong>維度與索引上限</strong>:pgvector 的 <code>vector</code> 型別只能索引 2000 維、<code>halfvec</code> 4000 維。4096 維模型要 MRL 截斷或改 binary 量化。
153
+ 3. <strong>儲存成本</strong>:<code>4 × 維度 + 8</code> bytes/向量;100 萬 chunk、1024 維約 4.1 GB,還沒算 HNSW 圖。</p>
154
+ <h2 id="_2">四、向量庫:先問「你已經有什麼」</h2>
155
+ <table>
156
+ <thead>
157
+ <tr>
158
+ <th>系統</th>
159
+ <th>版本線</th>
160
+ <th>索引</th>
161
+ <th>適合</th>
162
+ </tr>
163
+ </thead>
164
+ <tbody>
165
+ <tr>
166
+ <td>pgvector</td>
167
+ <td>0.8.x</td>
168
+ <td>HNSW、IVFFlat</td>
169
+ <td>已有 Postgres、&lt; 500 萬向量,首選</td>
170
+ </tr>
171
+ <tr>
172
+ <td>Qdrant</td>
173
+ <td>1.18</td>
174
+ <td>HNSW(filterable)</td>
175
+ <td>中大型、metadata 過濾很吃重</td>
176
+ </tr>
177
+ <tr>
178
+ <td>Milvus</td>
179
+ <td>2.6 / 3.0</td>
180
+ <td>HNSW、IVF、DiskANN、GPU</td>
181
+ <td>上億向量、願意養 K8s</td>
182
+ </tr>
183
+ <tr>
184
+ <td>Weaviate</td>
185
+ <td>1.38</td>
186
+ <td>HNSW+原生 hybrid</td>
187
+ <td>需要內建 BM25 融合</td>
188
+ </tr>
189
+ <tr>
190
+ <td>Chroma</td>
191
+ <td>1.5</td>
192
+ <td>HNSW</td>
193
+ <td>原型、notebook,別上生產</td>
194
+ </tr>
195
+ <tr>
196
+ <td>LanceDB</td>
197
+ <td>0.34</td>
198
+ <td>IVF_PQ、HNSW</td>
199
+ <td>嵌入式、S3 直讀、serverless</td>
200
+ </tr>
201
+ </tbody>
202
+ </table>
203
+ <p><strong>索引型別</strong>:Flat 暴力搜尋、100% 召回但 O(n),只適合 &lt;10 萬或當 ground truth;HNSW 召回與延遲最佳但吃 RAM、刪除需 vacuum;IVF/IVF-PQ 省記憶體但需訓練、召回較低。pgvector 的 HNSW 預設 <code>m=16</code>、<code>ef_construction=64</code>、<code>ef_search=40</code>,<code>ef_search</code> 是查詢時主要旋鈕。</p>
204
+ <h2 id="_3">五、檢索品質:三段式才是標配</h2>
205
+ <ol>
206
+ <li><strong>Hybrid</strong>:BM25/全文 + 向量,用 RRF 或加權融合。專有名詞、料號、人名靠關鍵字,語意改寫靠向量。單靠向量在中文專有名詞上常直接失效。</li>
207
+ <li><strong>Rerank</strong>:cross-encoder(如 Qwen3-Reranker 系列、bge-reranker)對 top-50 重排取 top-5。這是投報率最高的一步,常把 context precision 拉起兩位數百分點。</li>
208
+ <li><strong>Query 處理</strong>:改寫(補代名詞、拆多意圖)、HyDE(先產生假答案再檢索)、多查詢展開。過度改寫會引入幻覺,原查詢要一起檢索。</li>
209
+ </ol>
210
+ <h2 id="_4">六、五個常見失效模式</h2>
211
+ <table>
212
+ <thead>
213
+ <tr>
214
+ <th>症狀</th>
215
+ <th>真正原因</th>
216
+ <th>修法</th>
217
+ </tr>
218
+ </thead>
219
+ <tbody>
220
+ <tr>
221
+ <td>答非所問但文件在庫</td>
222
+ <td>chunk 切斷語意</td>
223
+ <td>加大 overlap、改結構導向</td>
224
+ </tr>
225
+ <tr>
226
+ <td>表格內容全錯</td>
227
+ <td>解析成亂碼或欄位錯位</td>
228
+ <td>表格轉 markdown、整塊保留</td>
229
+ </tr>
230
+ <tr>
231
+ <td>召回好但排序差</td>
232
+ <td>只做向量、沒 rerank</td>
233
+ <td>加 cross-encoder</td>
234
+ </tr>
235
+ <tr>
236
+ <td>檢索到相似但無用的段落</td>
237
+ <td>top-k 太大、缺 metadata filter</td>
238
+ <td>降 k、加權限/日期過濾</td>
239
+ </tr>
240
+ <tr>
241
+ <td>「查不到就編」</td>
242
+ <td>prompt 沒給退路</td>
243
+ <td>明確要求引用 chunk id,無依據則拒答</td>
244
+ </tr>
245
+ </tbody>
246
+ </table>
247
+ <p>top-k 不是越大越好:k=20 的雜訊會稀釋注意力,還會撞上長 context 成本倍率。<strong>先取 30–50 再 rerank 到 3–5</strong> 較穩。</p>
248
+ <h2 id="golden-set">七、評估:沒有 golden set 就沒有優化</h2>
249
+ <ul>
250
+ <li><strong>Recall@k</strong>:正確段落有沒有進候選。這是天花板,先修這個。</li>
251
+ <li><strong>Context precision</strong>:進到 prompt 的東西有多相關(rerank 後量)。</li>
252
+ <li><strong>答案層</strong>:正確性、引用是否真的支撐結論(faithfulness)。</li>
253
+ <li>Golden set 建法:從真實問題抽 100–300 題,人工標「答案在哪個 chunk」。用 LLM 生成題目可以,但要人工抽查 10–20%,否則是在評測自己造的偏誤。</li>
254
+ </ul>
255
+ <h2 id="_5">實務檢查清單</h2>
256
+ <ul>
257
+ <li>[ ] 抽 20 份最難的 PDF,人工看解析結果再往下做</li>
258
+ <li>[ ] chunk 前綴帶標題階層與文件來源</li>
259
+ <li>[ ] query 與 document 的 embedding 前綴邏輯寫在同一個函式</li>
260
+ <li>[ ] 先上 hybrid + rerank,再談換模型</li>
261
+ <li>[ ] 索引維度確認沒撞 pgvector 2000 維上限</li>
262
+ <li>[ ] 建 100 題以上的 golden set,改動前後都跑</li>
263
+ </ul>
264
+ <h2 id="_6">常見誤解</h2>
265
+ <ul>
266
+ <li>「換更好的 embedding 模型就會變準」:解析與 chunking 錯,換模型只是換一種錯法。</li>
267
+ <li>「維度越高越好」:召回增益有限,儲存與延遲是線性上升。</li>
268
+ <li>「向量搜尋可以取代關鍵字」:專有名詞與代碼類查詢,BM25 仍常勝。</li>
269
+ <li>「top-k 大一點比較保險」:雜訊與成本同時上升,precision 反而下降。</li>
270
+ </ul></article><p style="margin-top:28px;color:var(--muted);font-size:13px"><a href="index.html">← 回首頁</a></p></main></div></body></html>
04_Agent框架與工具呼叫.html ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
2
+ <title>04 · Agent 框架與工具呼叫 — AI/ML 應用工程主題</title>
3
+ <style>
4
+ :root{--bg:#0b1020;--panel:#121a2e;--panel2:#0f1628;--text:#e6edf7;--muted:#9fb0c8;--accent:#7aa2ff;--accent2:#4fd1c5;--border:#22304d;--code:#0a1222}
5
+ *{box-sizing:border-box} html,body{margin:0;padding:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Noto Sans TC","PingFang TC","Microsoft JhengHei",sans-serif;line-height:1.7}
6
+ a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
7
+ .layout{display:flex;min-height:100vh}
8
+ nav{width:290px;min-width:240px;background:var(--panel2);border-right:1px solid var(--border);padding:18px 14px;position:sticky;top:0;height:100vh;overflow:auto}
9
+ nav h2{font-size:15px;margin:0 0 10px 0;color:var(--muted);font-weight:600;letter-spacing:.02em}
10
+ nav .brand{font-size:17px;font-weight:700;color:var(--text);margin-bottom:14px;display:block}
11
+ nav ul{list-style:none;padding:0;margin:0}
12
+ nav li{margin:4px 0}
13
+ nav a{display:block;padding:6px 8px;border-radius:8px;color:var(--text);font-size:14px}
14
+ nav a:hover{background:rgba(122,162,255,.12);text-decoration:none}
15
+ nav a.active{background:rgba(122,162,255,.18);color:#fff}
16
+ main{flex:1;max-width:1100px;padding:28px 34px 60px}
17
+ h1{font-size:30px;margin:0 0 8px} h2{font-size:22px;margin:28px 0 10px;border-bottom:1px solid var(--border);padding-bottom:4px}
18
+ h3{font-size:18px;margin:22px 0 8px} p{margin:10px 0}
19
+ blockquote{margin:10px 0;padding:8px 14px;border-left:4px solid var(--accent);background:rgba(122,162,255,.08);color:var(--muted);border-radius:6px}
20
+ code{background:var(--code);padding:1px 6px;border-radius:5px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.92em}
21
+ pre{background:var(--code);border:1px solid var(--border);border-radius:10px;padding:14px;overflow:auto}
22
+ pre code{background:none;padding:0}
23
+ .table-wrap{overflow-x:auto;margin:14px 0;border:1px solid var(--border);border-radius:10px}
24
+ table{border-collapse:collapse;width:100%;min-width:520px;font-size:14px}
25
+ th,td{border-bottom:1px solid var(--border);padding:9px 12px;text-align:left;vertical-align:top}
26
+ th{background:#16223a;color:#dfe8f5;font-weight:600;position:sticky;top:0}
27
+ tr:nth-child(even) td{background:rgba(255,255,255,.02)}
28
+ tr:hover td{background:rgba(122,162,255,.06)}
29
+ ul,ol{padding-left:22px} li{margin:4px 0}
30
+ .cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:14px;margin-top:18px}
31
+ .card{background:var(--panel);border:1px solid var(--border);border-radius:12px;padding:14px}
32
+ .card h3{margin:0 0 6px;font-size:16px;border:0} .card p{margin:0;color:var(--muted);font-size:14px}
33
+ .badge{display:inline-block;background:rgba(79,209,197,.15);color:var(--accent2);padding:2px 8px;border-radius:999px;font-size:12px;margin-bottom:14px}
34
+ @media(max-width:900px){.layout{flex-direction:column} nav{position:relative;width:auto;height:auto;border-right:0;border-bottom:1px solid var(--border)} main{padding:18px}}
35
+ </style></head><body><div class="layout"><nav><a class="brand" href="index.html">AI/ML 應用工程主題</a><h2>文檔導覽</h2><ul><li><a href="index.html" class="">首頁</a></li><li><a href="00_主題總覽.html" class="">00 · AI/ML 應用工程主題總覽</a></li><li><a href="01_本地LLM部署與推理優化.html" class="">01 · 本地 LLM 部署與推理優化</a></li><li><a href="02_模型選型與成本算力.html" class="">02 · 模型選型與成本算力</a></li><li><a href="03_RAG與向量資料庫.html" class="">03 · RAG 與向量資料庫</a></li><li><a href="04_Agent框架與工具呼叫.html" class="active">04 · Agent 框架與工具呼叫</a></li><li><a href="05_微調與對齊流程.html" class="">05 · 微調與對齊流程</a></li><li><a href="06_ML工具鏈與實驗管理.html" class="">06 · ML 工具鏈與實驗管理</a></li><li><a href="07_評估與可觀測性.html" class="">07 · 評估與可觀測性</a></li><li><a href="08_上線架構與維運成本.html" class="">08 · 上線架構與維運成本</a></li></ul></nav><main><article><h1 id="04-agent">04 · Agent 框架與工具呼叫</h1>
36
+ <blockquote>
37
+ <p>來源:本機技能沉澱(無單一原始文件;以官方文件與實務經驗綜合)
38
+ 網路充實:2026-11-12(MCP 規格 2026-07-28 的 transport 變更、OpenAI Agents SDK/LangGraph/Pydantic AI/CrewAI 現況與版本)</p>
39
+ </blockquote>
40
+ <h2 id="tool-calling">一、tool calling 到底怎麼運作</h2>
41
+ <p>模型不會「執行程式」,只輸出結構化物件:</p>
42
+ <ol>
43
+ <li>工具清單(name、description、JSON Schema)放進 prompt。</li>
44
+ <li>模型回傳 <code>tool_calls</code>(可能一次多個,平行呼叫)。</li>
45
+ <li><strong>你的程式</strong>執行,把結果以 tool 訊息塞回對話。</li>
46
+ <li>模型再決定:繼續呼叫、或給最終答案。</li>
47
+ </ol>
48
+ <p>實務要點:</p>
49
+ <ul>
50
+ <li><strong>錯誤也要回傳給模型</strong>��不要拋例外中斷。格式化成 <code>{"error": "...", "retryable": true}</code>,模型多半能自己修參數。</li>
51
+ <li>平行呼叫要處理<strong>部分失敗</strong>,並保證工具 idempotent(重試不重複下單)。</li>
52
+ <li>參數驗證(Pydantic/Zod)擋在執行前,比事後修 prompt 有效得多。</li>
53
+ <li>工具超過 15–20 個,模型選錯率明顯上升;先分類,再用「選單工具」兩段式檢索。</li>
54
+ </ul>
55
+ <h2 id="_1">二、框架比較:先問「你需要什麼」</h2>
56
+ <table>
57
+ <thead>
58
+ <tr>
59
+ <th>框架</th>
60
+ <th>核心抽象</th>
61
+ <th>何時選</th>
62
+ </tr>
63
+ </thead>
64
+ <tbody>
65
+ <tr>
66
+ <td>原生 SDK</td>
67
+ <td>自己寫 while 迴圈</td>
68
+ <td>單一供應商、工具 &lt;10 個</td>
69
+ </tr>
70
+ <tr>
71
+ <td>OpenAI Agents SDK</td>
72
+ <td>Agents/Handoffs/Guardrails</td>
73
+ <td>OpenAI 為主,要小而完整</td>
74
+ </tr>
75
+ <tr>
76
+ <td>LangGraph</td>
77
+ <td>節點+邊+state+checkpointer</td>
78
+ <td>需明確狀態機、可回放、人為中斷</td>
79
+ </tr>
80
+ <tr>
81
+ <td>Pydantic AI</td>
82
+ <td>型別化 Agent、依賴注入</td>
83
+ <td>Python 團隊、要強驗證與可換模型</td>
84
+ </tr>
85
+ <tr>
86
+ <td>CrewAI</td>
87
+ <td>角色/任務/Crew+Flow</td>
88
+ <td>流程真能拆成角色分工</td>
89
+ </tr>
90
+ <tr>
91
+ <td>Google ADK</td>
92
+ <td>圖工作流+A2A</td>
93
+ <td>GCP/Gemini 生態</td>
94
+ </tr>
95
+ </tbody>
96
+ </table>
97
+ <p><strong>什麼時候不要用框架</strong>:單一 agent、三五個工具、無持久化需求。此時框架帶來的是抽象稅——出錯時要穿透三層才知道模型收到什麼。先用 200 行原生程式跑通,再決定是否引入。</p>
98
+ <h2 id="_2">三、三種推理模式的差異</h2>
99
+ <table>
100
+ <thead>
101
+ <tr>
102
+ <th>模式</th>
103
+ <th>行為</th>
104
+ <th>適用</th>
105
+ <th>風險</th>
106
+ </tr>
107
+ </thead>
108
+ <tbody>
109
+ <tr>
110
+ <td>ReAct</td>
111
+ <td>想一步→做一步→看結果</td>
112
+ <td>探索型、結果不可預測</td>
113
+ <td>步數膨脹、繞圈</td>
114
+ </tr>
115
+ <tr>
116
+ <td>Plan-and-execute</td>
117
+ <td>先產出計畫再逐步執行</td>
118
+ <td>步驟可預先拆解</td>
119
+ <td>計畫錯就一路錯</td>
120
+ </tr>
121
+ <tr>
122
+ <td>Reflection</td>
123
+ <td>產出後自我批評再修</td>
124
+ <td>寫作、程式碼、格式要求高</td>
125
+ <td>成本翻倍、可能愈改愈差</td>
126
+ </tr>
127
+ </tbody>
128
+ </table>
129
+ <p>多數系統是 <strong>ReAct 為骨幹+一次 reflection</strong>,而不是全部疊上。reflection 要有外部訊號(測試通過、schema 驗證)才有意義;純靠模型自評經常無效。</p>
130
+ <h2 id="_3">四、記憶:多數時候你不該給</h2>
131
+ <table>
132
+ <thead>
133
+ <tr>
134
+ <th>類型</th>
135
+ <th>實作</th>
136
+ <th>注意</th>
137
+ </tr>
138
+ </thead>
139
+ <tbody>
140
+ <tr>
141
+ <td>短期</td>
142
+ <td>context 內的訊息</td>
143
+ <td>最可靠,成本隨輪數成長</td>
144
+ </tr>
145
+ <tr>
146
+ <td>摘要式</td>
147
+ <td>舊對話壓成摘要</td>
148
+ <td>會丟細節,錯誤不可逆</td>
149
+ </tr>
150
+ <tr>
151
+ <td>長期向量</td>
152
+ <td>互動寫進向量庫</td>
153
+ <td>檢索不準等於注入雜訊</td>
154
+ </tr>
155
+ <tr>
156
+ <td>結構化</td>
157
+ <td>KV/資料庫</td>
158
+ <td>最可控,優先選</td>
159
+ </tr>
160
+ </tbody>
161
+ </table>
162
+ <p>原則:<strong>能寫進資料庫的就不要放進向量記憶</strong>。記憶要有寫入策略(什麼值得記)、讀取策略(何時檢索)、刪除機制(使用者要求遺忘)。缺這三者,記憶只是把錯誤長期化。</p>
163
+ <h2 id="mcp">五、MCP:工具生態的共通介面</h2>
164
+ <p>MCP 用 JSON-RPC 2.0,三種 primitive:<strong>tools</strong>(模型可呼叫,有副作用)、<strong>resources</strong>(可讀資料)、<strong>prompts</strong>(使用者可選的範本工作流)。</p>
165
+ <table>
166
+ <thead>
167
+ <tr>
168
+ <th>Transport</th>
169
+ <th>機制</th>
170
+ <th>場合</th>
171
+ </tr>
172
+ </thead>
173
+ <tbody>
174
+ <tr>
175
+ <td>stdio</td>
176
+ <td>客戶端啟動子程序,逐行 JSON</td>
177
+ <td>本機、單人、CLI 工具</td>
178
+ </tr>
179
+ <tr>
180
+ <td>Streamable HTTP</td>
181
+ <td>單一 endpoint、每訊息一個 POST、可回 SSE</td>
182
+ <td>遠端、多客戶端、共享服務</td>
183
+ </tr>
184
+ </tbody>
185
+ </table>
186
+ <p>2026-07-28 版移除 protocol 層 session 與 GET 串流,改以 <code>subscriptions/listen</code> 訂閱變更。<strong>一定要釘版本</strong>,舊教學的 session 寫法已不適用。</p>
187
+ <h2 id="_4">六、可靠度:四個必須的護欄</h2>
188
+ <ol>
189
+ <li><strong>輸入/輸出驗證</strong>:工具參數用 schema 擋;最終答案也要檢查(例如必須含引用)。</li>
190
+ <li><strong>步數與成本上限</strong>:<code>max_steps</code>(建議 8–15)、token 預算、timeout。沒有上限的 agent 一定會在某天爆帳。</li>
191
+ <li><strong>retry 與 idempotency</strong>:只重試可重試的錯誤(429、5xx、schema 錯);寫入型工具帶 idempotency key。</li>
192
+ <li><strong>human-in-the-loop</strong>:付款、刪除、對外發信暫停等核可,並把暫停狀態序列化,才能重啟續跑。</li>
193
+ </ol>
194
+ <p>可觀測性不是選配:每次 run 要能還原「模型看到什麼、回了什麼、工具做了什麼」。沒有 trace 只能重寫,無法除錯。</p>
195
+ <h2 id="_5">七、四個反模式</h2>
196
+ <ul>
197
+ <li><strong>多 agent 過度設計</strong>:一個 agent 能做好的事拆成五個角色,換來溝通成本與不可預測性。</li>
198
+ <li><strong>工具描述寫太爛</strong>:<code>"do stuff"</code> 等於沒有。要寫何時該用、何時不該用、參數格式、回傳長相。</li>
199
+ <li><strong>沒有限制步數</strong>:模型會禮貌地永無止境地「再確認一次」。</li>
200
+ <li><strong>工具結果原封不動塞回 context</strong>:一個 API 回 40 KB JSON,兩輪就爆。先裁剪、只留必要欄位。</li>
201
+ </ul>
202
+ <h2 id="_6">實務檢查清單</h2>
203
+ <ul>
204
+ <li>[ ] 每個工具都有 schema、範例、與「不要用」的說明</li>
205
+ <li>[ ] 工具錯誤回傳結構化訊息,而不是拋例外</li>
206
+ <li>[ ] 設 <code>max_steps</code>、token 上限、timeout 三件套</li>
207
+ <li>[ ] 寫入型操作具 idempotency key</li>
208
+ <li>[ ] 高風險動作接 human-in-the-loop,狀態可序列化</li>
209
+ <li>[ ] 每次 run 有完整 trace(prompt/tool/result/latency)</li>
210
+ <li>[ ] MCP server 釘住 protocol 版本,並驗證 Origin header</li>
211
+ </ul>
212
+ <h2 id="_7">常見誤解</h2>
213
+ <ul>
214
+ <li>「agent 會自己規劃,所以不用設計流程」:模型負責決策,流程與護欄仍要人寫。</li>
215
+ <li>「框架越重越專業」:抽象層越厚,除錯越貴;多數產品只需要一個迴圈。</li>
216
+ <li>「記憶越多越貼心」:錯的記憶比沒有記憶更傷害體驗。</li>
217
+ <li>「多 agent 比較強」:目前沒有獨立證據顯示某種多 agent 設計穩定勝過單 agent。</li>
218
+ <li>「tool calling 是模型在執行程式」:執行永遠在你的程式碼裡,那是信任邊界。</li>
219
+ </ul></article><p style="margin-top:28px;color:var(--muted);font-size:13px"><a href="index.html">← 回首頁</a></p></main></div></body></html>
05_微調與對齊流程.html ADDED
@@ -0,0 +1,313 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
2
+ <title>05 · 微調與對齊流程 — AI/ML 應用工程主題</title>
3
+ <style>
4
+ :root{--bg:#0b1020;--panel:#121a2e;--panel2:#0f1628;--text:#e6edf7;--muted:#9fb0c8;--accent:#7aa2ff;--accent2:#4fd1c5;--border:#22304d;--code:#0a1222}
5
+ *{box-sizing:border-box} html,body{margin:0;padding:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Noto Sans TC","PingFang TC","Microsoft JhengHei",sans-serif;line-height:1.7}
6
+ a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
7
+ .layout{display:flex;min-height:100vh}
8
+ nav{width:290px;min-width:240px;background:var(--panel2);border-right:1px solid var(--border);padding:18px 14px;position:sticky;top:0;height:100vh;overflow:auto}
9
+ nav h2{font-size:15px;margin:0 0 10px 0;color:var(--muted);font-weight:600;letter-spacing:.02em}
10
+ nav .brand{font-size:17px;font-weight:700;color:var(--text);margin-bottom:14px;display:block}
11
+ nav ul{list-style:none;padding:0;margin:0}
12
+ nav li{margin:4px 0}
13
+ nav a{display:block;padding:6px 8px;border-radius:8px;color:var(--text);font-size:14px}
14
+ nav a:hover{background:rgba(122,162,255,.12);text-decoration:none}
15
+ nav a.active{background:rgba(122,162,255,.18);color:#fff}
16
+ main{flex:1;max-width:1100px;padding:28px 34px 60px}
17
+ h1{font-size:30px;margin:0 0 8px} h2{font-size:22px;margin:28px 0 10px;border-bottom:1px solid var(--border);padding-bottom:4px}
18
+ h3{font-size:18px;margin:22px 0 8px} p{margin:10px 0}
19
+ blockquote{margin:10px 0;padding:8px 14px;border-left:4px solid var(--accent);background:rgba(122,162,255,.08);color:var(--muted);border-radius:6px}
20
+ code{background:var(--code);padding:1px 6px;border-radius:5px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.92em}
21
+ pre{background:var(--code);border:1px solid var(--border);border-radius:10px;padding:14px;overflow:auto}
22
+ pre code{background:none;padding:0}
23
+ .table-wrap{overflow-x:auto;margin:14px 0;border:1px solid var(--border);border-radius:10px}
24
+ table{border-collapse:collapse;width:100%;min-width:520px;font-size:14px}
25
+ th,td{border-bottom:1px solid var(--border);padding:9px 12px;text-align:left;vertical-align:top}
26
+ th{background:#16223a;color:#dfe8f5;font-weight:600;position:sticky;top:0}
27
+ tr:nth-child(even) td{background:rgba(255,255,255,.02)}
28
+ tr:hover td{background:rgba(122,162,255,.06)}
29
+ ul,ol{padding-left:22px} li{margin:4px 0}
30
+ .cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:14px;margin-top:18px}
31
+ .card{background:var(--panel);border:1px solid var(--border);border-radius:12px;padding:14px}
32
+ .card h3{margin:0 0 6px;font-size:16px;border:0} .card p{margin:0;color:var(--muted);font-size:14px}
33
+ .badge{display:inline-block;background:rgba(79,209,197,.15);color:var(--accent2);padding:2px 8px;border-radius:999px;font-size:12px;margin-bottom:14px}
34
+ @media(max-width:900px){.layout{flex-direction:column} nav{position:relative;width:auto;height:auto;border-right:0;border-bottom:1px solid var(--border)} main{padding:18px}}
35
+ </style></head><body><div class="layout"><nav><a class="brand" href="index.html">AI/ML 應用工程主題</a><h2>文檔導覽</h2><ul><li><a href="index.html" class="">首頁</a></li><li><a href="00_主題總覽.html" class="">00 · AI/ML 應用工程主題總覽</a></li><li><a href="01_本地LLM部署與推理優化.html" class="">01 · 本地 LLM 部署與推理優化</a></li><li><a href="02_模型選型與成本算力.html" class="">02 · 模型選型與成本算力</a></li><li><a href="03_RAG與向量資料庫.html" class="">03 · RAG 與向量資料庫</a></li><li><a href="04_Agent框架與工具呼叫.html" class="">04 · Agent 框架與工具呼叫</a></li><li><a href="05_微調與對齊流程.html" class="active">05 · 微調與對齊流程</a></li><li><a href="06_ML工具鏈與實驗管理.html" class="">06 · ML 工具鏈與實驗管理</a></li><li><a href="07_評估與可觀測性.html" class="">07 · 評估與可觀測性</a></li><li><a href="08_上線架構與維運成本.html" class="">08 · 上線架構與維運成本</a></li></ul></nav><main><article><h1 id="05">05 · 微調與對齊流程</h1>
36
+ <blockquote>
37
+ <p>來源:本機技能沉澱(無單一原始文件;以官方文件與實務經驗綜合)
38
+ 網路充實:2026-11-12(查證 TRL 1.9.x / Axolotl 0.18 / Unsloth 2026.8 版本線、現行 GPU 租金、QLoRA 顯存實測區間)</p>
39
+ </blockquote>
40
+ <h2 id="_1">一、先問「真的需要微調嗎」</h2>
41
+ <table>
42
+ <thead>
43
+ <tr>
44
+ <th>症狀</th>
45
+ <th>先試什麼</th>
46
+ <th>微調是否對症</th>
47
+ </tr>
48
+ </thead>
49
+ <tbody>
50
+ <tr>
51
+ <td>格式不穩、JSON 跑掉、語氣不符</td>
52
+ <td>prompt + few-shot + 結構化輸出約束</td>
53
+ <td>是,SFT 最強場景</td>
54
+ </tr>
55
+ <tr>
56
+ <td>缺領域知識、答案查不到</td>
57
+ <td>RAG / 工具呼叫</td>
58
+ <td>否,常讓幻覺更嚴重</td>
59
+ </tr>
60
+ <tr>
61
+ <td>該拒答卻亂答、偏好不對齊</td>
62
+ <td>規則護欄 → DPO</td>
63
+ <td>是</td>
64
+ </tr>
65
+ <tr>
66
+ <td>延遲��� token 成本太高</td>
67
+ <td>換小模型/量化/快取</td>
68
+ <td>只有蒸餾對症</td>
69
+ </tr>
70
+ </tbody>
71
+ </table>
72
+ <p>結論:<strong>知識走檢索、格式走 SFT、偏好走 DPO、成本走蒸餾</strong>。沒有 eval 與回滾就不要動模型。</p>
73
+ <h2 id="lora-qlora">二、LoRA / QLoRA 原理與超參數</h2>
74
+ <p>LoRA 凍結原權重,只訓練低秩分解 <code>B·A</code>(rank r);QLoRA 再把基底模型以 4-bit NF4 載入,訓練時反量化,讓 8B 模型能塞進單張 24GB 卡。</p>
75
+ <table>
76
+ <thead>
77
+ <tr>
78
+ <th>超參數</th>
79
+ <th>實務起點</th>
80
+ <th>說明</th>
81
+ </tr>
82
+ </thead>
83
+ <tbody>
84
+ <tr>
85
+ <td>rank r</td>
86
+ <td>8–16(格式)/32–64(風格、領域)</td>
87
+ <td>再大邊際效益低且易過擬合</td>
88
+ </tr>
89
+ <tr>
90
+ <td>alpha</td>
91
+ <td>通常 = 2r,或 = r</td>
92
+ <td>alpha/r 才是有效縮放倍率</td>
93
+ </tr>
94
+ <tr>
95
+ <td>target modules</td>
96
+ <td>全部線性層(q,k,v,o,gate,up,down)</td>
97
+ <td>只調 q,v 常學不動風格</td>
98
+ </tr>
99
+ <tr>
100
+ <td>learning rate</td>
101
+ <td>LoRA 1e-4 ~ 2e-4;全量 2e-5</td>
102
+ <td>QLoRA 可用 2e-4</td>
103
+ </tr>
104
+ <tr>
105
+ <td>epoch</td>
106
+ <td>1–3</td>
107
+ <td>第 2 輪後 val loss 回升就停</td>
108
+ </tr>
109
+ <tr>
110
+ <td>batch</td>
111
+ <td>等效 16–64(grad accum 補)</td>
112
+ <td>太小不穩、太大收斂慢</td>
113
+ </tr>
114
+ <tr>
115
+ <td>seq len</td>
116
+ <td>覆蓋 p95 樣本長度,勿一刀切 4096</td>
117
+ <td>長度直接決定顯存</td>
118
+ </tr>
119
+ </tbody>
120
+ </table>
121
+ <p>顯存實測(Llama 3.1 8B、seq 2048):Unsloth QLoRA 約 8GB,Axolotl/TRL 約 16–18GB;LoRA bf16 需 18–26GB。<strong>單張 24GB 跑 8B QLoRA 是穩的;跑全量微調不行(需 60GB+)。</strong></p>
122
+ <h2 id="sft">三、SFT 資料工程</h2>
123
+ <table>
124
+ <thead>
125
+ <tr>
126
+ <th>項目</th>
127
+ <th>要求</th>
128
+ </tr>
129
+ </thead>
130
+ <tbody>
131
+ <tr>
132
+ <td>格式</td>
133
+ <td>直接用模型原生 chat template(<code>apply_chat_template</code>),勿自創格式</td>
134
+ </tr>
135
+ <tr>
136
+ <td>資料量</td>
137
+ <td>數百筆可起步,1k–5k 為常見甜蜜點;&gt;10k 常是品質稀釋</td>
138
+ </tr>
139
+ <tr>
140
+ <td>品質</td>
141
+ <td>每筆都要能通過人工複審;錯誤示範會被 100% 學會</td>
142
+ </tr>
143
+ <tr>
144
+ <td>去重</td>
145
+ <td>對 prompt 正規化後做 MinHash/餘弦去重,門檻約 0.9</td>
146
+ </tr>
147
+ <tr>
148
+ <td>洩漏</td>
149
+ <td>eval set 與 train 在同一 prompt 上隔離</td>
150
+ </tr>
151
+ <tr>
152
+ <td>類別平衡</td>
153
+ <td>拒答/追問/正常回答都要有</td>
154
+ </tr>
155
+ </tbody>
156
+ </table>
157
+ <p>實務鐵律:<strong>1,000 筆高品質 &gt; 10,000 筆爬來的</strong>。資料量不足時先補資料,不要加 epoch。</p>
158
+ <h2 id="_2">四、對齊方法比較</h2>
159
+ <table>
160
+ <thead>
161
+ <tr>
162
+ <th>方法</th>
163
+ <th>需要什麼資料</th>
164
+ <th>實務採用度</th>
165
+ <th>適用</th>
166
+ </tr>
167
+ </thead>
168
+ <tbody>
169
+ <tr>
170
+ <td>SFT</td>
171
+ <td>prompt→理想回覆</td>
172
+ <td>最高</td>
173
+ <td>格式、風格、任務</td>
174
+ </tr>
175
+ <tr>
176
+ <td>DPO</td>
177
+ <td>chosen / rejected 配對</td>
178
+ <td>高</td>
179
+ <td>語氣、安全、拒答邊界</td>
180
+ </tr>
181
+ <tr>
182
+ <td>ORPO</td>
183
+ <td>配對(免 reference model)</td>
184
+ <td>中</td>
185
+ <td>顯存吃緊時的 DPO 替代</td>
186
+ </tr>
187
+ <tr>
188
+ <td>KTO</td>
189
+ <td>只要 good/bad 標籤</td>
190
+ <td>低–中</td>
191
+ <td>標註成本高、只有好壞時</td>
192
+ </tr>
193
+ <tr>
194
+ <td>PPO / GRPO</td>
195
+ <td>reward model 或可驗證獎勵</td>
196
+ <td>低(GRPO 在推理題回升)</td>
197
+ <td>有客觀正解(數學、程式)</td>
198
+ </tr>
199
+ </tbody>
200
+ </table>
201
+ <p>多數團隊的現實路徑是 <strong>SFT → DPO 就停</strong>。PPO 需要 reward model 加四個模型同時佔顯存,除非有明確可驗證的獎勵函數,投入產出比很差。</p>
202
+ <h2 id="_3">五、蒸餾:用大模型產訓練資料</h2>
203
+ <p>做法:用強模型對你的真實問題分佈產生答案(或思維鏈),過濾後餵給 7B/8B 小模型 SFT。</p>
204
+ <table>
205
+ <thead>
206
+ <tr>
207
+ <th>風險</th>
208
+ <th>對策</th>
209
+ </tr>
210
+ </thead>
211
+ <tbody>
212
+ <tr>
213
+ <td>教師錯誤被固化</td>
214
+ <td>用規則或單元測試驗證答案,只留可驗證樣本</td>
215
+ </tr>
216
+ <tr>
217
+ <td>分佈偏離真實使用者</td>
218
+ <td>題目一律取自你自己的流量</td>
219
+ </tr>
220
+ <tr>
221
+ <td>授權與條款</td>
222
+ <td>需查證各供應商 ToS 對「用輸出訓練競品」的限制</td>
223
+ </tr>
224
+ <tr>
225
+ <td>多樣性塌縮</td>
226
+ <td>保留溫度採樣與多教師混合</td>
227
+ </tr>
228
+ </tbody>
229
+ </table>
230
+ <p>蒸餾的收益是<strong>推論成本</strong>(小模型單價低一個數量級),不是品質;品質天花板永遠是教師模型。</p>
231
+ <h2 id="loss">六、訓練後評估:不要只看 loss</h2>
232
+ <p>loss 下降只證明「更像訓練資料」,不證明「任務變好」。</p>
233
+ <ol>
234
+ <li><strong>任務型 eval</strong>:固定 golden set,跑準確率/格式合法率/拒答正確率。</li>
235
+ <li><strong>回歸比對</strong>:新模型與舊模型在同一 set 上逐題 diff,人類只看變差的題目。</li>
236
+ <li><strong>人工抽樣</strong>:隨機 50–100 筆盲測(不告知哪個是微調版),兩人交叉標。</li>
237
+ <li><strong>災難性遺忘檢查</strong>:抽一般常識/指令遵循題,確認沒���步。</li>
238
+ <li><strong>上線前 shadow</strong>:與現行模型並跑,只記錄不影響使用者。</li>
239
+ </ol>
240
+ <h2 id="_4">七、成本估算</h2>
241
+ <p>以 2026-11 行情(需查證,變動快):A100 80GB 約 $1.0–1.4/GPU·hr、H100 SXM 約 $2.2–3.2、RTX 4090 24GB 約 $0.35–0.45、RTX 3090 約 $0.20。</p>
242
+ <table>
243
+ <thead>
244
+ <tr>
245
+ <th>情境</th>
246
+ <th>硬體</th>
247
+ <th>時間(約)</th>
248
+ <th>成本(約)</th>
249
+ </tr>
250
+ </thead>
251
+ <tbody>
252
+ <tr>
253
+ <td>8B QLoRA,3k 筆,2 epoch</td>
254
+ <td>1× RTX 4090</td>
255
+ <td>1–3 小時</td>
256
+ <td>&lt; $2</td>
257
+ </tr>
258
+ <tr>
259
+ <td>8B QLoRA 調參 10 組</td>
260
+ <td>1× A100 80GB</td>
261
+ <td>8–20 小時</td>
262
+ <td>$15–30</td>
263
+ </tr>
264
+ <tr>
265
+ <td>70B QLoRA</td>
266
+ <td>2× A100 80GB</td>
267
+ <td>10–30 小時</td>
268
+ <td>$30–90</td>
269
+ </tr>
270
+ </tbody>
271
+ </table>
272
+ <p>結論:<strong>微調的錢幾乎不是 GPU 費,而是標註與評估的人力</strong>。預算按「每輪迭代 2 人日」估,不是按 GPU 時數估。</p>
273
+ <h2 id="_5">實務檢查清單</h2>
274
+ <ul>
275
+ <li>[ ] 已寫下「不用微調會怎樣」的具體失敗案例</li>
276
+ <li>[ ] 先做過 prompt / RAG 對照實驗並留下數據</li>
277
+ <li>[ ] 資料集有版本號、去重紀錄、與 eval 無洩漏</li>
278
+ <li>[ ] 固定 seed,config 進 git,可一鍵重跑</li>
279
+ <li>[ ] 有 golden set 與回歸腳本,而非只看 loss 曲線</li>
280
+ <li>[ ] 產出 model card(資料來源、限制、評估結果)</li>
281
+ <li>[ ] 保留舊 adapter,路由可切換</li>
282
+ </ul>
283
+ <h2 id="_6">常見誤解</h2>
284
+ <table>
285
+ <thead>
286
+ <tr>
287
+ <th>誤解</th>
288
+ <th>事實</th>
289
+ </tr>
290
+ </thead>
291
+ <tbody>
292
+ <tr>
293
+ <td>「微調可以注入知識」</td>
294
+ <td>知識更新靠 RAG;微調擅長行為與格式</td>
295
+ </tr>
296
+ <tr>
297
+ <td>「rank 越高越好」</td>
298
+ <td>r=64 以上常只增加顯存,eval 不成長</td>
299
+ </tr>
300
+ <tr>
301
+ <td>「loss 更低就是更好」</td>
302
+ <td>loss 與任務指標可以背離,必須跑任務型 eval</td>
303
+ </tr>
304
+ <tr>
305
+ <td>「資料越多越好」</td>
306
+ <td>低品質資料會直接教壞模型,稀釋訊號</td>
307
+ </tr>
308
+ <tr>
309
+ <td>「DPO 一定比 SFT 好」</td>
310
+ <td>沒有乾淨配對資料時,DPO 會退化成亂拒答</td>
311
+ </tr>
312
+ </tbody>
313
+ </table></article><p style="margin-top:28px;color:var(--muted);font-size:13px"><a href="index.html">← 回首頁</a></p></main></div></body></html>
06_ML工具鏈與實驗管理.html ADDED
@@ -0,0 +1,303 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
2
+ <title>06 · ML 工具鏈與實驗管理 — AI/ML 應用工程主題</title>
3
+ <style>
4
+ :root{--bg:#0b1020;--panel:#121a2e;--panel2:#0f1628;--text:#e6edf7;--muted:#9fb0c8;--accent:#7aa2ff;--accent2:#4fd1c5;--border:#22304d;--code:#0a1222}
5
+ *{box-sizing:border-box} html,body{margin:0;padding:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Noto Sans TC","PingFang TC","Microsoft JhengHei",sans-serif;line-height:1.7}
6
+ a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
7
+ .layout{display:flex;min-height:100vh}
8
+ nav{width:290px;min-width:240px;background:var(--panel2);border-right:1px solid var(--border);padding:18px 14px;position:sticky;top:0;height:100vh;overflow:auto}
9
+ nav h2{font-size:15px;margin:0 0 10px 0;color:var(--muted);font-weight:600;letter-spacing:.02em}
10
+ nav .brand{font-size:17px;font-weight:700;color:var(--text);margin-bottom:14px;display:block}
11
+ nav ul{list-style:none;padding:0;margin:0}
12
+ nav li{margin:4px 0}
13
+ nav a{display:block;padding:6px 8px;border-radius:8px;color:var(--text);font-size:14px}
14
+ nav a:hover{background:rgba(122,162,255,.12);text-decoration:none}
15
+ nav a.active{background:rgba(122,162,255,.18);color:#fff}
16
+ main{flex:1;max-width:1100px;padding:28px 34px 60px}
17
+ h1{font-size:30px;margin:0 0 8px} h2{font-size:22px;margin:28px 0 10px;border-bottom:1px solid var(--border);padding-bottom:4px}
18
+ h3{font-size:18px;margin:22px 0 8px} p{margin:10px 0}
19
+ blockquote{margin:10px 0;padding:8px 14px;border-left:4px solid var(--accent);background:rgba(122,162,255,.08);color:var(--muted);border-radius:6px}
20
+ code{background:var(--code);padding:1px 6px;border-radius:5px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.92em}
21
+ pre{background:var(--code);border:1px solid var(--border);border-radius:10px;padding:14px;overflow:auto}
22
+ pre code{background:none;padding:0}
23
+ .table-wrap{overflow-x:auto;margin:14px 0;border:1px solid var(--border);border-radius:10px}
24
+ table{border-collapse:collapse;width:100%;min-width:520px;font-size:14px}
25
+ th,td{border-bottom:1px solid var(--border);padding:9px 12px;text-align:left;vertical-align:top}
26
+ th{background:#16223a;color:#dfe8f5;font-weight:600;position:sticky;top:0}
27
+ tr:nth-child(even) td{background:rgba(255,255,255,.02)}
28
+ tr:hover td{background:rgba(122,162,255,.06)}
29
+ ul,ol{padding-left:22px} li{margin:4px 0}
30
+ .cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:14px;margin-top:18px}
31
+ .card{background:var(--panel);border:1px solid var(--border);border-radius:12px;padding:14px}
32
+ .card h3{margin:0 0 6px;font-size:16px;border:0} .card p{margin:0;color:var(--muted);font-size:14px}
33
+ .badge{display:inline-block;background:rgba(79,209,197,.15);color:var(--accent2);padding:2px 8px;border-radius:999px;font-size:12px;margin-bottom:14px}
34
+ @media(max-width:900px){.layout{flex-direction:column} nav{position:relative;width:auto;height:auto;border-right:0;border-bottom:1px solid var(--border)} main{padding:18px}}
35
+ </style></head><body><div class="layout"><nav><a class="brand" href="index.html">AI/ML 應用工程主題</a><h2>文檔導覽</h2><ul><li><a href="index.html" class="">首頁</a></li><li><a href="00_主題總覽.html" class="">00 · AI/ML 應用工程主題總覽</a></li><li><a href="01_本地LLM部署與推理優化.html" class="">01 · 本地 LLM 部署與推理優化</a></li><li><a href="02_模型選型與成本算力.html" class="">02 · 模型選型與成本算力</a></li><li><a href="03_RAG與向量資料庫.html" class="">03 · RAG 與向量資料庫</a></li><li><a href="04_Agent框架與工具呼叫.html" class="">04 · Agent 框架與工具呼叫</a></li><li><a href="05_微調與對齊流程.html" class="">05 · 微調與對齊流程</a></li><li><a href="06_ML工具鏈與實驗管理.html" class="active">06 · ML 工具鏈與實驗管理</a></li><li><a href="07_評估與可觀測性.html" class="">07 · 評估與可觀測性</a></li><li><a href="08_上線架構與維運成本.html" class="">08 · 上線架構與維運成本</a></li></ul></nav><main><article><h1 id="06-ml">06 · ML 工具鏈與實驗管理</h1>
36
+ <blockquote>
37
+ <p>來源:本機技能沉澱(無單一原始文件;以官方文件與實務經驗綜合)
38
+ 網路充實:2026-11-12(查證 vLLM v0.32 / SGLang v0.5.23 現行版本、TGI 維護狀態、TRL 1.9 與實驗追蹤工具現況)</p>
39
+ </blockquote>
40
+ <h2 id="uv-conda-python">一、環境管理:uv 為預設,conda 只補非 Python 依賴</h2>
41
+ <table>
42
+ <thead>
43
+ <tr>
44
+ <th>工具</th>
45
+ <th>定位</th>
46
+ <th>何時用</th>
47
+ </tr>
48
+ </thead>
49
+ <tbody>
50
+ <tr>
51
+ <td>uv</td>
52
+ <td>Rust 寫的解析器 + venv + lockfile</td>
53
+ <td>預設首選,<code>uv sync</code> 秒級</td>
54
+ </tr>
55
+ <tr>
56
+ <td>conda / mamba</td>
57
+ <td>管 CUDA toolkit、cuDNN、編譯器</td>
58
+ <td>需要釘系統級 CUDA 時</td>
59
+ </tr>
60
+ <tr>
61
+ <td>pip-tools</td>
62
+ <td><code>requirements.in</code> → <code>requirements.txt</code></td>
63
+ <td>舊��案、要可審核的純 pip 流程</td>
64
+ </tr>
65
+ </tbody>
66
+ </table>
67
+ <p>實務建議:<strong>uv 管 Python 套件、Docker image 釘 CUDA</strong>,兩者分責,勿在同一專案用 conda 與 uv 裝同一個 torch。</p>
68
+ <p>CUDA 與 PyTorch 的坑(最常炸的地方):</p>
69
+ <table>
70
+ <thead>
71
+ <tr>
72
+ <th>現象</th>
73
+ <th>原因</th>
74
+ <th>解法</th>
75
+ </tr>
76
+ </thead>
77
+ <tbody>
78
+ <tr>
79
+ <td><code>driver version is insufficient</code></td>
80
+ <td>驅動低於 wheel 的 CUDA runtime</td>
81
+ <td>升級驅動或改裝較低 cuXXX wheel</td>
82
+ </tr>
83
+ <tr>
84
+ <td><code>undefined symbol</code></td>
85
+ <td>torch 與 vLLM / flash-attn 編譯期 CUDA 不一致</td>
86
+ <td>三者對齊同一 CUDA 版本</td>
87
+ </tr>
88
+ <tr>
89
+ <td>同一 image 本機可跑、叢集不行</td>
90
+ <td>驅動只在 host</td>
91
+ <td>用 nvidia-container-toolkit,勿在 image 裝驅動</td>
92
+ </tr>
93
+ </tbody>
94
+ </table>
95
+ <p>原則:<strong>PyTorch wheel 自帶 CUDA runtime,只需 host 驅動夠新</strong>(同 major 內有 minor 相容)。</p>
96
+ <h2 id="_1">二、實驗追蹤:先看你要不要自架</h2>
97
+ <table>
98
+ <thead>
99
+ <tr>
100
+ <th>工具</th>
101
+ <th>型態</th>
102
+ <th>優勢</th>
103
+ <th>弱點</th>
104
+ </tr>
105
+ </thead>
106
+ <tbody>
107
+ <tr>
108
+ <td>W&amp;B</td>
109
+ <td>SaaS 為主</td>
110
+ <td>曲線、sweep、報告最順</td>
111
+ <td>費用隨人數長、資料出企業</td>
112
+ </tr>
113
+ <tr>
114
+ <td>MLflow</td>
115
+ <td>可自架 OSS</td>
116
+ <td>追蹤 + registry + serving 一體</td>
117
+ <td>UI 較舊、大規模要養後端</td>
118
+ </tr>
119
+ <tr>
120
+ <td>TensorBoard</td>
121
+ <td>本地 / 免費</td>
122
+ <td>零成本、PyTorch 原生</td>
123
+ <td>無協作、無 hyperparam 比較</td>
124
+ </tr>
125
+ </tbody>
126
+ </table>
127
+ <p>決策:<strong>個人/小團隊先用 TensorBoard 或 MLflow 本機;要多人比較與 sweep 再上 W&amp;B;資料不能出境的用 MLflow 自架。</strong> 別為 3 個實驗架 SaaS。</p>
128
+ <p>最低要求(無論用哪個):每次 run 記下 git commit、資料集雜湊、完整 config、硬體、seed。少這五樣,實驗就不可重現。</p>
129
+ <h2 id="_2">三、資料版本控制</h2>
130
+ <table>
131
+ <thead>
132
+ <tr>
133
+ <th>方案</th>
134
+ <th>適用規模</th>
135
+ <th>說明</th>
136
+ </tr>
137
+ </thead>
138
+ <tbody>
139
+ <tr>
140
+ <td>git + 雜湊檔</td>
141
+ <td>&lt; 100MB</td>
142
+ <td>檔案放物件儲存,git 只存 <code>sha256</code> + 路徑 manifest</td>
143
+ </tr>
144
+ <tr>
145
+ <td>DVC</td>
146
+ <td>中大型</td>
147
+ <td><code>dvc.yaml</code> 定義 pipeline,資料存 S3/GCS,git 存指標</td>
148
+ </tr>
149
+ <tr>
150
+ <td>HF Datasets</td>
151
+ <td>公開/半公開</td>
152
+ <td>自帶版本、串流、<code>push_to_hub</code></td>
153
+ </tr>
154
+ </tbody>
155
+ </table>
156
+ <p>最小方案:<code>manifest.json</code> 記路徑+sha256,就夠回答「用哪版資料訓的」。</p>
157
+ <h2 id="_3">四、模型與權重管理</h2>
158
+ <ul>
159
+ <li>權重一律不進 git;用 HF Hub 私有 repo 或 S3 + 版本前綴。</li>
160
+ <li>Git LFS 只加速 clone,不省儲存費,部分 CI 也不支援。</li>
161
+ <li><strong>Model registry 至少要有</strong>:模型 id、base model、訓練 config、eval 分數、推出日期、資料集雜湊。</li>
162
+ <li>8B bf16 約 16GB、4-bit 約 5GB;70B bf16 約 140GB,傳輸成本要算進部署時間。</li>
163
+ </ul>
164
+ <p>實務上「資料夾 + JSON metadata + 物件儲存」已夠 90% 團隊;registry 的價值是上線審批紀錄,不是儲存本身。</p>
165
+ <h2 id="_4">五、服務框架:何時用哪個</h2>
166
+ <table>
167
+ <thead>
168
+ <tr>
169
+ <th>框架</th>
170
+ <th>適用</th>
171
+ <th>不適用</th>
172
+ </tr>
173
+ </thead>
174
+ <tbody>
175
+ <tr>
176
+ <td>FastAPI + vLLM</td>
177
+ <td>自架 LLM 推論的預設答案</td>
178
+ <td>需要複雜多模型 DAG 時</td>
179
+ </tr>
180
+ <tr>
181
+ <td>SGLang</td>
182
+ <td>大量共用前綴(agent、多輪、固定 RAG 模板)</td>
183
+ <td>需要最廣模型覆蓋時</td>
184
+ </tr>
185
+ <tr>
186
+ <td>Triton Inference Server</td>
187
+ <td>多框架、多模型、企業級排程</td>
188
+ <td>小團隊,維運成本過高</td>
189
+ </tr>
190
+ <tr>
191
+ <td>BentoML</td>
192
+ <td>要打包成可移植 artifact、快速出 API</td>
193
+ <td>極致吞吐調優</td>
194
+ </tr>
195
+ <tr>
196
+ <td>Ray Serve</td>
197
+ <td>多模型組合、需要弹性擴展的複合 pipeline</td>
198
+ <td>單一模型單純服務</td>
199
+ </tr>
200
+ </tbody>
201
+ </table>
202
+ <p>2026 現況:vLLM 與 SGLang 開箱效能接近,<strong>選型看「哪個先支援你的模型」與「流量是否共用前綴」</strong>,別看廠商貼的倍數。TGI 已由維護者建議遷移,新專案不要起。TensorRT-LLM 最快但學習曲線陡、僅 NVIDIA。</p>
203
+ <h2 id="image">六、容器與部署:image 大小是維運成本</h2>
204
+ <table>
205
+ <thead>
206
+ <tr>
207
+ <th>基底</th>
208
+ <th>大小(約)</th>
209
+ <th>建議</th>
210
+ </tr>
211
+ </thead>
212
+ <tbody>
213
+ <tr>
214
+ <td><code>nvidia/cuda:12.x-devel</code></td>
215
+ <td>8–12GB</td>
216
+ <td>只在編譯 stage 用</td>
217
+ </tr>
218
+ <tr>
219
+ <td><code>nvidia/cuda:12.x-runtime</code></td>
220
+ <td>3–5GB</td>
221
+ <td>推論 image 的合理起點</td>
222
+ </tr>
223
+ <tr>
224
+ <td><code>python:3.12-slim</code></td>
225
+ <td>150MB</td>
226
+ <td>純應用層(打 API、不碰 GPU)</td>
227
+ </tr>
228
+ </tbody>
229
+ </table>
230
+ <p>做法:multi-stage — builder 用 devel 裝 uv 與編譯依賴,runtime 只複製 venv 與程式。GPU 推論常見結果 6–8GB;權重用掛載卷或 init container 下載,<strong>不要烘進 image</strong>。</p>
231
+ <h2 id="cicd-for-ml">七、CI/CD for ML</h2>
232
+ <table>
233
+ <thead>
234
+ <tr>
235
+ <th>階段</th>
236
+ <th>檢查</th>
237
+ </tr>
238
+ </thead>
239
+ <tbody>
240
+ <tr>
241
+ <td>lint / type</td>
242
+ <td>ruff + mypy,30 秒內結束</td>
243
+ </tr>
244
+ <tr>
245
+ <td>單元測試</td>
246
+ <td>pytest,資料用 10 筆假資料</td>
247
+ </tr>
248
+ <tr>
249
+ <td>smoke test</td>
250
+ <td>真的載入小模型跑 1 次 forward,抓 CUDA/依賴錯</td>
251
+ </tr>
252
+ <tr>
253
+ <td>eval gate</td>
254
+ <td>golden set 分數低於基線就 fail</td>
255
+ </tr>
256
+ <tr>
257
+ <td>產出</td>
258
+ <td>model card 與 artifact</td>
259
+ </tr>
260
+ </tbody>
261
+ </table>
262
+ <p>CI 不要跑完整訓練。<strong>CI 證明「能跑」,不證明「效果好」</strong>;效果驗證放在獨立 eval pipeline。</p>
263
+ <h2 id="_5">八、專案結構建議</h2>
264
+ <pre><code>repo/
265
+ pyproject.toml # uv 管理
266
+ src/pkg/{data,train,eval,serve}/
267
+ configs/ # 每個實驗一份 yaml
268
+ tests/
269
+ data/manifest.json
270
+ </code></pre>
271
+ <p>config 管理:<strong>實驗參數用 Hydra(可組合、可覆寫),服務設定用 pydantic-settings(吃環境變數、有型別驗證)</strong>。Hydra 給訓練,pydantic 給 runtime,別互換。</p>
272
+ <h2 id="_6">實務檢查清單</h2>
273
+ <ul>
274
+ <li>[ ] <code>uv.lock</code> 與 Dockerfile 已提交,新人 10 分鐘可跑起</li>
275
+ <li>[ ] CUDA / torch / vLLM 版本對齊並寫進 README</li>
276
+ <li>[ ] 每次 run 記錄 commit、資料雜湊、config、seed</li>
277
+ <li>[ ] 權重不進 git,有 registry 或 manifest</li>
278
+ <li>[ ] CI 有 smoke test,能抓「載不進模型」這類錯</li>
279
+ <li>[ ] image &lt; 10GB,權重外掛,有 model card 與 eval gate</li>
280
+ </ul>
281
+ <h2 id="_7">常見誤解</h2>
282
+ <table>
283
+ <thead>
284
+ <tr>
285
+ <th>誤解</th>
286
+ <th>事實</th>
287
+ </tr>
288
+ </thead>
289
+ <tbody>
290
+ <tr>
291
+ <td>「DVC 是資料版控的必要條件」</td>
292
+ <td>雜湊 manifest 已能回答 90% 的問題</td>
293
+ </tr>
294
+ <tr>
295
+ <td>「Triton 一定比 vLLM 快」</td>
296
+ <td>單一 LLM 場景 vLLM 通常夠用且好維運</td>
297
+ </tr>
298
+ <tr>
299
+ <td>「CI 跑完整訓練才叫 ML CI」</td>
300
+ <td>CI 只驗證可執行性,效果驗證是另一條 pipeline</td>
301
+ </tr>
302
+ </tbody>
303
+ </table></article><p style="margin-top:28px;color:var(--muted);font-size:13px"><a href="index.html">← 回首頁</a></p></main></div></body></html>
07_評估與可觀測性.html ADDED
@@ -0,0 +1,343 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
2
+ <title>07 · 評估與可觀測性 — AI/ML 應用工程主題</title>
3
+ <style>
4
+ :root{--bg:#0b1020;--panel:#121a2e;--panel2:#0f1628;--text:#e6edf7;--muted:#9fb0c8;--accent:#7aa2ff;--accent2:#4fd1c5;--border:#22304d;--code:#0a1222}
5
+ *{box-sizing:border-box} html,body{margin:0;padding:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Noto Sans TC","PingFang TC","Microsoft JhengHei",sans-serif;line-height:1.7}
6
+ a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
7
+ .layout{display:flex;min-height:100vh}
8
+ nav{width:290px;min-width:240px;background:var(--panel2);border-right:1px solid var(--border);padding:18px 14px;position:sticky;top:0;height:100vh;overflow:auto}
9
+ nav h2{font-size:15px;margin:0 0 10px 0;color:var(--muted);font-weight:600;letter-spacing:.02em}
10
+ nav .brand{font-size:17px;font-weight:700;color:var(--text);margin-bottom:14px;display:block}
11
+ nav ul{list-style:none;padding:0;margin:0}
12
+ nav li{margin:4px 0}
13
+ nav a{display:block;padding:6px 8px;border-radius:8px;color:var(--text);font-size:14px}
14
+ nav a:hover{background:rgba(122,162,255,.12);text-decoration:none}
15
+ nav a.active{background:rgba(122,162,255,.18);color:#fff}
16
+ main{flex:1;max-width:1100px;padding:28px 34px 60px}
17
+ h1{font-size:30px;margin:0 0 8px} h2{font-size:22px;margin:28px 0 10px;border-bottom:1px solid var(--border);padding-bottom:4px}
18
+ h3{font-size:18px;margin:22px 0 8px} p{margin:10px 0}
19
+ blockquote{margin:10px 0;padding:8px 14px;border-left:4px solid var(--accent);background:rgba(122,162,255,.08);color:var(--muted);border-radius:6px}
20
+ code{background:var(--code);padding:1px 6px;border-radius:5px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.92em}
21
+ pre{background:var(--code);border:1px solid var(--border);border-radius:10px;padding:14px;overflow:auto}
22
+ pre code{background:none;padding:0}
23
+ .table-wrap{overflow-x:auto;margin:14px 0;border:1px solid var(--border);border-radius:10px}
24
+ table{border-collapse:collapse;width:100%;min-width:520px;font-size:14px}
25
+ th,td{border-bottom:1px solid var(--border);padding:9px 12px;text-align:left;vertical-align:top}
26
+ th{background:#16223a;color:#dfe8f5;font-weight:600;position:sticky;top:0}
27
+ tr:nth-child(even) td{background:rgba(255,255,255,.02)}
28
+ tr:hover td{background:rgba(122,162,255,.06)}
29
+ ul,ol{padding-left:22px} li{margin:4px 0}
30
+ .cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:14px;margin-top:18px}
31
+ .card{background:var(--panel);border:1px solid var(--border);border-radius:12px;padding:14px}
32
+ .card h3{margin:0 0 6px;font-size:16px;border:0} .card p{margin:0;color:var(--muted);font-size:14px}
33
+ .badge{display:inline-block;background:rgba(79,209,197,.15);color:var(--accent2);padding:2px 8px;border-radius:999px;font-size:12px;margin-bottom:14px}
34
+ @media(max-width:900px){.layout{flex-direction:column} nav{position:relative;width:auto;height:auto;border-right:0;border-bottom:1px solid var(--border)} main{padding:18px}}
35
+ </style></head><body><div class="layout"><nav><a class="brand" href="index.html">AI/ML 應用工程主題</a><h2>文檔導覽</h2><ul><li><a href="index.html" class="">首頁</a></li><li><a href="00_主題總覽.html" class="">00 · AI/ML 應用工程主題總覽</a></li><li><a href="01_本地LLM部署與推理優化.html" class="">01 · 本地 LLM 部署與推理優化</a></li><li><a href="02_模型選型與成本算力.html" class="">02 · 模型選型與成本算力</a></li><li><a href="03_RAG與向量資料庫.html" class="">03 · RAG 與向量資料庫</a></li><li><a href="04_Agent框架與工具呼叫.html" class="">04 · Agent 框架與工具呼叫</a></li><li><a href="05_微調與對齊流程.html" class="">05 · 微調與對齊流程</a></li><li><a href="06_ML工具鏈與實驗管理.html" class="">06 · ML 工具鏈與實驗管理</a></li><li><a href="07_評估與可觀測性.html" class="active">07 · 評估與可觀測性</a></li><li><a href="08_上線架構與維運成本.html" class="">08 · 上線架構與維運成本</a></li></ul></nav><main><article><h1 id="07">07 · 評估與可觀測性</h1>
36
+ <blockquote>
37
+ <p>來源:本機技能沉澱(無單一原始文件;以官方文件與實務經驗綜合)
38
+ 網路充實:2026-11-12(查證 OTel GenAI semconv 已於 2026-06 移出主庫且仍全為 Development、Langfuse/LangSmith 現行計價與自架條件)</p>
39
+ </blockquote>
40
+ <h2 id="_1">一、為什麼「感覺變好了」不算</h2>
41
+ <p>人眼對 LLM 輸出有強烈確認偏誤:換個 prompt 後看到的 3 個例子都變好,就宣布改善。問題是<strong>回歸通常發生在你沒看的那 200 題上</strong>。</p>
42
+ <table>
43
+ <thead>
44
+ <tr>
45
+ <th>要求</th>
46
+ <th>具體做法</th>
47
+ </tr>
48
+ </thead>
49
+ <tbody>
50
+ <tr>
51
+ <td>golden set</td>
52
+ <td>50–300 題,題目固定、答案經人確認</td>
53
+ </tr>
54
+ <tr>
55
+ <td>版本化</td>
56
+ <td>每次改動跑同一份 set,結果存檔可 diff</td>
57
+ </tr>
58
+ <tr>
59
+ <td>分���報告</td>
60
+ <td>不只總分,要按意圖/難度/語言分組</td>
61
+ </tr>
62
+ </tbody>
63
+ </table>
64
+ <p>先花兩天建一份 golden set,比再調一週 prompt 更有價值。</p>
65
+ <h2 id="llm">二、LLM 評測方法</h2>
66
+ <table>
67
+ <thead>
68
+ <tr>
69
+ <th>方法</th>
70
+ <th>適用</th>
71
+ <th>成本</th>
72
+ <th>風險</th>
73
+ </tr>
74
+ </thead>
75
+ <tbody>
76
+ <tr>
77
+ <td>規則比對(正規式、schema、單元測試)</td>
78
+ <td>格式、抽取、代碼</td>
79
+ <td>極低</td>
80
+ <td>只能測表層</td>
81
+ </tr>
82
+ <tr>
83
+ <td>Embedding 相似度</td>
84
+ <td>語意貼近、改寫</td>
85
+ <td>低</td>
86
+ <td>高分≠正確</td>
87
+ </tr>
88
+ <tr>
89
+ <td>LLM-as-a-judge</td>
90
+ <td>開放式回答、語氣</td>
91
+ <td>中</td>
92
+ <td>偏誤需校正</td>
93
+ </tr>
94
+ <tr>
95
+ <td>人工標註</td>
96
+ <td>最終仲裁、建立標尺</td>
97
+ <td>高</td>
98
+ <td>慢、標註不一致</td>
99
+ </tr>
100
+ </tbody>
101
+ </table>
102
+ <table>
103
+ <thead>
104
+ <tr>
105
+ <th>judge 偏誤</th>
106
+ <th>現象</th>
107
+ <th>對策</th>
108
+ </tr>
109
+ </thead>
110
+ <tbody>
111
+ <tr>
112
+ <td>位置偏誤</td>
113
+ <td>偏好第一個候選</td>
114
+ <td>隨機交換順序各跑一次</td>
115
+ </tr>
116
+ <tr>
117
+ <td>長度偏誤</td>
118
+ <td>偏愛長回答</td>
119
+ <td>評分準則排除長度,或分層</td>
120
+ </tr>
121
+ <tr>
122
+ <td>自我偏好</td>
123
+ <td>偏好同家族輸出</td>
124
+ <td>換家族當 judge,或用 rubric</td>
125
+ </tr>
126
+ <tr>
127
+ <td>寬鬆偏誤</td>
128
+ <td>幾乎都給 4–5 分</td>
129
+ <td>用 pairwise 取代絕對分</td>
130
+ </tr>
131
+ </tbody>
132
+ </table>
133
+ <p>實務結論:<strong>judge 只用於相對比較與粗篩,關鍵決策要人看。</strong> 與人一致率先抽 50 題量過,低於約 0.7 就不要信。</p>
134
+ <h2 id="_2">三、評測集的建法</h2>
135
+ <ol>
136
+ <li><strong>從真實流量抽樣</strong>:取 2–4 週生產查詢,按意圖分層,權重貼近真實分佈。</li>
137
+ <li><strong>補邊界案例</strong>:空輸入、超長、多語言、對抗性提問、應拒答題。</li>
138
+ <li><strong>去識別化</strong>:PII 遮蔽後才進 eval set。</li>
139
+ <li><strong>版本化並凍結</strong>:<code>eval/v1.2.jsonl</code>,改題目開新版本。</li>
140
+ <li><strong>保留 holdout</strong>:小部分題目不對外公開。</li>
141
+ </ol>
142
+ <h2 id="_3">四、線上可觀測性</h2>
143
+ <table>
144
+ <thead>
145
+ <tr>
146
+ <th>工具</th>
147
+ <th>型態</th>
148
+ <th>關鍵取捨</th>
149
+ </tr>
150
+ </thead>
151
+ <tbody>
152
+ <tr>
153
+ <td>Langfuse</td>
154
+ <td>MIT 核心,2026-01 併入 ClickHouse</td>
155
+ <td>可自架(需 Postgres/ClickHouse/Redis/S3);Core 約 $29/月起,按事件計費</td>
156
+ </tr>
157
+ <tr>
158
+ <td>LangSmith</td>
159
+ <td>閉源,LangChain 生態</td>
160
+ <td>與 LangGraph 整合深;自架僅 Enterprise;按 seat+trace 計費</td>
161
+ </tr>
162
+ <tr>
163
+ <td>OpenTelemetry</td>
164
+ <td>開放標準</td>
165
+ <td>不綁廠商,可同時送多家</td>
166
+ </tr>
167
+ </tbody>
168
+ </table>
169
+ <p><strong>2026-11 查證</strong>:OTel <code>gen_ai.*</code> 規範已於 2026-06-12 從主庫 deprecate 並移到獨立 repo,<strong>目前全為 Development、無穩定版</strong>。屬性名仍會變,<strong>儀表板請建在指標(duration、token usage、TTFT)上</strong>。</p>
170
+ <p>一條 trace 至少要含:請求 id、模型與版本、prompt 版本、檢索文件 id、token 數、延遲、錯誤類型。</p>
171
+ <h2 id="_4">五、監控指標</h2>
172
+ <table>
173
+ <thead>
174
+ <tr>
175
+ <th>指標</th>
176
+ <th>定義</th>
177
+ <th>健康參考</th>
178
+ </tr>
179
+ </thead>
180
+ <tbody>
181
+ <tr>
182
+ <td>TTFT</td>
183
+ <td>首 token 時間</td>
184
+ <td>互動式 &lt; 1s(p95)</td>
185
+ </tr>
186
+ <tr>
187
+ <td>TPOT</td>
188
+ <td>每輸出 token 時間</td>
189
+ <td>約 20–40ms 屬正常</td>
190
+ </tr>
191
+ <tr>
192
+ <td>p50 / p95 延遲</td>
193
+ <td>全鏈路</td>
194
+ <td>p95 常是 p50 的 2–4 倍</td>
195
+ </tr>
196
+ <tr>
197
+ <td>錯誤率</td>
198
+ <td>5xx + 逾時 + 解析失敗</td>
199
+ <td>&lt; 1%</td>
200
+ </tr>
201
+ <tr>
202
+ <td>cache hit rate</td>
203
+ <td>命中/總請求</td>
204
+ <td>精確快取看重複度;語意快取常見 20–45%</td>
205
+ </tr>
206
+ </tbody>
207
+ </table>
208
+ <p><strong>只看平均延遲會錯過所有問題</strong>:使用者抱怨幾乎都來自 p95 之後的尾巴。</p>
209
+ <h2 id="_5">六、漂移與品質退化偵測</h2>
210
+ <table>
211
+ <thead>
212
+ <tr>
213
+ <th>訊號</th>
214
+ <th>怎麼看</th>
215
+ <th>觸發動作</th>
216
+ </tr>
217
+ </thead>
218
+ <tbody>
219
+ <tr>
220
+ <td>輸入分佈</td>
221
+ <td>查詢長度、語言、意圖佔比</td>
222
+ <td>明顯偏移 → 查上游或行銷活動</td>
223
+ </tr>
224
+ <tr>
225
+ <td>拒答率</td>
226
+ <td>「無法回答」的比例</td>
227
+ <td>突升 → 檢索壞了或 prompt 被改</td>
228
+ </tr>
229
+ <tr>
230
+ <td>解析失敗率</td>
231
+ <td>JSON/schema 驗證失敗</td>
232
+ <td>突升 → 供應商改了模型版本</td>
233
+ </tr>
234
+ <tr>
235
+ <td>使用者回饋</td>
236
+ <td>thumbs down、重問率</td>
237
+ <td>週環比惡化 &gt; 20% 即告警</td>
238
+ </tr>
239
+ <tr>
240
+ <td>檢索品質</td>
241
+ <td>文件與答案的關聯</td>
242
+ <td>抽樣人工複核</td>
243
+ </tr>
244
+ </tbody>
245
+ </table>
246
+ <p>原則:<strong>先把「壞掉的樣子」定義成指標</strong>。重問率(同一人 5 分鐘內改問類似問題)是被低估的免費訊號。</p>
247
+ <h2 id="ab-llm">七、A/B 測試 LLM 功能實務</h2>
248
+ <table>
249
+ <thead>
250
+ <tr>
251
+ <th>項目</th>
252
+ <th>做法</th>
253
+ </tr>
254
+ </thead>
255
+ <tbody>
256
+ <tr>
257
+ <td>主指標</td>
258
+ <td>任務完成率或留存,不要用「平均對話長度」</td>
259
+ </tr>
260
+ <tr>
261
+ <td>護欄指標</td>
262
+ <td>延遲 p95、成本/請求、錯誤率、安全事件</td>
263
+ </tr>
264
+ <tr>
265
+ <td>分流</td>
266
+ <td>按使用者 id 雜湊,確保體驗一致</td>
267
+ </tr>
268
+ <tr>
269
+ <td>樣本量</td>
270
+ <td>每組約 <code>16 × p(1−p) / δ²</code>;p=0.5、δ=2% 約需 1 萬/組</td>
271
+ </tr>
272
+ <tr>
273
+ <td>期間</td>
274
+ <td>至少一週,涵蓋工作日與週末</td>
275
+ </tr>
276
+ </tbody>
277
+ </table>
278
+ <p>LLM 功能的效果量通常只有 1–3%,<strong>樣本不足時的「顯著」多半是噪音</strong>;流量不夠就改用 offline eval + 小流量 shadow。</p>
279
+ <h2 id="_6">八、成本監控</h2>
280
+ <table>
281
+ <thead>
282
+ <tr>
283
+ <th>層級</th>
284
+ <th>指標</th>
285
+ <th>用途</th>
286
+ </tr>
287
+ </thead>
288
+ <tbody>
289
+ <tr>
290
+ <td>per-request</td>
291
+ <td>輸入/輸出 token、快取命中、實際單價</td>
292
+ <td>找出最貴的 prompt</td>
293
+ </tr>
294
+ <tr>
295
+ <td>per-tenant</td>
296
+ <td>月累計成本、P95 單人成本</td>
297
+ <td>定價與配額依據</td>
298
+ </tr>
299
+ <tr>
300
+ <td>全域</td>
301
+ <td>每日花費、較昨日增幅</td>
302
+ <td>異常放大偵測</td>
303
+ </tr>
304
+ </tbody>
305
+ </table>
306
+ <p>異常偵測:<strong>單日成本超過 30 日移動平均 2 倍即告警</strong>,並查重試、prompt 洩漏、token 暴增。</p>
307
+ <h2 id="_7">實務檢查清單</h2>
308
+ <ul>
309
+ <li>[ ] 有版本化的 golden set,每次改動都跑</li>
310
+ <li>[ ] judge 與人一致率已量測,並記錄偏誤校正</li>
311
+ <li>[ ] trace 有 request id,可從回報追到單筆</li>
312
+ <li>[ ] 儀表板建在指標上,不依賴 experimental 的 span 屬性名</li>
313
+ <li>[ ] 有 p50/p95 分佈圖,不只看平均</li>
314
+ <li>[ ] 拒答率、解析失敗率、重問率都有告警</li>
315
+ <li>[ ] 每次模型或 prompt 變更都走 A/B 或 shadow</li>
316
+ </ul>
317
+ <h2 id="_8">常見誤解</h2>
318
+ <table>
319
+ <thead>
320
+ <tr>
321
+ <th>誤解</th>
322
+ <th>事實</th>
323
+ </tr>
324
+ </thead>
325
+ <tbody>
326
+ <tr>
327
+ <td>「LLM-judge 分數就是品質」</td>
328
+ <td>它是有偏誤的代理指標,需與人工對齊</td>
329
+ </tr>
330
+ <tr>
331
+ <td>「平均延遲達標就沒問題」</td>
332
+ <td>體驗由 p95 決定,平均值會掩蓋長尾</td>
333
+ </tr>
334
+ <tr>
335
+ <td>「OTel GenAI 已是穩定標準」</td>
336
+ <td>2026-11 仍全部是 Development</td>
337
+ </tr>
338
+ <tr>
339
+ <td>「自架可觀測性一定比較省」</td>
340
+ <td>省授權費,貴的是 4–5 個有狀態服務的維運</td>
341
+ </tr>
342
+ </tbody>
343
+ </table></article><p style="margin-top:28px;color:var(--muted);font-size:13px"><a href="index.html">← 回首頁</a></p></main></div></body></html>
08_上線架構與維運成本.html ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
2
+ <title>08 · 上線架構與維運成本 — AI/ML 應用工程主題</title>
3
+ <style>
4
+ :root{--bg:#0b1020;--panel:#121a2e;--panel2:#0f1628;--text:#e6edf7;--muted:#9fb0c8;--accent:#7aa2ff;--accent2:#4fd1c5;--border:#22304d;--code:#0a1222}
5
+ *{box-sizing:border-box} html,body{margin:0;padding:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Noto Sans TC","PingFang TC","Microsoft JhengHei",sans-serif;line-height:1.7}
6
+ a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
7
+ .layout{display:flex;min-height:100vh}
8
+ nav{width:290px;min-width:240px;background:var(--panel2);border-right:1px solid var(--border);padding:18px 14px;position:sticky;top:0;height:100vh;overflow:auto}
9
+ nav h2{font-size:15px;margin:0 0 10px 0;color:var(--muted);font-weight:600;letter-spacing:.02em}
10
+ nav .brand{font-size:17px;font-weight:700;color:var(--text);margin-bottom:14px;display:block}
11
+ nav ul{list-style:none;padding:0;margin:0}
12
+ nav li{margin:4px 0}
13
+ nav a{display:block;padding:6px 8px;border-radius:8px;color:var(--text);font-size:14px}
14
+ nav a:hover{background:rgba(122,162,255,.12);text-decoration:none}
15
+ nav a.active{background:rgba(122,162,255,.18);color:#fff}
16
+ main{flex:1;max-width:1100px;padding:28px 34px 60px}
17
+ h1{font-size:30px;margin:0 0 8px} h2{font-size:22px;margin:28px 0 10px;border-bottom:1px solid var(--border);padding-bottom:4px}
18
+ h3{font-size:18px;margin:22px 0 8px} p{margin:10px 0}
19
+ blockquote{margin:10px 0;padding:8px 14px;border-left:4px solid var(--accent);background:rgba(122,162,255,.08);color:var(--muted);border-radius:6px}
20
+ code{background:var(--code);padding:1px 6px;border-radius:5px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.92em}
21
+ pre{background:var(--code);border:1px solid var(--border);border-radius:10px;padding:14px;overflow:auto}
22
+ pre code{background:none;padding:0}
23
+ .table-wrap{overflow-x:auto;margin:14px 0;border:1px solid var(--border);border-radius:10px}
24
+ table{border-collapse:collapse;width:100%;min-width:520px;font-size:14px}
25
+ th,td{border-bottom:1px solid var(--border);padding:9px 12px;text-align:left;vertical-align:top}
26
+ th{background:#16223a;color:#dfe8f5;font-weight:600;position:sticky;top:0}
27
+ tr:nth-child(even) td{background:rgba(255,255,255,.02)}
28
+ tr:hover td{background:rgba(122,162,255,.06)}
29
+ ul,ol{padding-left:22px} li{margin:4px 0}
30
+ .cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:14px;margin-top:18px}
31
+ .card{background:var(--panel);border:1px solid var(--border);border-radius:12px;padding:14px}
32
+ .card h3{margin:0 0 6px;font-size:16px;border:0} .card p{margin:0;color:var(--muted);font-size:14px}
33
+ .badge{display:inline-block;background:rgba(79,209,197,.15);color:var(--accent2);padding:2px 8px;border-radius:999px;font-size:12px;margin-bottom:14px}
34
+ @media(max-width:900px){.layout{flex-direction:column} nav{position:relative;width:auto;height:auto;border-right:0;border-bottom:1px solid var(--border)} main{padding:18px}}
35
+ </style></head><body><div class="layout"><nav><a class="brand" href="index.html">AI/ML 應用工程主題</a><h2>文檔導覽</h2><ul><li><a href="index.html" class="">首頁</a></li><li><a href="00_主題總覽.html" class="">00 · AI/ML 應用工程主題總覽</a></li><li><a href="01_本地LLM部署與推理優化.html" class="">01 · 本地 LLM 部署與推理優化</a></li><li><a href="02_模型選型與成本算力.html" class="">02 · 模型選型與成本算力</a></li><li><a href="03_RAG與向量資料庫.html" class="">03 · RAG 與向量資料庫</a></li><li><a href="04_Agent框架與工具呼叫.html" class="">04 · Agent 框架與工具呼叫</a></li><li><a href="05_微調與對齊流程.html" class="">05 · 微調與對齊流程</a></li><li><a href="06_ML工具鏈與實驗管理.html" class="">06 · ML 工具鏈與實驗管理</a></li><li><a href="07_評估與可觀測性.html" class="">07 · 評估與可觀測性</a></li><li><a href="08_上線架構與維運成本.html" class="active">08 · 上線架構與維運成本</a></li></ul></nav><main><article><h1 id="08">08 · 上線架構與維運成本</h1>
36
+ <blockquote>
37
+ <p>來源:本機技能沉澱(無單一原始文件;以官方文件與實務經驗綜合)
38
+ 網路充實:2026-11-12(查證 2026-11 GPU 現行租金、vLLM/SGLang 前綴快取機制、prompt caching 各家倍率、語意快取誤命中實務數據)</p>
39
+ </blockquote>
40
+ <h2 id="_1">一、部署拓撲</h2>
41
+ <table>
42
+ <thead>
43
+ <tr>
44
+ <th>拓撲</th>
45
+ <th>適用</th>
46
+ <th>代價</th>
47
+ </tr>
48
+ </thead>
49
+ <tbody>
50
+ <tr>
51
+ <td>單體 FastAPI(含模型)</td>
52
+ <td>原型、內部工具、&lt; 10 QPS</td>
53
+ <td>無法獨立擴縮,重啟即斷線</td>
54
+ </tr>
55
+ <tr>
56
+ <td>推論服務 + 應用服務分離</td>
57
+ <td>正式產品預設</td>
58
+ <td>多一層網路與版本管理</td>
59
+ </tr>
60
+ <tr>
61
+ <td>Serverless GPU</td>
62
+ <td>尖離峰差異大、批次</td>
63
+ <td>冷啟動(載入 8B 約數十秒)</td>
64
+ </tr>
65
+ <tr>
66
+ <td>自架 K8s + GPU Operator</td>
67
+ <td>多團隊、多模型、要配額</td>
68
+ <td>維運門檻最高</td>
69
+ </tr>
70
+ </tbody>
71
+ </table>
72
+ <p>結論:<strong>先做「應用 + 推論」兩層分離,別一開始上 K8s</strong>;單一模型、&lt; 20 QPS 時複雜度大於收益。</p>
73
+ <h2 id="_2">二、擴縮策略</h2>
74
+ <ul>
75
+ <li><strong>佇列 + worker</strong>:請求進佇列、worker 固定併發拉取;可預測、可背壓,代價是多一跳延遲。</li>
76
+ <li><strong>連續批次</strong>:vLLM/SGLang 預設,把不同請求的 decode 拼進同一批,GPU 利用率可從 &lt; 20% 拉到 60%+。自架 LLM 最重要的一項。</li>
77
+ <li><strong>多模型共存</strong>:80GB 卡可放一個 30B FP8 或數個 8B,但 KV cache 會互擠,須限制各自 <code>gpu_memory_utilization</code>。</li>
78
+ <li><strong>GPU 共享</strong>:MIG 適合硬隔離推論;時間切片不適合 LLM(KV cache 被換出)。</li>
79
+ </ul>
80
+ <p>指標看<strong>佇列深度與 TTFT p95</strong>,別看 GPU 使用率——它常年在 90% 卻仍在排隊。</p>
81
+ <h2 id="_3">三、快取策略</h2>
82
+ <table>
83
+ <thead>
84
+ <tr>
85
+ <th>層</th>
86
+ <th>命中率(約)</th>
87
+ <th>何時開</th>
88
+ </tr>
89
+ </thead>
90
+ <tbody>
91
+ <tr>
92
+ <td>精確快取(hash)</td>
93
+ <td>取決於流量重複度</td>
94
+ <td>一律先開</td>
95
+ </tr>
96
+ <tr>
97
+ <td>前綴/KV 快取</td>
98
+ <td>agent、多輪、固定 RAG 模板 50–90%</td>
99
+ <td>一律先開</td>
100
+ </tr>
101
+ <tr>
102
+ <td>供應商 prompt caching</td>
103
+ <td>視前綴共用度</td>
104
+ <td>有長 system prompt 就開</td>
105
+ </tr>
106
+ <tr>
107
+ <td>語意快取</td>
108
+ <td>常見 20–45%</td>
109
+ <td>低風險 FAQ 才開</td>
110
+ </tr>
111
+ </tbody>
112
+ </table>
113
+ <p>Prompt caching(2026-11,需查證):OpenAI 對 ≥1,024 token 前綴自動啟用,cached input 約原價 10%,但 GPT-5.6 起<strong>寫入另計</strong>(30 分 1.25×、12 小時 2.5×);Anthropic 顯式 breakpoint,讀 0.1×、寫 1.25×(5 分)或 2×(1 小時)。</p>
114
+ <p>語意快取鐵律:<strong>上線前量「假命中率」,不是只看命中率。</strong> 閾值從 0.95 起下調,甜蜜點約 0.92,低於 0.88 就在賭。對話歷史必須進 cache key,並抽樣重生成比對命中答案。</p>
115
+ <h2 id="_4">四、限流與配額</h2>
116
+ <table>
117
+ <thead>
118
+ <tr>
119
+ <th>機制</th>
120
+ <th>做法</th>
121
+ </tr>
122
+ </thead>
123
+ <tbody>
124
+ <tr>
125
+ <td>Token-based 限流</td>
126
+ <td>按輸入+輸出 token 計,非按請求數</td>
127
+ </tr>
128
+ <tr>
129
+ <td>per-tenant 配額</td>
130
+ <td>每日/每月 token 上限+burst,防單客拖垮全站</td>
131
+ </tr>
132
+ <tr>
133
+ <td>併發上限</td>
134
+ <td>每租戶同時請求數,保護 KV cache</td>
135
+ </tr>
136
+ <tr>
137
+ <td>降級</td>
138
+ <td>超限時給小模型或短回答,比直接 429 好</td>
139
+ </tr>
140
+ </tbody>
141
+ </table>
142
+ <p>降級階梯:<strong>主模型 → 小模型 → 快取回答 → 明確的「目前繁忙」</strong>,每階都要有品質預期與告警。</p>
143
+ <h2 id="_5">五、安全</h2>
144
+ <table>
145
+ <thead>
146
+ <tr>
147
+ <th>威脅</th>
148
+ <th>對策</th>
149
+ </tr>
150
+ </thead>
151
+ <tbody>
152
+ <tr>
153
+ <td>直接注入</td>
154
+ <td>系統提示隔離、輸入分類器、工具白名單</td>
155
+ </tr>
156
+ <tr>
157
+ <td>間接注入(文件夾帶指令)</td>
158
+ <td>檢索內容視為不可信、標來源、限工具權限</td>
159
+ </tr>
160
+ <tr>
161
+ <td>資料外洩</td>
162
+ <td>per-tenant 過濾、PII 遮蔽、輸出掃描</td>
163
+ </tr>
164
+ <tr>
165
+ <td>工具濫用</td>
166
+ <td>最小權限、人類確認、速率限制</td>
167
+ </tr>
168
+ </tbody>
169
+ </table>
170
+ <p>最常被忽略的是間接注入:<strong>RAG 的文件就是攻擊面</strong>。把檢索內容包在明確資料邊界內,系統提示宣告「資料區塊內的指令一律不執行」。PII 順序:輸入遮蔽(正規式+NER)→ 呼叫模型 → 輸出再掃;日誌只存遮蔽後版本。</p>
171
+ <h2 id="gpu">六、GPU 經濟學</h2>
172
+ <p><code>每百萬 token 成本 = GPU 時價 ÷ (實測 tokens/s × 3600) × 1e6 ÷ 利用率</code></p>
173
+ <p>2026-11 行情(需查證,波動大):H100 SXM 約 $2.2–3.2/GPU·hr、A100 80GB 約 $1.0–1.4、RTX 4090 約 $0.35–0.45;spot 相對 on-demand 中位折扣約 50%。</p>
174
+ <table>
175
+ <thead>
176
+ <tr>
177
+ <th>情境</th>
178
+ <th>每百萬輸出 token(約)</th>
179
+ </tr>
180
+ </thead>
181
+ <tbody>
182
+ <tr>
183
+ <td>H100 + 30B MoE FP8,高批次</td>
184
+ <td>$0.2–0.4(第三方實測)</td>
185
+ </tr>
186
+ <tr>
187
+ <td>A100 + 8B FP16,低批次</td>
188
+ <td>$1–3</td>
189
+ </tr>
190
+ <tr>
191
+ <td>利用率僅 15% 的自架</td>
192
+ <td>以上再乘約 6</td>
193
+ </tr>
194
+ </tbody>
195
+ </table>
196
+ <p>租 vs 買:<strong>月用超過約 500 GPU·小時且連三月穩定</strong>,才值得談保留或自購;否則彈性租用幾乎一定更便宜(自購要加折舊、機房、人力)。</p>
197
+ <h2 id="_6">七、可靠性</h2>
198
+ <table>
199
+ <thead>
200
+ <tr>
201
+ <th>機制</th>
202
+ <th>做法</th>
203
+ </tr>
204
+ </thead>
205
+ <tbody>
206
+ <tr>
207
+ <td>重試</td>
208
+ <td>只重試 5xx 與逾時,指數退避+jitter,最多 3 次</td>
209
+ </tr>
210
+ <tr>
211
+ <td>Fallback</td>
212
+ <td>主模型失敗或逾時 → 不同供應商的次級模型</td>
213
+ </tr>
214
+ <tr>
215
+ <td>健康檢查</td>
216
+ <td><code>/healthz</code> 要真跑一次 mini inference</td>
217
+ </tr>
218
+ <tr>
219
+ <td>逾時分層</td>
220
+ <td>連線 5s、首 token 10–30s、整體 60s</td>
221
+ </tr>
222
+ </tbody>
223
+ </table>
224
+ <p><strong>串流重試特別危險</strong>:已輸出後失敗再重試會造成重複內容。做法是標記「已開始輸出」後改為回報錯誤。</p>
225
+ <h2 id="_7">八、上線與回滾</h2>
226
+ <p>上線前必過:模型與 prompt 版本釘死、有 golden set 回歸報告、有配額與成本告警、可追單筆 trace、壓測過 p95。</p>
227
+ <p>回滾三層:<strong>路由層</strong>(flag 切舊模型/prompt,秒級)、<strong>版本層</strong>(image 與權重帶版號,可 <code>rollout undo</code>)、<strong>資料層</strong>(新舊並存至少一週)。</p>
228
+ <p>觸發條件先寫死:<strong>錯誤率 &gt; 5%、p95 翻倍、thumbs down 週環比 +50%、單日成本超標 2 倍</strong>,任一成立即回滾。</p>
229
+ <h2 id="_8">實務檢查清單</h2>
230
+ <ul>
231
+ <li>[ ] 應用與推論分離,推論層可獨立擴縮</li>
232
+ <li>[ ] 已開精確快取與前綴快取,並量過命中率</li>
233
+ <li>[ ] 語意快取有假命中率量測與抽樣重生成</li>
234
+ <li>[ ] 限流以 token 計,且有 per-tenant 配額</li>
235
+ <li>[ ] 檢索內容視為不可信,工具權限最小化</li>
236
+ <li>[ ] 有 fallback 與分層逾時</li>
237
+ <li>[ ] 每百萬 token 成本有實測,回滾條件可一鍵執行</li>
238
+ </ul>
239
+ <h2 id="_9">常見誤解</h2>
240
+ <table>
241
+ <thead>
242
+ <tr>
243
+ <th>誤解</th>
244
+ <th>事實</th>
245
+ </tr>
246
+ </thead>
247
+ <tbody>
248
+ <tr>
249
+ <td>「GPU 使用率高就是效率好」</td>
250
+ <td>真正瓶頸常是排隊與 KV cache 不足</td>
251
+ </tr>
252
+ <tr>
253
+ <td>「語意快取一定省錢」</td>
254
+ <td>假命中的代價是信任,且每筆都要付 embedding</td>
255
+ </tr>
256
+ <tr>
257
+ <td>「自架一定比 API 便宜」</td>
258
+ <td>利用率低於約 30% 時,多數情況 API 更便宜</td>
259
+ </tr>
260
+ <tr>
261
+ <td>「serverless GPU 省最多」</td>
262
+ <td>冷啟動對互動式產品是致命的</td>
263
+ </tr>
264
+ </tbody>
265
+ </table></article><p style="margin-top:28px;color:var(--muted);font-size:13px"><a href="index.html">← 回首頁</a></p></main></div></body></html>
README.md CHANGED
@@ -1,10 +1,12 @@
1
  ---
2
- title: Ai Ml App Engineering
3
- emoji: 📈
4
- colorFrom: blue
5
- colorTo: green
6
  sdk: static
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
1
  ---
2
+ title: AI/ML 應用工程主題
3
+ emoji: 📚
4
+ colorFrom: indigo
5
+ colorTo: blue
6
  sdk: static
7
  pinned: false
8
  ---
9
 
10
+ # AI/ML 應用工程主題
11
+
12
+ 靜態知識庫(由 Markdown 自動轉換;免費 Static Space)。
index.html CHANGED
@@ -1,19 +1,35 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html><html lang="zh-Hant"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
2
+ <title>首頁 — AI/ML 應用工程主題</title>
3
+ <style>
4
+ :root{--bg:#0b1020;--panel:#121a2e;--panel2:#0f1628;--text:#e6edf7;--muted:#9fb0c8;--accent:#7aa2ff;--accent2:#4fd1c5;--border:#22304d;--code:#0a1222}
5
+ *{box-sizing:border-box} html,body{margin:0;padding:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Noto Sans TC","PingFang TC","Microsoft JhengHei",sans-serif;line-height:1.7}
6
+ a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
7
+ .layout{display:flex;min-height:100vh}
8
+ nav{width:290px;min-width:240px;background:var(--panel2);border-right:1px solid var(--border);padding:18px 14px;position:sticky;top:0;height:100vh;overflow:auto}
9
+ nav h2{font-size:15px;margin:0 0 10px 0;color:var(--muted);font-weight:600;letter-spacing:.02em}
10
+ nav .brand{font-size:17px;font-weight:700;color:var(--text);margin-bottom:14px;display:block}
11
+ nav ul{list-style:none;padding:0;margin:0}
12
+ nav li{margin:4px 0}
13
+ nav a{display:block;padding:6px 8px;border-radius:8px;color:var(--text);font-size:14px}
14
+ nav a:hover{background:rgba(122,162,255,.12);text-decoration:none}
15
+ nav a.active{background:rgba(122,162,255,.18);color:#fff}
16
+ main{flex:1;max-width:1100px;padding:28px 34px 60px}
17
+ h1{font-size:30px;margin:0 0 8px} h2{font-size:22px;margin:28px 0 10px;border-bottom:1px solid var(--border);padding-bottom:4px}
18
+ h3{font-size:18px;margin:22px 0 8px} p{margin:10px 0}
19
+ blockquote{margin:10px 0;padding:8px 14px;border-left:4px solid var(--accent);background:rgba(122,162,255,.08);color:var(--muted);border-radius:6px}
20
+ code{background:var(--code);padding:1px 6px;border-radius:5px;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.92em}
21
+ pre{background:var(--code);border:1px solid var(--border);border-radius:10px;padding:14px;overflow:auto}
22
+ pre code{background:none;padding:0}
23
+ .table-wrap{overflow-x:auto;margin:14px 0;border:1px solid var(--border);border-radius:10px}
24
+ table{border-collapse:collapse;width:100%;min-width:520px;font-size:14px}
25
+ th,td{border-bottom:1px solid var(--border);padding:9px 12px;text-align:left;vertical-align:top}
26
+ th{background:#16223a;color:#dfe8f5;font-weight:600;position:sticky;top:0}
27
+ tr:nth-child(even) td{background:rgba(255,255,255,.02)}
28
+ tr:hover td{background:rgba(122,162,255,.06)}
29
+ ul,ol{padding-left:22px} li{margin:4px 0}
30
+ .cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:14px;margin-top:18px}
31
+ .card{background:var(--panel);border:1px solid var(--border);border-radius:12px;padding:14px}
32
+ .card h3{margin:0 0 6px;font-size:16px;border:0} .card p{margin:0;color:var(--muted);font-size:14px}
33
+ .badge{display:inline-block;background:rgba(79,209,197,.15);color:var(--accent2);padding:2px 8px;border-radius:999px;font-size:12px;margin-bottom:14px}
34
+ @media(max-width:900px){.layout{flex-direction:column} nav{position:relative;width:auto;height:auto;border-right:0;border-bottom:1px solid var(--border)} main{padding:18px}}
35
+ </style></head><body><div class="layout"><nav><a class="brand" href="index.html">AI/ML 應用工程主題</a><h2>文檔導覽</h2><ul><li><a href="index.html" class="active">首頁</a></li><li><a href="00_主題總覽.html" class="">00 · AI/ML 應用工程主題總覽</a></li><li><a href="01_本地LLM部署與推理優化.html" class="">01 · 本地 LLM 部署與推理優化</a></li><li><a href="02_模型選型與成本算力.html" class="">02 · 模型選型與成本算力</a></li><li><a href="03_RAG與向量資料庫.html" class="">03 · RAG 與向量資料庫</a></li><li><a href="04_Agent框架與工具呼叫.html" class="">04 · Agent 框架與工具呼叫</a></li><li><a href="05_微調與對齊流程.html" class="">05 · 微調與對齊流程</a></li><li><a href="06_ML工具鏈與實驗管理.html" class="">06 · ML 工具鏈與實驗管理</a></li><li><a href="07_評估與可觀測性.html" class="">07 · 評估與可觀測性</a></li><li><a href="08_上線架構與維運成本.html" class="">08 · 上線架構與維運成本</a></li></ul></nav><main><span class="badge">Static 知識庫 · 9 節</span><h1>AI/ML 應用工程主題</h1><p>由 Markdown 蒸餾文檔自動轉為靜態站(深色主題、可離線閱讀)。左側導覽可切換各子主題。</p><div class="cards"><div class="card"><h3><a href="00_主題總覽.html">00 · AI/ML 應用工程主題總覽</a></h3><p>本主題聚焦「��模型用起來」而非訓練理論:從本地推理、成本選型、檢索增強、代理編排、微調對齊、實驗工具、評估監控到上線維運,形成一條可落地的工程鏈。</p></div><div class="card"><h3><a href="01_本地LLM部署與推理優化.html">01 · 本地 LLM 部署與推理優化</a></h3><p>TGI 已於 2025-12 轉維護模式、2026-03 封存,新專案直接跳過。選型關鍵不是「誰最快」,而是「你的流量是否共享前綴」:獨立 prompt 下 vLLM 與 SGLang 在 70B 級差距只有幾個百分點;共享前綴重(多輪、RAG、工具定義很長)時 SGLang 的命中率明顯較高。</p></div><div class="card"><h3><a href="02_模型選型與成本算力.html">02 · 模型選型與成本算力</a></h3><p>實務結論:能够用「一個抽象層+可換模型」寫的應用,就先這樣寫。把 model id、temperature、tool schema 收斂到一處設定,換模型是改一行而不是重寫。</p></div><div class="card"><h3><a href="03_RAG與向量資料庫.html">03 · RAG 與向量資料庫</a></h3><p>解析 → chunk → embedding → 索引 → 檢索 → rerank → 組 prompt</p></div><div class="card"><h3><a href="04_Agent框架與工具呼叫.html">04 · Agent 框架與工具呼叫</a></h3><p>模型不會「執行程式」,只輸出結構化物件:</p></div><div class="card"><h3><a href="05_微調與對齊流程.html">05 · 微調與對齊流程</a></h3><p>結論:知識走檢索、格式走 SFT、偏好走 DPO、成本走蒸餾。沒有 eval 與回滾就不要動模型。</p></div><div class="card"><h3><a href="06_ML工具鏈與實驗管理.html">06 · ML 工具鏈與實驗管理</a></h3><p>實務建議:uv 管 Python 套件、Docker image 釘 CUDA,兩者分責,勿在同一專案用 conda 與 uv 裝同一個 torch。</p></div><div class="card"><h3><a href="07_評估與可觀測性.html">07 · 評估與可觀測性</a></h3><p>人眼對 LLM 輸出有強烈確認偏誤:換個 prompt 後看到的 3 個例子都變好,就宣布改善。問題是回歸通常發生在你沒看的那 200 題上。</p></div><div class="card"><h3><a href="08_上線架構與維運成本.html">08 · 上線架構與維運成本</a></h3><p>結論:先做「應用 + 推論」兩層分離,別一開始上 K8s;單一模型、&lt; 20 QPS 時複雜度大於收益。</p></div></div></main></div></body></html>