提示设计策略

本页面介绍在设计提示时可以采用的一些常规提示设计策略。虽然提示设计方法没有正确或错误一说,但您可以使用常见的策略来影响模型的回答。严格的测试和评估对于优化模型性能仍然至关重要。

大语言模型 (LLM) 使用大量文本数据进行训练,以学习语言单元之间的模式和关系。在获得一些文本(提示)后,语言模型可以预测接下的内容,就像一个复杂的自动补全工具。因此,在设计提示时,请考虑可能影响模型预测结果的不同因素。

提供清晰而具体的说明

通过向模型提供关于要做什么的指令,可以有效且高效地自定义模型行为。请确保您提供的指令清晰而具体。 指令可以很简单,比如分步说明列表;也可以很复杂,例如反映用户的体验和思维模式。

定义要执行的任务

详细描述您希望模型执行的任务。任务可以很简单,比如分步说明列表,也可以很复杂,例如反映用户的体验和思维模式。以下提示告诉模型提取一段文本的摘要:

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 utilize quantum mechanical phenomena to perform computations significantly faster
than traditional computers. By exploiting the particle-wave duality of matter at tiny scales,
quantum computers leverage specialized hardware to solve problems beyond classical physics.
Potential applications include breaking encryption, developing better weather forecasting, and more.
However, current quantum computers are still largely experimental and impractical.
  

指定任何限制条件

指定有关读取提示或生成回答的任何限制条件。您可以告诉模型要做什么和不应该做什么。在上一个示例中,摘要比原始文本短,但您可能想要更短的摘要。在这种情况下,您可以在提示中指定一个限制条件,以限制摘要长度:

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 use quantum mechanical phenomena to perform calculations much faster than
traditional computers. They have the potential to revolutionize various fields, but current quantum
computers are still experimental and impractical.
    

定义回答格式

您还可以提供指定回答格式的说明。例如,您可以要求将回答格式设置为表格、项目符号列表、电梯间推销、关键字、句子或段落。以下提示指示模型使用项目符号列出文本中的关键信息:

Summarize this text as bullets points of key information.
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 quantum mechanical phenomena for exponentially faster calculations than
  traditional computers.
- Quantum devices utilize specialized hardware to exploit tiny-scale matter behavior as both
  particles and waves.
- Their operating principles transcend classical physics.
- Potential applications include cybersecurity, weather forecasting, and more.
- Current quantum computers are largely experimental and impractical.
  

摘要

  • 向模型提供有关要做什么的指令。
  • 说明应清晰而具体。
  • 指定输出的任何限制条件或格式要求。

添加少样本示例

您可以在提示中添加示例,以向模型示范想要的结果。模型会尝试识别示例中的模式和关系,并在生成回答时应用它们。包含少量示例的提示称为少样本提示,而不提供示例的提示称为零样本提示。少样本提示通常用于规定模型回答的格式、措辞、范围界定或一般模式。使用具体而多样的示例来帮助模型缩小其重点并生成更准确的结果。

我们建议始终在提示中包含少样本示例。不含少样本示例的提示可能效果不大,因为它们向模型展示了如何应用指令。事实上,如果您的示例足够清晰地显示当前任务,则您可以从提示中移除指令。

零样本提示与少样本提示

以下零样本提示要求模型选择最佳解释。

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
  

如果您的用例要求模型生成简洁的回答,您可以在提示中添加偏好简洁回答的示例。

以下提示提供了两个偏好较短解释的示例。在回答中,您可以看到这些示例引导模型选择较短的解释(解释 2),而不是像之前那样较长的解释(解释 1)。

Please choose the best explanation to the question:

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

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:
  
Explanation2
  

找到最佳示例数量

您可以改变提示中提供的示例数量来进行实验,以获得最想要的结果。PaLM 和 Gemini 等模型通常可以通过少量示例来学习模式,但您可能需要进行多次实验,以找到可产生所需结果的示例数量。对于 BERT 等较简单的模型,您可能需要更多示例。但是,如果您添加太多示例,模型的回答可能会与示例过拟合

使用示例展示模式而不是反模式

使用示例向模型展示要遵循的模式比使用示例向模型展示要避免的反模式更有效。

反模式

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.
  
1. **Check the Cables:**
- Make sure that all cables are securely connected to your Google Wifi router and modem.
- Try replacing the cables with new ones to ensure they are not faulty.


2. **Reboot the Router:**
- Unplug your Google Wifi router and modem from the power outlet.
- Wait for at least 30 seconds.
- Plug the modem and router back in and wait for them to start up.


3. **Factory Reset the Router:**
- Resetting the router will restore it to its default settings.
- Find the small reset button on the back of the router.
- Press and hold the reset button with a paper clip or similar object for 10 seconds.
- Release the button and wait for the router to restart.


4. **Check Your Internet Connection:**
- Use a wired connection to connect a device directly to your modem.
- If the internet works, the issue may be with your router.
- If the internet still doesn't work, contact your Internet Service Provider (ISP).


5. **Update Router's Firmware:**
- Check if there is a firmware update available for your Google Wifi router.
- Download and install the latest firmware update from the Google Wifi app.


6. **Interference and Obstacles:**
- Make sure the router is not near electronic devices that can cause interference, like microwaves or cordless phones.
- Remove any obstacles between the router and your devices, such as furniture or thick walls.


7. **Change Wi-Fi Channel:**
- In the Google Wifi app, go to Settings > Network & General > Advanced Networking.
- Try changing the Wi-Fi channel to a less congested one.


8. **Contact Google Support:**
- If you've tried the above steps and the issue persists, contact Google support for further assistance.
  

这个回答看起来是通用的问题排查信息,并非特定于路由器或 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:
  
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.
  
```
{
"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
}
```
  

可以看到,输出中排除了“芝士汉堡”,因为点餐中没有芝士汉堡。

提示模型设置其回答的格式

补全策略也有助于设置回答的格式。以下示例提示模型创建论文大纲:

Create an outline for an essay about hummingbirds.
  
I. Introduction
A. Hook: Begin with an intriguing fact or description of hummingbirds.
B. Provide some background information on hummingbirds.
1. Mention their small size and long, thin beaks.
2. Highlight their unique ability to hover and fly backward.

II. Body
A. Anatomy and Physiology
1. Elaborate on their tiny size, making comparisons to other animals.
2. Describe their wings and how they enable hovering and swift flight.
3. Discuss their metabolism and energy requirements, emphasizing their caloric needs.

B. Feeding Habits and Diet
1. Explain their primary food source, nectar, and how they extract it from flowers.
2. Describe their ability to feed from a wide variety of flowers.
3. Mention their consumption of insects and the nutritional value they provide.

C. Migration Patterns
1. Discuss the incredible long-distance migrations of some hummingbird species.
2. Explain the phenomenon of seasonal migration and the environmental factors driving it.
3. Describe the routes taken by different species during migration.

D. Conservation and Threats
1. Highlight the importance of hummingbirds in ecosystems as pollinators.
2. Discuss the challenges and threats they face, such as habitat loss, nectar scarcity, and climate change.
3. Mention conservation efforts and ways to support hummingbird populations.

III. Conclusion
A. Summarize the main points covered in the essay.
B. Reinforce the importance of hummingbirds and their ecological role.
C. Provide a final thought or call to action to protect these fascinating creatures.
  

提示没有指定大纲的格式,模型为您选择了格式。如需使模型以特定格式返回大纲,您可以添加表示大纲开头的文本,并让模型根据您起头的模式补全大纲。

Create an outline for an essay about hummingbirds.
I. Introduction
   *
  
I. Introduction
* Capture the reader's attention with an interesting anecdote or fact about hummingbirds.
* Provide a brief background on hummingbirds, including their unique characteristics.
* State the main points that will be covered in the essay.

II. The Unique Characteristics of Hummingbirds
* Discuss the hummingbird's exceptionally rapid wing-beat, allowing them to hover and fly backward.
* Highlight their tiny size, making them the smallest birds in the world.
* Describe their long, slender beaks and tongues, allowing them to reach deep into flowers to extract nectar.
* Explain their iridescent plumage, which creates brilliant and vibrant colors.

III. Hummingbird Migration and Distribution
* Discuss the extensive migration patterns of hummingbirds, covering vast distances across continents.
* Describe the different habitats where hummingbirds can be found, including tropical rainforests, deserts, and urban areas.
* Explain the seasonal changes that influence hummingbird migration and habitat selection.

IV. Hummingbird Diet and Feeding Habits
* Focus on the hummingbird's primary food source, which is nectar from flowers.
* Describe their ability to consume insects, spiders, and other small invertebrates for protein.
* Explain how hummingbirds use their long, slender tongues to extract nectar from flowers.
* Discuss the importance of flowering plants in providing sustenance for hummingbirds.

V. Reproduction and Nesting Behavior
* Describe the mating rituals and courtship behavior of hummingbirds.
* Explain the process of nest-building, including the materials used and the unique characteristics of hummingbird nests.
* Discuss the role of the female hummingbird in laying and incubating eggs.
* Describe the development of hummingbird chicks and the role of both parents in providing care.

VI. Threats to Hummingbirds and Conservation Efforts
* Highlight habitat loss, climate change, and deforestation as significant threats to hummingbird populations.
* Describe the role of habitat conservation initiatives in protecting hummingbird habitats.
* Explain the importance of creating hummingbird-friendly gardens and landscapes.
* Discuss the efforts of organizations and individuals working to conserve hummingbird populations.

VII. Conclusion
* Summarize the main points covered in the essay, emphasizing the unique characteristics, migration patterns, diet, reproduction, and conservation of hummingbirds.
* Provide a closing statement that leaves a lasting impression on the reader and reinforces the significance of these fascinating creatures.
  

摘要

  • 如果您为模型提供部分输入,模型会根据提示中的任何可用示例或上下文补全输入。
  • 有时,让模型补全输入可能比用自然语言描述任务更容易。
  • 在提示中添加部分答案,可引导模型遵循所需的模式或格式。

将提示细分为简单的组成部分

对于需要复杂提示的用例,您可以将内容细分为更简单的组成部分,以帮助模型管理这种复杂情况。

细分指令

不要在一个提示中包含多条指令,而要为每条指令创建一个提示。您可以根据用户的输入选择要处理的提示。

串联提示

对于涉及多个连续步骤的复杂任务,请将每个步骤设为提示,并将提示按顺序串联在一起。在这一连串的提示中,序列中一个提示的输出将成为下一个提示的输入。序列中的最后一个提示的输出是最终输出。

汇总回答

汇总是指对数据的不同部分执行不同的并行任务,并汇总结果以生成最终输出。例如,您可以指示模型对数据的第一部分执行一项操作,对其余数据执行另一项操作并汇总结果。

摘要

  • 将复杂的指令细分为每条指令的提示,并根据用户的输入决定要应用哪个提示。
  • 将多个连续步骤细分为单独的提示,并将它们串联在一起,使前一个提示上的输出成为下一个提示的输入。
  • 细分并行任务并汇总回答以生成最终输出。

使用不同的参数值进行实验

您向模型发送的每次调用都包含控制模型如何生成回答的参数值。对于不同的参数值,模型会生成不同的结果。因此请尝试不同的参数值,以获得任务的最佳值。不同模型的可用参数可能有所不同。最常见的参数如下:

  • 输出词元数上限
  • 温度
  • Top-K
  • Top-P

输出词元数上限

回答中可生成的词元数量上限。词元约为 4 个字符。100 个词元对应大约 20 个单词。

指定较低的值可获得较短的响应,指定较高的值可获得较长的响应。

温度

温度在生成响应期间用于采样,在应用 topPtopK 时会生成响应。温度可以控制词元选择的随机程度。较低的温度有利于需要更具确定性、更少开放性或创造性响应的提示,而较高的温度可以导致更具多样性或创造性的结果。温度为 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 answer: mythology

Explanations:

The Odyssey is an epic poem in ancient Greek literature. It is one of two major ancient Greek epic
poems attributed to Homer. The Odyssey is an epic poem that tells the story of Odysseus, a Greek
hero who fought in the Trojan War and then spent ten years trying to return home to Ithaca. The
Odyssey is a work of mythology.
  

回答正确,但模型超出了选项的范围。您还希望模型仅使用其中一个选项(而不是完整的句子)来回答。在这种情况下,您可以将指令改为单选题,并要求模型选择一个选项。

Multiple choice problem: Which of the following options describes the book The Odyssey?
Options:
- thriller
- sci-fi
- mythology
- biography
  
The answer is: mythology
  

更改提示内容的顺序

提示中内容的顺序有时会影响回答。请尝试更改内容顺序,看看对回答有何影响。

Version 1:
[examples]
[context]
[input]

Version 2:
[input]
[examples]
[context]

Version 3:
[examples]
[input]
[context]

后备响应

后备回答是当提示或回答触发安全过滤条件时模型返回的回答。后备回答的一个示例是“我无法提供帮助,因为我只是一个语言模型”。

如果模型给出后备回答,请尝试提高温度。

需要避免的事项

  • 避免依赖模型来生成事实信息。
  • 对于数学和逻辑问题请谨慎使用。

后续步骤

  • 现在您对提示设计有了更深入的了解,接下来可以试着编写 使用 Google AI Studio 构建自己的提示。
  • 如需了解多模态提示,请参阅 提示媒体文件