Introduction-Examples of Prompts

提示範例實作 (Examples of Prompts)

上一節介紹了如何撰寫提示來與大型語言模型(LLMs)互動的基本概念。
本節將透過更多實例,展示如何利用提示完成各種任務,並在過程中介紹關鍵概念。

學習提示工程的最佳方式之一,就是透過實例操作與觀察輸出結果
以下這些例子能幫助你掌握如何精心設計提示,並應用於不同任務情境中。


本篇目標


接下來的每個小節都將提供一個清楚的提示範例(Prompt),並說明設計重點與預期輸出結果,幫助你快速上手提示工程技術。

文本摘要(Text Summarization)

文本摘要是自然語言生成(NLG)中的標準任務之一。
其應用非常廣泛,從新聞濃縮、會議紀錄整理,到產品評論摘要都有用武之地。

事實上,語言模型最令人期待的應用之一,就是能將文章與概念快速轉換成簡潔易讀的摘要。
以下是使用提示進行基本摘要任務的範例:


任務:介紹「抗生素」是什麼

Prompt

Explain antibiotics  
A:

Output

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

"A:" 是一種明確的提示格式,常見於問答(QA)任務中。
你在這裡使用它,是為了告訴模型「接下來會有一個答案」的提示訊號。

在這個例子中,使用 "A:" 是否有明顯幫助還不容易看出來,
但在後面的進階範例中,我們會更清楚地看到它的作用。


現在,假設上述段落內容太多,我們想要將它進一步濃縮成一句話的摘要。
你可以這樣指示模型進行單句摘要:

Prompt

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

Explain the above in one sentence:

Output

Antibiotics are medications used to treat bacterial infections by either killing the bacteria or stopping them from reproducing, but they are not effective against viruses and overuse can lead to antibiotic resistance.

先暫時不考慮上方輸出的正確性(這部分我們會在後續章節更深入討論),
重點是模型成功地嘗試將段落內容濃縮為一句話。

你可以透過更有創意或更明確的指令來達到不同的摘要效果,
但這些我們會在後面章節中再深入介紹。

建議你先在這裡暫停,實際操作看看,看看你能不能用不同的方式提示模型,獲得更理想的摘要結果。


資訊擷取(Information Extraction)

雖然大型語言模型(LLMs)主要訓練來執行自然語言生成(NLG)任務,但它們也非常擅長分類(Classification)以及其他各種自然語言處理(NLP)任務。

以下是一個從段落中擷取資訊的提示範例:

Prompt

Author-contribution statements and acknowledgements in research papers should state clearly and specifically whether, and to what extent, the authors used AI technologies such as ChatGPT in the preparation of their manuscript and analysis. They should also indicate which LLMs were used. This will alert editors and reviewers to scrutinize manuscripts more carefully for potential biases, inaccuracies and improper source crediting. Likewise, scientific journals should be transparent about their use of LLMs, for example when selecting submitted manuscripts.

Mention the large language model based product mentioned in the paragraph above:

Output

The large language model based product mentioned in the paragraph above is ChatGPT.

雖然你可以透過許多方式進一步提升輸出的品質,但即便是這樣的基本提示,也已經非常實用了。

目前應該已經很明顯,只要你透過簡單的指令告訴模型要做什麼,它就能執行各種不同任務。

這正是 AI 產品開發者們現在廣泛運用的強大能力,用來打造功能強大且具有價值的應用與體驗。

段落來源: ChatGPT: five priorities for research


問答(Question Answering)

讓模型產出具體且準確的回答,其中一個最佳方法就是改善提示的格式(prompt format)。

如先前所提,提示(prompt)可以結合以下元素來獲得更好的結果:

  • Instruction(指令):說明你希望模型執行的任務。
  • Context(上下文):提供額外的資訊協助模型做出正確判斷。
  • Input(輸入):你希望得到答案的問題或資料。
  • Output Indicator(輸出指示):指示模型產生的內容型態。

雖然這些元素並非必要,但越是明確地指示任務,結果通常會越好。


範例:

Prompt

Answer the question based on the context below. Keep the answer short and concise. Respond "Unsure about answer" if not sure about the answer.

Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.

Question: What was OKT3 originally sourced from?

Answer:

Output

Mice.

上下文來至於 Nature.


文本分類(Text Classification)

到目前為止,我們已經使用簡單的指令來執行任務。作為一名提示工程師(Prompt Engineer),你需要學會提供更精準的指令,但這還不夠!

對於較複雜的情境,光是下指令往往無法得到理想結果,此時你就需要考慮更多提示元素,例如:

  • 上下文(Context)
  • 輸入資料(Input Data)
  • 範例示範(Examples)

範例一:單一指令

Prompt

Classify the text into neutral, negative or positive.
Text: I think the food was okay.
Sentiment:

Output

Neutral

模型成功地根據文字內容分類為 “Neutral”。但假設你希望輸出格式是小寫 neutral,該怎麼辦?


範例二:加入示範範例(Few-shot)

Prompt

Classify the text into neutral, negative or positive. 

Text: I think the vacation is okay.
Sentiment: neutral 

Text: I think the food was okay. 
Sentiment:

Output

neutral

這次模型正確回應了你期望的小寫 neutral,因為提示中加入了正確範例格式,有助於引導模型生成正確輸出!


範例三:錯誤拼字導致誤導

Prompt

Classify the text into nutral, negative or positive.
Text: I think the vacation is okay.
Sentiment:

Output

Neutral

這次模型回應的 neutral 完全符合你期望的格式!


總結

  • 模型雖然理解語意,但不一定會自動照你想要的格式輸出。
  • 加入示範是讓模型模仿你期待格式的有效方法。
  • 提示工程師必須思考不只是內容的準確性,也要掌握輸出格式的精準控制

接下來我們會繼續介紹為什麼拼字錯誤、上下文缺乏會影響分類行為。


對話(Conversation)

在提示工程中,最有趣的應用之一是能夠透過提示來指示語言模型的行為、角色與語氣。這在建立像是客服機器人或對話系統時非常實用。


技術風格的研究助理

Prompt

The following is a conversation with an AI research assistant. The assistant tone is technical and scientific.

Human: Hello, who are you?
AI: Greeting! I am an AI research assistant. How can I help you today?
Human: Can you tell me about the creation of blackholes?
AI:

Output

Sure! Black holes are regions of spacetime where the gravitational force is so strong that nothing, not even light, can escape from it. They are created when a very massive star dies and its core collapses in on itself, forming a singularity of infinite density. The intense gravity of the singularity pulls in all the matter and radiation around it, creating the black hole.

上面AI 助理的回答非常科學、技術性強,適合科研用途。


淺顯易懂的回答風格

現在讓我們換個語氣,讓 AI 以更簡單、適合小學生理解的方式回答。

Prompt

TThe following is a conversation with an AI research assistant. The assistant answers should be easy to understand even by primary school students.

Human: Hello, who are you?
AI: Greeting! I am an AI research assistant. How can I help you today?
Human: Can you tell me about the creation of black holes?
AI: 

Output

Sure! Black holes are created when a very massive star runs out of fuel and collapses in on itself. The gravity of the star is so strong that nothing, not even light, can escape its pull. This creates a point in space-time with an intense gravitational force called a black hole.

總結

  • 使用明確的敘述來設定角色、語氣與風格,可以讓模型根據需求調整回應方式。
  • 這種做法稱為 Role Prompting(角色提示)
  • 你還可以透過加入更多範例(few-shot prompting)進一步細緻地調整模型的行為與語氣。

想讓 AI 更精準地模仿特定語氣或說話方式?加入更多對話示範是一個有效的做法!


程式碼生成(Code Generation)

LLMs(大型語言模型)在程式碼生成方面非常強大。像 GitHub Copilot 就是這類應用的最佳例子之一。你可以透過設計巧妙的提示,讓模型協助你完成各種程式碼任務。以下是幾個範例:


範例 1:簡單的互動程式

Prompt

/*
Ask the user for their name and say "Hello"
*/

Output

let name = prompt("What is your name?");
console.log(`Hello, ${name}!`);

你甚至不需要指定程式語言,模型就能自行推測並給出合理的結果。

範例 2:SQL 查詢生成

現在我們試試更進階的任務。以下是一個提示與生成 MySQL 查詢的例子,展示了 LLMs 在理解結構化資料與語意上的強大能力。

Prompt

"""
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
Create a MySQL query for all students in the Computer Science Department
"""

Output

SELECT StudentId, StudentName 
FROM students 
WHERE DepartmentId IN (SELECT DepartmentId FROM departments WHERE DepartmentName = 'Computer Science');

在這個例子中,你提供了資料表結構的上下文,並請模型撰寫查詢語句。結果非常符合預期,這顯示了語言模型在理解資料架構與生成語法正確查詢方面的能力。


總結:

  • 程式碼生成不需要你提供完整的程式語法或語言指令,有時只需自然語言描述即可。
  • 當提供結構化資訊(如資料表與欄位)時,LLMs 能夠理解上下文並自動產出有效程式碼。
  • 你可以使用此技術來協助撰寫查詢、API 請求、資料轉換腳本、單元測試程式等多種開發任務。

建議:逐步調整提示的內容與細節,會顯著提升生成程式碼的準確度與實用性。


推理(Reasoning)

推理是目前 LLM(大型語言模型)最具挑戰性的任務之一,卻也是最令人著迷的領域,因為它衍生出了許多高階應用。

雖然在數學推理方面已有些許進展,但值得注意的是:目前的 LLM 仍然容易在推理任務中失誤,因此這需要更加進階的提示工程技巧。我們會在下一章深入探討這類進階技巧。現在,讓我們先透過一些簡單的例子來觀察其算術能力。


範例 1:基本乘法

Prompt

What is 9,000 * 9,000?

Output

81,000,000

範例 2:奇數加總

Prompt

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. 

A: 

Output

No, the odd numbers in this group add up to an odd number: 119.

錯誤!這答案不對。讓我們嘗試透過改寫提示來改善結果。

範例 3:逐步推理解題

Prompt

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. 

Solve by breaking the problem into steps. First, identify the odd numbers, add them, and indicate whether the result is odd or even. 

Output

Odd numbers: 15, 5, 13, 7, 1
Sum: 41 
41 is an odd number.

這次好多了!這說明了「逐步指示(step-by-step instruction)」在推理任務中能有效提升模型準確度。


總結

  • 推理任務(如數學運算、邏輯分析)對 LLM 而言仍具挑戰。
  • 逐步拆解任務步驟(Chain-of-Thought Prompting) 是提高準確度的關鍵技巧。
  • 若搭配範例(Few-shot)與具體指示,可以進一步穩定結果。
  • 推理相關任務需要更多測試與反覆優化,下一節我們會進一步介紹進階提示技巧。

References

Prompt Engineering Guide


上一篇:介紹 - 設計提示語的技巧
下一篇:Prompting Techniques - 簡介