迭代式提示工程与优化

使用 OpenAI API 的 Prompt Engineering

Fouad Trad

Machine Learning Engineer

迭代式提示工程

  • 没有提示一开始就是完美的
  • 提示工程是一个迭代过程,我们需:
    • 构建提示
    • 输入给模型
    • 观察并分析输出
    • 反复改进提示

表示循环的图像。一个蓝色圆圈,四个箭头首尾相连。

使用 OpenAI API 的 Prompt Engineering

优化提示

初始提示
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 的 Prompt Engineering

示例:分析 Python 函数

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

显示矩形及其面积公式的图像:给定宽和长,面积 = 长\*宽。

使用 OpenAI API 的 Prompt Engineering

示例:初始提示

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 Engineering

示例:提示优化

我们修改提示以获取编程语言

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 Engineering

示例:提示优化

我们修改提示以获得结构化输出

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 的 Prompt Engineering

示例:提示优化

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 的 Prompt Engineering

少样本提示优化

  • 天气描述分类

初始提示

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 的 Prompt Engineering

少样本提示优化

  • 天气描述分类

初始提示

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 的 Prompt Engineering

少样本提示优化

优化后的提示

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 的 Prompt Engineering

不同提示类型的优化

  • 少样本提示:优化示例
  • 多步提示:优化引导步骤
  • 思维链与自一致性提示:优化问题描述

图像显示三行带拨杆的控件,一只手调整拨杆以体现优化。

使用 OpenAI API 的 Prompt Engineering

Vamos praticar!

使用 OpenAI API 的 Prompt Engineering

Preparing Video For Download...