Ітеративне створення й вдосконалення промптів

Інженерія підказок з OpenAI API

Fouad Trad

Machine Learning Engineer

Ітеративне створення промптів

  • Жоден промпт не буде ідеальним спочатку
  • Prompt Engineering — це ітеративний процес, у якому ми:
    • Створюємо промпт
    • Передаємо його моделі
    • Спостерігаємо й аналізуємо результат
    • Повторюємо, щоб зробити промпт кращим

Зображення циклу. Синє коло з чотирма стрілками, що переходять одна в одну.

Інженерія підказок з OpenAI API

Вдосконалення промптів

Початковий промпт
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 = "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   |
Інженерія підказок з OpenAI API

Приклад: аналіз функції на Python

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

Зображення прямокутника та формули обчислення його площі за шириною й довжиною. Площа = довжина\*ширина.

Інженерія підказок з OpenAI API

Приклад: початковий промпт

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.
Інженерія підказок з OpenAI API

Приклад: уточнення промпту

Модифікуємо промпт, щоб отримати мову програмування

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.
Інженерія підказок з OpenAI API

Приклад: уточнення промпту

Модифікуємо промпт для структурованого виводу

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))
Інженерія підказок з OpenAI API

Приклад: уточнення промпту

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.
Інженерія підказок з OpenAI API

Вдосконалення few-shot промптів

  • Класифікація описів погоди

Початковий промпт

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
Fresh snowfall with freezing temperatures. -> 
"""
print(get_response(prompt))
Snowy
Інженерія підказок з OpenAI API

Вдосконалення few-shot промптів

  • Класифікація описів погоди

Початковий промпт

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
Інженерія підказок з OpenAI API

Вдосконалення few-shot промптів

Уточнений промпт

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
Інженерія підказок з OpenAI API

Вдосконалення для різних типів промптів

  • Few-shot промпти: уточнюйте приклади
  • Багатокрокові промпти: уточнюйте кроки-настанови
  • Chain-of-thought і self-consistency: уточнюйте опис задачі

Зображення з трьома рядами повзунків; рука налаштовує їх для вдосконалення.

Інженерія підказок з OpenAI API

Давайте потренуємось!

Інженерія підказок з OpenAI API

Preparing Video For Download...