API GET API GET学习资料 · WorkBuddy 教程库

AI 学习资料 · 提示设计策略

提示词设计的基本策略

Google(Gemini API 文档;页脚署名与许可声明出自 Google)CC BY 4.0(正文);Apache-2.0(代码与提示示例)出处与许可 约 28 分钟

编者注:本文改编自 Google 的 Gemini API 文档《提示设计策略》,分三章,本章讲通用方法。示例中的提示词和模型回答按原文保留英文;标有“gemini-2.5-flash”的回答是原文用该模型生成的,“.....”是原文自带的截断。Gemini 专有的参数和功能,本站在相应位置加了编者注。

提示词设计是指编写提示词(即用自然语言提出的请求)的过程,目的是让语言模型给出准确、优质的回答。

本文介绍一些基本概念、策略和最佳实践,帮助您着手设计提示词,充分发挥 Gemini AI 模型的能力。

注意:提示词工程需要反复迭代。这些准则和模板只是起点,请根据您的具体使用场景和观察到的模型回答,不断试验和改进。

针对特定主题的提示词指南

想找更具体的提示词策略?Gemini API 文档还有以下主题的提示词指南:

提示词库里还有更多示例提示词,以互动方式演示本指南提到的许多概念。

清晰具体的指令

向模型提供清晰、具体的指令,是定制模型行为的一种有效而高效的方法。指令可以是一个问题、一串分步任务,也可以复杂到梳理出用户的体验和思维方式。

输入

输入是提示词中必不可少的文本,也就是您希望模型回应的内容。输入可以是模型要回答的问题(问题输入)、模型要执行的任务(任务输入)、模型要处理的实体(实体输入),也可以是让模型补全或续写的部分内容(补全输入)。

输入类型:问题

提示词:

示例
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.

回答(gemini-2.5-flash):

示例
{
  "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:

回答(gemini-2.5-flash):

示例
```
{
  "hamburger": 2,
  "drink": 1,
  "fries": 1
}
```

可以看到,输出中没有“cheeseburger”(芝士汉堡),因为订单里没有点它。

虽然可以在提示词中指定简单 JSON 回答对象的格式,但如果要为回答指定更复杂的 JSON Schema,建议使用 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.

回答(gemini-2.5-flash):

示例
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?

回答(gemini-2.5-flash):

示例
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.

回答(gemini-2.5-flash):

示例
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
   *

回答(gemini-2.5-flash):

示例
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.
.....

零样本与少样本提示词

您可以在提示词中加入示例,向模型展示怎样才算做对。模型会尝试从示例中找出模式和关系,并在生成回答时套用。包含少量示例的提示词称为少样本(few-shot)提示词,不提供示例的则称为零样本(zero-shot)提示词。少样本提示词常用来规范模型回答的格式、措辞、范围或总体模式。使用具体而多样的示例,可以帮助模型聚焦,生成更准确的结果。

建议始终在提示词中加入少样本示例,不带示例的提示词往往效果较差。事实上,如果示例已经足够清楚地展示了要做的任务,您甚至可以把指令从提示词中去掉。

下面的零样本提示词要求模型选出最佳解释。

提示词:

示例
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:

回答(gemini-2.5-flash):

示例
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:

回答(gemini-2.5-flash):

示例
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.

回答(gemini-2.5-flash):

示例
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.

回答(gemini-2.5-flash):

示例
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.

把提示词拆分成几个部分

对于需要复杂提示词的用例,可以把内容拆成更简单的几个部分,帮助模型应对这种复杂性。

  1. 拆分指令:不要把多条指令塞进一个提示词,而是每条指令写一个提示词。可以根据用户的输入选择处理哪个提示词。
  2. 串联提示词:对于包含多个先后步骤的复杂任务,把每一步写成一个提示词,再按顺序串起来。在这串提示词中,前一个提示词的输出就是下一个提示词的输入,最后一个提示词的输出就是最终结果。
  3. 汇总回答:汇总是指对数据的不同部分并行执行不同的任务,再把结果汇总成最终输出。例如,可以让模型对第一部分数据执行一种操作,对其余数据执行另一种操作,然后汇总结果。

试验模型参数

您发给模型的每次调用都带有一些参数值,用来控制模型如何生成回答。参数值不同,模型生成的结果也会不同。请试验不同的参数值,找出最适合任务的取值。不同模型可用的参数可能不同。最常见的参数如下:

编者注:本节的参数名称(如 topK、topP、stop_sequences)、默认值(如 topP 默认 0.95)以及下面关于温度的注意事项,都是针对 Gemini API 写的。其他模型或平台的参数名称、取值范围和默认值可能不同,有的客户端界面也不开放这些参数,请以所用平台的文档为准。

  1. 最大输出 token 数:指定回答中最多可以生成多少个 token。一个 token 大约相当于 4 个字符,100 个 token 约合 60–80 个单词。

  2. 温度:温度控制选择 token 时的随机程度。温度用于生成回答时的采样,采样发生在应用 topP 和 topK 的环节。提示词要求回答更确定、开放性更低时,适合用较低的温度;较高的温度则能带来更多样或更有创意的结果。温度为 0 时结果是确定的,也就是总选择概率最高的回答。

    注意:temperature、top_p 和 top_k 参数控制的是较早模型生成回答的方式。Gemini 4 和 Gemini 3.6 及以上的模型会忽略 temperature、top_p 和 top_k 的自定义值,并返回弃用警告标头(在今后的版本中会返回 400 错误)。请从请求配置中删去 temperature、top_p 和 top_k。对于较早的 Gemini 3 模型,请让这些参数保持默认值,以免推理性能下降。

  3. topK:topK 参数会改变模型选择输出 token 的方式。topK 为 1 表示选中的 token 是模型词汇表所有 token 中概率最高的那个(也称为贪心解码);topK 为 3 则表示借助温度,从概率最高的 3 个 token 中选出下一个 token。每一步选择 token 时,都会对概率最高的 topK 个 token 进行采样,再根据 topP 进一步筛选,最后用温度采样选出最终的 token。

  4. topP:topP 参数也会改变模型选择输出 token 的方式。token 按概率从高到低依次选入,直到它们的概率之和达到 topP 的值。例如,如果 token A、B、C 的概率分别为 0.3、0.2 和 0.1,topP 为 0.5,模型就会借助温度在 A 和 B 中选一个作为下一个 token,C 被排除在候选之外。topP 的默认值为 0.95。

  5. stop_sequences:设置停止序列,让模型遇到它就停止生成内容。停止序列可以是任意字符序列。尽量避免使用可能出现在生成内容里的字符序列。

提示词迭代策略

有时提示词要改几轮,才能稳定地得到想要的回答。本节介绍迭代提示词时可以尝试的一些做法:

  1. 换一种措辞:即使意思相同,提示词中的用词或措辞不同,模型的回答往往也会不同。如果提示词没能得到预期结果,试着换个说法。

    示例
    Version 1:
    How do I bake a pie?
    
    Version 2:
    Suggest a recipe for a pie.
    
    Version 3:
    What's a good pie recipe?
    
  2. 换成类似的任务:如果无法让模型遵循某个任务的指令,可以改用一个能达到同样结果的类似任务来下指令。

    下面的提示词让模型按预先给定的类别给一本书分类:

    提示词:

    示例
    Which category does The Odyssey belong to:
    thriller
    sci-fi
    mythology
    biography
    

    回答(gemini-2.5-flash):

    示例
    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
    

    回答(gemini-2.5-flash):

    示例
    The correct answer is mythology.
    
  3. 调整提示词内容的顺序:提示词中内容的顺序有时会影响回答。试着调换内容的顺序,看看回答有什么变化。

    示例
    Version 1:
    [examples]
    [context]
    [input]
    
    Version 2:
    [input]
    [examples]
    [context]
    
    Version 3:
    [examples]
    [input]
    [context]
    

后备回答

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

如果模型返回了后备回答,可以尝试提高温度。

编者注:“提高温度”是针对 Gemini 的建议;而且按上文的注意事项,Gemini 4 和 Gemini 3.6 及以上的模型已经会忽略自定义温度。在其他模型或客户端里,温度不一定能调,调了也不一定有用。

事实依据(Grounding)与代码执行

Gemini 可以借助工具,在原本可能给出错误回答的场景中避免幻觉。

借助 Google 搜索建立事实依据(Grounding with Google Search)可以把 Gemini 模型连上实时网页内容;当模型可能需要了解冷门或最新的事实时,就应启用它。

Gemini 的代码执行工具可以让模型生成并运行 Python 代码;只要模型需要做算术、计数或其他计算,就应启用它。

编者注:这两项都是 Gemini API 提供的工具。其他模型或客户端有没有联网搜索、运行代码之类的工具,以及怎样开启,要看各自的说明。

来源与许可

本文改编自 Google 创作并分享的作品《提示设计策略》(Gemini API 文档,https://ai.google.dev/gemini-api/docs/prompting-strategies?hl=zh-cn ,原文最后更新 2026-10-13 UTC,本站 2026-10-27 获取),依据知识共享署名 4.0 国际许可(CC BY 4.0,https://creativecommons.org/licenses/by/4.0/)使用;文中代码与提示示例依据 Apache License 2.0(https://www.apache.org/licenses/LICENSE-2.0)使用。原中文为 Google 的 AI 翻译,API GET 已对照英文原文校订,并做了删节、分章和格式转换(见“本库改动”)。API GET 与 Google 无关联,本文不表示 Google 认可本站。Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 4.0 Attribution License.

本章改动:2026-10-27 按 Owner 决定补回原页面开头的两段导语和一条“注意”(中文版原文照录,与英文原文意思一致);转成 Markdown,示例提示与模型回答保留英文原样放进代码块;以 Google 的 AI 中文译文为底稿,对照英文原文校订;统一“词元(token)”等术语,指 Google 自己的“我们”改为中性表述;范围:针对特定主题的提示指南、清晰具体的指令、零样本与少样本、添加上下文、拆分提示、模型参数、迭代策略、后备回答、Grounding 与代码执行;输入类型表格拆成三组“提示/生成的输出”;各例“提示/回答”表格拆成标签加代码块;选择题示例的选项恢复为提示文本中的列表;路由器与《奥德赛》两段回答去掉多余缩进;改正温度一句(“在应用 topP 和 topK 时会生成回答”→采样发生在应用 topP 和 topK 的环节)、“模型需要解释许多内容”等误译;“接地”改为“事实依据(Grounding)”;“使用补全策略”一句的页内链接改为文字说明;加编者注:开头说明;模型参数为 Gemini 专有;“提高温度”只针对 Gemini;Grounding 与代码执行是 Gemini API 工具;2026-10-27 校对:按本库术语把指 prompt 的“提示”统一为“提示词”、“词元”统一为“token”(作品名《提示设计策略》不变),标题随之改为“提示词设计的基本策略”;三组输入类型示例的“提示词/生成的输出”标签改为粗体,与其他示例一致;对照英文润色导语、“注意”、部分输入补全、限制条件、添加上下文和温度等处的句子,“使用媒体文件进行提示”链接文字改为“结合媒体文件编写提示词”

按约定只取正文从开头导语至“后续步骤”部分,删去站点导航、标志、翻译图标、反馈组件和页脚;把正文转成 Markdown,按内容分为通用策略、Gemini 3、智能体工作流三章;原页面用表格排版的“提示/回答”改成依次排列的标签和代码块,模型名标注并入“回答”标签,代码块中原有的加粗强调随之去掉;修正了一个选择题示例中被渲染成网页列表的选项,以及两段回答示例的多余缩进;以 Google 的 AI 中文译文为底稿,对照英文原文改正误译和语序错误(如温度采样一句、内部“思考”一句、“需要模型自己理解”一句、后续步骤中的 AI Studio 一句),润色措辞,统一术语(token 保留英文,不译作“词元”;指 prompt 的“提示”统一为“提示词”),把未翻译的“Agentic Workflows”标题译成中文,把指 Google 的“我们”改为中性表述,页内锚点链接改成文字说明,AI Studio 链接改为 https;在 Gemini 专有的参数、工具和行为处加了编者注。