關於生成式模型

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

這個模型回應看起來有點奇怪。模型不只傳回 德國的首都 (您手寫提示中的最後一個國家/地區),也包括 國家/地區和大小寫組合的完整清單。這是因為 生成式模型正「持續」如果您只是想試試 建立一個函式,指出輸入國家/地區的大寫 (「德國: 柏林」),則也許您完全不在意模型的任何文字 產生的結果。事實上,身為應用程式設計人員 截斷多餘的範例另外,您可能會想 parameterize:讓德國不是固定字串,而是變數 使用者提供的功能:

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. 「輸出權杖數量上限」:指定可包含的權杖數量上限 回應中要保留的 IP 位址一個符記約為四個字元,100 名 符記就會對應至大約 60 到 80 個字詞

  2. 隨機性參數:溫度會決定符記的隨機程度 。系統在產生回應時,會採計隨機性參數 套用 topPtopK 時就會發生這種情形。溫度較低 對於需要更確定性或較沒有開放回應的提示 隨機性參數則可能較多元或有創意的結果。A 罩杯 隨機性參數為「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 up over the ...」時,生成式模型 來產生可能的下一個字詞陣列:

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

這項程序具有確定性;生成式模型就會產生 每次輸入相同的提示文字時,系統都會執行分佈動作

在第二階段,生成式模型會將這些分佈情形 透過其中一種解碼策略達到實際的文字回應成效簡單 則可能會在每個時間步中選出最有可能出現的符記。這個 隨時都會有確定性不過,您也可以選擇 會隨機取樣 (對 模型這個過程會隨機 (隨機)。控管 設定隨機性參數A 罩杯 如果隨機性參數為 0,表示系統只會選取可能性最高的符記,而且不會 隨機性參數相反地,如果隨機性參數過高 導致模型選擇的符記出現漏洞 模型回應

延伸閱讀

  • 現在您已深入瞭解提示和生成式模型 嘗試自行撰寫提示 Google AI Studio
  • 如要瞭解相關資訊,請參閱提示指南。 進一步瞭解建立提示的最佳做法。