Applied AI School
v0 · 規劃中
Anthropic

三種讓 prompt 更穩的技巧

Be specific、be direct、用 XML tag——三招最高 CP 值的 prompt engineering 技巧一次講完。

TL;DR

  • Direct:第一句直接祭出 action verb(write / generate / identify),不要繞
  • Specific:列 guidelines 寫死要包什麼欄位、什麼格式、什麼長度
  • XML tags:用 <athlete_information>...</athlete_information> 包 input,比 markdown header 還穩

一個情境:把 baseline 從 2.3 推到 8.5

接續上一篇——你的 baseline 是這個爛 prompt:

What should this person eat?

- Height: 180
- Weight: 75
- Goal: build muscle
- Dietary restrictions: vegetarian

Eval 分數 2.3 / 10。Claude 答了一段含糊的建議,沒 calorie、沒分餐、portion 也沒寫。下面三招按順序套上去,每招都讓你往上爬幾分。這是 PE 的「主力武器」。

招 1:Be direct——第一句就下指令

第一句是整個 prompt 最重要的一行。新手常見毛病:

  • 用問句:「What should this person eat?」(Claude 不知道要寫多長、什麼形式)
  • 轉彎:「I'm working on a project where I need to think about meal planning, and I was wondering if maybe you could help me...」
  • 沒動詞:列了一堆條件但沒講要產出什麼

直接版本長這樣:

直接
What should this person eat?Generate a one-day meal plan for an athlete that meets their dietary restrictions.
那種屋頂上裝太陽能那種東西怎麼運作?Write three paragraphs about how solar panels work.
哪些國家用地熱發電啊?Identify three countries that use geothermal energy. Include generation stats for each.

關鍵:動詞起手 + 講清楚 task + 帶上關鍵 constraint。光改第一句,原課程的範例就從 2.3 漲到 3.9。

招 2:Be specific——列 guidelines 寫死

光「直接」還不夠。「Generate a one-day meal plan」聽起來明確,但 Claude 還是不知道你要不要 calorie、要不要 portion、要不要 timing。Specific 就是把這些寫死成 guidelines。

Generate a one-day meal plan for an athlete that meets their dietary restrictions.

Guidelines:
1. Include accurate daily calorie total
2. Show protein, fat, and carb amounts in grams
3. Specify when to eat each meal (e.g. 7am breakfast, 12pm lunch)
4. Use only foods that fit the athlete's restrictions
5. List all portion sizes in grams
6. Stay budget-friendly if mentioned

加上 guidelines,原課程分數從 3.9 跳到 7.9,是單一最大幅度的進步。CP 值之王。

Guidelines 有兩種寫法:

類型用途何時用
Output qualities(屬性清單)控制長度、結構、必含欄位幾乎每個 prompt 都該有
Process steps(思考步驟)強制 model 想過 X 個面向複雜分析、debug、決策題

範例 process steps:

Steps:
1. First calculate target daily calories based on weight and goal
2. Then split into 3 meals + 2 snacks
3. For each meal, pick foods that fit the restrictions
4. Verify total calories and macros match the plan

招 3:用 XML tag 包 input

Prompt 越長越容易讓 Claude 搞混「哪段是指令、哪段是資料」。XML tag 就是給每段內容貼標籤:

Generate a one-day meal plan for the athlete below.

<athlete_information>
- Height: 180 cm
- Weight: 75 kg
- Goal: Build muscle
- Dietary restrictions: Vegetarian
</athlete_information>

Guidelines:
...

戲劇化的例子是 debug code 的 prompt:

# Not great
Debug my code below using the provided documentation.

def add(a, b): return a - b
The add function should return the sum of two numbers.

# Better
Debug my code using the provided documentation.

<my_code>
def add(a, b): return a - b
</my_code>

<docs>
The add function should return the sum of two numbers.
</docs>

第一版 Claude 要自己猜哪段是 code、哪段是 docs;第二版 zero ambiguity。

為什麼是 XML,不是 markdown header?

  • Anthropic 在訓練 Claude 時用了大量 XML 標記過的 example——它對 <tag> 開合特別敏感
  • Markdown header(## Athlete Info)跟 model 一般輸出長得太像,分隔效果弱
  • XML 可以自訂語意化命名——<sales_records><data>## Records 都更明確
  • Tag 可以巢狀(<example><input>...</input><output>...</output></example>

Tag 命名要 specific

<data>...</data><sales_records>...</sales_records>
<text>...</text><athlete_information>...</athlete_information>
<input>...</input><user_email>...</user_email>

Tag 名字本身也是給 Claude 的提示——名字越具體越能引導正確處理。

三招組合起來

完整的 v4 prompt:

Generate a one-day meal plan for the athlete below that meets their dietary restrictions.

<athlete_information>
- Height: {height} cm
- Weight: {weight} kg
- Goal: {goal}
- Dietary restrictions: {restrictions}
</athlete_information>

Guidelines:
1. Include accurate daily calorie total
2. Show protein, fat, and carb amounts in grams
3. Specify when to eat each meal
4. Use only foods that fit the restrictions
5. List all portion sizes in grams

從 2.3(baseline)→ 3.9(direct)→ 7.9(specific)→ 8.5(XML),三招吃下大部分的進步空間。剩下從 8.5 推到 9 分以上的,下一篇講 few-shot。

接下來

三招幫你打底,但有一類 task 用講的怎麼講都不夠精確——譬如特定的 JSON 結構、語氣、邊角案例。這時候要拿出 PE 的最後一個大絕招:直接給 Claude 看範例