P
Prompt Master

Prompt 大师

掌握和 AI 对话的艺术

Chain-of-Thought (CoT)

通过中间推理步骤提升 reasoning tasks 的稳定性

链式思考(CoT)提示

COT

图片来源:Wei 等人(2022)

Wei 等人(2022) 中引入的链式思考(CoT)提示通过中间推理步骤实现了复杂的推理能力。您可以将其与少样本提示相结合,以获得更好的结果,以便在回答之前进行推理的更复杂的任务。

提示:

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.

请记住,作者声称这是足够大的语言模型才会出现的新兴能力。

Prompt Lab

把这章的知识,直接变成实战能力

进入交互式实验室,用真实任务练 Prompt,10 分钟快速上手。

立即查看 →

零样本 COT 提示

Zero-shot COT

图片来源:Kojima 等人(2022)

最近提出的一个新想法是 零样本 CoT( Kojima 等人,2022 年),它基本上涉及将“让我们逐步思考”添加到原始提示中。让我们尝试一个简单的问题,看看模型的表现如何:

提示:

I went to the market and bought 10 apples. I gave 2 apples to my neighbor and 2 apples to the repairman. Then I went and bought 5 more apples and ate 1. How many apples do I have left?

输出:

11 apples

答案是不正确的!现在让我们尝试使用特殊提示。

提示:

I went to the market and bought 10 apples. I gave 2 apples to my neighbor and 2 apples to the repairman. Then I went and bought 5 more apples and ate 1. How many apples do I have left?

Let's think step by step.

输出:

First, you start with 10 apples.
You gave 2 apples to your neighbor and 2 apples to the repairman, so you have 6 apples left.
Then you bought 5 more apples, so now you have 11 apples.
Finally, you ate 1 apple, so you have 10 apples left.

令人印象深刻的是,这个简单的提示在这个任务中非常有效。这在您没有太多示例可用于提示时特别有用。

自动思维链(Auto-CoT)

当使用思维链提示时,这个过程需要手工制作有效且多样化的例子。这种手动工作可能会导致次优解决方案。Zhang et al. (2022) 提出了一种消除人工的方法,即利用 LLMs “让我们一步一步地思考” 提示来生成一个接一个的推理链。这种自动过程仍然可能在生成的链中出现错误。为了减轻错误的影响,演示的多样性很重要。这项工作提出了 Auto-CoT,它对具有多样性的问题进行采样,并生成推理链来构建演示。

Auto-CoT 主要由两个阶段组成:

  • 阶段 1:问题聚类:将给定问题划分为几个聚类
  • 阶段 2:演示抽样:从每组数组中选择一个具有代表性的问题,并使用带有简单启发式的 Zero-Shot-CoT 生成其推理链

简单的启发式方法可以是问题的长度(例如,60 个 tokens)和理由的步骤数(例如,5 个推理步骤)。这鼓励模型使用简单而准确的演示。

该过程如下图所示:

AUTOCOT

图片来源:Zhang 等人(2022)

Auto-CoT 的代码可在这里找到:Github

📚 相关资源

❓ 常见问题

关于本章主题最常被搜索的问题,点击展开答案

Chain-of-Thought 跟普通 few-shot 的区别在哪?

区别在每条 demonstration 的「答案」字段。Few-shot 只写 `A: Answer is False.`,CoT(Wei et al. 2022)会写 `A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.`——把中间推理步骤显式塞进示范。模型会模仿这个推理过程,奇偶和那个例子一下就答对。

Zero-shot CoT 是怎么回事?只加一句话就能涨智商?

Kojima et al. 2022(arxiv 2205.11916)发现:在原 prompt 末尾加一句 `Let's think step by step.`,模型自动产生推理链。教程里 10 个苹果的算术题,原版 prompt 输出 `11 apples`(错),加了 step-by-step 后正确分解到 `10 apples left`。零示范,零工程量。

CoT 是 emergent ability,什么意思?

Wei et al. 2022 原文:CoT 只在「足够大」的语言模型上出现——小模型加推理链反而更差。具体阈值跟模型架构有关,但典型的「emergent」转折点出现在 100B 参数以上。这意味着用 small model 走 CoT 不仅没收益,可能拖累准确率。

Auto-CoT 跟普通 CoT 比省了什么?

省了人工写 demonstration 的成本。普通 CoT 需要手写多样化的有效示例,工作量大且容易选偏。Zhang et al. 2022(arxiv 2210.03493)的 Auto-CoT 两阶段:(1) 问题聚类,(2) 每簇里选代表问题,用 zero-shot CoT「Let's think step by step」自动生成推理链。启发式:60 token 长度 + 5 步推理。

什么时候用 CoT,什么时候不用?

用 CoT:算术、常识推理、符号推理、多跳问答这种需要中间步骤的任务。不用 CoT:分类、提取、翻译、总结这种 input→output 直接映射的任务——加 step-by-step 反而让输出变长、加 token 成本。CoT 让 token 数翻几倍,cost-sensitive 的高频任务要慎用。