logo
P
Prompt Master

Prompt 大师

掌握和 AI 对话的艺术

Chain-of-Thought (CoT)

Improve reasoning task reliability with intermediate thinking steps

Chain-of-Thought (CoT) Prompting

COT

Image source: Wei et al. (2022)

Chain-of-thought (CoT) prompting, introduced in Wei et al. (2022), enables complex reasoning through intermediate steps. Combine it with few-shot prompting and you get much better results on tasks that require thinking before answering.

Prompt:

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:

Output:

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

Perfect result once we provided reasoning steps. And here's what's cool -- you can get away with fewer examples. Even just one example is enough:

Prompt:

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:

Output:

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

Keep in mind that the authors claim this is an emergent ability that only appears in sufficiently large language models.

Prompt Lab

Turn this chapter's knowledge into practical skills

Enter the interactive lab and practice Prompt with real tasks. Get started in 10 minutes.

View Now

Zero-Shot CoT Prompting

Zero-shot COT

Image source: Kojima et al. (2022)

Zero-shot CoT (Kojima et al., 2022) is a neat idea: just add "Let's think step by step" to your prompt. That's it. Let's see how it works on a simple problem.

Prompt:

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?

Output:

11 apples

Wrong. Now let's try with the magic phrase.

Prompt:

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.

Output:

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.

Pretty impressive that such a simple prompt works this well. It's especially useful when you don't have examples to include in your prompt.

Auto-CoT (Automatic Chain-of-Thought)

Crafting effective and diverse CoT examples by hand is tedious. And manual work often leads to suboptimal solutions. Zhang et al. (2022) proposed a way to eliminate the manual effort: use LLMs with "Let's think step by step" to automatically generate reasoning chains. But auto-generated chains can contain errors. So diversity matters -- that's why they proposed Auto-CoT, which samples questions with diversity and generates reasoning chains for demonstrations.

Auto-CoT has two main stages:

  • Stage 1: Question Clustering -- partition the given questions into clusters
  • Stage 2: Demonstration Sampling -- pick a representative question from each cluster and use Zero-Shot-CoT with simple heuristics to generate its reasoning chain

The heuristics are things like question length (e.g., 60 tokens) and number of reasoning steps (e.g., 5 steps). This encourages simple and accurate demonstrations.

Here's the process:

AUTOCOT

Image source: Zhang et al. (2022)

Auto-CoT code is available here: Github.

📚 相关资源