Iterativní prompt engineering a jeho zdokonalování

Prompt Engineering s OpenAI API

Fouad Trad

Machine Learning Engineer

Iterativní prompt engineering

  • Žádný prompt není hned dokonalý
  • Prompt engineering je iterativní proces:
    • Vytvoříme prompt
    • Předáme ho modelu
    • Sledujeme a analyzujeme výstup
    • Iterujeme pro zlepšení promptu

Obrázek znázorňující cyklus. Modrý kruh se čtyřmi šipkami navazujícími na sebe.

Prompt Engineering s OpenAI API

Zdokonalování promptů

Počáteční 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.
Zdokonalený 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 s OpenAI API

Příklad: analýza funkce v Pythonu

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

Obrázek znázorňující obdélník a vzorec pro výpočet jeho obsahu na základě šířky a délky. Area = length\*width.

Prompt Engineering s OpenAI API

Příklad: počáteční 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 s OpenAI API

Příklad: zdokonalení promptu

Upravíme prompt pro získání programovacího jazyka

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 s OpenAI API

Příklad: zdokonalení promptu

Upravíme prompt pro získání strukturovaného výstupu

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 s OpenAI API

Příklad: zdokonalení promptu

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 s OpenAI API

Zdokonalování few-shot promptů

  • Klasifikace popisu počasí

Počáteční 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 s OpenAI API

Zdokonalování few-shot promptů

  • Klasifikace popisu počasí

Počáteční 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 s OpenAI API

Zdokonalování few-shot promptů

Zdokonalený 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 s OpenAI API

Zdokonalování promptů různých typů

  • Few-shot prompty: zdokonalování příkladů
  • Vícekrokové prompty: zdokonalování kroků
  • Chain-of-thought a self-consistency prompty: zdokonalování popisu problému

Obrázek se třemi řadami přepínačů, kde ruka upravuje přepínače znázorňující zdokonalování.

Prompt Engineering s OpenAI API

Pojďme si procvičit!

Prompt Engineering s OpenAI API

Preparing Video For Download...