本页面介绍在设计提示时可以采用的一些常规提示设计策略。
大型语言模型 (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. |
如果您的用例要求模型生成简洁的回答,您可以在提示中添加偏好简洁回答的示例。
以下提示提供了两个偏好较短解释的示例。在回答中,您可以看到这些示例引导模型选择较短的解释(解释 2),而不是像之前那样较长的解释(解释 1)。
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. |
摘要
- 在您希望模型在生成回答时使用的提示中添加信息(上下文)。
- 为模型提供有关如何使用上下文信息的指令。
添加前缀
前缀是您添加到提示内容中的字词或短语,有多种用途,具体取决于前缀的位置:
- 输入前缀:为输入添加前缀可向模型指示输入中在语义上有意义的部分。例如,前缀“英语:”和“法语:”区分两种不同的语言。
- 输出前缀:虽然输出由模型生成,您也可以在提示中为输出添加前缀。输出前缀可为模型提供有关预期回答的信息。例如,输出前缀“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 |
让模型补全部分输入
生成式语言模型的工作方式类似于高级自动补全工具。当您提供部分内容时,模型可以提供剩余的内容或者它认为是该内容的延续内容来作为回答。如果您添加了任何示例或上下文,模型可以参考这些示例或上下文。
以下示例提供了包含指令和实体输入的提示:
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 } ``` |
可以看到,输出中排除了“芝士汉堡”,因为点餐中没有芝士汉堡。
提示模型设置其回答的格式
补全策略也有助于设置回答的格式。以下示例提示模型创建论文大纲:
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
输出词元数上限
回答中可生成的词元数量上限。词元约为 4 个字符。100 个词元对应大约 20 个单词。指定较低的值可获得较短的响应,指定较高的值可获得较长的响应。
温度
温度在生成响应期间用于采样,在应用topP
和 topK
时会生成响应。温度可以控制词元选择的随机程度。较低的温度有利于需要更具确定性、更少开放性或创造性响应的提示,而较高的温度可以导致更具多样性或创造性的结果。温度为 0
表示确定,即始终选择概率最高的响应。
对于大多数应用场景,不妨先试着将温度设为 0.2
。如果模型返回的响应过于笼统、过于简短,或者模型给出后备响应,请尝试提高温度。
Top-K
Top-K 可更改模型选择输出词元的方式。如果 top-K 设为1
,表示所选词元是模型词汇表的所有词元中概率最高的词元(也称为贪心解码)。如果 top-K 设为 3
,则表示系统将从 3 个概率最高的词元(通过温度确定)中选择下一个词元。
在每个词元选择步骤中,系统都会对概率最高的 top-K 词元进行采样。然后,系统会根据 top-P 进一步过滤词元,并使用温度采样选择最终的词元。
指定较低的值可获得随机程度较低的响应,指定较高的值可获得随机程度较高的响应。默认的 top-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 编写自己的提示。
- 如需了解多模态提示,请参阅使用媒体文件进行提示。