第八章:调试与优化¶
常见问题¶
输出不符合预期¶
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 回答太长 | 无长度限制 | 添加字数约束 |
| 格式混乱 | 未指定格式 | 提供输出模板 |
| 内容偏离 | 任务不清晰 | 明确任务描述 |
| 风格不符 | 角色设定弱 | 强化角色描述 |
调试方法¶
迭代优化¶
优化流程¶
优化记录¶
## 版本记录
### v1
提示词:写一篇产品介绍
结果:内容太泛,没有重点
问题:缺少具体要求
### v2
提示词:为一款智能手表写产品介绍,突出健康功能
结果:内容较好,但格式不统一
问题:缺少格式约束
### v3
提示词:为一款智能手表写产品介绍
要求:
- 突出健康监测功能
- 分点列出功能特点
- 300字左右
结果:符合预期 ✓
A/B 测试¶
prompts = {
"A": "请分析这段文本的情感",
"B": "作为情感分析专家,请判断以下文本的情感倾向(正面/负面/中性)"
}
results = {}
for name, prompt in prompts.items():
response = llm.call(prompt + text)
results[name] = evaluate(response)
# 比较结果
print(f"A准确率: {results['A']['accuracy']}")
print(f"B准确率: {results['B']['accuracy']}")
评估指标¶
定性评估¶
定量评估¶
def evaluate_prompt(prompt, test_cases):
scores = []
for case in test_cases:
response = llm.call(prompt.format(**case['input']))
score = calculate_similarity(response, case['expected'])
scores.append(score)
return sum(scores) / len(scores)
最佳实践¶
提示词清单¶
版本管理¶
prompts/
├── v1/
│ └── qa_prompt.md
├── v2/
│ └── qa_prompt.md
├── current/
│ └── qa_prompt.md
└── CHANGELOG.md
小结¶
本章学习了提示词的调试与优化方法。至此,Prompt Engineering 教程全部完成。