Chain of Thought (CoT)
Chain of Thought (CoT)
思维链( CoT)提示是一种通过生成中间推理步骤来提升大语言模型推理能力的技术。它能帮助模型输出更准确的答案。对于需要先推理再回答的复杂任务,可结合少量示例提示获得更佳效果 —— 因为零样本思维链本身存在挑战。
思维链具备诸多优势:首先它成本低廉且高效,能直接适配现成的大语言模型(无需微调)。其推理过程具有可解释性,用户可通过模型的响应步骤学习推理逻辑,一旦出错也便于定位。在不同版本的大语言模型间迁移时,思维链能保持更强的鲁棒性,这意味着提示模板的性能波动会小于非推理链式提示。当然其缺点也较为直观。
由于大语言模型的响应包含思维链推理过程,这意味着需要输出更多标记( token),从而导致预测成本更高且耗时更长。
为了解释表 11 中的示例,我们首先尝试创建一个不使用思维链提示( CoT)的提示,以展示大语言模型的缺陷。
提示When I was 3 years old, my partner was 3 times my age. Now, I am 20 years old. How old is my partner?
输出63 岁
表 11. 尝试解决数学问题的提示示例
糟糕,这显然是错误答案。事实上,大语言模型( LLM)在处理数学任务时常常表现不佳,甚至可能连简单的两数乘法都会出错。这是因为它们主要基于海量文本数据进行训练,而数学问题往往需要不同的解决思路。让我们试试通过添加中间推理步骤来改进输出结果。
提示词When I was 3 years old, my partner was 3 times my age. Now, I am 20 years old. How old is my partner? Let's think step by step.
输出1. 假设我当前的年龄是 “x” 岁。
- 当我 3 岁时,我的年龄是 3 岁。
- 当时我伴侣的年龄是我的 3 倍,这意味着我伴侣的年龄是 3 * n•~ ‚P~ nð~ 岁。
- 现在,我 20 岁了,这意味着我的年龄比 3 = 17 增加了 20 岁。
自从我 3 岁起已经过去了这些年。
- 由于我 3 岁时伴侣的年龄是 9 岁,而我的年龄增加了 17 岁,那么伴侣的年龄也一定增加了 17 岁。
ð x0
- 因此,伴侣当前的年龄是 nð~ ‚ ~ npnÐ~ ‚P~ n€nÀ~ 岁。
所以,我的伴侣现在是 26 岁。
表 12. 思维链提示的一个示例
好的,现在最终答案是正确的。这是因为我们明确指示大语言模型逐步解释而不仅仅是返回答案。有趣的是,模型将增加的 17 年累加起来。在我脑海中,我会计算伴侣与我之间的年龄差再相加(n€n`‚ (9‑3))。让我们帮助模型更像我这样思考一点。
表 12 展示了 ‘ 零样本 ’ 思维链的示例。当与单样本或少样本结合时,思维链提示技术会变得非常强大,如表 13 所示:
提示词Q: When my brother was 2 years old, I was double his age. Now I am 40 years old. How old is my brother? Let's think step by step.
A: When my brother was 2 years, I was 2 * 2 = 4 years old.
That's an age difference of 2 years and I am older. Now I am 40 years old, so my brother is 40 - 2= 38 years old. The answer is 38.
Q: When I was 3 years old, my partner was 3 times my age. Now, I am 20 years old. How old is my partner? Let's think step by step.
A:
输出我 3 岁时,伴侣的年龄是 3 * n•~ ‚P~ nð~ 岁。这是年龄相差 6 岁且伴侣年长。现在我 20 岁,因此我的 pa ner 的年龄是 n€n`~ ‚ ~ nÀ~ ‚P~ n€nÀ~ 岁。答案是 26 岁。
表 13. 单样本思维链提示的示例
思维链可适用于多种场景。例如代码生成,将需求分解为若干步骤,并映射到具体的代码行。
或是在拥有类似 “The product is called XYZ, write a description guiding the model through the assumptions you would make based on the product given title.” 种子数据时创建合成数据。一般而言,任何能通过 ‘ 逐步推演 ’ 解决的问题都适合采用思维链。如果你能阐述问题解决的步骤,就尝试使用思维链。
请参阅 GoogleCloudPlatform Github 仓库中的 notebook10,该文档将深入探讨思维链提示技术:
全站分卷索引 · 34 卷
- 1Introduction
- 2Prompt engineering
- 3LLM output configuration
- 4Sampling controls
- 5Top-K and top-P
- 6Putting it all together
- 7Prompting techniques
- 8One-shot & few-shot
- 9System, contextual and role prompting
- 10System prompting
- 11Role prompting
- 12Contextual prompting
- 13Step-back prompting
- 14Chain of Thought (CoT)
- 15Self-consistency
- 16Tree of Thoughts (ToT)
- 17ReAct (reason & act)
- 18Automatic Prompt Engineering
- 19Code prompting
- 20Prompts for explaining code
- 21Prompts for translating code
- 22Prompts for debugging and reviewing code
- 23What about multimodal prompting?
- 24Design with simplicity
- 25Be specific about the output
- 26Control the max token length
- 27Experiment with input formats and writing styles
- 28Adapt to model updates
- 29JSON Repair
- 30Working with Schemas
- 31Experiment together with other prompt engineers
- 32CoT Best practices
- 33Summary
- 34Endnotes