Ingineria iterativă a prompturilor

Prompt Engineering cu OpenAI API

Fouad Trad

Machine Learning Engineer

Ingineria iterativă a prompturilor

  • Niciun prompt nu poate fi perfect de la început
  • Ingineria prompturilor este un proces iterativ:
    • Construiți un prompt
    • Trimiteți-l modelului
    • Observați și analizați răspunsul
    • Reiterați pentru a îmbunătăți promptul

Imagine reprezentând un ciclu. Un cerc albastru cu patru săgeți care se succed.

Prompt Engineering cu OpenAI API

Rafinarea prompturilor

Prompt inițial
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.
Prompt rafinat
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 cu OpenAI API

Exemplu: analiza unei funcții Python

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

Imagine cu un dreptunghi și formula pentru calculul ariei în funcție de lățime și lungime. Arie = lungime\*lățime.

Prompt Engineering cu OpenAI API

Exemplu: prompt inițial

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

Exemplu: rafinarea promptului

Modificăm promptul pentru a obține limbajul de programare

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

Exemplu: rafinarea promptului

Modificăm promptul pentru a obține un rezultat structurat

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

Exemplu: rafinarea promptului

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

Rafinarea prompturilor few-shot

  • Clasificarea descrierilor meteorologice

Prompt inițial

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

Rafinarea prompturilor few-shot

  • Clasificarea descrierilor meteorologice

Prompt inițial

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

Rafinarea prompturilor few-shot

Prompt rafinat

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

Rafinarea prompturilor pentru diverse tipuri

  • Prompturi few-shot: rafinați exemplele
  • Prompturi multi-etapă: rafinați pașii de ghidare
  • Prompturi chain-of-thought și self-consistency: rafinați descrierea problemei

Imagine cu trei rânduri de comutatoare, o mână ajustând comutatoarele pentru a ilustra rafinarea.

Prompt Engineering cu OpenAI API

Să exersăm!

Prompt Engineering cu OpenAI API

Preparing Video For Download...