小型 LLM 實用的提示工程

Maud Nalpas
Maud Nalpas

大型語言模型的效能主要取決於我們提供的操作說明。提示工程是一種構思問題的過程,目的是從 LLM 中取得最佳輸出內容。這是導入 LLM 功能的關鍵步驟。

「提示工程」是疊代的過程。如果考慮過不同 LLM,您可能會注意到必須調整提示,才能獲得更出色的成果。

不同大小的模型也是如此。

採用大型 LLM 的即時通訊介面 (例如 Gemini 或 ChatGPT) 通常能在最短的時間內,產生令人滿意的結果。不過,使用無法微調的預設較小 LLM 時,您必須調整做法。

較小型的 LLM 效果較差,資訊也較少,可供從中汲取。

「小型 LLM」是什麼意思?

定義 LLM 大小的過程很複雜,而且製造商不一定會揭露這些大小。

在本文件中,「小型 LLM」是指任何低於 300 億個參數的模型。 截至目前,只有幾百萬至幾億個參數的模型可以在消費者等級裝置上實際在瀏覽器中執行。

小型 LLM 的用途為何?

  • 例如,使用 Gemma 2B 搭配 MediaPipe 的 LLM Inference API (甚至適用於僅使用 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 而言特別實用,因為這類 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 差異

LLM 雲端 API,例如 Gemini API 或 OpenAI,通常是大型 LLM 的進入點,提供實用的提示功能。例如,Gemini 1.5 Pro 提供了系統操作說明JSON 模式

這些功能目前尚無法供自訂模型使用,也不適用於使用瀏覽器內 AI API 存取的小型 LLM,例如 MediaPipe LLM Inference APITransformers.js。雖然這不一定是技術限制,但瀏覽器內的 AI API 通常會比較精簡。

Mind 權杖限制

由於小型 LLM 的提示必須包含範例或更詳細的操作說明,因此可能需要更長的輸入符記限制 (如果有的話),而且會佔用較多的輸入符記限制。

此外,較小的模型通常越小,輸入符記限制也比較小。舉例來說,Gemini 1.5 Pro 的輸入符記限制為 100 萬,而 Gemma 模型有 8K 個背景區間

請使用權杖計數函式以免達到上限。

調整預估時間

將提示設計和測試時間納入考量。

由於 API 差異和權杖限制,在編寫小 LLM 提示時,您可能需要更多時間與心力。測試及驗證 LLM 的輸出內容可能也比較困難。

無論是提示工程師還是微調模型?

對於網頁開發人員,建議工程是利用生成式 AI 而非自訂訓練和微調的方式。但在某些情況下,進階提示工程可能還不夠,尤其是使用較小的 LLM 時。

在以下情況下使用微調功能:

  • 您需要一件特定工作才能取得最頂級的準確率和效能。微調功能則會直接調整模型的內部參數,以獲得最佳結果。
  • 您精心整理的資料與工作相關,並已加上偏好的輸出內容標籤。您可以運用這項資料有效微調模型。
  • 您重複使用模型的目的也相同。微調可以一次進行,並重複用於特定工作。