首页 / 资料库 / 提示工程指南

资料库18 分钟读完MIT提示词思维链少样本进阶技巧

提示词进阶用法

译自《Advanced Prompting》 · 查看英文原文

原文出处Advanced Prompting 原作者:DAIR.AI / Elvis Saravia · 许可证:MIT License 中文译本由诸葛AI学院整理,仅供学习参考,版权归原作者与 DAIR.AI 所有。

读到这里,你应该已经明白:把提示词写好,不同任务的结果就会更好。这就是提示工程的全部出发点。前面那些例子权当热身,在进入更进阶的概念之前,先把几个基本概念正式讲清楚。

本篇主题:

  • 零样本提示(Zero-shot Prompting)
  • 少样本提示(Few-shot Prompting)
  • 思维链提示(Chain-of-Thought Prompting)
  • 零样本思维链(Zero-shot CoT)
  • 自我一致性(Self-Consistency)
  • 生成知识提示(Generated Knowledge Prompting)
  • 自动提示工程师(Automatic Prompt Engineer, APE)

零样本提示(Zero-shot Prompting)

今天的大语言模型在海量数据上训练,又针对"跟随指令"做过调优,本身就具备零样本执行任务的能力。上一节我们已经试过几个零样本的例子,这里拿其中一个来看:

提示: ``` Classify the text into neutral, negative, or positive.

Text: I think the vacation is okay. Sentiment: ```

输出: Neutral

这个例子的用途是让模型做情感分类,判断一句话是中性、消极还是积极。注意,上面的提示里我们没有给模型任何示例,这就是零样本能力在起作用。当零样本行不通时,建议转而在提示中提供演示或示例。下面讲的正是所谓少样本提示。


少样本提示(Few-shot Prompting)

大语言模型的零样本能力已经相当不错,可在更复杂的任务上,纯零样本的表现还是不够。为了补上这块短板,人们把少样本提示当作一种上下文学习(in-context learning)技术来用:在提示中给出演示,引导模型走向更好的表现。这些演示相当于给后续示例做条件铺垫,而我们正是希望模型对后续示例生成回答。

我们用 Brown et al. 2020 论文里的一个例子来演示少样本提示。这个任务是用一个生词造出正确的句子。

提示: A "whatpu" is a small, furry animal native to Tanzania. An example of a sentence that uses the word whatpu is: We were traveling in Africa and we saw these very cute whatpus. To do a "farduddle" means to jump up and down really fast. An example of a sentence that uses the word farduddle is:

输出: When we won the game, we all started to farduddle in celebration.

可以看到,模型只看了一个例子(也就是 1-shot),就以某种方式学会了怎么做这个任务。对更难的任务,我们可以尝试增加演示的数量(比如 3-shot、5-shot、10-shot 等等)。

按照 Min et al. (2022) 的结论,做少样本提示时,关于演示样本还有几条 tips:

  • "演示所指定的标签空间与输入文本的分布,两者都很重要(哪怕个别输入对应的标签是错的也一样)"
  • 你采用的格式对表现影响也很大。哪怕标签全是随机贴的,也远好过完全没有标签。
  • 额外的实验结果显示,从标签的真实分布(而非均匀分布)中随机选标签,同样有帮助。

我们来试几个例子。先看标签完全随机分配的情况(意思是 Negative 和 Positive 这两个标签被随机贴到输入句子上):

提示: This is awesome! // Negative This is bad! // Positive Wow that movie was rad! // Positive What a horrible show! //

输出: Negative

这个例子的用途同样是对句子做情感分类。尽管标签是随机化的,我们还是得到了正确答案。注意我们同时保留了格式,格式也有帮助。事实上,继续实验发现,我们测试的新一代 GPT 模型甚至对混乱格式都变得更"抗造"了。例如:

提示: Positive This is awesome! This is bad! Negative Wow that movie was rad! Positive What a horrible show! --

输出: Negative

上面这个格式毫无一致性可言,模型却仍然预测出了正确标签。这个现象是否适用于更复杂、更多样的任务和多变的提示,我们得做更彻底的分析才能下结论。

少样本提示的局限

标准的少样本提示在很多任务上好使,但谈不上完美技术,遇到更复杂的推理任务尤其如此。来看为什么。还记得前面那个题目吗:

``` The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.

A: ```

再跑一次,模型会输出:

Yes, the odd numbers in this group add up to 107, which is an even number.

这不是正确答案。它既暴露了这类系统的局限,也说明提示工程还需要更进阶的手段。

我们试着加一些示例,看看少样本提示能不能改善结果。

提示: ``` The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1. A: The answer is False.

The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24. A: The answer is True.

The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24. A: The answer is True.

The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2. A: The answer is False.

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. A: ```

输出: The answer is True.

还是不行。看来光靠少样本提示,不足以对这类推理问题给出可靠回答。上面的示例只提供了任务的基本信息。仔细看,这类任务其实需要多几步推理。换句话说,如果把问题拆成步骤、并把拆解过程演示给模型看,可能会有帮助。近两年,思维链(chain of thought, CoT)提示走红,正是用来处理更复杂的算术、常识和符号推理任务的。

总的来看,提供示例对解决一部分任务确实有用。当零样本和少样本都不够用时,可能意味着模型已学到的东西不足以做好这个任务。这时建议开始考虑微调(fine-tuning)模型,或者尝试更进阶的提示技术。接下来要讲的,就是眼下最流行的提示技术之一:思维链提示。


思维链提示(Chain-of-Thought Prompting)

思维链提示由 Wei et al. (2022) 提出,核心是通过中间的推理步骤来实现复杂推理。你可以把它和少样本提示结合使用,让那些"必须先推理再作答"的复杂任务拿到更好的结果。

提示: ``` The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1. A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.

The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24. A: Adding all the odd numbers (17, 19) gives 36. The answer is True.

The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24. A: Adding all the odd numbers (11, 13) gives 24. The answer is True.

The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2. A: Adding all the odd numbers (17, 9, 13) gives 39. The answer is False.

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. A: ```

输出: Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False.

这个提示把"先挑出奇数、再求和、最后判断奇偶"的推理步骤写进了每个示例,模型照方抓药。太好了!只要提供推理步骤,就能得到完全正确的结果。甚至少给几个示例也够用,比如只给一个:

提示: ``` The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1. A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. A: ```

输出: Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False.

要记住一点:论文作者声称,这是一种涌现能力(emergent ability),只在语言模型足够大时才会出现。


零样本思维链(Zero-shot CoT)

更近一些的想法是零样本思维链(Kojima et al. 2022),做法简单:在原提示后面加一句 "Let's think step by step"(让我们一步步思考)。先拿一道简单题试试模型裸跑的表现:

提示: I went to the market and bought 10 apples. I gave 2 apples to the neighbor and 2 to the repairman. I then went and bought 5 more apples and ate 1. How many apples did I remain with?

输出: 11 apples

答错了!现在加上那句特殊提示再试。

提示: ``` I went to the market and bought 10 apples. I gave 2 apples to the neighbor and 2 to the repairman. I then went and bought 5 more apples and ate 1. How many apples did I remain with?

Let's think step by step. ```

输出: First, you started with 10 apples. You gave away 2 apples to the neighbor and 2 to the repairman, so you had 6 apples left. Then you bought 5 more apples, so now you had 11 apples. Finally, you ate 1 apple, so you would remain with 10 apples.

一句这么简单的话就能让这道题做对,相当了不起。当你手里没有多少示例可以放进提示时,这招尤其好用。


自我一致性(Self-Consistency)

自我一致性算是提示工程里更进阶的技巧之一。它由 Wang et al. (2022) 提出,目标是"取代思维链提示里朴素的贪心解码"。思路是:用少样本思维链采样出多条互不相同的推理路径,再从这些生成结果里选出最一致的回答。这能增强 CoT 提示在算术和常识推理任务上的表现。

先看看不加自我一致性时,一道算术推理题的效果:

提示: When I was 6 my sister was half my age. Now I’m 70 how old is my sister?

输出: 35

答案错了!那用自我一致性怎么改进?来试。我们使用 Wang et al. 2022 论文表 17 里的少样本示例:

提示: ``` Q: There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done, there will be 21 trees. How many trees did the grove workers plant today? A: We start with 15 trees. Later we have 21 trees. The difference must be the number of trees they planted. So, they must have planted 21 - 15 = 6 trees. The answer is 6.

Q: If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot? A: There are 3 cars in the parking lot already. 2 more arrive. Now there are 3 + 2 = 5 cars. The answer is 5.

Q: Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total? A: Leah had 32 chocolates and Leah’s sister had 42. That means there were originally 32 + 42 = 74 chocolates. 35 have been eaten. So in total they still have 74 - 35 = 39 chocolates. The answer is 39.

Q: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny? A: Jason had 20 lollipops. Since he only has 12 now, he must have given the rest to Denny. The number of lollipops he has given to Denny must have been 20 - 12 = 8 lollipops. The answer is 8.

Q: Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now? A: He has 5 toys. He got 2 from mom, so after that he has 5 + 2 = 7 toys. Then he got 2 more from dad, so in total he has 7 + 2 = 9 toys. The answer is 9.

Q: There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room? A: There are 4 days from monday to thursday. 5 computers were added each day. That means in total 4 * 5 = 20 computers were added. There were 9 computers in the beginning, so now there are 9 + 20 = 29 computers. The answer is 29.

Q: Michael had 58 golf balls. On tuesday, he lost 23 golf balls. On wednesday, he lost 2 more. How many golf balls did he have at the end of wednesday? A: Michael initially had 58 balls. He lost 23 on Tuesday, so after that he has 58 - 23 = 35 balls. On Wednesday he lost 2 more so now he has 35 - 2 = 33 balls. The answer is 33.

Q: Olivia has $23. She bought five bagels for $3 each. How much money does she have left? A: She bought 5 bagels for $3 each. This means she spent 5

Q: When I was 6 my sister was half my age. Now I’m 70 how old is my sister? A: ```

每个示例都带着完整的分步推理,模型会照着这个样式往下算。我们多次采样,得到几个不同的输出:

输出一: When I was 6 my sister was half my age, so she was 3. Now I am 70, so she is 70 - 3 = 67. The answer is 67.

输出二: When the narrator was 6, his sister was half his age, which is 3. Now that the narrator is 70, his sister would be 70 - 3 = 67 years old. The answer is 67.

输出三: When I was 6 my sister was half my age, so she was 3. Now I am 70, so she is 70/2 = 35. The answer is 35.

要算出最终答案还需要几个步骤(细节见论文),但为了简单起见,我们可以看到多数答案已经浮现出来了,三次里有两次都落在 67,这个多数答案基本就可以当作最终答案。


生成知识提示(Generated Knowledge Prompting)

大语言模型一直在进化,其中一种流行技巧是把知识或信息掺进提示里,帮模型做出更准确的预测。

顺着一样的思路,能不能让模型先自己生成知识,再拿这些知识去做预测?Liu et al. 2022 的论文做的就是这件事:生成知识,作为提示的一部分。对常识推理这类任务,这招有多大帮助?

先看一个简单提示:

提示: Part of golf is trying to get a higher point total than others. Yes or No?

输出: Yes.

这类错误暴露了大语言模型在执行"需要更多世界知识"的任务时的局限。怎么用知识生成来改进?

第一步,先生成几条"知识"(knowledges):

提示: ``` Input: Greece is larger than mexico. Knowledge: Greece is approximately 131,957 sq km, while Mexico is approximately 1,964,375 sq km, making Mexico 1,389% larger than Greece.

Input: Glasses always fog up. Knowledge: Condensation occurs on eyeglass lenses when water vapor from your sweat, breath, and ambient humidity lands on a cold surface, cools, and then changes into tiny drops of liquid, forming a film that you see as fog. Your lenses will be relatively cool compared to your breath, especially when the outside air is cold.

Input: A fish is capable of thinking. Knowledge: Fish are more intelligent than they appear. In many areas, such as memory, their cognitive powers match or exceed those of ’higher’ vertebrates including non-human primates. Fish’s long-term memories help them keep track of complex social relationships.

Input: A common effect of smoking lots of cigarettes in one’s lifetime is a higher than normal chance of getting lung cancer. Knowledge: Those who consistently averaged less than one cigarette per day over their lifetime had nine times the risk of dying from lung cancer than never smokers. Among people who smoked between one and 10 cigarettes per day, the risk of dying from lung cancer was nearly 12 times higher than that of never smokers.

Input: A rock is the same size as a pebble. Knowledge: A pebble is a clast of rock with a particle size of 4 to 64 millimetres based on the Udden-Wentworth scale of sedimentology. Pebbles are generally considered larger than granules (2 to 4 millimetres diameter) and smaller than cobbles (64 to 256 millimetres diameter).

Input: Part of golf is trying to get a higher point total than others. Knowledge: ```

这个提示的格式沿用自 Liu et al. 2022 论文:前面几条"输入 → 知识"是演示,最后一条留空,让模型自己补出关于高尔夫规则的知识。模型生成了两条候选知识:

知识一: The objective of golf is to play a set of holes in the least number of strokes. A round of golf typically consists of 18 holes. Each hole is played once in the round on a standard golf course. Each stroke is counted as one point, and the total number of strokes is used to determine the winner of the game.

知识二: Golf is a precision club-and-ball sport in which competing players (or golfers) use many types of clubs to hit balls into a series of holes on a course using the fewest number of strokes. The goal is to complete the course with the lowest score, which is calculated by adding up the total number of strokes taken on each hole. The player with the lowest score wins the game.

我们使用的提示来自 Liu et al. 2022 的论文。

下一步是把知识和问题拼在一起,让模型给出预测。我们把问题重新整理成 QA 格式,用来引导回答的格式。

提示: ``` Question: Part of golf is trying to get a higher point total than others. Yes or No?

Knowledge: The objective of golf is to play a set of holes in the least number of strokes. A round of golf typically consists of 18 holes. Each hole is played once in the round on a standard golf course. Each stroke is counted as one point, and the total number of strokes is used to determine the winner of the game.

Explain and Answer: ```

回答一(置信度非常高): No, the objective of golf is not to get a higher point total than others. Rather, the objective is to play a set of holes in the least number of strokes. The total number of strokes is used to determine the winner of the game, not the total number of points.

提示(换用知识二): ``` Question: Part of golf is trying to get a higher point total than others. Yes or No?

Knowledge: Golf is a precision club-and-ball sport in which competing players (or golfers) use many types of clubs to hit balls into a series of holes on a course using the fewest number of strokes. The goal is to complete the course with the lowest score, which is calculated by adding up the total number of strokes taken on each hole. The player with the lowest score wins the game.

Explain and Answer: ```

回答二(置信度低了不少): Yes, part of golf is trying to get a higher point total than others. Each player tries to complete the course with the lowest score, which is calculated by adding up the total number of strokes taken on each hole. The player with the lowest score wins the game.

这个例子里发生了一些很有意思的事。第一条回答模型非常笃定,第二条就没那么自信了。我们为了演示把流程简化了,真正要定出最终答案还有更多细节要考虑。想看完整做法,去读原论文。


自动提示工程师(APE, Automatic Prompt Engineer)

Zhou et al., (2022) 提出了自动提示工程师(automatic prompt engineer, APE):一个自动生成并筛选指令的框架。生成指令的问题被当作自然语言合成来处理,并用大语言模型以黑盒优化(black-box optimization)的方式生成、搜索候选解。原文此处有一张图,画的就是 APE 这套"生成候选 → 执行评估 → 挑选最优"的流程示意。

第一步是让一个大语言模型(作为推理模型)看到若干输出演示,为一个任务生成指令候选。这些候选解会引导搜索过程。指令在目标模型上执行,然后根据算出来的评估分数,选出最合适的那条指令。

APE 找到了一条比人类设计的 "Let's think step by step"(Kojima et al., 2022)更好的零样本思维链提示。

提示 "Let's work this out in a step by step way to be sure we have the right answer." 能激发思维链推理,并在 MultiArith 和 GSM8K 两个基准上改善了表现。原文此处有一张图,展示的正是这条提示与 "Let's think step by step" 在这两个基准上的成绩对比,APE 发现的提示全面占优。

这篇论文触及了提示工程的一个重要话题:自动优化提示。本指南不在这个方向上深入,如果你感兴趣,以下几篇关键论文值得先读:

  • AutoPrompt:提出一种基于梯度引导搜索的方法,为一组多样的任务自动创建提示。
  • Prefix Tuning:微调的轻量替代方案,为自然语言生成任务在前面拼接一段可训练的连续前缀。
  • Prompt Tuning:提出一种通过反向传播学习软提示(soft prompts)的机制。

上一节(提示工程基础)

下一节(提示应用)

这篇在讲什么,跟咱们的课怎么对?

资料库是大厂公开教材的中文译本,偏原理和工程做法。想看面向中小企业的白话版本,去入门课场景课