Chain-of-thought 與 self-consistency 提示

使用 OpenAI API 的提示工程

Fouad Trad

Machine Learning Engineer

Chain-of-thought 提示

  • 要求 LLM 先給出推理步驟(想法)再回答
  • 用於複雜推理任務
  • 有助降低模型錯誤

示意圖:chain-of-thought 提示要求模型逐步解題,輸出包含每一步的推理。

使用 OpenAI API 的提示工程

Chain-of-thought 提示

以標準提示解推理題
prompt = """Q: You start with 15 books in your collection. At the bookstore, you 
purchase 8 new books. Then, you lend 3 to your friend and 2 to your cousin. Later, 
you visit another bookstore and buy 5 more books. How many books do you have now? 
A: The answer is"""

print(get_response(prompt))
25 books
使用 OpenAI API 的提示工程

Chain-of-thought 提示

以 chain-of-thought 提示解推理題
prompt = """Q: You start with 15 books in your collection. At the bookstore, you 
purchase 8 new books. Then, you lend 3 to your friend and 2 to your cousin. Later, 
you visit another bookstore and buy 5 more books. How many books do you have now? 
A: Let's think step by step""" 
print(get_response(prompt))
Step 1: Start with the number of books in your collection: 15 books
Step 2: Purchase 8 new books at the bookstore: 15 + 8 = 23 books
Step 3: Lend 3 books to your friend: 23 - 3 = 20 books
Step 4: Lend 2 books to your cousin: 20 - 2 = 18 books
Step 5: Visit another bookstore and buy 5 more books: 18 + 5 = 23 books
Therefore, you have 23 books now.
使用 OpenAI API 的提示工程

少樣本的 chain-of-thought 提示

example = """
Q: The odd numbers in this group add up to an even number:  9, 10, 13, 4, 2.
A: Adding all the odd numbers (9, 13) gives 22. The answer is True.
"""

question = """ Q: The odd numbers in this group add up to an even number: 15, 13, 82, 7. A: """
prompt = example + question print(get_response(prompt))
Adding all the odd numbers (15, 13, 7) gives 35. The answer is False.
使用 OpenAI API 的提示工程

Chain-of-thought 與多步驟提示之別

多步驟提示

  • 在提示中內嵌步驟

示意圖:多步驟提示會給模型一連串需依序執行的步驟。

使用 OpenAI API 的提示工程

Chain-of-thought 與多步驟提示之別

多步驟提示

  • 在提示中內嵌步驟

示意圖:多步驟提示會給模型一連串需依序執行的步驟。

Chain-of-thought 提示

  • 要求模型生成中間步驟

示意圖:在 chain-of-thought 提示中,步驟會隨輸出一併由模型產生。

使用 OpenAI API 的提示工程

Chain-of-thought 的限制

  • 任一步驟失誤 → 結果失敗
  • 引入了 self-consistency 提示

示意圖:chain-of-thought 提示中,只要某一步推理有瑕疵,就可能導致失敗結果。

使用 OpenAI API 的提示工程

Self-consistency 提示

  • 透過多次提示生成多條 chain-of-thought
  • 以多數決取得最終輸出

示意圖:self-consistency 提示會產生多條 chain-of-thought,各自有輸出,最後以多數決決定結果。

使用 OpenAI API 的提示工程

Self-consistency 提示

可以用定義 多個提示,或讓一個 提示生成多個 回應來達成。

self_consistency_instruction = "Imagine three completely independent experts who 
reason differently are answering this question. The final answer is obtained by 
majority vote. The question is: "

problem_to_solve = "If there are 10 cars in the parking lot and 3 more cars arrive. Half the original number of cars leave. Then, half of the current number of cars arrive. How many cars are there in the parking?"
prompt = self_consistency_instruction + problem_to_solve print(get_response(prompt))
使用 OpenAI API 的提示工程

Self-consistency 提示範例

Expert 1: Let's go step by step [...] Therefore, the total number of cars in the 
parking lot is 8 + 4 = 12.

Expert 2: First, let's calculate [...] Therefore, the total number of cars in the 
parking lot is now 5 + 2 = 7 cars.

Expert 3: Initially, there are 10 cars [...] Thus, the final answer is 8 + 4 = 12 
cars in the parking lot.

Based on the majority vote, the final answer is that there are 12 cars in the 
parking lot.
使用 OpenAI API 的提示工程

一起來練習吧!

使用 OpenAI API 的提示工程

Preparing Video For Download...