生成模型简介

生成式人工智能 (AI) 模型(例如 Gemini 模型系列)能够根据不同类型的数据输入(包括文本、图片和音频)创建内容。从最基本的层面来讲,这些模型的运作方式与复杂的自动补全应用类似。给定输入文本(“您可以引一匹马去水”),生成模型可以根据从训练数据中学到的模式,预测在统计学上可能会紧跟的输出文本(“但不能让它喝水”)。您可以将生成模型的这一基本属性用于各种应用:

  • 生成创造性写作,例如诗歌、短篇故事、隐喻和博文
  • 将结构化数据转换为自由格式文本
  • 从自由格式文本中提取和汇总信息
  • 生成代码
  • 互译语言

使用生成模型构建这些功能并不需要任何机器学习 (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 不是固定字符串,而是最终用户提供的变量:

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:

您可能已经注意到,这些少样本提示的确切模式略有不同。除了包含示例之外,在提示中提供说明是您在编写自己的提示时可以考虑的另一种策略,因为它有助于将您的意图传达给模型。

提示与传统软件开发

与按照精心编写的规范设计的传统软件不同,生成模型的行为对于模型训练器而言在很大程度上是不透明的。因此,您通常无法提前预测哪些类型的提示结构最适合特定模型。此外,生成模型的行为在很大程度上取决于其训练数据,由于模型会不断在新数据集上进行调整,因此有时模型会发生变化,导致它无意间改变了哪个提示结构效果最佳。这对您来说意味着什么?进行实验!请尝试其他提示格式。

模型参数

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

  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,则模型将根据温度选择 A 或 B 作为下一个词元,并将 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"}

深入了解生成模型

本部分旨在解答以下问题:生成模型的回答是随机性,还是确定性?

简单来说,“两者都适用”。在您提示生成模型时,系统会分两个阶段生成文本回复。在第一阶段,生成模型会处理输入提示,并针对后续可能出现的词元(字词)生成概率分布。例如,如果您使用输入文本“The dog jumped over the ...”进行提示,生成模型将生成一组可能的后续字词:

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

此过程是确定的;每次输入相同的提示文本时,生成模型都会生成相同的分布。

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

深入阅读

  • 现在您对提示和生成模型有了更深入的了解,请尝试使用 Google AI Studio 编写自己的提示。
  • 如需详细了解创建提示的最佳实践,请参阅提示指南