code-abyss 1.6.16 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/package.json +2 -2
  2. package/skills/SKILL.md +24 -16
  3. package/skills/domains/ai/SKILL.md +2 -2
  4. package/skills/domains/ai/prompt-and-eval.md +279 -0
  5. package/skills/domains/architecture/SKILL.md +2 -3
  6. package/skills/domains/architecture/security-arch.md +87 -0
  7. package/skills/domains/data-engineering/SKILL.md +188 -26
  8. package/skills/domains/development/SKILL.md +1 -4
  9. package/skills/domains/devops/SKILL.md +3 -5
  10. package/skills/domains/devops/performance.md +63 -0
  11. package/skills/domains/devops/testing.md +97 -0
  12. package/skills/domains/frontend-design/SKILL.md +12 -3
  13. package/skills/domains/frontend-design/claymorphism/SKILL.md +117 -0
  14. package/skills/domains/frontend-design/claymorphism/references/tokens.css +52 -0
  15. package/skills/domains/frontend-design/engineering.md +287 -0
  16. package/skills/domains/frontend-design/glassmorphism/SKILL.md +138 -0
  17. package/skills/domains/frontend-design/glassmorphism/references/tokens.css +32 -0
  18. package/skills/domains/frontend-design/liquid-glass/SKILL.md +135 -0
  19. package/skills/domains/frontend-design/liquid-glass/references/tokens.css +81 -0
  20. package/skills/domains/frontend-design/neubrutalism/SKILL.md +141 -0
  21. package/skills/domains/frontend-design/neubrutalism/references/tokens.css +44 -0
  22. package/skills/domains/infrastructure/SKILL.md +174 -34
  23. package/skills/domains/mobile/SKILL.md +211 -21
  24. package/skills/domains/orchestration/SKILL.md +1 -0
  25. package/skills/domains/security/SKILL.md +4 -6
  26. package/skills/domains/security/blue-team.md +57 -0
  27. package/skills/domains/security/red-team.md +54 -0
  28. package/skills/domains/security/threat-intel.md +50 -0
  29. package/skills/orchestration/multi-agent/SKILL.md +195 -46
  30. package/skills/run_skill.js +134 -0
  31. package/skills/tools/gen-docs/SKILL.md +6 -4
  32. package/skills/tools/gen-docs/scripts/doc_generator.js +349 -0
  33. package/skills/tools/verify-change/SKILL.md +8 -6
  34. package/skills/tools/verify-change/scripts/change_analyzer.js +270 -0
  35. package/skills/tools/verify-module/SKILL.md +6 -4
  36. package/skills/tools/verify-module/scripts/module_scanner.js +145 -0
  37. package/skills/tools/verify-quality/SKILL.md +5 -3
  38. package/skills/tools/verify-quality/scripts/quality_checker.js +276 -0
  39. package/skills/tools/verify-security/SKILL.md +7 -5
  40. package/skills/tools/verify-security/scripts/security_scanner.js +133 -0
  41. package/skills/__pycache__/run_skill.cpython-312.pyc +0 -0
  42. package/skills/domains/COVERAGE_PLAN.md +0 -232
  43. package/skills/domains/ai/model-evaluation.md +0 -790
  44. package/skills/domains/ai/prompt-engineering.md +0 -703
  45. package/skills/domains/architecture/compliance.md +0 -299
  46. package/skills/domains/architecture/data-security.md +0 -184
  47. package/skills/domains/data-engineering/data-pipeline.md +0 -762
  48. package/skills/domains/data-engineering/data-quality.md +0 -894
  49. package/skills/domains/data-engineering/stream-processing.md +0 -791
  50. package/skills/domains/development/dart.md +0 -963
  51. package/skills/domains/development/kotlin.md +0 -834
  52. package/skills/domains/development/php.md +0 -659
  53. package/skills/domains/development/swift.md +0 -755
  54. package/skills/domains/devops/e2e-testing.md +0 -914
  55. package/skills/domains/devops/performance-testing.md +0 -734
  56. package/skills/domains/devops/testing-strategy.md +0 -667
  57. package/skills/domains/frontend-design/build-tools.md +0 -743
  58. package/skills/domains/frontend-design/performance.md +0 -734
  59. package/skills/domains/frontend-design/testing.md +0 -699
  60. package/skills/domains/infrastructure/gitops.md +0 -735
  61. package/skills/domains/infrastructure/iac.md +0 -855
  62. package/skills/domains/infrastructure/kubernetes.md +0 -1018
  63. package/skills/domains/mobile/android-dev.md +0 -979
  64. package/skills/domains/mobile/cross-platform.md +0 -795
  65. package/skills/domains/mobile/ios-dev.md +0 -931
  66. package/skills/domains/security/secrets-management.md +0 -834
  67. package/skills/domains/security/supply-chain.md +0 -931
  68. package/skills/domains/security/threat-modeling.md +0 -828
  69. package/skills/run_skill.py +0 -153
  70. package/skills/tests/README.md +0 -225
  71. package/skills/tests/SUMMARY.md +0 -362
  72. package/skills/tests/__init__.py +0 -3
  73. package/skills/tests/__pycache__/test_change_analyzer.cpython-312.pyc +0 -0
  74. package/skills/tests/__pycache__/test_doc_generator.cpython-312.pyc +0 -0
  75. package/skills/tests/__pycache__/test_module_scanner.cpython-312.pyc +0 -0
  76. package/skills/tests/__pycache__/test_quality_checker.cpython-312.pyc +0 -0
  77. package/skills/tests/__pycache__/test_security_scanner.cpython-312.pyc +0 -0
  78. package/skills/tests/test_change_analyzer.py +0 -558
  79. package/skills/tests/test_doc_generator.py +0 -538
  80. package/skills/tests/test_module_scanner.py +0 -376
  81. package/skills/tests/test_quality_checker.py +0 -516
  82. package/skills/tests/test_security_scanner.py +0 -426
  83. package/skills/tools/gen-docs/scripts/__pycache__/doc_generator.cpython-312.pyc +0 -0
  84. package/skills/tools/gen-docs/scripts/doc_generator.py +0 -520
  85. package/skills/tools/verify-change/scripts/__pycache__/change_analyzer.cpython-312.pyc +0 -0
  86. package/skills/tools/verify-change/scripts/change_analyzer.py +0 -529
  87. package/skills/tools/verify-module/scripts/__pycache__/module_scanner.cpython-312.pyc +0 -0
  88. package/skills/tools/verify-module/scripts/module_scanner.py +0 -321
  89. package/skills/tools/verify-quality/scripts/__pycache__/quality_checker.cpython-312.pyc +0 -0
  90. package/skills/tools/verify-quality/scripts/quality_checker.py +0 -481
  91. package/skills/tools/verify-security/scripts/__pycache__/security_scanner.cpython-312.pyc +0 -0
  92. package/skills/tools/verify-security/scripts/security_scanner.py +0 -374
@@ -1,703 +0,0 @@
1
- ---
2
- name: prompt-engineering
3
- description: Prompt 工程技术。Zero-shot、Few-shot、CoT、ReAct、ToT、模板设计、优化技巧。当用户提到 Prompt 工程、Few-shot、CoT、思维链、ReAct、提示词优化时使用。
4
- ---
5
-
6
- # 🎨 符箓秘典 · Prompt 工程 (Prompt Engineering)
7
-
8
- ## Prompt 模式
9
-
10
- ```
11
- Zero-shot → Few-shot → CoT → Self-Consistency → ToT → ReAct
12
- │ │ │ │ │ │
13
- └─ 直接 ───┴─ 示例 ──┴─ 推理 ─────┴─ 多路 ────────┴─ 行动
14
- ```
15
-
16
- ### 模式对比
17
- | 模式 | 复杂度 | 准确性 | Token 消耗 | 适用场景 |
18
- |------|--------|--------|------------|----------|
19
- | Zero-shot | 低 | 中 | 低 | 简单任务、通用问题 |
20
- | Few-shot | 中 | 高 | 中 | 格式化输出、分类 |
21
- | CoT | 中 | 高 | 中 | 推理、数学、逻辑 |
22
- | Self-Consistency | 高 | 极高 | 高 | 关键决策 |
23
- | ToT | 极高 | 极高 | 极高 | 复杂规划 |
24
- | ReAct | 高 | 高 | 高 | 工具调用、Agent |
25
-
26
- ## Zero-shot Prompting
27
-
28
- ### 基础模板
29
- ```python
30
- prompt = """
31
- 任务: {task_description}
32
-
33
- 输入: {input}
34
-
35
- 输出:
36
- """
37
-
38
- # 示例
39
- prompt = """
40
- 任务: 将以下文本分类为正面、负面或中性情感。
41
-
42
- 输入: 这个产品质量不错,但价格有点贵。
43
-
44
- 输出:
45
- """
46
- ```
47
-
48
- ### 指令优化
49
- ```python
50
- # ❌ 模糊指令
51
- "告诉我关于 Python 的事情"
52
-
53
- # ✅ 清晰指令
54
- """
55
- 请用 3 个要点总结 Python 的核心特性:
56
- 1. 语言特点
57
- 2. 主要应用领域
58
- 3. 生态系统优势
59
-
60
- 每个要点不超过 50 字。
61
- """
62
- ```
63
-
64
- ### 角色设定
65
- ```python
66
- system_prompt = """
67
- 你是一位资深的 Python 安全专家,拥有 10 年渗透测试经验。
68
- 你的回答应该:
69
- - 技术准确,引用 CVE 编号
70
- - 提供可执行的代码示例
71
- - 强调安全最佳实践
72
- """
73
-
74
- user_prompt = "如何防御 SQL 注入?"
75
- ```
76
-
77
- ## Few-shot Prompting
78
-
79
- ### 标准 Few-shot
80
- ```python
81
- prompt = """
82
- 将产品评论分类为正面或负面。
83
-
84
- 示例 1:
85
- 评论: 这个耳机音质很棒,佩戴舒适。
86
- 分类: 正面
87
-
88
- 示例 2:
89
- 评论: 电池续航太差,用不到一天就没电了。
90
- 分类: 负面
91
-
92
- 示例 3:
93
- 评论: 包装精美,但产品质量一般。
94
- 分类: 负面
95
-
96
- 现在分类:
97
- 评论: {new_review}
98
- 分类:
99
- """
100
- ```
101
-
102
- ### Few-shot 示例选择
103
- ```python
104
- from langchain.prompts import FewShotPromptTemplate, PromptTemplate
105
- from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
106
- from langchain.vectorstores import Chroma
107
- from langchain.embeddings import OpenAIEmbeddings
108
-
109
- # 示例库
110
- examples = [
111
- {"input": "2+2", "output": "4"},
112
- {"input": "3*5", "output": "15"},
113
- {"input": "10/2", "output": "5"},
114
- {"input": "sqrt(16)", "output": "4"},
115
- ]
116
-
117
- # 语义相似度选择器
118
- example_selector = SemanticSimilarityExampleSelector.from_examples(
119
- examples,
120
- OpenAIEmbeddings(),
121
- Chroma,
122
- k=2 # 选择最相似的 2 个示例
123
- )
124
-
125
- # 动态 Few-shot
126
- example_prompt = PromptTemplate(
127
- input_variables=["input", "output"],
128
- template="输入: {input}\n输出: {output}"
129
- )
130
-
131
- prompt = FewShotPromptTemplate(
132
- example_selector=example_selector,
133
- example_prompt=example_prompt,
134
- prefix="计算以下表达式:",
135
- suffix="输入: {input}\n输出:",
136
- input_variables=["input"]
137
- )
138
-
139
- print(prompt.format(input="sqrt(25)"))
140
- ```
141
-
142
- ### 结构化输出
143
- ```python
144
- prompt = """
145
- 从文本中提取实体,输出 JSON 格式。
146
-
147
- 示例:
148
- 文本: 张三在北京的阿里巴巴工作,年薪 50 万。
149
- 输出:
150
- {
151
- "person": "张三",
152
- "location": "北京",
153
- "organization": "阿里巴巴",
154
- "salary": "50万"
155
- }
156
-
157
- 现在提取:
158
- 文本: {text}
159
- 输出:
160
- """
161
- ```
162
-
163
- ## Chain-of-Thought (CoT)
164
-
165
- ### 标准 CoT
166
- ```python
167
- prompt = """
168
- 问题: 一个班级有 23 名学生。如果每个学生需要 3 支铅笔,
169
- 老师已经有 17 支铅笔,还需要买多少支?
170
-
171
- 让我们一步步思考:
172
- 1. 总共需要的铅笔数 = 23 × 3 = 69 支
173
- 2. 已有铅笔数 = 17 支
174
- 3. 还需要购买 = 69 - 17 = 52 支
175
-
176
- 答案: 52 支
177
- """
178
- ```
179
-
180
- ### Zero-shot CoT
181
- ```python
182
- # 神奇的咒语: "Let's think step by step"
183
- prompt = """
184
- 问题: {question}
185
-
186
- 让我们一步步思考:
187
- """
188
-
189
- # 中文版本
190
- prompt = """
191
- 问题: {question}
192
-
193
- 让我们逐步分析:
194
- """
195
- ```
196
-
197
- ### Few-shot CoT
198
- ```python
199
- prompt = """
200
- 问题: 咖啡店有 9 杯咖啡,卖出 5 杯后又做了 7 杯,现在有多少杯?
201
-
202
- 推理过程:
203
- 1. 初始: 9 杯
204
- 2. 卖出后: 9 - 5 = 4 杯
205
- 3. 又做了: 4 + 7 = 11 杯
206
-
207
- 答案: 11 杯
208
-
209
- ---
210
-
211
- 问题: 停车场有 12 辆车,开走 3 辆,又来了 8 辆,现在有多少辆?
212
-
213
- 推理过程:
214
- 1. 初始: 12 辆
215
- 2. 开走后: 12 - 3 = 9 辆
216
- 3. 又来了: 9 + 8 = 17 辆
217
-
218
- 答案: 17 辆
219
-
220
- ---
221
-
222
- 问题: {new_question}
223
-
224
- 推理过程:
225
- """
226
- ```
227
-
228
- ### Self-Consistency CoT
229
- ```python
230
- from collections import Counter
231
-
232
- def self_consistency_cot(question: str, n_samples: int = 5):
233
- prompt = f"""
234
- 问题: {question}
235
-
236
- 让我们一步步思考:
237
- """
238
-
239
- answers = []
240
- for _ in range(n_samples):
241
- response = llm.predict(prompt, temperature=0.7)
242
- # 提取最终答案
243
- answer = extract_final_answer(response)
244
- answers.append(answer)
245
-
246
- # 多数投票
247
- most_common = Counter(answers).most_common(1)[0][0]
248
- return most_common
249
- ```
250
-
251
- ## ReAct (Reasoning + Acting)
252
-
253
- ### ReAct 模板
254
- ```python
255
- prompt = """
256
- 你可以使用以下工具:
257
- - Search[query]: 搜索信息
258
- - Calculate[expression]: 计算数学表达式
259
- - Finish[answer]: 返回最终答案
260
-
261
- 使用格式:
262
- Thought: 我需要做什么
263
- Action: 工具名[参数]
264
- Observation: 工具返回结果
265
- ... (重复 Thought/Action/Observation)
266
- Thought: 我现在知道答案了
267
- Action: Finish[最终答案]
268
-
269
- 问题: 埃菲尔铁塔的高度是多少米?它比自由女神像高多少?
270
-
271
- Thought: 我需要先查询埃菲尔铁塔的高度
272
- Action: Search[埃菲尔铁塔高度]
273
- Observation: 埃菲尔铁塔高 330 米(含天线)
274
-
275
- Thought: 现在需要查询自由女神像的高度
276
- Action: Search[自由女神像高度]
277
- Observation: 自由女神像高 93 米(含底座)
278
-
279
- Thought: 现在可以计算差值
280
- Action: Calculate[330 - 93]
281
- Observation: 237
282
-
283
- Thought: 我现在知道答案了
284
- Action: Finish[埃菲尔铁塔高 330 米,比自由女神像高 237 米]
285
- """
286
- ```
287
-
288
- ### LangChain ReAct Agent
289
- ```python
290
- from langchain.agents import initialize_agent, Tool, AgentType
291
- from langchain.tools import DuckDuckGoSearchRun
292
- from langchain.utilities import PythonREPL
293
-
294
- # 定义工具
295
- search = DuckDuckGoSearchRun()
296
- python_repl = PythonREPL()
297
-
298
- tools = [
299
- Tool(
300
- name="Search",
301
- func=search.run,
302
- description="搜索互联网信息"
303
- ),
304
- Tool(
305
- name="Python",
306
- func=python_repl.run,
307
- description="执行 Python 代码进行计算"
308
- )
309
- ]
310
-
311
- # 初始化 ReAct Agent
312
- agent = initialize_agent(
313
- tools,
314
- llm,
315
- agent=AgentType.REACT_DOCSTORE,
316
- verbose=True,
317
- max_iterations=5
318
- )
319
-
320
- result = agent.run("2024 年奥运会在哪里举办?有多少个国家参加?")
321
- ```
322
-
323
- ## Tree-of-Thoughts (ToT)
324
-
325
- ### ToT 实现
326
- ```python
327
- class TreeOfThoughts:
328
- def __init__(self, llm, max_depth=3, beam_width=3):
329
- self.llm = llm
330
- self.max_depth = max_depth
331
- self.beam_width = beam_width
332
-
333
- def solve(self, problem: str):
334
- # 1. 生成初始思路
335
- thoughts = self._generate_thoughts(problem, depth=0)
336
-
337
- # 2. 评估思路
338
- scored_thoughts = self._evaluate_thoughts(problem, thoughts)
339
-
340
- # 3. 选择最佳路径(Beam Search)
341
- best_thoughts = sorted(scored_thoughts, key=lambda x: x[1], reverse=True)[:self.beam_width]
342
-
343
- # 4. 递归扩展
344
- if self.max_depth > 1:
345
- for thought, score in best_thoughts:
346
- sub_problem = f"{problem}\n已有思路: {thought}\n继续推理:"
347
- return self.solve(sub_problem)
348
-
349
- return best_thoughts[0][0]
350
-
351
- def _generate_thoughts(self, problem: str, depth: int):
352
- prompt = f"""
353
- 问题: {problem}
354
-
355
- 生成 3 个不同的解决思路:
356
- 1.
357
- 2.
358
- 3.
359
- """
360
- response = self.llm.predict(prompt)
361
- return self._parse_thoughts(response)
362
-
363
- def _evaluate_thoughts(self, problem: str, thoughts: list):
364
- scored = []
365
- for thought in thoughts:
366
- prompt = f"""
367
- 问题: {problem}
368
- 思路: {thought}
369
-
370
- 评估这个思路的质量(0-10 分):
371
- - 逻辑性
372
- - 可行性
373
- - 完整性
374
-
375
- 分数:
376
- """
377
- score = float(self.llm.predict(prompt).strip())
378
- scored.append((thought, score))
379
- return scored
380
- ```
381
-
382
- ## 模板设计
383
-
384
- ### System/User/Assistant 结构
385
- ```python
386
- messages = [
387
- {
388
- "role": "system",
389
- "content": "你是一位专业的 Python 开发者,擅长代码审查和优化。"
390
- },
391
- {
392
- "role": "user",
393
- "content": "请审查这段代码:\n```python\n{code}\n```"
394
- },
395
- {
396
- "role": "assistant",
397
- "content": "我会从以下方面审查:\n1. 代码质量\n2. 性能优化\n3. 安全问题"
398
- },
399
- {
400
- "role": "user",
401
- "content": "重点关注安全问题"
402
- }
403
- ]
404
- ```
405
-
406
- ### 分隔符与结构化
407
- ```python
408
- prompt = """
409
- ### 指令
410
- 将以下代码转换为 TypeScript。
411
-
412
- ### 输入代码
413
- ```python
414
- {python_code}
415
- ```
416
-
417
- ### 输出要求
418
- 1. 保持原有逻辑
419
- 2. 添加类型注解
420
- 3. 使用 ES6+ 语法
421
-
422
- ### 输出代码
423
- ```typescript
424
- """
425
- ```
426
-
427
- ### 变量插值
428
- ```python
429
- from langchain.prompts import PromptTemplate
430
-
431
- template = """
432
- 作为 {role},请完成以下任务:
433
-
434
- 任务: {task}
435
- 上下文: {context}
436
- 约束条件:
437
- {constraints}
438
-
439
- 输出格式: {output_format}
440
- """
441
-
442
- prompt = PromptTemplate(
443
- input_variables=["role", "task", "context", "constraints", "output_format"],
444
- template=template
445
- )
446
-
447
- formatted = prompt.format(
448
- role="安全工程师",
449
- task="分析漏洞",
450
- context="Web 应用",
451
- constraints="- 仅分析 OWASP Top 10\n- 提供修复建议",
452
- output_format="JSON"
453
- )
454
- ```
455
-
456
- ## 优化技巧
457
-
458
- ### 清晰性原则
459
- ```python
460
- # ❌ 模糊
461
- "写一些关于 AI 的东西"
462
-
463
- # ✅ 清晰
464
- """
465
- 写一篇 500 字的文章,介绍 AI 在医疗领域的应用。
466
- 包含:
467
- 1. 疾病诊断
468
- 2. 药物研发
469
- 3. 个性化治疗
470
- 每个部分约 150 字。
471
- """
472
- ```
473
-
474
- ### 分步指令
475
- ```python
476
- prompt = """
477
- 请按以下步骤分析代码:
478
-
479
- 步骤 1: 识别代码的主要功能
480
- 步骤 2: 列出潜在的安全问题
481
- 步骤 3: 对每个问题提供修复建议
482
- 步骤 4: 给出优化后的代码
483
-
484
- 代码:
485
- ```python
486
- {code}
487
- ```
488
-
489
- 开始分析:
490
- """
491
- ```
492
-
493
- ### 约束与边界
494
- ```python
495
- prompt = """
496
- 生成一个 Python 函数,满足以下要求:
497
-
498
- 功能: 计算斐波那契数列第 n 项
499
-
500
- 约束:
501
- - 使用递归实现
502
- - 添加缓存优化
503
- - 包含类型注解
504
- - 添加 docstring
505
- - 处理边界情况 (n < 0)
506
-
507
- 不要:
508
- - 使用循环
509
- - 使用全局变量
510
- - 导入外部库
511
-
512
- 代码:
513
- """
514
- ```
515
-
516
- ### 示例驱动
517
- ```python
518
- prompt = """
519
- 将自然语言转换为 SQL 查询。
520
-
521
- 示例 1:
522
- 输入: 查询所有年龄大于 30 的用户
523
- 输出: SELECT * FROM users WHERE age > 30;
524
-
525
- 示例 2:
526
- 输入: 统计每个部门的员工数量
527
- 输出: SELECT department, COUNT(*) FROM employees GROUP BY department;
528
-
529
- 示例 3:
530
- 输入: 查询销售额前 10 的产品
531
- 输出: SELECT * FROM products ORDER BY sales DESC LIMIT 10;
532
-
533
- 现在转换:
534
- 输入: {natural_language}
535
- 输出:
536
- """
537
- ```
538
-
539
- ## 高级技巧
540
-
541
- ### 思维链提示词库
542
- ```python
543
- COT_PROMPTS = {
544
- "zh": [
545
- "让我们一步步思考:",
546
- "让我们逐步分析:",
547
- "让我们分解这个问题:",
548
- "首先,我们需要理解...",
549
- ],
550
- "en": [
551
- "Let's think step by step:",
552
- "Let's break this down:",
553
- "Let's approach this systematically:",
554
- "First, we need to understand...",
555
- ]
556
- }
557
- ```
558
-
559
- ### 元提示(Meta-Prompting)
560
- ```python
561
- meta_prompt = """
562
- 你是一个 Prompt 工程专家。给定一个任务,生成最优的 Prompt。
563
-
564
- 任务: {task}
565
-
566
- 生成的 Prompt 应该:
567
- 1. 清晰定义角色和目标
568
- 2. 包含具体的输出格式
569
- 3. 提供 2-3 个示例
570
- 4. 设置必要的约束条件
571
-
572
- 生成的 Prompt:
573
- """
574
-
575
- # 使用 LLM 生成 Prompt
576
- optimized_prompt = llm.predict(meta_prompt.format(task="代码审查"))
577
- ```
578
-
579
- ### 自我批评
580
- ```python
581
- def self_critique(question: str):
582
- # 第一次生成
583
- answer = llm.predict(f"问题: {question}\n答案:")
584
-
585
- # 自我批评
586
- critique_prompt = f"""
587
- 问题: {question}
588
- 答案: {answer}
589
-
590
- 请批评这个答案的不足之处:
591
- 1. 准确性
592
- 2. 完整性
593
- 3. 清晰度
594
-
595
- 批评:
596
- """
597
- critique = llm.predict(critique_prompt)
598
-
599
- # 改进答案
600
- improve_prompt = f"""
601
- 问题: {question}
602
- 初始答案: {answer}
603
- 批评意见: {critique}
604
-
605
- 基于批评意见,生成改进后的答案:
606
- """
607
- improved = llm.predict(improve_prompt)
608
- return improved
609
- ```
610
-
611
- ### 多角色对话
612
- ```python
613
- prompt = """
614
- 模拟三位专家讨论问题:
615
-
616
- 问题: {question}
617
-
618
- 专家 A (乐观派):
619
- {optimistic_view}
620
-
621
- 专家 B (悲观派):
622
- {pessimistic_view}
623
-
624
- 专家 C (中立派):
625
- 综合 A 和 B 的观点,我认为...
626
- """
627
- ```
628
-
629
- ## Prompt 模板库
630
-
631
- ### 代码生成
632
- ```yaml
633
- code_generation:
634
- system: "你是一位资深的 {language} 开发者。"
635
- template: |
636
- 生成 {language} 代码实现以下功能:
637
-
638
- 功能描述: {description}
639
-
640
- 要求:
641
- - 遵循 {language} 最佳实践
642
- - 添加必要的注释
643
- - 处理异常情况
644
- - 包含使用示例
645
-
646
- 代码:
647
- ```
648
-
649
- ### 文本摘要
650
- ```yaml
651
- summarization:
652
- template: |
653
- 将以下文本总结为 {length} 字的摘要:
654
-
655
- 文本:
656
- {text}
657
-
658
- 摘要要求:
659
- - 保留关键信息
660
- - 语言简洁
661
- - 逻辑清晰
662
-
663
- 摘要:
664
- ```
665
-
666
- ### 数据提取
667
- ```yaml
668
- extraction:
669
- template: |
670
- 从文本中提取以下信息:
671
-
672
- 提取字段: {fields}
673
-
674
- 文本:
675
- {text}
676
-
677
- 输出格式: JSON
678
-
679
- 提取结果:
680
- ```
681
-
682
- ## 工具与资源
683
-
684
- | 工具 | 类型 | 用途 |
685
- |------|------|------|
686
- | LangChain | 框架 | Prompt 模板管理 |
687
- | PromptBase | 市场 | Prompt 交易平台 |
688
- | OpenPrompt | 库 | Prompt 学习框架 |
689
- | Guidance | 库 | 结构化生成 |
690
- | LMQL | 语言 | Prompt 编程语言 |
691
-
692
- ## 最佳实践
693
-
694
- - ✅ 清晰指令:具体、明确、可执行
695
- - ✅ 结构化:使用分隔符、编号、格式
696
- - ✅ 示例驱动:提供 2-5 个高质量示例
697
- - ✅ 约束明确:说明要做什么和不做什么
698
- - ✅ 迭代优化:测试、分析、改进
699
- - ✅ 版本管理:记录 Prompt 变更历史
700
- - ✅ A/B 测试:对比不同 Prompt 效果
701
- - ❌ 避免:模糊指令、过长 Prompt、无示例
702
-
703
- ---