思考の連鎖と自己一貫性プロンプティング

OpenAI API によるプロンプトエンジニアリング

Fouad Trad

Machine Learning Engineer

思考の連鎖プロンプト

  • LLMに、回答を出す前に推論のステップ(思考)を示すよう求める
  • 複雑な推論タスクに使用
  • モデルのエラーを減らすのに役立つ

Image showing how a chain-of-thought prompt asks the model to solve a problem step by step, and the output contains the reasoning for each step followed.

OpenAI API によるプロンプトエンジニアリング

思考の連鎖プロンプト

推論タスクを解くための標準的なプロンプティング
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 によるプロンプトエンジニアリング

思考の連鎖プロンプト

推論タスクを解くための思考の連鎖プロンプティング

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 によるプロンプトエンジニアリング

few-shotを使った思考の連鎖プロンプティング

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 によるプロンプトエンジニアリング

思考の連鎖とマルチステップ・プロンプティングの比較

マルチステップ・プロンプト

  • プロンプト内に手順を組み込む

Diagram showing that a multi-step prompt is a prompt that gives the model a series of sequential steps to perform.

OpenAI API によるプロンプトエンジニアリング

思考の連鎖とマルチステップ・プロンプティングの比較

マルチステップ・プロンプト

  • プロンプト内に手順を組み込む

Diagram showing that a multi-step prompt is a prompt that gives the model a series of sequential steps to perform.

思考の連鎖プロンプト

  • モデルに中間ステップを生成するよう求める

Image showing that in chain-of-thought prompts, the steps are produced by the model while generating the output.

OpenAI API によるプロンプトエンジニアリング

思考の連鎖の限界

  • 1つの思考がうまくいかない → 結果もうまくいかない
  • 自己一貫性プロンプトが導入されました

Diagram for a chain-of-thought prompt highlighting that one thought with flawed reasoning will lead to an unsuccessful outcome.

OpenAI API によるプロンプトエンジニアリング

自己一貫性プロンプティング

  • モデルに複数回プロンプトを与えることで、複数の思考の連鎖を生成する
  • 多数決で最終出力を得る

Image showing that a self-consistency prompt is just about multiple chain-of-thoughts, each of which having an output, and the final outcome is obtained by majority vote.

OpenAI API によるプロンプトエンジニアリング

自己一貫性プロンプティング

複数のプロンプトを定義するか、複数の回答を生成するプロンプトを使用して実行できます。

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 によるプロンプトエンジニアリング

自己一貫性プロンプト

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...