生成模型简介

生成式人工智能 (AI) 模型,例如 模型能够根据不同类型的数据输入创建内容,包括 文本、图片和音频这些模型最基本的运行方式 复杂的自动完成应用程序。给定输入文本(“你可以带领一匹马” 一个词元,生成模型可以预测 (“但你不能让它喝酒”), 从训练数据中学到的规律。您可以使用 适用于各种应用的生成模型:

  • 生成诗歌、短篇故事、隐喻等创意写作, 博文
  • 将结构化数据转换为自由格式文本
  • 从自由格式文本中提取和总结信息
  • 生成代码
  • 在多种语言之间互译

使用生成模型构建这些功能不需要任何机器 学习 (ML) 专业知识。您不需要收集大型数据集,也不需要训练 模型。只需描述想要实现的目标 用几句话对模型进行的操作。

仅处理文本输入和输出的生成模型也称为 大型语言模型 (LLM)。Gemini 系列模型可以处理更多数据, 虽然它们可以执行 LLM 的功能, 称为生成模型

示例应用

以下示例显示了 生成模型。

生成诗词

用户输入:Write me a four-line poem about puppies and Android phones. Make sure it rhymes.

模型输出:

Puppies are cute,
Android phones are neat,
They both bring joy,
And make life complete.

生成列表

用户输入:Generate a bulleted list of items I need to pack for a three-day camping trip.

模型输出:

*   Tent
*   Sleeping bag
*   Sleeping pad
*   Camp stove
*   Pots and pans
*   Utensils
*   Food
*   Water
*   First-aid kit
*   Sunscreen
*   Insect repellent
*   ...

你可以让生成模型产生各种有用行为, 只需编写正确的输入文本(也称为“提示”)即可实现。艺术 找出正确的措辞,让生成模型能够 称为提示设计(也称为“提示工程”或 “提示”)。

提示设计基础知识

上一部分展示了几个包含 例如“帮我写一首诗”这种说明可能适合 特定类型的任务。但对于其他应用,另一个提示 少样本提示方法可能效果更好。少样本提示 一个好处在于,大语言模型 识别和复制文本数据中的模式。具体方法是 生成文本模式。例如,假设您 您希望构建一个应用,该应用将国家/地区名称作为输入,并输出其 都市。下面的文本提示正好可以做到这一点:

Italy : Rome
France : Paris
Germany :

在此提示中,您需要建立一种模式:[country] : [capital]。如果您发送 大型语言模型时,它会自动填充模式并 返回如下内容:

     Berlin
Turkey : Ankara
Greece : Athens

此模型回答可能看起来有点奇怪。模型不仅返回了 德国的首都(手写提示中的最后一个国家),但也有 其他国家/地区和首都对的完整列表。这是因为 则是“延续这种模式”。如果您只是想 构建一个函数,用于告诉您输入国家/地区的首都(“Germany : 柏林”),那么您可能并不真正关注模型 在“Berlin”之后生成的内容。实际上,作为应用设计人员 截断这些不相关的样本。更重要的是,您可能希望 参数化输入,以便德国不是固定字符串,而是变量 由最终用户提供:

Italy : Rome
France : Paris
<user input here> :

您刚刚编写了几个用于生成国家/地区首都的提示。

只需按照这个少样本提示操作,您就可以完成大量任务 模板。这是几个样本提示,其格式略有不同, 将 Python 转换为 JavaScript:

Convert Python to JavaScript.
Python: print("hello world")
JavaScript: console.log("hello world")
Python: for x in range(0, 100):
JavaScript: for(var i = 0; i < 100; i++) {
Python: ${USER INPUT HERE}
JavaScript:

或者,取这个“反向字典”提示。在给定定义的情况下,该方法会返回 符合该定义的字词:

Given a definition, return the word it defines.
Definition: When you're happy that other people are also sad.
Word: schadenfreude
Definition: existing purely in the mind, but not in physical reality
Word: abstract
Definition: ${USER INPUT HERE}
Word:

您可能已经注意到,这些少样本提示的确切模式各不相同 除了包含示例之外, 提示是编写提示时需要考虑的另一种策略, 这有助于将您的意图传达给模型。

提示与传统软件开发

与按照精心编写的规范设计的传统软件不同, 生成模型的行为在很大程度上是不透明的,即使对于模型训练器也是如此。作为 您通常无法提前预测哪些类型的提示结构 最适合特定模型。更重要的是,生成式 AI 的行为 很大程度上取决于训练数据, 持续对新数据集进行调优,有时模型会发生变化, 无意中改变了效果最佳的提示结构。这对 你呢?进行实验!请尝试不同的提示格式。

模型参数

您发送到模型的每个提示都包含参数值,用于控制 模型生成回答。对于不同的参数值,模型会生成不同的结果。最常见的模型参数包括:

  1. 最大输出词元数:指定可以 生成的所有信息词元约为 4 个字符。100 个 大约有 60-80 个字词。

  2. 温度:温度用于控制词元的随机性 选择。在生成响应期间,温度用于采样, 在应用 topPtopK 时发生。温度较低是好现象 对于需要更具确定性或不太开放回答的提示, 而较高的温度会引发更加多样化或更具创造性的结果。答 温度为 0 表示确定性,即概率最高的 响应始终处于选中状态。

  3. topKtopK 参数可更改模型为 输出。topK 为 1 表示所选词元是以下这些词元中概率最高的 模型词汇表中的所有词元(也称为贪心解码), 而 topK 为 3 表示从 3 个词元中选择下一个词元 概率最高。对于每个词元选择步骤, 对概率最高的 topK 个词元进行采样。然后, 根据 topP 进一步过滤,并使用 温度采样。

  4. topPtopP 参数可更改模型为 输出。系统会按照概率从最高到最低的顺序选择词元,直到 它们的概率等于 topP 值。例如,如果词元 A、B、 和 C 的概率分别为 0.3、0.2 和 0.1,topP 值为 0.5, 则模型将使用 将 C 排除在候选温度之外。topP 的默认值为 0.95。

  5. stop_sequences:将停止序列设置为 告诉模型停止生成内容。停止序列可以是任意 字符序列。请尽量避免使用 可能会出现在生成内容中。

提示类型

根据其中包含的上下文信息的级别, 大致可分为三类。

零样本提示

这些提示不包含要复制的模型示例。零样本 从本质上讲,该模型 更多示例或信息。这意味着模型必须依赖于 以生成合理的答案。

下面是一些常用的零样本提示模式:

  • 说明内容
<Overall instruction>
<Content to operate on>

例如,

Summarize the following into two sentences at the third-grade level:

Hummingbirds are the smallest birds in the world, and they are also one of the
most fascinating. They are found in North and South America, and they are known
for their long, thin beaks and their ability to fly at high speeds.

Hummingbirds are made up of three main parts: the head, the body, and the tail.
The head is small and round, and it contains the eyes, the beak, and the brain.
The body is long and slender, and it contains the wings, the legs, and the
heart. The tail is long and forked, and it helps the hummingbird to balance
while it is flying.

Hummingbirds are also known for their coloration. They come in a variety of
colors, including green, blue, red, and purple. Some hummingbirds are even able
to change their color!

Hummingbirds are very active creatures. They spend most of their time flying,
and they are also very good at hovering. Hummingbirds need to eat a lot of food
in order to maintain their energy, and they often visit flowers to drink nectar.

Hummingbirds are amazing creatures. They are small, but they are also very
powerful. They are beautiful, and they are very important to the ecosystem.
  • 说明-内容-说明
<Overall instruction or context setting>
<Content to operate on>
<Final instruction>

例如,

Here is some text I'd like you to summarize:

Hummingbirds are the smallest birds in the world, and they are also one of the
most fascinating. They are found in North and South America, and they are known
for their long, thin beaks and their ability to fly at high speeds. Hummingbirds
are made up of three main parts: the head, the body, and the tail. The head is
small and round, and it contains the eyes, the beak, and the brain. The body is
long and slender, and it contains the wings, the legs, and the heart. The tail
is long and forked, and it helps the hummingbird to balance while it is flying.
Hummingbirds are also known for their coloration. They come in a variety of
colors, including green, blue, red, and purple. Some hummingbirds are even able
to change their color! Hummingbirds are very active creatures. They spend most
of their time flying, and they are also very good at hovering. Hummingbirds need
to eat a lot of food in order to maintain their energy, and they often visit
flowers to drink nectar. Hummingbirds are amazing creatures. They are small, but
they are also very powerful. They are beautiful, and they are very important to
the ecosystem.

Summarize it in two sentences at the third-grade reading level.
  • 继续。有时,您可以让模型继续输入文本, 操作说明。例如,下面是一个零样本提示,其中模型 旨在继续输入:
Once upon a time, there was a little sparrow building a nest in a farmer's
barn. This sparrow

使用零样本提示生成富有创意的文本格式,例如诗歌、代码、 脚本、音乐作品、电子邮件或信件。

单样本提示

这些提示为模型提供了一个示例,供其进行复制并继续 模式。这样就可以根据 模型。

例如,你可以生成如下食物搭配:

Food: Apple
Pairs with: Cheese
Food: Pear
Pairs with:

少样本提示

这些提示为模型提供了多个可复制的示例。使用 几个样本提示来完成复杂任务,例如基于数据合成

示例提示可以是:

Generate a grocery shopping list for a week for one person. Use the JSON format
given below.
{"item": "eggs", "quantity": "6"}
{"item": "bread", "quantity": "one loaf"}

生成式模型探秘

本部分旨在回答以下问题:生成式 AI 是否会带来随机性 模特的还是确定性的?

简而言之,两个选项都是肯定的。当您向生成模型提示时, 系统会分两个阶段生成响应。在第一阶段,生成模型 处理输入提示,并生成概率分布 下一个可能出现的词元(字词)。例如,如果你提示 如果输入文本“The dog jumped over the ...”,则生成模型会 生成一组可能的后续字词:

[("fence", 0.77), ("ledge", 0.12), ("blanket", 0.03), ...]

此过程具有确定性;就可以生成 生成相同的提示文本。

在第二阶段,生成模型将这些分布转换为 从多种解码策略中任选一种来预测实际文本响应。简单的 解码策略可能会在每个时间步选择最有可能的词元。这个 过程始终是确定的。不过,您可以改为选择 通过对模型返回的分布图进行随机采样来生成响应, 模型。此过程是随机(随机)的。你可以控制 通过设置温度来允许在此解码过程中实现随机性。答 温度为 0 表示只选择最有可能的词元, 随机性。相反,高温会注入高度随机性, 模型选择的词元,从而导致更出人意料的 模型响应。

深入阅读

  • 现在,您对提示和生成模型有了更深入的了解, 尝试使用 Google AI Studio
  • 如需了解详情,请参阅提示准则 详细了解创建提示的最佳做法