사고 사슬과 자기 일관성 프롬프트

OpenAI API로 배우는 프롬프트 엔지니어링

Fouad Trad

Machine Learning Engineer

사고 사슬 프롬프트

  • LLM이 답변 전 추론 단계(생각)를 제시하도록 요구
  • 복잡한 추론 작업에 사용
  • 모델 오류 감소에 도움

사고 사슬 프롬프트가 문제를 단계별로 풀도록 요구하고, 각 단계의 추론이 출력에 포함됨을 보여 주는 이미지.

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로 배우는 프롬프트 엔지니어링

훈련 예시를 포함한 사고 사슬 프롬프트

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로 배우는 프롬프트 엔지니어링

사고 사슬 vs. 다단계 프롬프트

다단계 프롬프트

  • 프롬프트에 단계 포함

다단계 프롬프트는 모델에 순차적 단계를 수행하도록 지시하는 프롬프트임을 보여 주는 다이어그램.

OpenAI API로 배우는 프롬프트 엔지니어링

사고 사슬 vs. 다단계 프롬프트

다단계 프롬프트

  • 프롬프트에 단계 포함

다단계 프롬프트는 모델에 순차적 단계를 수행하도록 지시하는 프롬프트임을 보여 주는 다이어그램.

사고 사슬 프롬프트

  • 출력 전 중간 단계를 생성하도록 요청

사고 사슬 프롬프트에서는 출력 생성 중에 단계가 모델에 의해 생성됨을 보여 주는 이미지.

OpenAI API로 배우는 프롬프트 엔지니어링

사고 사슬의 한계

  • 한 번의 잘못된 사고 → 잘못된 결과
  • 자기 일관성 프롬프트 도입

사고 사슬 프롬프트에서 잘못된 추론이 한 번만 있어도 실패한 결과로 이어짐을 강조하는 다이어그램.

OpenAI API로 배우는 프롬프트 엔지니어링

자기 일관성 프롬프트

  • 모델을 여러 번 호출해 여러 사고 사슬 생성
  • 최종 답은 다수결로 결정

자기 일관성 프롬프트는 여러 사고 사슬을 생성하고 각 출력에서 다수결로 최종 결과를 얻는 과정을 보여 주는 이미지.

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