Repository navigation
Expand file tree
/
Copy pathch19.html
More file actions
426 lines (388 loc) · 18.7 KB
/
Copy pathch19.html
File metadata and controls
426 lines (388 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="AI学习笔记 - 从零开始系统学习人工智能,涵盖前置基础、核心原理、工程实践、应用工具、进阶展望">
<meta name="keywords" content="AI, 人工智能, 机器学习, 深度学习, Python, 数学基础">
<meta name="author" content="AI Learner">
<title>第十九章:高级提示技术 - AI学习笔记</title>
<!-- Google Fonts - Inter -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<!-- Font Awesome Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<!-- KaTeX - 轻量级数学公式渲染 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js"
onload="renderMathInElement(document.body, {
delimiters: [
{left: '$$', right: '$$', display: true},
{left: '\\[', right: '\\]', display: true},
{left: '\\(', right: '\\)', display: false},
{left: '$', right: '$', display: false}
],
throwOnError: false
});"></script>
<!-- Prism.js 代码高亮 - tomorrow-night 主题 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-bash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script>
<!-- 自定义样式表 -->
<link rel="stylesheet" href="../../css/main.css">
<link rel="stylesheet" href="../../css/dark-mode.css">
<link rel="stylesheet" href="../../css/mobile-optimizations.css">
<!-- 内联CSS样式 -->
<!-- AI-Notes共享样式 -->
<link rel="stylesheet" href="assets/ai-notes.css">
</head>
<body>
<!-- ==================== 导航栏 ==================== -->
<nav class="ai-nav">
<a href="../../index.html" class="ai-nav-brand">
<i class="fas fa-brain"></i>
AI学习笔记
</a>
<ul class="ai-nav-links" id="navLinks">
<li><a href="../../index.html"><i class="fas fa-home"></i> 首页</a></li>
<li><a href="ch15.html"><i class="fas fa-robot"></i> Agent</a></li>
<li><a href="ch16.html"><i class="fas fa-tools"></i> 工具</a></li>
<li><a href="ch17.html"><i class="fas fa-book-open"></i> 知识库</a></li>
<li><a href="ch18.html"><i class="fas fa-layer-group"></i> 记忆</a></li>
<li><a href="ch19.html"><i class="fas fa-comment-dots"></i> 提示</a></li>
<li><a href="ch20.html"><i class="fas fa-database"></i> 数据</a></li>
<li><a href="ch21.html"><i class="fas fa-exclamation-circle"></i> 局限</a></li>
<li><a href="ch22.html"><i class="fas fa-toolbox"></i> 平台</a></li>
<li><a href="ch23.html"><i class="fas fa-history"></i> 简史</a></li>
<li><a href="ch24.html"><i class="fas fa-rocket"></i> 实战</a></li>
</ul>
<button class="ai-nav-toggle" id="navToggle" aria-label="切换导航">
<i class="fas fa-bars"></i>
</button>
</nav>
<!-- ==================== Hero Section ==================== -->
<section class="ai-hero">
<h1><i class="fas fa-brain"></i> AI 学习笔记</h1>
<p>第十九章:高级提示技术</p>
<div class="ai-hero-badges">
<span class="ai-hero-badge"><i class="fas fa-book"></i> 系统学习</span>
<span class="ai-hero-badge"><i class="fas fa-code"></i> 实践导向</span>
</div>
</section>
<!-- ==================== 主内容区域 ==================== -->
<div class="content-wrapper">
<!-- ========== 主内容 ========== -->
<main class="main-content">
<nav class="ai-chapter-nav">
<div class="breadcrumb">
<a href="../../index.html"><i class="fas fa-home"></i> 首页</a>
<span class="separator">/</span>
<span class="current">第十九章:高级提示技术</span>
</div>
<div class="chapter-nav-links">
<a href="ch18.html" class="prev-chapter"><i class="fas fa-arrow-left"></i> 上一章</a>
<a href="ch20.html" class="next-chapter">下一章 <i class="fas fa-arrow-right"></i></a>
</div>
</nav>
<!-- 第十九章:高级提示技术 -->
<!-- ============================================================ -->
<h2 class="ai-section-title"><i class="fas fa-comment-dots"></i> 第十九章:高级提示技术</h2>
<div class="ai-box ai-box--objectives">
<h3><i class="fas fa-bullseye"></i> 学习目标</h3>
<ul>
<li>掌握思维链(Chain of Thought)提示技术</li>
<li>理解Zero-shot和Few-shot提示的区别和适用场景</li>
<li>掌握ReAct提示模式</li>
<li>了解更多实用的Prompt技巧和评估方法</li>
</ul>
</div>
<div class="ai-box ai-box--prereq">
<h3><i class="fas fa-book"></i> 前置要求</h3>
<p>建议先学习<strong>第十一章:大语言模型(LLM)详解</strong>,了解LLM的基本原理和Prompt概念。</p>
</div>
<figure class="ai-figure">
<img src="https://trae-api-cn.mchost.guru/api/ide/v1/text_to_image?prompt=Prompt%20engineering%20techniques%20diagram%2C%20few-shot%20learning%2C%20chain%20of%20thought%20reasoning%20steps%2C%20clean%20educational%20infographic%20style%2C%20flat%20design%2C%20white%20background&image_size=landscape_4_3" alt="高级提示技术概念图" loading="lazy">
<figcaption><strong>图19-1</strong> 高级提示技术:通过少样本示例和思维链等结构化提示方法,引导LLM逐步推理以提升复杂问题的回答质量</figcaption>
</figure>
<div class="concept-section">
<h2><i class="fas fa-link"></i> 思维链(Chain of Thought, CoT)</h2>
<p><span class="keyword-highlight">思维链(Chain of Thought)</span>是一种通过引导LLM逐步推理来提升复杂问题回答质量的提示技术。核心思想是:让模型"展示思考过程",而不是直接给出答案。</p>
<div class="concept-box">
<h4><i class="fas fa-lightbulb"></i> 基本方法</h4>
<p>在提示词中添加"请一步一步思考"或"让我们逐步分析"等引导语,促使模型在给出最终答案前先展示推理过程。这种方法特别适用于数学计算、逻辑推理和多步骤问题。</p>
</div>
<h3>CoT效果流程</h3>
<div class="flow-diagram">
<div class="flow-step">
<div class="step-number">1</div>
<div class="step-content">
<h4>接收问题</h4>
<p>用户提出需要推理的复杂问题</p>
</div>
</div>
<div class="flow-arrow"><i class="fas fa-arrow-down"></i></div>
<div class="flow-step">
<div class="step-number">2</div>
<div class="step-content">
<h4>逐步推理</h4>
<p>模型展示分步思考过程</p>
</div>
</div>
<div class="flow-arrow"><i class="fas fa-arrow-down"></i></div>
<div class="flow-step">
<div class="step-number">3</div>
<div class="step-content">
<h4>得出结论</h4>
<p>基于推理过程给出准确答案</p>
</div>
</div>
</div>
<h3>CoT的典型应用</h3>
<ul>
<li><strong>数学问题</strong>:分步计算,减少计算错误</li>
<li><strong>逻辑推理</strong>:逐步分析条件,避免跳跃性错误</li>
<li><strong>多步决策</strong>:分解复杂决策为多个判断步骤</li>
<li><strong>代码生成</strong>:先分析需求,再设计逻辑,最后编写代码</li>
<li><strong>文本分析</strong>:逐步提取关键信息,综合得出结论</li>
</ul>
</div>
<div class="concept-section">
<h2><i class="fas fa-balance-scale"></i> Zero-shot vs Few-shot</h2>
<div class="concept-box">
<h4><i class="fas fa-info-circle"></i> Zero-shot提示</h4>
<p><strong>Zero-shot</strong>:不给任何示例,直接让模型完成任务。依赖模型预训练知识。</p>
<p>示例:<code>将以下英文翻译为中文:Hello World</code></p>
</div>
<div class="concept-box">
<h4><i class="fas fa-info-circle"></i> Few-shot提示</h4>
<p><strong>Few-shot</strong>:在提示中给出几个示例,让模型学习输入-输出模式后再完成任务。</p>
<p>示例:</p>
<p><code>正面 → 积极<br/>负面 → 消极<br/>一般 → 中性<br/>这个产品非常好 → ?</code></p>
<p>模型通过示例学会了"情感分类"的模式,输出"积极"。</p>
</div>
<h3>何时使用Few-shot?</h3>
<table class="comparison-table">
<tr>
<th>场景</th>
<th>推荐方式</th>
<th>原因</th>
</tr>
<tr>
<td>常见任务(翻译、摘要)</td>
<td>Zero-shot</td>
<td>模型已有丰富知识</td>
</tr>
<tr>
<td>特定格式输出</td>
<td>Few-shot</td>
<td>需要示例来明确格式要求</td>
</tr>
<tr>
<td>领域专业任务</td>
<td>Few-shot</td>
<td>示例帮助模型理解领域术语</td>
</tr>
<tr>
<td>分类/标注任务</td>
<td>Few-shot</td>
<td>示例明确分类标准</td>
</tr>
<tr>
<td>创意写作</td>
<td>Zero-shot</td>
<td>过多示例可能限制创造性</td>
</tr>
</table>
</div>
<div class="concept-section">
<h2><i class="fas fa-sync"></i> ReAct提示</h2>
<p><span class="keyword-highlight">ReAct提示</span>是将推理(Reasoning)和行动(Acting)结合的提示模式,让LLM在思考过程中交替进行推理和工具调用。</p>
<div class="concept-box">
<h4><i class="fas fa-comment-dots"></i> ReAct示例</h4>
<p><strong>问题</strong>:清华大学和北京大学哪个建校更早?</p>
<p><strong>Thought 1</strong>:我需要查询清华大学的建校时间。</p>
<p><strong>Action 1</strong>:搜索"清华大学建校时间"</p>
<p><strong>Observation 1</strong>:清华大学建于1911年</p>
<p><strong>Thought 2</strong>:我还需要查询北京大学的建校时间。</p>
<p><strong>Action 2</strong>:搜索"北京大学建校时间"</p>
<p><strong>Observation 2</strong>:北京大学建于1898年</p>
<p><strong>Thought 3</strong>:北京大学(1898年)比清华大学(1911年)建校更早。</p>
<p><strong>Answer</strong>:北京大学建校更早,建于1898年,比清华大学的1911年早了13年。</p>
</div>
</div>
<div class="concept-section">
<h2><i class="fas fa-star"></i> 更多Prompt技巧</h2>
<h3>1. 角色设定(Role Prompting)</h3>
<p>通过给LLM设定特定角色,可以显著提升回答的专业性和针对性。</p>
<pre><code class="language-python"># 角色设定示例
prompt = """
你是一位有20年经验的资深Python开发者和技术导师。
你的教学风格是:用简单的比喻解释复杂概念,提供可运行的代码示例,
并指出初学者常见的陷阱。
请解释什么是装饰器(Decorator)。
"""</code></pre>
<h3>2. 输出格式约束</h3>
<p>明确指定输出格式,确保LLM返回符合预期的结构化结果。</p>
<pre><code class="language-python"># 格式约束示例
prompt = """
请分析以下文本的情感,并以JSON格式返回结果:
{
"sentiment": "正面/负面/中性",
"confidence": 0.0-1.0,
"keywords": ["关键词1", "关键词2"],
"reason": "判断理由"
}
文本:这款手机的拍照效果非常出色,电池续航也很给力!
"""</code></pre>
<h3>3. 分解任务(Task Decomposition)</h3>
<p>将复杂任务分解为多个简单子任务,逐步完成。</p>
<pre><code class="language-python"># 任务分解示例
prompt = """
请按以下步骤完成任务:
步骤1:从文本中提取所有人名
步骤2:分析每个人物的关系
步骤3:生成人物关系图描述
步骤4:总结主要人物和核心冲突
文本:...
"""</code></pre>
<h3>4. 提供上下文(Context Provision)</h3>
<p>在提示中提供充分的背景信息,帮助LLM给出更准确的回答。</p>
<pre><code class="language-python"># 提供上下文示例
prompt = """
背景信息:我们是一家电商公司,主要销售3C数码产品。
目标用户:18-35岁的年轻消费者。
当前问题:最近一个月,网站流量增长了30%,但转化率下降了15%。
请基于以上背景信息,分析可能的原因并给出改进建议。
"""</code></pre>
</div>
<div class="concept-section">
<h2><i class="fas fa-check-double"></i> Prompt评估方法</h2>
<p>评估Prompt质量需要从多个维度考量:</p>
<table class="comparison-table">
<tr>
<th>评估维度</th>
<th>说明</th>
<th>评估方法</th>
</tr>
<tr>
<td><strong>一致性(Consistency)</strong></td>
<td>相同Prompt多次运行,结果是否稳定一致</td>
<td>多次运行统计结果分布</td>
</tr>
<tr>
<td><strong>相关性(Relevance)</strong></td>
<td>输出是否与用户意图相关</td>
<td>人工评估或使用LLM-as-Judge</td>
</tr>
<tr>
<td><strong>完整性(Completeness)</strong></td>
<td>输出是否完整回答了所有问题</td>
<td>检查清单逐项验证</td>
</tr>
<tr>
<td><strong>准确性(Accuracy)</strong></td>
<td>输出内容是否事实正确</td>
<td>与标准答案对比</td>
</tr>
<tr>
<td><strong>格式合规</strong></td>
<td>输出是否符合要求的格式</td>
<td>格式解析验证</td>
</tr>
</table>
</div>
<div class="ai-box ai-box--summary">
<h3><i class="fas fa-clipboard-list"></i> 本章小结</h3>
<ol>
<li>思维链(CoT)通过引导LLM逐步推理来提升复杂问题的回答质量,特别适用于数学、逻辑推理和多步骤任务。</li>
<li>Zero-shot直接让模型凭预训练知识完成任务,Few-shot通过提供示例帮助模型理解任务模式。格式敏感和领域专业任务推荐Few-shot。</li>
<li>ReAct提示将推理和行动结合,让LLM在思考过程中交替进行推理和工具调用,适合需要获取外部信息的任务。</li>
<li>高级Prompt技巧包括角色设定、输出格式约束、任务分解和提供上下文,可以显著提升LLM输出的质量和可控性。</li>
<li>Prompt评估需关注一致性、相关性、完整性、准确性和格式合规等多个维度。</li>
</ol>
</div>
<div class="ai-box ai-box--exercise">
<h3><i class="fas fa-pencil-alt"></i> 练习题</h3>
<ol>
<li>
<p><strong>思维链(CoT)提示的核心思想是什么?</strong></p>
<p>A. 让模型一次性给出答案<br/>B. 让模型展示逐步推理过程后再给出答案<br/>C. 让模型调用更多工具<br/>D. 让模型使用更长的上下文</p>
<p><strong>答案:B</strong>。CoT的核心思想是引导LLM"展示思考过程",通过逐步推理来提升复杂问题的回答质量,减少跳跃性错误。</p>
</li>
<li>
<p><strong>以下哪种场景最适合使用Few-shot提示?</strong></p>
<p>A. 将"你好"翻译为英文<br/>B. 按特定格式提取文档中的实体<br/>C. 写一首关于春天的诗<br/>D. 总结一篇文章的主要观点</p>
<p><strong>答案:B</strong>。Few-shot提示适合需要明确输出格式的任务,如实体提取、分类标注等。翻译和写诗等常见任务用Zero-shot即可。</p>
</li>
<li>
<p><strong>以下哪个不是Prompt评估的维度?</strong></p>
<p>A. 一致性<br/>B. 相关性<br/>C. 模型参数量<br/>D. 完整性</p>
<p><strong>答案:C</strong>。Prompt评估的维度包括一致性、相关性、完整性、准确性和格式合规。模型参数量是模型本身的属性,不是Prompt质量的评估维度。</p>
</li>
</ol>
</div>
<!-- ============================================================ -->
</main>
</div>
<!-- 页脚 -->
<!-- ============================================================ -->
<footer class="footer">
<div class="footer-content">
<div class="footer-section">
<h3><i class="fas fa-graduation-cap"></i> AI学习笔记</h3>
<p>从零开始系统学习人工智能</p>
<p>涵盖前置基础、核心原理、工程实践、应用工具、进阶展望</p>
</div>
<div class="footer-section">
<h3><i class="fas fa-bookmark"></i> 内容导航</h3>
<p>第1-11章:基础与核心(见 ai-notes-core.html)</p>
<p>第12-23章:工程实践与进阶(本文件)</p>
<p>附录A:GPU与算力指南</p>
<p>附录B:术语表</p>
</div>
<div class="footer-section">
<h3><i class="fas fa-info-circle"></i> 说明</h3>
<p>本笔记仅供学习参考,内容如有错误欢迎指正。</p>
<p>AI技术发展迅速,部分信息可能随时间变化。</p>
</div>
</div>
<div class="footer-bottom">
<p>AI学习笔记 © 2024 | 持续更新中</p>
</div>
</footer>
</div><!-- /ai-notes-container -->
<!-- Scripts -->
<script src="../../js/mobile-menu.js"></script>
<script src="../../js/dark-mode.js"></script>
<script>
// 目录滚动高亮
(function() {
const tocLinks = document.querySelectorAll('.toc-sidebar a');
const sections = [];
tocLinks.forEach(link => {
const id = link.getAttribute('href');
if (id && id.startsWith('#')) {
const el = document.querySelector(id);
if (el) sections.push({ link, el });
}
});
function updateActive() {
const scrollY = window.scrollY + 100;
let current = sections[0];
for (const s of sections) {
if (s.el.offsetTop <= scrollY) current = s;
}
tocLinks.forEach(l => l.classList.remove('active'));
if (current) current.link.classList.add('active');
}
window.addEventListener('scroll', updateActive);
updateActive();
})();
</script>
<script src="assets/ai-notes.js"></script>
</body>
</html>