提示工程的關鍵原則

使用 OpenAI API 的提示工程

Fouad Trad

Machine Learning Engineer

清楚且精確的提示

一張示意圖:麵包店與房子之間有兩條路線;一條簡短有效,另一條複雜冗長且無效。

使用 OpenAI API 的提示工程

關鍵原則

 

  • 🎬 適當的動作動詞
  • 💬 具體且精確的說明
  • ❗ 結構良好的分隔提示
使用 OpenAI API 的提示工程

使用動作動詞

  • 指示模型要做什麼

動詞對照表:建議使用 write、complete、explain、describe、evaluate。

使用 OpenAI API 的提示工程

避免含糊的動詞

  • 會混淆模型的理解

動詞對照表:建議使用 write、complete、explain、describe、evaluate;避免使用 understand、think、feel、try、know。

使用 OpenAI API 的提示工程

動詞帶來的有效提示

prompt = "Think about the issue of 
deforestation."
response = get_response(prompt)
print(response)
Deforestation is a significant 
environmental issue involving 
permanently removing
or destroying forests and woodlands. 
It has far-reaching impacts on the 
environment, ecosystems, wildlife, 
and human communities. [...]
prompt = "Propose strategies to 
reduce deforestation."
response = get_response(prompt)
print(response)
Reducing deforestation requires 
a comprehensive and multi-dimensional 
approach involving various stakeholders. 
Here are several strategies 
that can help address the issue:
Strengthen Forest Governance
Promote Sustainable Land Use [...]
使用 OpenAI API 的提示工程

撰寫詳細說明

 

就下列面向提供具體、可描述、且詳盡的說明

  • 📖 情境
  • 📏 輸出長度
  • 🖌 格式與風格
  • 👥👥 受眾
使用 OpenAI API 的提示工程

含說明的有效提示

無效提示「Tell me about dogs.」

有效提示

prompt = "Write a descriptive paragraph about the behavior and characteristics of 
Golden Retrievers, highlighting their friendly nature, intelligence, 
and suitability as family pets."
print(get_response(prompt))
Golden Retrievers are beloved worldwide for their exceptional behavior, remarkable 
characteristics, and friendly nature. They are highly intelligent, trainable, and 
adaptable, making them great companions for families, including those with children.
使用 OpenAI API 的提示工程

限制輸出長度

max_tokens:

  • 代幣數上限
  • 輸出無法繞過此限制
  • 可能導致回應不完整或被截斷

Prompt:

  • 限制字數、句數或段落數
  • 輸出可能繞過此限制
  • 回應可完整呈現
使用 OpenAI API 的提示工程

提示的組成

  • 說明與要處理的輸入資料
  • 例:文字摘要
    • 說明:摘要給定文字
    • 輸入資料:要摘要的文字

文字摘要任務的圖示

使用 OpenAI API 的提示工程

用分隔符打造良好結構的提示

  • 以說明開頭
  • 使用分隔符(括號、方括號、反引號等)標示輸入部分
  • 說明所用的分隔符
prompt = """Summarize the text delimited by triple backticks into bullet points.
           ```TEXT GOES HERE```"""
response = get_response(prompt)
使用 OpenAI API 的提示工程

使用格式化字串(f-strings)

  • 將定義好的字串放入另一個字串
text = "This is a sample text to summarize"

prompt = f"""Summarize the text delimited by triple backticks into bullet points. ```{text}```"""
print(prompt)
Summarize the text delimited by triple backticks into bullet points.
```This is a sample text to summarize```
使用 OpenAI API 的提示工程

一起來練習吧!

使用 OpenAI API 的提示工程

Preparing Video For Download...