Iteratives Prompt-Engineering und Verfeinerung

Prompt-Engineering mit der OpenAI-API

Fouad Trad

Machine Learning Engineer

Iteratives Prompt-Engineering

  • Kein Prompt ist von Anfang an perfekt.
  • Prompt Engineering ist ein iterativer Prozess, bei dem wir:
    • Einen Prompt erstellen
    • Den Prompt an das Modell übergeben
    • Die Ausgabe analysiere
    • Den Prozess wiederholen, um den Prompt zu verbessern

Bild, das einen Zyklus zeigt. Ein blauer Kreis mit vier Pfeilen, die ineinander übergehen.

Prompt-Engineering mit der OpenAI-API

Verfeinerung der Prompts

Initialer Prompt
prompt = "Generate an Excel sheet containing
five student names and their grades"

print(get_response(prompt))
I'm sorry, but as a text-based AI, I am 
unable to directly provide an Excel sheet. 
However, I can help you generate a sample
representation of the data you requested.
Verfeinerter Prompt
prompt = "Generate a table that I can copy 
to Excel, containing five student names and 
their grades"
print(get_response(prompt))
| Student Name | Grade |
|--------------|-------|
| Student 1    |  90   |
| Student 2    |  85   |
| Student 3    |  95   |
| Student 4    |  88   |
| Student 5    |  92   |
Prompt-Engineering mit der OpenAI-API

Beispiel: Eine Python-Funktion analysieren

code = '''
def calculate_rectangle_area(length, width):
    area = length * width
    return area
'''

Bild, das ein Rechteck und die Formel zeigt, mit der man seine Fläche aus Breite und Länge berechnen kann. Fläche = Länge * Breite.

Prompt-Engineering mit der OpenAI-API

Beispiel: initialer Prompt

prompt = f"""
  Analyze the code delimited by triple backticks with one sentence
  ```{code}```.
"""
print(get_response(prompt))
The code calculates the area of a rectangle based on its length and width.
Prompt-Engineering mit der OpenAI-API

Beispiel: Verfeinerung des Prompt

Wir ändern den Prompt, um die Programmiersprache zu ermitteln.

prompt = f"""
  Analyze the code delimited by triple backticks and provide its programming 
  language with one sentence 
  ```{code}```.
"""
print(get_response(prompt))
The provided code is a function written in Python that calculates the area of a 
rectangle based on its length and width.
Prompt-Engineering mit der OpenAI-API

Beispiel: Verfeinerung des Prompt

Wir ändern den Prompt, um eine strukturierte Ausgabe zu erhalten.

prompt = f"""
  For the function delimited by triple backticks, provide in a structured format
  the following:
  - description: one sentence short description
  - language: the programming language used
  - input: the inputs to the function
  - output: the output returned by the function
  ```{code}```.
"""
print(get_response(prompt))
Prompt-Engineering mit der OpenAI-API

Beispiel: Verfeinerung des Prompt

description: This function calculates the area of a rectangle.

language: Python

input:
 - length: The length of the rectangle.
 - width: The width of the rectangle.

output:
 - area: The calculated area of the rectangle, which is the product of the length 
 and width.
Prompt-Engineering mit der OpenAI-API

Few-Shot Prompt-Verfeinerung

  • Wetterbeschreibungsklassifizierung

Initialer Prompt:

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
Fresh snowfall with freezing temperatures. -> 
"""
print(get_response(prompt))
Snowy
Prompt-Engineering mit der OpenAI-API

Few-Shot Prompt-Verfeinerung

  • Wetterbeschreibungsklassifizierung

Initialer Prompt

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
The wind of change brought a refreshing breeze to the company's operations. -> 
"""
print(get_response(prompt))
Windy
Prompt-Engineering mit der OpenAI-API

Few-Shot Prompt-Verfeinerung

Verfeinerter Prompt

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
The political climate in the country was stormy -> Unknown
The wind of change brought a refreshing breeze to the company's operations. -> 
"""
print(get_response(prompt))
Unknown
Prompt-Engineering mit der OpenAI-API

Verbesserung des Prompts für verschiedene Arten von Prompts

  • Few-Shot-Prompts: Beispiele verfeinern
  • Multi-Step Prompts: Verfeinern die Anleitungsschritte
  • Chain-of-thought und self-consistency Prompts: Problemdarstellung verfeinern

Bild mit drei Reihen von Kippschaltern, die von einer Hand verstellt werden, um die Verfeinerung zu zeigen.

Prompt-Engineering mit der OpenAI-API

Lass uns üben!

Prompt-Engineering mit der OpenAI-API

Preparing Video For Download...