提示设计是指创建提示(即自然语言请求)的过程,这些提示将用来从语言模型获得准确优质的回答。
本页介绍了一些基本概念、策略和最佳实践,可帮助您开始设计提示,以充分利用 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. |
添加前缀
前缀是您添加到提示内容中的字词或短语,有多种用途,具体取决于前缀的位置:
- 输入前缀:为输入添加前缀可向模型指示输入中在语义上有意义的部分。例如,前缀“英语:”和“法语:”区分两种不同的语言。
- 输出前缀:虽然输出由模型生成,您也可以在提示中为输出添加前缀。输出前缀可为模型提供有关预期回答的信息。例如,输出前缀“JSON:”告诉模型输出应采用 JSON 格式。
- 示例前缀:在少样本提示中,为示例添加前缀提供了模型在生成输出时可以使用的标签,使解析输出内容变得更加容易。
在以下示例中,“文本:”是输入前缀,“答案是:”是输出前缀。
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 |
将提示细分为组成部分
对于需要复杂提示的用例,您可以将内容细分为更简单的组成部分,以帮助模型管理这种复杂情况。
拆解指令:不要在一个提示中包含多条指令,而要为每条指令创建一个提示。您可以根据用户的输入选择要处理的提示。
串联提示:对于涉及多个连续步骤的复杂任务,请将每个步骤设为一个提示,并将这些提示按顺序串联在一起。在这一连串的提示中,序列中一个提示的输出将成为下一个提示的输入。序列中最后一个提示的输出将是最终输出。
汇总响应:汇总是指对数据的不同部分执行不同的并行任务,并汇总结果以生成最终输出。例如,您可以指示模型对数据的第一部分执行一项操作,对其余数据执行另一项操作并汇总结果。
对模型参数进行实验
您向模型发送的每次调用都包含控制模型如何生成回答的参数值。对于不同的参数值,模型会生成不同的结果。因此请尝试不同的参数值,以获得任务的最佳值。不同模型的可用参数可能有所不同。最常见的参数如下:
输出词元数量上限:指定回答中可生成的词元数量上限。词元约为 4 个字符。100 个令牌对应大约 60-80 个单词。
温度:温度可以控制词元选择的随机性。温度在生成回答期间用于采样,在应用
topP
和topK
时会生成回答。较低的温度有利于需要更具确定性或更少开放性回答的提示,而较高的温度可以带来更具多样性或创造性的结果。温度为 0 表示确定性,即始终选择概率最高的回答。topK
:topK
参数可更改模型选择输出词元的方式。如果topK
设为 1,表示所选 token 是模型词汇表的所有 token 中概率最高的 token(也称为贪心解码)。如果topK
设为 3,则表示系统将从 3 个概率最高的 token 中选择下一个 token(通过温度确定)。在每个词元选择步骤中,系统都会对概率最高的topK
个词元进行采样。然后,系统会根据topP
进一步过滤词元,并使用温度采样选择最终的词元。topP
:topP
参数可更改模型选择输出词元的方式。系统会按照概率从最高到最低的顺序选择 token,直到所选 token 的概率总和等于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 表示仅选择概率最高的 token,不存在随机性。反之,高温度会为模型选择的 token 注入高度的随机性,从而导致模型给出更出乎意料、令人惊讶的回答。
后续步骤
- 现在,您对问题设计有了更深入的了解,不妨尝试使用 Google AI Studio 编写自己的问题。
- 如需了解多模态提示,请参阅使用媒体文件进行提示。
- 如需了解如何编写图片生成提示,请参阅 Imagen 提示指南
- 如需了解视频提示,请参阅 Veo 提示指南