การปรับปรุง Prompt อย่างวนซ้ำ

Prompt Engineering กับ OpenAI API

Fouad Trad

Machine Learning Engineer

Iterative prompt engineering

  • ไม่มี prompt ใดที่สมบูรณ์แบบตั้งแต่แรก
  • Prompt Engineering เป็นกระบวนการวนซ้ำที่ประกอบด้วย:
    • สร้าง prompt
    • ส่งให้โมเดล
    • สังเกตและวิเคราะห์ผลลัพธ์
    • ปรับปรุง prompt ให้ดีขึ้น

ภาพแสดงวงจร วงกลมสีน้ำเงินที่มีลูกศรสี่ดอกต่อเนื่องกัน

Prompt Engineering กับ OpenAI API

การปรับปรุง prompt

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.
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 กับ OpenAI API

ตัวอย่าง: วิเคราะห์ฟังก์ชัน Python

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

ภาพแสดงรูปสี่เหลี่ยมผืนผ้าและสูตรคำนวณพื้นที่จากความกว้างและความยาว Area = length\*width

Prompt Engineering กับ OpenAI API

ตัวอย่าง: 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 กับ OpenAI API

ตัวอย่าง: การปรับปรุง prompt

ปรับ prompt เพื่อดึงข้อมูลภาษาโปรแกรม

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 กับ OpenAI API

ตัวอย่าง: การปรับปรุง prompt

ปรับ prompt เพื่อให้ได้ผลลัพธ์ที่มีโครงสร้าง

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 กับ OpenAI API

ตัวอย่าง: การปรับปรุง 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 กับ OpenAI API

การปรับปรุง few-shot prompt

  • การจำแนกประเภทคำอธิบายสภาพอากาศ

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 กับ OpenAI API

การปรับปรุง few-shot prompt

  • การจำแนกประเภทคำอธิบายสภาพอากาศ

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 กับ OpenAI API

การปรับปรุง few-shot prompt

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 กับ OpenAI API

การปรับปรุง prompt สำหรับแต่ละประเภท

  • Few-shot prompt: ปรับปรุงตัวอย่าง
  • Multi-step prompt: ปรับปรุงขั้นตอนชี้นำ
  • Chain-of-thought และ self-consistency prompt: ปรับปรุงคำอธิบายปัญหา

ภาพแสดงสามแถวที่มีสวิตช์ปรับ โดยมือกำลังปรับสวิตช์เพื่อสะท้อนการปรับปรุง

Prompt Engineering กับ OpenAI API

มาฝึกกันเถอะ!

Prompt Engineering กับ OpenAI API

Preparing Video For Download...