提示设计策略

提示设计是指创建提示(即自然语言请求)的过程,目的是让语言模型生成准确、优质的回答。

本页介绍了基本概念、策略和最佳实践,可帮助您开始设计提示,以便充分利用 Gemini AI 模型。

针对特定主题的提示指南

想要了解更具体的提示策略?请查看我们的其他提示指南,了解以下内容:

您可以在提示库 中找到其他示例提示,这些提示旨在以互动方式展示本指南中分享的许多概念。

清晰具体的指令

通过向模型提供清晰具体的指令,可以有效且高效地自定义模型行为。指令可以采用问题的形式、分步任务的形式,也可以很复杂,例如反映用户的体验和思维模式。

输入

输入是您希望模型为其提供回答的提示中的必需文本。输入可以是模型回答的问题(问题输入)、模型执行的任务(任务输入)、模型操作的实体(实体输入),或模型完成或继续的部分输入(补全输入)。

输入类型 提示 生成的输出
问题

    What's a good name for a flower shop that specializes in selling bouquets of
    dried flowers? Create a list of 5 options with just the names.

    Here are 5 names for a flower shop specializing in dried flowers:

    1. Everlasting Blooms
    2. Dried & Delightful
    3. The Petal Preserve
    4. Whispers of Wildflowers
    5. Forever Blooms
    
任务
    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.
实体

    Classify the following items as [large, small]:
Elephant
Mouse
Snail

Here's the classification:

* **Elephant:** Large
* **Mouse:** Small
* **Snail:** Small
    

部分输入补全

生成式语言模型的工作方式类似于高级自动补全工具。当您提供部分内容时,模型可以提供剩余的内容或者它认为是该内容的延续内容来作为回答。如果您添加了任何示例或上下文,模型可以参考这些示例或上下文。

以下示例提供了包含指令和实体输入的提示:

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
}
```
  

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

虽然您可以使用提示指定简单 JSON 回答对象的格式, 我们建议您在为回答指定 更复杂的 JSON 架构时使用 Gemini API 的 结构化输出功能。

限制条件

指定有关读取提示或生成回答的任何限制条件。您可以告诉模型要做什么和不应该做什么。例如,您可以在提示中指定一个限制条件,以限制摘要长度:

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 2026?
  
Starting a business selling DVDs in 2026 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 2026 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 等模型通常可以通过少量示例来学习模式,但您可能需要进行多次实验,以找到可产生最佳结果的提示示例数量。但是,如果您添加太多示例, 模型的回答可能会与示例过拟合

采用风格一致的内容形式

确保少样本示例的结构和格式相同,以避免回答具有不需要的格式。在提示中添加少样本示例的主要目标之一是向模型展示回答格式。因此,必须确保在所有示例中使用一致的格式,尤其是注意 XML 标记、空格、换行符和示例分隔符。

添加上下文

您可以在提示中添加模型解决问题所需的指令和信息,而不是假设模型具备所有必要信息。这些上下文信息可帮助模型了解您要求其执行的操作的限制和详细信息。

以下示例要求模型提供路由器的问题排查指导:

What should I do to fix my disconnected wifi? The light on my Google
Wifi router is yellow and blinking slowly.