小型 LLM 實用的提示工程

Maud Nalpas
Maud Nalpas

大型語言模型的效果非常仰賴我們 沒有得到任何好處提示工程是 以生成問題,讓 LLM 生成最佳輸出內容。 實作 LLM 式功能是相當重要的步驟。

提示工程是疊代過程。如果您已使用 您可能會注意到需要調整提示 進一步提升成效

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

採用大型 LLM 技術的 Chat 介面,例如 Gemini 通常只要用極少量的心力,就能產生令人滿意的結果。 然而,在使用不會微調的預設小型大型語言模型時 請務必調整做法。

規模較小的 LLM 較不強大,需要的資訊量較少。

「較小的 LLM」是指什麼?

定義 LLM 大小相當複雜,而且不一定每次都會顯示 然後由開發者揭露

在這份文件中,我們介紹的是「較小的 LLM」代表任何小於 300 億個參數的模型 目前,含有數百萬到數十億個參數的模型 實際上是在消費者等級裝置上的瀏覽器中執行。

哪些國家/地區會使用較小的大型語言模型?

  • 裝置端/瀏覽器內的生成式 AI,例如使用 Gemma 2B 搭配 MediaPipe 的 LLM Inference API (即使是只適用於 CPU 的裝置) 或直接使用 Transformers.js。 只有下載模型並在使用者的裝置上進行推論 以確保網頁下載項目合理且符合需求 裝置的記憶體和 GPU/CPU 限制。
  • 自訂伺服器端生成式 AI。例如 Gemma 2B、 您可以在自己的伺服器上執行 Gemma 7B 或 Gemma 27B (並且 可視需要微調)

開始使用

採用 Gemini 或 ChatGPT 等大型 LLM 技術的 Chat 介面,通常只需要執行少量提示就能產生令人滿意的結果。然而,處理小型大型語言模型時,必須調整做法。規模較小的 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:
輸入內容 (審核) 輸出 (評分)
  大型大型語言模型 (Gemini 1.5) 小型大型語言模型 (Gemma 2B)
完全喜歡貼合度!即使在進行全天健行活動時,仍能舒適地分配體重。建議使用。 5 4 顆星 (滿分 5 顆星)**
背帶很緊張,而它們開始在負載量太大的情況下開始挖掘我的肩膀。 1 2/5'

有了這個簡單的提示,Gemini 1.5 就能生成所需輸出內容 輸出內容不符合:

  • 格式有誤。我們已要求整數來進行評分。
  • 評分似乎不準確。第一則評論的熱忱足以證明某則 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 提供便利的提示功能例如:Gemini 1.5 Pro 提供系統操作說明JSON 模式

這些功能目前尚未支援自訂模型使用方式。 適合透過瀏覽器內 AI API 存取的小型大型語言模型 MediaPipe LLM Inference APITransformers.js。 雖然這不一定是一項技術限制,但瀏覽器內 AI API 通常會 做出明智的選擇

正念符記限制

針對較小的 LLM 提示,必須包含範例或更詳細的資料 就會花上更長的時間,並佔用更多輸入符記 以及限制 (如果有的話)

此外,小型模型的輸入符記數量上限通常較低。適用對象 例如,Gemini 1.5 Pro 有 100 萬個輸入詞元的限制 Gemma 模型則有 8,000 個脈絡窗口

使用符記計數函式 以免達到上限

調整您的預估時間

將工程時間估算結果的提示設計及測試納入考量。

基於 API 差異和符記限制,您可能需要更多時間和精力 據此針對規模較小的 LLM 編寫提示測試與驗證 提高 LLM 輸出結果可能會比較費時

還是提示工程師或微調?

對網頁程式開發人員來說,提示工程是我們建議的 生成式 AI 甚至可以微調自訂訓練和微調作業。 但在某些情況下,進階提示工程可能並不足以因應某些用途 特別是在小型 LLM 的情況下

在下列情況下,請使用微調功能:

  • 您需具備頂尖的準確率和效能,才能處理特定工作。細緻 調整功能會直接調整模型的內部參數,以獲得最佳結果。
  • 並收錄了與工作相關的精心整理資料, 偏好的輸出內容並需要這些資料才能有效進行微調。
  • 您重複針對相同用途使用模型。可以微調 並重複用於特定工作