Chain-of-thought และ self-consistency prompting

Prompt Engineering กับ OpenAI API

Fouad Trad

Machine Learning Engineer

Chain-of-thought prompting

  • กำหนดให้ LLM แสดงขั้นตอนการคิด (thoughts) ก่อนให้คำตอบ
  • ใช้กับงานที่ต้องอาศัยการใช้เหตุผลซับซ้อน
  • ช่วยลดข้อผิดพลาดของโมเดล

ภาพแสดง chain-of-thought prompt ที่ให้โมเดลแก้ปัญหาทีละขั้นตอน โดย output มีเหตุผลของแต่ละขั้นตอน

Prompt Engineering กับ OpenAI API

Chain-of-thought prompting

Standard prompting สำหรับงานที่ต้องใช้การใช้เหตุผล
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
Prompt Engineering กับ OpenAI API

Chain-of-thought prompting

Chain-of-thought prompting สำหรับงานที่ต้องใช้การใช้เหตุผล
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.
Prompt Engineering กับ OpenAI API

Chain-of-thought prompting แบบ 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.
Prompt Engineering กับ OpenAI API

Chain-of-thought เทียบกับ multi-step prompting

Multi-step prompts

  • ระบุขั้นตอนไว้ภายใน prompt โดยตรง

ไดอะแกรมแสดงว่า multi-step prompt คือ prompt ที่กำหนดลำดับขั้นตอนให้โมเดลดำเนินการตามลำดับ

Prompt Engineering กับ OpenAI API

Chain-of-thought เทียบกับ multi-step prompting

Multi-step prompts

  • ระบุขั้นตอนไว้ภายใน prompt โดยตรง

ไดอะแกรมแสดงว่า multi-step prompt คือ prompt ที่กำหนดลำดับขั้นตอนให้โมเดลดำเนินการตามลำดับ

Chain-of-thought prompts

  • ให้โมเดลสร้างขั้นตอนกลางขณะสร้าง output

ภาพแสดงว่าใน chain-of-thought prompts โมเดลเป็นผู้สร้างขั้นตอนต่างๆ ระหว่างการสร้าง output

Prompt Engineering กับ OpenAI API

ข้อจำกัดของ chain-of-thought

  • thought ที่ผิดพลาดเพียงหนึ่งขั้น --> ผลลัพธ์ที่ผิดพลาด
  • จึงมีการนำ self-consistency prompts มาใช้

ไดอะแกรม chain-of-thought prompt ที่เน้นให้เห็นว่าการคิดที่มีข้อบกพร่องเพียงขั้นเดียวจะนำไปสู่ผลลัพธ์ที่ผิดพลาด

Prompt Engineering กับ OpenAI API

Self-consistency prompting

  • สร้าง chain-of-thought หลายชุดโดย prompt โมเดลหลายครั้ง
  • ใช้เสียงข้างมากเพื่อได้ผลลัพธ์สุดท้าย

ภาพแสดงว่า self-consistency prompt คือการสร้าง chain-of-thought หลายชุด แต่ละชุดมี output ของตัวเอง และผลลัพธ์สุดท้ายได้จากเสียงข้างมาก

Prompt Engineering กับ OpenAI API

Self-consistency prompting

ทำได้โดยกำหนด prompt หลายชุด หรือ prompt ที่สร้างคำตอบหลายรายการ

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))
Prompt Engineering กับ OpenAI API

Self-consistency prompt

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.
Prompt Engineering กับ OpenAI API

มาฝึกกันเถอะ!

Prompt Engineering กับ OpenAI API

Preparing Video For Download...