提示設計是指建立提示或自然語言要求的程序,從語言模型中提取準確且高品質的回應。
本頁面將介紹基本概念、策略和最佳做法,協助您開始設計提示,充分發揮 Gemini AI 模型的效益。
特定主題的提示指引
想瞭解更具體的提示策略嗎?請參閱其他提示指南:
Google AI Studio 也提供提示庫,可透過互動方式展示本指南中分享的許多概念。
提供清楚明確的指令
如要有效且有效率地自訂模型行為,請提供明確且具體的指示。指示可以是問題形式、逐步作業,或是複雜的使用者體驗和心態地圖。
輸入
輸入內容是提示中要求的文字,您希望模型提供回應。輸入內容可以是模型回答的問題 (問題輸入內容)、模型執行的任務 (任務輸入內容)、模型操作的實體 (實體輸入內容),或是模型完成或繼續編寫的不完整輸入內容 (補完型輸入內容)。
輸入類型 | 提示 | 生成的輸出內容 |
---|---|---|
Question |
|
|
工作 | Give me a simple list of just the things that I must bring on a camping trip. The list should have 5 items. |
1. **Tent:** Your shelter from the elements. 2. **Sleeping Bag:** Stay warm and comfortable. 3. **Sleeping Pad:** Insulation and cushioning for a good night's sleep. 4. **Headlamp or Flashlight:** Light for nighttime activities. 5. **First Aid Kit:** Be prepared for minor injuries. |
實體 |
|
|
部分輸入內容完成
生成式語言模型的運作方式類似進階自動完成工具。當您提供部分內容,模型就能提供其餘的內容,或是認為是該內容延續的內容做為回應。在這種情況下,如果您加入任何範例或脈絡,模型就能將這些範例或脈絡納入考量。
以下範例提供提示,其中包含操作說明和實體輸入內容:
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. { "cheeseburger": 0, "hamburger": 1, "fries": 0, "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 } ``` |
請注意,由於「起司漢堡」並非訂單的一部分,因此已從輸出內容中排除。
限制
指定讀取提示或產生回覆時的任何限制。您可以告訴模型要做什麼和不做什麼。舉例來說,您可以在提示中指定限制,說明您希望摘要的長度為何:
Summarize this text in one sentence: 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.
Exploiting quantum mechanical phenomena, quantum computers can perform calculations exponentially faster than traditional computers for potential applications like cybersecurity and meteorology, although they are currently largely experimental and impractical. |
回覆格式
您可以提供指示,指定回應的格式。舉例來說,您可以要求回覆以表格、項目符號清單、電梯簡報、關鍵字、句子或段落格式呈現。以下系統指示會告訴模型,回覆時應更具對話性:
All questions should be answered comprehensively with details, unless the user requests a concise response specifically. What is a smart way to make a business that sells DVD's in 2025? Starting a business selling DVDs in 2025 presents significant challenges due to the overwhelming dominance of digital streaming and on-demand services; a "smart" approach would therefore require a highly specialized niche, a strong value proposition beyond just the disc, and likely a focus on community or collector markets rather than mainstream consumption. Here's a breakdown of what a smart DVD business in 2025 would likely entail: 1. Extreme Niche Specialization: You cannot compete with streaming libraries. Your business must focus on what digital doesn't offer easily or at all. This could include: * Out-of-Print & Rare Titles: Sourcing films or TV shows that have never been released digitally or are currently unavailable on major streaming platforms. .... |
使用完成策略格式化回覆
完成策略也可以協助您設定回應格式。以下範例會提示模型建立論文大綱:
Create an outline for an essay about hummingbirds. Here is a possible outline for an essay about hummingbirds: I. Introduction A. Hook: Start with a captivating fact or description about hummingbirds (e.g., their speed, iridescent colors, or tiny size). B. Background: Briefly introduce hummingbirds – where they are found (Americas), their reputation as unique birds. C. Thesis Statement: State the main point of the essay, which could be about the remarkable adaptations, behaviors, and ecological importance of hummingbirds. II. Appearance and Identification ..... |
提示未指定大綱的格式,因此模型會為您選擇格式。如要讓模型以特定格式傳回大綱,您可以新增代表大綱開頭的文字,讓模型根據您啟動的模式完成大綱。
Create an outline for an essay about hummingbirds. I. Introduction * Okay, here is an outline for an essay about hummingbirds, starting from the introduction you provided: I. Introduction * Hook: Start with a captivating fact or description about hummingbirds (e.g., their speed, iridescent colors, or tiny size). * Background: Briefly introduce hummingbirds – where they are found (Americas), their reputation as unique birds, and their general characteristics. * Thesis Statement: State the main point of the essay, focusing on the remarkable adaptations, behaviors, and ecological significance that make hummingbirds extraordinary. ..... |
零樣本和少量樣本提示
您可以在提示中加入範例,向模型說明正確的回覆內容。模型會嘗試從範例中找出模式和關聯,並在產生回覆時套用這些模式和關聯。包含少量範例的提示稱為「少量樣本」提示,而未提供範例的提示稱為「零樣本」提示。少量示例提示經常用於規範模型回覆的格式、措詞、範圍或一般模式。使用具體且多樣化的示例協助模型縮小範圍,生成更準確的結果。
建議您在提示中一律加入少量樣本。沒有少量樣本的提示可能成效不彰。事實上,如果範例能清楚顯示目前的任務,您可以從提示中移除說明。
下列零樣本提示要求模型選擇最佳說明。
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: Explanation1 is the better explanation because it provides more detail on the process, including how ice crystals combine and grow into snowflakes as they fall through the atmosphere. |
如果用途要求模型產生簡潔的回覆,您可以在提示中加入示例,讓模型優先提供簡潔的回覆。
以下提示提供兩個範例,顯示較短的說明較佳。在回覆中,您可以看到範例引導模型選擇較短的說明 (Explanation2
),而非像先前那樣選擇較長的說明 (Explanation1
)。
Below are some examples showing a question, explanation, and answer format: Question: Why is the 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. ..... |
回應內容看起來像是通用的疑難排解資訊,並未針對路由器或 LED 指示燈的狀態提供特定資訊。
如要為特定路由器自訂回覆內容,您可以將路由器的疑難排解指南加入提示中,做為提供回覆時的參考內容。
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 |
將提示拆分為元件
如果應用程式需要複雜的提示,您可以將內容細分為較簡單的元件,協助模型管理這項複雜性。
分解指示:請不要在一個提示中加入多個指示,而是為每個指示建立一個提示。您可以根據使用者的輸入內容,選擇要處理哪個提示。
串連提示:如果工作複雜,需要多個依序步驟,請將每個步驟設為提示,並依序串連提示。在這個按順序顯示的提示鏈結中,序列中一個提示的輸出內容會成為下一個提示的輸入內容。序列中最後一個提示的輸出內容即為最終輸出內容。
匯總回應:匯總是指在資料的不同部分執行不同的平行工作,並匯總結果以產生最終輸出內容。舉例來說,您可以指示模型對資料的前半部執行一項運算,對其餘資料執行另一項運算,然後匯總結果。
嘗試使用模型參數
您傳送至模型的每個呼叫都含有參數值,用來控制模型生成回覆的方式,參數值不同,生成的結果也可能會不同。實驗不同的參數值,取得最適合工作目標的值。不同模型可用的參數不盡相同。最常見的參數如下:
輸出符記數量上限:指定回覆中可產生的符記數量上限。一個符記約為四個字元,100 個符記大約對應 60 到 80 個字詞。
溫度:溫度參數會決定選取詞元時的隨機程度。溫度會在應用
topP
和topK
時,用於回應產生期間的取樣。如果您想藉由提示生成更具確定性或較不具開放性的回覆,建議調低溫度參數。另一方面,如果溫度參數較高,則可能產生較多元或有創意的結果。Temperature 為「0」代表具有確定性,即模型一律會選取可能性最高的回覆。topK
:topK
參數會變更模型選取輸出符記的方式。如果topK
設為 1,代表所選詞元是模型詞彙表的所有詞元中可能性最高者 (也稱為「貪婪解碼」)。如果topK
設為 3,則代表模型會依據溫度參數,從可能性最高的 3 個詞元中選取下一個詞元。在每個符記選取步驟中,模型會對機率最高的topK
符記取樣,接著進一步根據topP
篩選符記,最後依 temperature 選出最終符記。topP
:topP
參數會變更模型選取輸出符記的方式。模型會按照可能性最高到最低的順序選取符記,直到所選符記的機率總和等於topP
值。舉例來說,假設詞元 A、B 和 C 的可能性分別為 0.3、0.2 和 0.1,而topP
值為 0.5,模型會依據溫度參數選擇 A 或 B 做為下一個詞元,並排除 C 做為候選詞元。預設的topP
值為 0.95。stop_sequences
:設定停止序列,告知模型停止產生內容。停止序列可以是任何字元序列。請盡量避免使用可能出現在產生內容中的字元序列。
提示疊代策略
提示設計有時需要經過數次疊代,才能持續獲得所需回應。本節將說明在重複提示時,您可以嘗試的幾個做法:
使用不同的詞彙:在提示中使用不同的字詞或詞彙,即使意思相同,模型的回覆通常也會不同。如果提示未提供預期結果,請嘗試重新表達。
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]
備用回應
備用回應是指模型在提示或回應觸發安全篩選器時傳回的回應。備用回應的範例為「我只是語言模型,無法提供協助」。
如果模型回覆備用回應,請嘗試調高溫度參數。
應避免的事項
- 避免依賴模型產生事實資訊。
- 在數學和邏輯問題上使用時請謹慎。
生成式模型的幕後運作
本節旨在回答以下問題:生成式模型的回覆是否具有隨機性,還是確定性的?
簡單來說,答案是肯定的。當您提示生成式模型時,系統會分兩個階段產生文字回應。在第一個階段,生成式模型會處理輸入提示,並針對可能出現的後續符記 (字詞) 產生機率分布。舉例來說,如果您輸入的文字是「The dog jumped over the ...」,生成式模型會產生一組可能的後續字詞:
[("fence", 0.77), ("ledge", 0.12), ("blanket", 0.03), ...]
這個程序是確定性的,生成式模型每次輸入相同的提示文字時,都會產生相同的分配結果。
在第二階段,生成式模型會透過其中一種解碼策略,將這些分布轉換為實際的文字回應。簡單的解碼策略可能會在每個時間步中選取最可能的符記。這個程序一律會是確定性的。不過,您可以選擇在模型傳回的分布中隨機取樣,產生回應。這項程序是隨機的。您可以設定溫度,控制這個解碼程序允許的隨機程度。如果溫度參數為 0,代表系統只會選取最可能的符記,不會隨機選取。相反地,高溫度參數會在模型選取的符記中注入高度隨機性,進而產生更出乎意料的模型回覆。
後續步驟
- 您現在對提示設計有更深入的瞭解,不妨嘗試使用 Google AI Studio 編寫自己的提示。
- 如要瞭解多模態提示,請參閱「使用媒體檔案提示」。
- 如要瞭解圖像提示功能,請參閱 Imagen 提示指南
- 如要瞭解影片提示,請參閱 Veo 提示指南