Prompt Engineering – Part 5: Chain-of-Thought Pattern

In most cases, AI can think and give correct answers; however, in some complex cases, AI sometimes gives the wrong answer. By providing the step by step guides, AI can produce better results. This is called the Chain of Thought pattern. In this article, we will go through how it looks and works.

Image Source: Wei et al. (2022)

In this example, instead of guiding, AI provides the final answer only and provides a step-by-step guide for calculating the answer. AI answers better in this case.

This pattern was introduced in Wei et al. (2022) This enables complex reasoning capabilities by providing intermediate reasoning steps. We can mix this pattern with Prompt Engineering – Part 2: Few-shots Example for better results. As AI is not good at logical problems, this pattern is useful to solve these problems.

Another technique to force AI to think step by step is adding the phrase “Let’s think step by step.” according to Kojima et al. (2022)

Image Source: Kojima et al. (2022)

Another example of “forms of address” in Vietnamese.

Cách xưng hô (only when User speak Vietnamese):
Trong tiếng việt, Assistant luôn gọi User theo danh xưng lịch sự là 'anh' nếu giới tính là NAM hoặc 'chị' nếu giới tính là NỮ. Assistant luôn luôn PHẢI tự xưng bản thân là 'Em'; 
Example 1:
- User: Em ơi 
- Reasoning: Không xác định được giới tính User nên sẽ gọi User là 'anh/chị' và tự xưng là 'em' 
- Assistant: Dạ, Em có thể giúp gì cho anh/chị? Và anh/chị có thể cho em biết tên để em hỗ trợ mình tốt hơn được không ạ?; 
- User: Cho anh hỏi một chút 
- Reasoning: User xưng 'anh' nên xác định được giới tính là Nam nên sẽ gọi User là 'Anh' và tự xưng là 'Em'
- Assistant: Dạ em có thể giúp gì cho anh? Và anh có thể cho em biết tên để em hỗ trợ anh tốt hơn được không ạ?; 
Example 2: 
- User: Chị là Linh, chào em 
- Reasoning: Xác định được giới tính User là NỮ nên gọi User là 'Chị' và tự xưng là 'Em' 
- Assistant: Dạ, chào chị Linh, em có thể giúp gì cho chị ạ?

Here I mixed Chain-of-Thought with Template pattern to make AI still think before producing the answer but only output the final answer.

Sometimes, AI is in a “hurry” to answer, so we can give it a context that it has time to respond. e.g., instead of using “Determine if the student’s solution is correct or not.”, you can ask, “First, work out your solution to the problem. Then, compare your solution to the student’s solution and evaluate if the student’s solution is correct or not. Don’t decide if the student’s solution is correct until you have done the problem yourself.” (example from Open AI). It appears that AI considers thinking time a factor in producing the answer.

In conclusion, Chain-of-Thought is a super useful pattern to improve the accuracy of answers. It can be mixed with many other patterns to construct a better result. Give it a try now!