小型 LLM 實用的提示工程

大型語言模型的效用很大程度取決於我們提供的指令。提示工程是指設計問題的過程,目的是讓大型語言模型產生最佳輸出內容。這是實作 LLM 基礎功能的重要步驟。

提示工程是疊代程序。如果您嘗試過不同的 LLM,可能會發現需要調整提示詞,才能獲得更好的結果。

不同大小的模型也適用這項原則。

採用大型 LLM 技術的聊天介面 (例如 Gemini 或 ChatGPT) 通常只要簡單提示,就能產生令人滿意的結果。不過,使用未經過微調的預設小型 LLM 時,您需要調整做法。

較小的 LLM 功能較弱,可從中擷取的資訊也較少。

「較小的 LLM」是什麼意思?

定義大型語言模型大小很複雜,而且製造商不一定會揭露。

在本文件中,「較小的 LLM」是指任何參數少於 300 億的模型。時至今日,參數量從數百萬到數十億的模型,都能在瀏覽器和消費級裝置上實際執行。

較小的 LLM 用於哪些用途?

  • 裝置端/瀏覽器內生成式 AI,例如搭配 MediaPipe 的 LLM 推論 API 使用 Gemma 2B (甚至適用於僅有 CPU 的裝置),或在瀏覽器中搭配 Transformers.js 使用 DistilBert。 只有這些較小的 LLM 才能在使用者裝置上進行模型下載和推論,以確保網路下載量合理,並符合裝置的記憶體和 GPU/CPU 限制。
  • 自訂伺服器端生成式 AI:您可以自行在伺服器上執行 Gemma 2B、Gemma 7B 或 Gemma 27B 等小型開放權重模型 (也可以選擇微調)。

開始使用

由 Gemini 或 ChatGPT 等大型語言模型支援的聊天介面,通常只要簡單提示,就能產生令人滿意的結果。不過,使用較小的 LLM 時,您需要調整做法。較小的 LLM 功能較弱,可從中擷取的資訊也較少。

提供背景資訊和精確的格式指示

如要透過小型 LLM 獲得最佳結果,請撰寫更詳細具體的提示。

例如:

Based on a user review, provide a product rating as an integer between 1 and 5. \n
Only output the integer.

Review: "${review}"
Rating:
輸入 (審查) 輸出內容 (評分)
  大型 LLM (Gemini 1.5) 較小的 LLM (Gemma 2B)
我非常喜歡這款錶帶的貼合度!可平均分散重量,即使整天健行也能保持舒適。會推薦。 5 4 顆星 (滿分 5 顆星)**
背帶很薄,負重時會開始壓迫肩膀。 1 2/5'

雖然 Gemini 1.5 可透過這個簡單的提示提供所需輸出內容,但 Gemma 的輸出內容並不令人滿意:

  • 格式有誤,我們要求評等為整數。
  • 評分似乎不太準確。第一則評論的熱情程度足以判斷為 5 星評論。

如要修正這個問題,我們需要使用提示工程技術,包括單樣本、少量樣本和多樣本提示,以及思維鏈提示。我們也必須提供明確的格式指示,並堅持模型應使用完整範圍的評分。

例如:

`Analyze a product review, and then based on your analysis give me the
corresponding rating (integer). The rating should be an integer between 1 and
5. 1 is the worst rating, and 5 is the best rating. A strongly dissatisfied
review that only mentions issues should have a rating of 1 (worst). A strongly
satisfied review that only mentions positives and upsides should have a rating
of 5 (best). Be opinionated. Use the full range of possible ratings (1 to
5). \n\n
    \n\n
    Here are some examples of reviews and their corresponding analyses and
    ratings:
    \n\n
    Review: 'Stylish and functional. Not sure how it'll handle rugged outdoor
    use, but it's perfect for urban exploring.'
    Analysis: The reviewer appreciates the product's style and basic
    functionality. They express some uncertainty about its ruggedness but
    overall find it suitable for their intended use, resulting in a positive,
    but not top-tier rating.
    Rating (integer): 4
    \n\n
    Review: 'It's a solid backpack at a decent price. Does the job, but nothing
    particularly amazing about it.'
    Analysis: This reflects an average opinion. The backpack is functional and
    fulfills its essential purpose. However, the reviewer finds it unremarkable
    and lacking any standout features deserving of higher praise.
    Rating (integer): 3
    \n\n
    Review: 'The waist belt broke on my first trip! Customer service was unresponsive too. Would not recommend.'
    Analysis: A serious product defect and poor customer service experience naturally warrants the lowest possible rating. The reviewer is extremely unsatisfied with both the product and the company.
    Rating (integer): 1
    \n\n
    Review: 'Love how many pockets and compartments it has. Keeps everything organized on long trips. Durable too!'
    Analysis: The enthusiastic review highlights specific features the user loves (organization and durability), indicating great satisfaction with the product. This justifies the highest rating.
    Rating (integer): 5
    \n\n
    Review: 'The straps are a bit flimsy, and they started digging into my shoulders under heavy loads.'
    Analysis: While not a totally negative review, a significant comfort issue leads the reviewer to rate the product poorly. The straps are a key component of a backpack, and their failure to perform well under load is a major flaw.
    Rating (integer): 1
    \n\n
    Now, here is the review you need to assess:
    \n
    Review: "${review}" \n`;
輸入 (審查) 使用較小的 LLM (Gemma 2B) 輸出

我非常喜歡這款錶帶的貼合度!重量分布均勻,即使整天健行,也能保持舒適。會推薦。

分析:這則評論非常正面,強調產品的合身度和舒適度,顯示消費者對產品非常滿意。因此獲得高評分。

評分 (整數):5

背帶很薄,負重時會開始壓迫我的肩膀。

**分析:** 評論指出錶帶有重大舒適度問題。這也表示產品可能不適合裝載重物或長時間外出。

**評分:** 1 分 (最差)

我們採用更詳細的提示,並結合少量樣本提示和思維鏈提示技術,提供更準確的評分。

預期會手動剖析輸出內容

雖然您應一律手動清理及解讀 LLM 的輸出內容,但對於較小的 LLM 而言,這點尤其重要,因為這類模型可能會產生較不精確的結果,且可能需要思維鏈提示。

在第一個範例中,我們使用了思維鏈提示,因此輸出內容包含分析和評分,我們需要手動剖析評分。此外,請注意上一節輸出內容的格式不一致:模型有時會輸出 Markdown,但並非每次都這樣。

// Use try/catch to catch (parsing) errors
try {
  // Parse the output for a rating
  const int = /[1-5]/;
  const ratingAsString = llmOutput.match(int)[0];
  // Parse as int
  const ratingAsInt = parseInt(ratingAsString);
  // `checkRating` checks the rating is a number between 1 and 5, since the
  // regEx may catch a number like "100"
  const finalRating = checkRating(ratingAsInt);
} catch (e) {
  console.error('Error', e);
}

Mind API 差異

Gemini API 或 OpenAI 等 LLM 雲端 API 通常是較大型 LLM 的進入點,提供實用的提示功能。舉例來說,Gemini 1.5 Pro 提供系統指令JSON 模式

目前,這些功能不一定適用於自訂模型,或透過瀏覽器內建 AI API 存取較小的 LLM,例如 MediaPipe LLM 推論 APITransformers.js。雖然這不一定是技術限制,但瀏覽器內建的 AI API 往往較為精簡。

請注意詞元限制

因為較小的 LLM 需要範例或更詳細的指令,提示可能會較長,並佔用更多輸入權杖限制 (如有)。

此外,較小的模型通常輸入符記上限也較小。舉例來說,Gemini 1.5 Pro 的輸入詞元上限為 100 萬個,而 Gemma 模型則有8K 的脈絡窗口

使用權杖計數函式,避免達到上限。

調整時間預估

在工程時間預估中,將提示設計和測試納入考量。

由於 API 差異和權杖限制,相較於較大的 LLM,您可能需要花費更多時間和精力,為較小的 LLM 製作提示。測試及驗證 LLM 輸出內容的難度也可能較高。

提示工程師與微調?

對網頁開發人員來說,提示工程是我們偏好的生成式 AI 運用方式,優於自訂訓練和微調。但即使是進階的提示工程,在某些情況下可能仍不夠用,特別是使用較小的 LLM 時。

在下列情況下使用微調:

  • 您需要特定工作達到頂尖的準確度和效能。微調功能會直接調整模型的內部參數,以獲得最佳結果。
  • 您已準備好與任務相關的精選資料,並標示偏好的輸出內容。您需要這項資料才能有效微調模型。
  • 您重複使用模型,但用途相同。微調作業只需執行一次,即可重複用於特定工作。