問對問題是用好 ChatGPT 的關鍵 - 吳恩達教你學會下 Prompt 的技巧

問對問題是用好 ChatGPT 的關鍵 - 吳恩達教你學會下 Prompt 的技巧

吳恩達,人工智慧專家、史丹佛客座教授。曾任 Google Brain 團隊的創始人和領導者,現為 deeplearning.ai 和 Landing AI 的創始人和 CEO。日前他在 Twitter 宣佈跟 OpenAI 共同出品免費的公開課程:ChatGPT Prompt Engineering for Developers,課程材料是由 OpenAI 和 DeepLearning.ai 團隊共同開發。最終目標是激發開發人員的想像力,應用 LLMs 開發新的應用程式。

這門課程主要是針對開發人員介紹如何使用大型語言模型 (LLMs) 進行提示(Prompting),進而建立軟體應用程式,課程大約一個半小時而已,並且有提供 Jupyter Notebook 程式碼搭配說明,非常推薦工程師學習,我將我認為的重點整理到本篇文章。

課程的六個主題:

  1. Iterative Prompt Development(迭代提示開發):使用將分析和精煉提示,從產品說明書中生成營銷文案。
  2. Inferring(推論):學習如何從產品評論和新聞文章中推斷情感和主題。
  3. Summarizing(摘要):著重於如何針對特定主題進行文本摘要。
  4. Transforming(轉換):探索如何使用大型語言模型進行文本轉換任務,例如語言翻譯、拼寫和語法檢查、語氣調整和格式轉換。
  5. Expanding(擴展):生成針對每位客戶評論量身定制的客戶服務電子郵件。
  6. Chatbot(聊天機器人):利用聊天格式進行與聊天機器人的延續對話,並進行個性化或特定任務或行為的設置。

原則一:提供明確且具體的指令

很多人覺得 ChatGPT 不好用實際上可能是因為問錯了問題或是下錯的 Prompt。問題不夠好,就不會得到好的答案。在對 ChatGPT 提出問題時,問對問題非常重要,要提供清晰和具體的指令。這包括清楚地表達我們的意圖和期望,以避免產生歧義。同時,為了確保得到適切的回答,我們需要提供足夠的細節來限定生成範圍。

具體我們有四個對應的處理策略:

使用分隔符號明確指示輸入的不同部分

Summarize the text delimited by triple backticks into a single sentence.
```{text}```

要求 GPT 產生結構化輸出

Generate a list of three made-up book titles along with their authors and genres. 
Provide them in JSON format with the following keys: 
book_id, title, author, genre.

要求 GPT 檢查條件是否滿足

You will be provided with text delimited by triple quotes. 
If it contains a sequence of instructions, re-write those instructions in the following format:

Step 1 - ...
Step 2 - …
…
Step N - …

If the text does not contain a sequence of instructions, then simply write "No steps provided."

```{text}```

使用 Few-Shot 提示方法

Few-shot 是指在模型訓練中僅使用少量的樣本資料來完成一個任務。傳統的機器學習演算法需要大量的樣本資料來訓練模型,而 few-shot learning 則是一種更為高效的方法,它可以在非常有限的樣本資料下學習並完成一個任務。在自然語言處理中,few-shot prompting 可以幫助模型更快地適應新的任務,並在僅有少量的樣本資料的情況下完成這些任務。

Your task is to answer in a consistent style.

<child>: Teach me about patience.

<grandparent>: The river that carves the deepest valley flows from a modest spring; the grandest symphony originates from a single note; the most intricate tapestry begins with a solitary thread.

<child>: Teach me about resilience.

原則二:引導 GPT 回答前走過完整的思考路徑

指定完成任務所需的步驟

Perform the following actions: 
1 - Summarize the following text delimited by triple ackticks with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the following keys: french_summary, num_names.

Separate your answers with line breaks.

Text:
```{text}```

請 GPT 按照指定格式輸出

Your task is to perform the following actions: 
1 - Summarize the following text delimited by 
  <> with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the 
  following keys: french_summary, num_names.

Use the following format:
Text: <text to summarize>
Summary: <summary>
Translation: <summary translation>
Names: <list of names in Italian summary>
Output JSON: <json with summary and num_names>

Text: <{text}>

指示 GPT 在匆忙得出結論之前自行解決問題

Determine if the student's solution is correct or not.

Question:
I'm building a solar power installation and I need help working out the financials. 
- Land costs $100 square foot
- I can buy solar panels for $250 square foot
- I negotiated a contract for maintenance that will cost me a flat $100k per year, and an additional $10 square foot
What is the total cost for the first year of operations 
as a function of the number of square feet.

Student's Solution:
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000

你想考模型一個學生的數學解題過程及結果對不對,它可能一上來分析是錯的。如果你讓它先自己按照步驟做一下,先把答案寫出來,然後再去評價學生的答案是否正確,這樣模型就可以給出準確的答案。更好的 Prompt 應該是這樣:

Your task is to determine if the student's solution is correct or not.
To solve the problem do the following:
- First, work out your own solution to the problem. 
- Then compare your solution to the student's solution and evaluate if the student's solution is correct or not. 
Don't decide if the student's solution is correct until 
you have done the problem yourself.

Use the following format:
Question:
```
question here
```
Student's solution:
```
student's solution here
```
Actual solution:
```
steps to work out the solution and your solution here
```
Is the student's solution the same as actual solution just calculated:
```
yes or no
```
Student grade:
```
correct or incorrect
```

Question:
```
I'm building a solar power installation and I need help working out the financials. 
- Land costs $100 square foot
- I can buy solar panels for $250 square foot
- I negotiated a contract for maintenance that will cost me a flat $100k per year, and an additional $10 square foot
What is the total cost for the first year of operations as a function of the number of square feet.
``` 
Student's solution:
```
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
```
Actual solution:

結論:善用 LLM 模型的特性

我們很難在初次嘗試中就設計出最佳的提示。需要根據 GPT 的初步回應進行評估,不斷調整和優化提示,直到生成滿意的結果。這需要一定的耐心和經驗。

尤其 LLM 模型在生成文本時可能會出現 Hallucinations(幻覺) 的問題,也就是說它可能會生成看似合理但實際上不符合現實的內容。這種情況通常發生在模型受到了一些不符合現實的提示或信息的影響,例如錯誤的資料或含有誤導性的信息。這些 Hallucinations 可能會對應用的準確性和可靠性帶來影響,因此在使用 LLM 模型進行文本生成時,需要格外小心,確保輸入的提示和數據是真實可靠的,以避免產生不實或有害的結果。

這門課程提到了大型語言模型的基本原則與技巧,包括撰寫明確的指示、給模型充足的時間思考、迭代式的提示開發過程,以及大型語言模型的多種應用能力,如摘要、推論、轉換、擴展、和如何建構自定義的聊天機器人。

簡而言之,提示原則主要是提供清晰明確的指令和充足的思考時間,以幫助模型更好地完成任務。在具體操作上,可通過使用分隔符、要求結構化輸出、檢查條件是否滿足、Few-shot 提示等策略來實現。同時,還可以指定完成任務所需的步驟,或是指示模型在得出結論之前先自行解決問題。

在這個充滿活力且不斷發展的領域中,學習這些知識可以讓我們開發出別人不知道如何開發的應用程式。開發者們在使用大型語言模型的過程中,要謹慎使用,並且只構建有正面影響的應用程式。