Nhắc chuỗi suy nghĩ và tự nhất quán

Kỹ thuật Prompt với OpenAI API

Fouad Trad

Machine Learning Engineer

Nhắc chuỗi suy nghĩ

  • Yêu cầu LLM nêu các bước lý luận (suy nghĩ) trước khi trả lời
  • Dùng cho bài toán suy luận phức tạp
  • Giúp giảm lỗi mô hình

Hình minh họa cách nhắc chuỗi suy nghĩ yêu cầu mô hình giải từng bước và đầu ra chứa lý do cho mỗi bước.

Kỹ thuật Prompt với OpenAI API

Nhắc chuỗi suy nghĩ

Nhắc chuẩn để giải bài suy luận
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
Kỹ thuật Prompt với OpenAI API

Nhắc chuỗi suy nghĩ

Nhắc chuỗi suy nghĩ để giải bài suy luận
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.
Kỹ thuật Prompt với OpenAI API

Nhắc chuỗi suy nghĩ với 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.
Kỹ thuật Prompt với OpenAI API

Chuỗi suy nghĩ so với nhắc nhiều bước

Nhắc nhiều bước

  • Đưa các bước vào trong prompt

Sơ đồ cho thấy nhắc nhiều bước là prompt đưa cho mô hình một loạt bước tuần tự để thực hiện.

Kỹ thuật Prompt với OpenAI API

Chuỗi suy nghĩ so với nhắc nhiều bước

Nhắc nhiều bước

  • Đưa các bước vào trong prompt

Sơ đồ cho thấy nhắc nhiều bước là prompt đưa cho mô hình một loạt bước tuần tự để thực hiện.

Nhắc chuỗi suy nghĩ

  • Yêu cầu mô hình tạo các bước trung gian

Hình minh họa rằng với nhắc chuỗi suy nghĩ, các bước được mô hình tạo ra khi sinh đầu ra.

Kỹ thuật Prompt với OpenAI API

Hạn chế của chuỗi suy nghĩ

  • Một suy nghĩ sai → kết quả sai
  • Đã giới thiệu nhắc tự nhất quán

Sơ đồ cho nhắc chuỗi suy nghĩ, nhấn mạnh rằng một bước lý luận lỗi sẽ dẫn đến kết quả thất bại.

Kỹ thuật Prompt với OpenAI API

Nhắc tự nhất quán

  • Tạo nhiều chuỗi suy nghĩ bằng cách nhắc mô hình nhiều lần
  • Bỏ phiếu đa số để lấy kết quả cuối

Hình cho thấy nhắc tự nhất quán là tạo nhiều chuỗi suy nghĩ, mỗi chuỗi có một đầu ra, và kết quả cuối lấy theo đa số.

Kỹ thuật Prompt với OpenAI API

Nhắc tự nhất quán

Có thể thực hiện bằng cách định nghĩa nhiều prompt hoặc một prompt tạo nhiều phản hồi.

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))
Kỹ thuật Prompt với OpenAI API

Ví dụ nhắc tự nhất quán

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.
Kỹ thuật Prompt với OpenAI API

Hãy thực hành!

Kỹ thuật Prompt với OpenAI API

Preparing Video For Download...