多步驟提示

使用 OpenAI API 的提示工程

Fouad Trad

Machine Learning Engineer

多步驟提示

  • 將最終目標拆成多個步驟
  • 模型依序執行每步產生最終輸出
  • 多步驟提示適用於:
    • 連續性任務
    • 認知性任務

示意圖:多步驟提示會提供一連串需依序執行的步驟給模型。

使用 OpenAI API 的提示工程

把多步驟提示當作藏寶地圖

代表藏寶地圖與尋寶線索的圖片。

使用 OpenAI API 的提示工程

單步提示:寫一篇部落格

prompt = "Compose a travel blog"
print(get_response(prompt))
Title: Exploring the Enchanting Landscapes of Iceland

Introduction: Welcome to my travel blog! Today, I am thrilled to share my unforgettable journey through 
the mesmerizing landscapes of Iceland. 

Day 1: Reykjavik - The Charming Capital [...]
Day 2: Golden Circle - Nature's Wonders [...]
Day 3: South Coast - A Journey of Ice and Fire [...]
Day 4: Glacier Lagoon - A Frozen Wonderland [...]
Day 5: Blue Lagoon - A Relaxing Finale [...]
使用 OpenAI API 的提示工程

多步驟提示:寫旅遊部落格文章

prompt = """Compose a travel blog as follows:
Step 1: Introduce the destination.
Step 2: Share personal adventures during the trip.
Step 3: Summarize the journey.
"""
print(get_response(prompt))
使用 OpenAI API 的提示工程

撰寫旅遊部落格文章

Title: Exploring the Enchanting Streets of Barcelona

Step 1: Introduce the destination. Welcome to Barcelona, a vibrant city nestled along the stunning Mediterranean coast of Spain [...]
Step 2: Share personal adventures during the trip. Exploring the narrow, winding streets of the Gothic Quarter, I stumbled upon hidden gems at every turn. [...]
Step 3: Summarize the journey. As my journey through Barcelona came to an end, I couldn't help but feel grateful for the incredible experiences and memories I had made [...]
使用 OpenAI API 的提示工程

分析解答正確性

  • 檢查解答正確性常需多步驟
  • 例:
    • 用於計算的 Python 函式程式碼

計算機圖片

使用 OpenAI API 的提示工程

分析解答正確性

常見待檢查的解答

calculator = """
def add(a, b):
    return a + b
def subtract(a, b):
    return a - b
def multiply(a, b):
    return a * b
def divide(a, b):
    return a / b
"""
單步提示:
prompt = f"""Determine if the code delimited 
by triple backticks is correct or not.
Answer by yes or no.
```{calculator}```"""

print(get_response(prompt))
Yes
使用 OpenAI API 的提示工程

用多步驟提示分析解答正確性

多步驟提示:
prompt = f"""Determine the correctness of the code delimited by triple backticks 
as follows: 
Step 1: Check the code correctness in each function. 
Step 2: Verify if the divide function handles the case when dividing by 0. 
Code: ```{calculator}```"""

print(get_response(prompt))
The code appears to be correct. It defines four functions: add, subtract, multiply, and 
divide. Each function performs the corresponding mathematical operation and returns the
result. However, it does not handle the case when dividing by 0, which can result in a 
ZeroDivisionError.
使用 OpenAI API 的提示工程

多步驟 vs. 少樣本提示

步驟

  • 明確告訴模型要做什麼

示意圖:多步驟提示將指令拆成一系列步驟,讓模型逐一處理。

使用 OpenAI API 的提示工程

多步驟 vs. 少樣本提示

步驟

  • 明確告訴模型要做什麼

示意圖:多步驟提示將指令拆成一系列步驟,讓模型逐一處理。

樣本

  • 問答範例可供模型學習

示意圖:少樣本提示以多組範例問答加上當前問題作為輸入。

使用 OpenAI API 的提示工程

一起來練習吧!

使用 OpenAI API 的提示工程

Preparing Video For Download...