EP03. "Prompt 工程技巧:从模糊到精准的五个改法"
🔒 登录后可标记已读- 同一个运动员餐单生成任务,评估分数从 2.32 一路改到 7.86——光靠「写清楚第一句话」「加具体指引」这两招就能翻三倍分数,不用换模型
- Prompt Engineering 是一个「设目标 → 写初稿 → 评估 → 套用技巧改进 → 再评估」的循环,第 4、5 步会反复跑很多轮,不是写一次就定案
- 每次只改一个地方、评估一次,才知道是哪个改动真正有效——同时改好几处,分数变好也不知道是哪招起作用
- 涵盖四个具体技巧:写清楚直接的开头、加具体指引、用 XML tag 分隔内容、给参考范例(one-shot / multi-shot)
- 前置知识:读过 [EP02](./EP02_prompt评估工作流.md),知道怎么跑一套评估管线拿到客观分数
重点内容
迭代改进的五步循环
- 设目标:先定清楚这个 prompt 要做到什么
- 写初稿:先写一个最基本的版本,不用一次到位
- 评估:拿评估标准测一遍,看分数
- 套用 prompt engineering 技巧:针对性地改
- 重新评估:确认这次改动真的有让分数变好
📌 第 4、5 步会反复循环,直到分数满意为止。
实战案例:运动员一日餐单生成器
全篇拿「根据身高体重目标和饮食限制,生成一日餐单」这个任务当示范,贯穿整个模块。
用 PromptEvaluator 类跑评估:
evaluator = PromptEvaluator(max_concurrent_tasks=5)
💡 并发数建议先设低一点(比如 3),避免撞到 API 的 rate limit 报错。
生成测试数据集:
dataset = evaluator.generate_dataset(
task_description="Write a compact, concise 1 day meal plan for a single athlete",
prompt_inputs_spec={
"height": "Athlete's height in cm",
"weight": "Athlete's weight in kg",
"goal": "Goal of the athlete",
"restrictions": "Dietary restrictions of the athlete"
},
output_file="dataset.json",
num_cases=3
)
最初版本的 prompt(刻意写得很模糊,方便后面示范改进):
def run_prompt(prompt_inputs):
prompt = f"""
What should this person eat?
- Height: {prompt_inputs["height"]}
- Weight: {prompt_inputs["weight"]}
- Goal: {prompt_inputs["goal"]}
- Dietary restrictions: {prompt_inputs["restrictions"]}
"""
messages = []
add_user_message(messages, prompt)
return chat(messages)
评估时额外给一段人工审核标准(extra_criteria),告诉裁判模型该看什么:
results = evaluator.run_evaluation(
run_prompt_function=run_prompt,
dataset_file="dataset.json",
extra_criteria="""
The output should include:
- Daily caloric total
- Macronutrient breakdown
- Meals with exact foods, portions, and timing
"""
)
技巧一:写清楚、写直接
📌 "Prompt 的第一句话,是整个请求里最重要的一部分。"
两个原则:
- 清楚:用简单、不含糊的语言,不要绕弯子
- ❌ "I need to know about those things people put on their roofs that use sun"
- ✅ "Write three paragraphs about how solar panels work."
- 直接:优先用祈使句下指令,不要用问句
- ❌ "What countries use geothermal energy?"
- ✅ "Identify three countries that use geothermal energy. Include generation stats for each."
套到餐单案例上:
- 改前:
"What should this person eat?" - 改后:
"Generate a one-day meal plan for an athlete that meets their dietary restrictions."
改后这句话一次讲清楚了动作(generate)、要交付的东西(meal plan)、限制条件(一天、运动员、饮食限制)。
结果:评估分数从 2.32 涨到 3.92。
技巧二:具体指引(Being Specific)
不写清楚的地方,就是留给模型自己诠释——诠释出来的结果不受你控制。给具体指引能大幅收窄这个不确定性。
指引分两种:
| 类型 | 控制什么 | 适合用在 |
|---|---|---|
| Output Quality Guidelines | 回答长度、结构格式、要包含的具体元素、语气风格 | 几乎每个 prompt 都用得上 |
| Process Steps | 引导模型按步骤系统性思考 | 排查复杂问题、要做决策、需要批判性思考、要多角度分析的任务 |
餐单案例加了六条具体指引:
Guidelines:
1. Include accurate daily calorie amount
2. Show protein, fat, and carb amounts
3. Specify when to eat each meal
4. Use only foods that fit restrictions
5. List all portion sizes in grams
6. Keep budget-friendly if mentioned
结果:评估分数从 3.92 涨到 7.86——只加了这六条指引,分数几乎翻倍。
💡 实务上通常两种指引一起用:Output Guidelines 保证输出稳定、Process Steps 保证思考够周全。
技巧三:用 XML Tag 划分内容边界
Prompt 里塞了大量内容、又没有明确分界时,Claude 有时会分不清楚「哪几段文字是一组的」。XML tag 能当成分隔符,帮模型分清楚指令跟数据的界线。
常见场景:
- 一整批销售记录包进
<sales_records>里 - 代码跟对应文档分别包进
<my_code>和<docs>,不会混在一起 - 变量内插的内容比较复杂时,用 tag 圈起来更清楚
📌 Tag 名字要用有描述性的,不要用泛泛的 <data>——比如运动员数据就叫 <athlete_information>,一看就知道里面是什么。
示范:
<athlete_information>
- Height: 6'2"
- Weight: 180 lbs
- Goal: Build muscle
- Dietary restrictions: Vegetarian
</athlete_information>
Generate a meal plan based on the athlete information above.
什么时候该用:
- 要塞进大量上下文的时候
- 混合了不同类型的内容
- 需要明确划分内容边界
- 用了比较复杂的内插变量
技巧四:给参考范例(One-shot / Multi-shot)
范例是最有效的技巧之一,专门用来处理「光讲规则讲不清楚」的场景。
案例:推文情绪分析
讽刺语气是常见的难题。比如这句话:"Yeah, sure, that was the best movie I've seen since 'Plan 9 from Outer Space'"——字面看像正面,实际是负面吐槽。
改进做法:给几个范例,用 <sample_input> 和 <ideal_output> 标出来:
- 一条清楚的正面范例:"Great game tonight!" → "Positive"
- 一条讽刺范例:"Oh yeah, I really needed a flight delay tonight! Excellent!" → "Negative"
- 补充说明讽刺语气该怎么判断
One-shot vs Multi-shot:One-shot 给一个范例;Multi-shot 给多个、覆盖不同场景的范例。
范例哪里来:直接从评估流程里挑出打满分(10/10)的输出,当作输入/输出范例对,不用另外现编。
给范例加上下文说明:不是只丢 input/output 对,最好补一句「这个范例为什么算好」,比如:"This example is well-structured, provides detailed information on food choices and quantities, and aligns with the athlete's goals and restrictions."
最佳实践总结:
- 用 XML tag 清楚标出范例
- 明确说明这个范例是要示范什么
- 至少放一个能处理常见失败案例的范例
- 解释为什么这个输出算「理想输出」
- 范例内容要跟实际任务相关,不要随便找几个不相干的例子凑数
常见错误
- ❌ 一次改好几个地方再评估——分数变好也不知道哪个改动真正起作用,无法判断该保留哪招、舍弃哪招
- ❌ Prompt 开头用问句而不是祈使句——问句容易得到模糊、不受控的回答,改成直接下指令
- ❌ Tag 名字用
<data>/<info>这种泛用词——模型(和未来的自己)看不出这段内容到底是什么,改用描述性的名字 - ❌ 范例只给 input/output,不解释「为什么这是好答案」——少了这层说明,模型可能学到范例的表面格式,没学到背后的判断逻辑
Sources
官方文档:
- Building with the Claude API(Claude Academy 课程)— https://academy.claude.com/courses/building-with-the-claude-api