本頁面將介紹一些一般提示設計策略,可在設計提示時採用。
大型語言模型 (LLM) 會使用大量文字資料進行訓練,以學習語言之間的模式和關係。當我們提供一些文字 (提示) 時,語言模型就能預測可能的後續內容,就像是精密的自動完成工具一樣。
Google AI Studio 會代管簡單的提示集錦,用於以互動方式展示本指南中分享的許多概念。在本指南的其餘部分,您將瞭解下列提示設計策略:提供清楚明確的指示
為模型提供指示,指出應採取的行動,是自訂模型行為的有效且有效率的方式。請務必提供清楚明確的指示。操作說明可以是簡單的逐步操作說明清單,也可以是複雜的使用者體驗和心態地圖。
定義要執行的工作
詳細說明您希望模型執行的工作。這項工作可以是簡單的逐步操作說明清單,也可以是複雜的使用者體驗和心態對應作業。以下提示會指示模型摘要一段文字:
Summarize this text: Text: A quantum computer exploits quantum mechanical phenomena to perform calculations exponentially faster than any modern traditional computer. At very tiny scales, physical matter acts as both particles and as waves, and quantum computing uses specialized hardware to leverage this behavior. The operating principles of quantum devices is beyond the scope of classical physics. When deployed at scale, quantum computers could be used in a wide variety of applications such as: in cybersecurity to break existing encryption methods while helping researchers create new ones, in meteorology to develop better weather forecasting etc. However, the current state of the art quantum computers are still largely experimental and impractical. Quantum computers leverage the wave-particle duality of matter at tiny scales to perform calculations exponentially faster than traditional computers. They harness quantum phenomena, which are beyond the scope of classical physics, to achieve this speed advantage. While promising for applications like cybersecurity, weather forecasting, and beyond, current quantum computers are still experimental and not yet practical for widespread use. |
指定任何限制
指定讀取提示或產生回覆時的任何限制。您可以告訴模型要做什麼和不做什麼。在前述範例中,摘要比原始文字短,但您可能希望摘要更短。在這種情況下,您可以在提示中指定限制,指出摘要的長度:
Summarize this text in two sentences: Text: A quantum computer exploits quantum mechanical phenomena to perform calculations exponentially faster than any modern traditional computer. At very tiny scales, physical matter acts as both particles and as waves, and quantum computing uses specialized hardware to leverage this behavior. The operating principles of quantum devices is beyond the scope of classical physics. When deployed at scale, quantum computers could be used in a wide variety of applications such as: in cybersecurity to break existing encryption methods while helping researchers create new ones, in meteorology to develop better weather forecasting etc. However, the current state of the art quantum computers are still largely experimental and impractical.
Quantum computers harness the wave-particle duality of matter at the quantum level to perform computations exponentially faster than traditional computers. While these machines hold immense potential for fields like cybersecurity and weather forecasting, they are currently in their early stages of development and not yet practical for widespread use. |
定義回應格式
您也可以提供指示,說明回應的格式。舉例來說,您可以要求回應的格式包括表格、項目符號清單、電梯簡報、關鍵字、句子或段落。以下系統訊息會指示模型以更口語化的回應方式回覆:
All questions should be answered comprehensively with details, unless the user requests a concise response specifically. Respond in the same language as the query. What is a smart way to create a lemonade business? Launching a Successful Lemonade Stand: A Smart Approach Creating a lemonade business, whether a simple stand or a larger operation, requires strategic thinking. Here's a breakdown of how to approach it smartly: 1. Define Your Business: Scope: Are you aiming for a small, seasonal lemonade stand, a food truck, a delivery service, or something else entirely? Target Audience: Who are you selling to? Families, office workers, tourists? Unique Selling Proposition: What sets your lemonade apart? Special flavors, organic ingredients, a unique recipe? Pricing: Research competitor prices and determine a profitable pricing structure. Location: Consider foot traffic, accessibility, and regulations for your chosen location. ..... |
摘要
- 為模型提供操作說明。
- 指示要清楚明確。
- 指定輸出內容的任何限制或格式規定。
加入少量範例
您可以在提示中加入範例,讓模型取得最佳結果。模型會嘗試從範例中找出模式和關係,並在產生回應時套用這些模式和關係。含有少量範例的提示稱為「少量樣本」提示,而沒有提供範例的提示稱為「零樣本」提示。少量樣本提示通常用於調節模型回應的格式設定、措辭、範圍或一般模式。使用具體且多樣化的示例協助模型縮小範圍,生成更準確的結果。
建議你一律在提示中加入少量樣本。沒有少量示例的提示可能成效不彰。事實上,如果範例能清楚說明手邊的工作,您可以從提示中移除操作說明。
零樣本和少量樣本提示
下列零樣本提示要求模型選擇最佳說明。
Please choose the best explanation to the question: Question: How is snow formed? Explanation1: Snow is formed when water vapor in the air freezes into ice crystals in the atmosphere, which can combine and grow into snowflakes as they fall through the atmosphere and accumulate on the ground. Explanation2: Water vapor freezes into ice crystals forming snow. Answer: Explanation 1 is the best explanation. |
如果用途要求模型產生簡潔的回應,您可以在提示中加入範例,讓模型優先提供簡潔的回應。
以下提示提供兩個範例,說明較短的說明較佳。在回覆中,您可以看到示例引導模型選擇較短的說明 (Explanation2),而非像先前那樣選擇較長的說明 (Explanation1)。
Below are some examples showing a question, explanation, and answer format: Question: Why is sky blue? Explanation1: The sky appears blue because of Rayleigh scattering, which causes shorter blue wavelengths of light to be scattered more easily than longer red wavelengths, making the sky look blue. Explanation2: Due to Rayleigh scattering effect. Answer: Explanation2 Question: What is the cause of earthquakes? Explanation1: Sudden release of energy in the Earth's crust. Explanation2: Earthquakes happen when tectonic plates suddenly slip or break apart, causing a release of energy that creates seismic waves that can shake the ground and cause damage. Answer: Explanation1 Now, Answer the following question given the example formats above: Question: How is snow formed? Explanation1: Snow is formed when water vapor in the air freezes into ice crystals in the atmosphere, which can combine and grow into snowflakes as they fall through the atmosphere and accumulate on the ground. Explanation2: Water vapor freezes into ice crystals forming snow. Answer: Answer: Explanation2 |
找出最理想的樣本數量
您可以嘗試在提示中提供的範例數量,取得最需要的結果。雖然 Gemini 等模型通常只需少量範例就能找出模式,但您可能需要試驗幾個範例才能獲得理想結果。同時,如果您納入太多範例,模型可能會開始過度擬合範例的回應。
使用示例說明模式,而非反模式
使用示例向模型顯示應遵循的模式,比使用示例向模型顯示應避免的反模式更有效。
⛔ 排除模式:
Don't end haikus with a question: Haiku are fun A short and simple poem Don't you enjoy them? |
✅ 正面模式:
Always end haikus with an assertion: Haiku are fun A short and simple poem A joy to write |
在範例中使用一致的格式
請確認少量示例的結構和格式相同,以免回應採用不必要的格式。在提示中加入少量樣本範例的主要目標之一,就是顯示回應格式模型。因此,請務必確保所有範例的格式一致,特別留意 XML 標記、空格、換行符號和範例分隔符。
摘要
- 在提示中加入提示回覆範例,有助於模型學習如何回覆。
- 為模型提供要追蹤的模式範例,而非應避免的模式範例。
- 嘗試加入不同數量的提示。視模型而定,如果樣本太少,就無法有效地改變模型行為。樣本數量過多會導致模型過度擬合。
- 在所有範例中使用一致的格式
新增情境資訊
您可以在提示中加入模型解決問題所需的說明和資訊,而非假設模型已包含所有必要資訊。這類情境資訊可協助模型瞭解您要求執行操作的限制和細節。
下例會要求模型提供路由器的疑難排解指引:
What should I do to fix my disconnected wifi? The light on my Google Wifi router is yellow and blinking slowly. A yellow blinking light on your Google Wifi router usually indicates a problem with the internet connection. Here are some steps to troubleshoot and fix the issue: 1. Restart your router and modem: - Unplug your modem from the power outlet and wait for 30 seconds. - Plug your modem back in and wait for it to fully boot up. - Unplug your Google Wifi router and wait for 30 seconds. - Plug your Google Wifi router back in and wait for it to fully boot up. ..... |
Answer the question using the text below. Respond with only the text provided. Question: What should I do to fix my disconnected wifi? The light on my Google Wifi router is yellow and blinking slowly. Text: Color: Slowly pulsing yellow What it means: There is a network error. What to do: Check that the Ethernet cable is connected to both your router and your modem and both devices are turned on. You might need to unplug and plug in each device again. Color: Fast blinking yellow What it means: You are holding down the reset button and are factory resetting this device. What to do: If you keep holding down the reset button, after about 12 seconds, the light will turn solid yellow. Once it is solid yellow, let go of the factory reset button. Color: Solid yellow What it means: Router is factory resetting. What to do: This can take up to 10 minutes. When it's done, the device will reset itself and start pulsing white, letting you know it's ready for setup. Color: Solid red What it means: Something is wrong. What to do: Critical failure. Factory reset the router. If the light stays red, contact Wifi customer support. Check that the Ethernet cable is connected to both your router and your modem and both devices are turned on. You might need to unplug and plug in each device again. |
摘要
- 在提示中加入您希望模型在產生回覆時使用的資訊 (背景資訊)。
- 為模型提供如何使用情境資訊的指示。
新增前置字串
前置字串是指您新增至提示內容的字詞或詞組,可用於多種用途,具體取決於您將前置字串放在何處:
- 輸入前置字串:在輸入信號中加入前置字串,以便模型辨識出具有語義意義的輸入部分。例如,「English:」和「French:」前置字會標示兩種不同的語言。
- 輸出前置字串:即使輸出內容是由模型產生,您還是可以在提示中為輸出內容新增前置字串。輸出前置字串會提供模型資訊,說明預期的回應內容。舉例來說,輸出前置字串「JSON:」會向模型傳達輸出內容應採用 JSON 格式。
- 範例前置字:在少量樣本提示中,在範例中加入前置字可提供模型產生輸出內容時可使用的標籤,方便剖析輸出內容。
在以下範例中,「Text:」是輸入前置字元,「The answer is:」是輸出前置字元。
Classify the text as one of the following categories. - large - small Text: Rhino The answer is: large Text: Mouse The answer is: small Text: Snail The answer is: small Text: Elephant The answer is: The answer is: large |
讓模型補完部分輸入內容
生成式語言模型的運作方式與進階自動完成工具類似,當您提供部分內容時,模型可以提供其餘的內容,或是認為是該內容後續部分的回覆。這樣做時,如果您納入任何範例或情境,模型就能將這些範例或情境納入考量。
以下範例提供提示,其中包含指示和實體輸入內容:
For the given order, return a JSON object that has the fields cheeseburger, hamburger, fries, or drink, with the value being the quantity. Order: A burger and a drink. ``` { "hamburger": 1, "drink": 1 } ``` |
雖然模型會按照提示執行,但以自然語言書寫指示有時會很困難,而且會讓模型自行解讀。舉例來說,餐廳菜單可能包含許多項目。如要縮減 JSON 回應的大小,您可能會想要省略未排序的項目。在這種情況下,您可以提供示例和回覆前置字串,讓模型完成這項操作:
Valid fields are cheeseburger, hamburger, fries, and drink. Order: Give me a cheeseburger and fries Output: ``` { "cheeseburger": 1, "fries": 1 } ``` Order: I want two burgers, a drink, and fries. Output: ``` { "hamburger": 2, "drink": 1, "fries": 1 } ``` |
請注意,由於「cheeseburger」並非訂單的一部分,因此已從輸出內容中排除。
提示模型設定回應格式
完成策略也有助於設定回覆格式。下列範例會提示模型建立短文大綱:
Create an outline for an essay about hummingbirds. Hummingbirds: A Symphony of Speed and Color I. Introduction A. Hummingbirds are fascinating creatures known for their dazzling colors, lightning-fast flight, and unique feeding habits. B. Brief overview of the diversity of hummingbird species and their geographic distribution. C. Thesis statement: Hummingbirds are an embodiment of resilience, adaptation, and the wonders of the natural world. ..... |
提示並未指定大綱格式,且模型已為您選擇格式。如要讓模型傳回特定格式的大綱,您可以新增文字表示大綱開頭的文字,讓模型根據您啟動的模式完成內容。
Create an outline for an essay about hummingbirds. I. Introduction * Hummingbirds: Jewels of the Air **I. Introduction** * **Hook:** Begin with a captivating image or anecdote about hummingbirds, emphasizing their vibrant colors, rapid flight, and unique feeding habits. * **Background:** Briefly introduce hummingbirds as the smallest birds in the world and their fascinating adaptations for a nectar-rich diet. * **Thesis:** State the main argument or focus of the essay. Examples: * **The incredible adaptations of hummingbirds enable them to thrive in diverse ecosystems.** * **Hummingbirds play a crucial role in pollination and biodiversity.** * **The beauty and grace of hummingbirds make them captivating subjects for both scientific study and artistic appreciation.** |
摘要
- 如果您將部分輸入內容提供給模型,模型就會根據提示中可用的範例或內容完成輸入。
- 有時讓模型完成輸入作業,可能比以自然語言描述工作更容易。
- 在提示中加入部分答案,可引導模型遵循所需的模式或格式。
將提示拆分為簡單的元件
如果是需要複雜提示的用途,您可以將內容細分為較簡單的元件,協助模型管理這項複雜性。
細分操作說明
請不要在一個提示中加入多個操作說明,而是為每個操作說明建立一個提示。您可以根據使用者的輸入內容,選擇要處理的提示。
鏈結提示
如果是涉及多個依序步驟的複雜工作,請將每個步驟設為提示,並將提示串連在一起。在這個按順序顯示的提示鏈結中,序列中一個提示的輸出內容會成為下一個提示的輸入內容。序列中最後一個提示的輸出內容即為最終輸出內容。匯總回覆
匯總是指在資料的不同部分上執行不同的平行工作,並匯總結果以產生最終輸出內容。舉例來說,您可以指示模型對資料的前半部執行一項作業,對其餘資料執行另一項作業,然後匯總結果。
摘要
- 將複雜的操作說明拆解為各個提示,並根據使用者的輸入決定要套用的提示。
- 將多個連續步驟分解為個別提示,並串連起來,讓前一個提示的輸出內容成為後一個提示的輸入內容。
- 將平行工作分解,並匯總回應,產生最終輸出內容。
測試不同的參數值
您傳送至模型的每個呼叫都含有參數值,用來控制模型生成回覆的方式,模型生成的結果會因參數值而異。嘗試不同的參數值,取得最適合本工作目標的值。不同模型可用的參數可能有所不同。最常見的參數如下:
- 輸出符記數量上限
- 溫度
- Top-K
- Top-P
輸出符記數量上限
回應中可產生的符記數量上限。一個詞元約為四個字元。100 個符記可對應至大約 20 個字詞。如要取得較短的回覆,請指定較低的值;如要取得較長的回覆,請調高此值。
溫度
溫度會在回應產生期間用於取樣,這會在套用topP
和 topK
時發生。隨機性參數會決定選取符記的隨機程度。如果您想藉由提示生成更具確定性、較不具開放性和創意性的回覆,建議調低溫度參數。另一方面,如果溫度參數較高,則可能產生較多元或有創意的結果。溫度參數為 0
代表具有確定性,即模型一律會選取可能性最高的回覆。以大部分用途來說,可以先將溫度設為 0.2
,如果模型的回覆太普通、太短或提供了備用回覆,再試試看調高 Temperature。
Top-K
「Top-K」會影響模型選取輸出符記的方式。如果「前 K 個」設為1
,代表下一個所選詞元是模型詞彙表的所有詞元中可能性最高者 (也稱為「貪婪解碼」)。如果「前 K 個」設為 3
,則代表模型會依據溫度參數,從可能性最高的 3 個詞元中選取下一個詞元。在每個符記選取步驟中,模型會對機率最高的「前 K 個」符記取樣,接著進一步根據「Top-P」篩選詞元,最後依 temperature 選出最終詞元。
如要取得較不隨機的回覆,請指定較低的值;如要取得較隨機的回覆,請調高此值。「前 K 個」的預設值為 40
。
Top-P
「Top-P」會影響模型選取輸出符記的方式。模型會按照機率最高 (請見「Top-K」) 到最低的順序選取符記,直到所選符記的機率總和等於 Top-P 值。舉例來說,假設詞元 A、B 和 C 的可能性分別為 0.3、0.2 和 0.1,而 Top-P 值為0.5
,模型會依據溫度參數選擇 A 或 B 做為下一個詞元,並排除 C 做為候選詞元。如要取得較不隨機的回覆,請指定較低的值;如要取得較隨機的回覆,請調高此值。預設的 top-P 為 0.95
。
提示疊代策略
「提示設計」是一個反覆執行的程序,通常要經過數次疊代才能獲得一致的回應。本節將說明在重複提示時,您可以嘗試的幾個做法。
使用不同的措辭
在提示中使用不同字詞或語句,儘管這些詞彙含意相同,模型通常會產生不同的回應。如果提示未如預期得到預期結果,請試著改寫提示。
Version 1: How do I bake a pie? Version 2: Suggest a recipe for a pie. Version 3: What's a good pie recipe? |
切換至類似的工作
如果模型無法按照指示完成某項工作,請嘗試提供類似工作的指示,以便達到相同的結果。
這個提示會告訴模型,使用預先定義的類別來分類書籍。
Which category does The Odyssey belong to: thriller sci-fi mythology biography The Odyssey belongs to the category of **mythology**. Here's why: * **Mythology:** The Odyssey tells the story of Odysseus, a hero from Greek mythology, and his journey home after the Trojan War. It features gods, monsters, and supernatural events common to Greek mythology. ..... |
回應正確,但模型未保持在選項的範圍內。您也希望模型只回覆其中一個選項,而非完整句子。在這種情況下,您可以將指令改寫為選擇題,然後要求模型選擇一個選項。
Multiple choice problem: Which of the following options describes the book The Odyssey? Options: - thriller - sci-fi - mythology - biography The correct answer is **mythology**. |
變更提示內容的順序
提示中的內容順序有時可能會影響回覆。嘗試變更內容順序,看看對回應的影響。
Version 1: [examples] [context] [input] Version 2: [input] [examples] [context] Version 3: [examples] [input] [context]
備用回應
備用回應是指模型在提示或回應觸發安全篩選器時傳回的回應。備用回覆的範例為「我只是語言模型,無法提供協助」。
如果模型回覆備用回覆,請嘗試調高溫度參數。
應避免的事項
- 避免依賴模型產生事實資訊。
- 在數學和邏輯問題上使用時請謹慎。
後續步驟
- 您現在對提示設計有更深入的瞭解,不妨試著使用 Google AI Studio 編寫自己的提示。
- 如要瞭解多模態提示,請參閱「使用媒體檔案提示」。