Kỹ thuật nhắc lặp và tinh chỉnh

Kỹ thuật Prompt với OpenAI API

Fouad Trad

Machine Learning Engineer

Kỹ thuật nhắc lặp

  • Không có nhắc nào hoàn hảo ngay từ đầu
  • Kỹ thuật nhắc là quy trình lặp gồm:
    • Tạo nhắc
    • Gửi vào mô hình
    • Quan sát và phân tích đầu ra
    • Lặp lại để cải thiện nhắc

Hình minh họa một vòng lặp. Một vòng tròn xanh với bốn mũi tên nối tiếp nhau.

Kỹ thuật Prompt với OpenAI API

Tinh chỉnh nhắc

Nhắc ban đầu
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.
Nhắc đã tinh chỉnh
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   |
Kỹ thuật Prompt với OpenAI API

Ví dụ: phân tích hàm Python

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

Hình chữ nhật và công thức tính diện tích từ chiều rộng và chiều dài. Area = length\*width.

Kỹ thuật Prompt với OpenAI API

Ví dụ: nhắc ban đầu

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.
Kỹ thuật Prompt với OpenAI API

Ví dụ: tinh chỉnh nhắc

Chúng ta sửa nhắc để lấy ngôn ngữ lập trình

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.
Kỹ thuật Prompt với OpenAI API

Ví dụ: tinh chỉnh nhắc

Chúng ta sửa nhắc để lấy đầu ra có cấu trúc

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))
Kỹ thuật Prompt với OpenAI API

Ví dụ: tinh chỉnh nhắc

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.
Kỹ thuật Prompt với OpenAI API

Tinh chỉnh nhắc few-shot

  • Phân loại mô tả thời tiết

Nhắc ban đầu

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
Fresh snowfall with freezing temperatures. -> 
"""
print(get_response(prompt))
Snowy
Kỹ thuật Prompt với OpenAI API

Tinh chỉnh nhắc few-shot

  • Phân loại mô tả thời tiết

Nhắc ban đầu

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
Kỹ thuật Prompt với OpenAI API

Tinh chỉnh nhắc few-shot

Nhắc đã tinh chỉnh

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
Kỹ thuật Prompt với OpenAI API

Tinh chỉnh cho các loại nhắc khác nhau

  • Nhắc few-shot: tinh chỉnh ví dụ
  • Nhắc nhiều bước: tinh chỉnh bước hướng dẫn
  • Nhắc chain-of-thought và self-consistency: tinh chỉnh mô tả bài toán

Hình với ba hàng có các nút gạt, một bàn tay đang điều chỉnh để tinh chỉnh.

Kỹ thuật Prompt với OpenAI API

Ayo berlatih!

Kỹ thuật Prompt với OpenAI API

Preparing Video For Download...