關於生成式模型

生成式人工智慧 (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
*   ...

您可以讓生成式模型產生類似這樣的各種實用行為,只要生成正確的輸入文字 (也稱為「提示」) 即可。「提示設計」 (又稱為「提示工程」或「提示設計」) 可協助生成式模型輸入正確的字詞,讓生成式模型達到您的需求。

提示設計基本概念

上一節列舉了一些包含指令的提示範例,例如「寫一首詩」。這類指示可能適用於特定類型的工作。不過,對其他應用程式來說,另一種名為「few-shot 提示」的提示可能較為適合。少量樣本提示的優點是,大型語言模型非常適合辨識及複製文字資料中的模式。其目的是將生成式模型學習的文字模式 傳送給模型例如,假設您想建構的應用程式做為輸入國家/地區名稱,並輸出其市值。以下是專為達成此目標而設計的文字提示:

Italy : Rome
France : Paris
Germany :

在這則提示中,您會建立模式:[country] : [capital]。如果您將此提示傳送至大型語言模型,它將自動完成該模式,並傳回如下的內容:

     Berlin
Turkey : Ankara
Greece : Athens

這個模型的回應可能看起來有點奇怪。這個模型不僅傳回了德國的大寫字母 (手寫提示中的最後一個國家/地區),還傳回了其他國家/地區和大寫字母組合的完整清單。這是因為生成式模型 是「持續運作模式」如果您只嘗試建構一個函式來指出輸入國家/地區的首字母 (例如「德國 : 柏林」),您可能就不在乎模型在「柏林」之後產生的任何文字。事實上,身為應用程式設計人員,您可能要刪掉這些多餘的範例。或者,您可能想將輸入參數化,這樣一來,德國不是固定字串,而是使用者提供的變數:

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. 輸出符記數量上限:指定回應中可產生的符記數量上限。一個符記約為四個字元,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 over the ... 」,生成式模型就會產生可能出現的下一個字詞陣列:

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

這個過程具有確定性;生成式模型每次輸入相同的提示文字時,都會產生相同的分佈情形。

在第二階段中,生成式模型會透過數種解碼策略,將這些分佈資料轉換成實際的文字回應。簡單的解碼策略可能會在每個時間步中選擇最有可能的符記。這項程序一律具有確定性。不過,您可以改為選擇透過隨機取樣,針對模型傳回的分佈情形產生回應。這項程序是隨機的 (隨機)。設定溫度,即可控制此解碼程序允許的隨機程度。隨機性參數為 0 代表僅選取可能性最高的符記,且沒有隨機性。相反地,高溫會將高隨機性插入模型選取的符記,導致模型回應出現更意料之外的意外情況。

其他資訊

  • 現在您已對提示和生成式模型有更深入的瞭解,不妨使用 Google AI Studio 撰寫自己的提示。
  • 如要進一步瞭解建立提示的最佳做法,請參閱提示指南