Chain-of-thought und self-consistency Prompting

Prompt-Engineering mit der OpenAI-API

Fouad Trad

Machine Learning Engineer

Gedankenketten (Chain-of-Thought) Prompting

  • Erfordert, dass LLMs ihre Gedankengänge (Überlegungen) darlegen, bevor sie eine Antwort geben.
  • Für komplizierte Denkaufgaben
  • Hilft, Modellfehler zu reduzieren

Bild, das zeigt, wie ein Chain-of-Thought-Prompt das Modell auffordert, ein Problem Schritt für Schritt zu lösen, und die Ausgabe die Begründung für jeden durchgeführten Schritt enthält.

Prompt-Engineering mit der OpenAI-API

Gedankenketten (Chain-of-Thought) Prompting

Standard Prompting zum Lösen einer Denkaufgabe
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 mit der OpenAI-API

Gedankenketten (Chain-of-Thought) Prompting

Chain-of-thought Prompting, zum Lösen einer Denkaufgabe
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 mit der OpenAI-API

Few-shot chain-of-thought Prompting

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 mit der OpenAI-API

Chain-of-thought versus Multi-Step Prompting

Multi-Step Prompts

  • Schritte in den Prompt einbauen.

Diagramm, das zeigt, dass eine mehrstufige Prompt ein Prompt ist, die dem Modell eine Reihe von aufeinanderfolgenden Schritten vorgibt, die es ausführen soll.

Prompt-Engineering mit der OpenAI-API

Chain-of-thought versus Multi-Step Prompting

Multi-Step Prompts

  • Schritte in den Prompt einbauen.

Diagramm, das zeigt, dass eine mehrstufige Prompt ein Prompt ist, die dem Modell eine Reihe von aufeinanderfolgenden Schritten vorgibt, die es ausführen soll.

Chain-of-thought Prompts

  • Zu Zwischenschritten auffordern.

Bild, das zeigt, dass bei Chain-of-Thought-Prompts die Schritte vom Modell gemacht werden, während es die Ausgabe erzeugt.

Prompt-Engineering mit der OpenAI-API

Grenzen von Chain-of-thought

  • Ein misslungener Gedanke --> ein schlechtes Ergebnis
  • Einführung von Self-consistency Prompts

Diagramm für eine Gedankenkette, das zeigt, dass ein Gedanke mit fehlerhafter Argumentation zu einem erfolglosen Ergebnis führt.

Prompt-Engineering mit der OpenAI-API

Self-consistency Prompting

  • Generiert mehrere Gedankengänge, indem das Modell mehrmals dazu aufgefordert wird.
  • Mehrheitsbeschluss, um das Endergebnis festzulegen.

Bild, das zeigt, dass es bei einer Selbstkonsistenz-Aufforderung einfach um mehrere Gedankengänge geht, von denen jeder zu einem Ergebnis führt, und dass das Endergebnis durch Mehrheitsbeschluss erzielt wird.

Prompt-Engineering mit der OpenAI-API

Self-consistency Prompting

Indem mehrere Prompts oder ein Prompt mit mehreren Antworten definiert wird.

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 mit der 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 mit der OpenAI-API

Lass uns üben!

Prompt-Engineering mit der OpenAI-API

Preparing Video For Download...