适用于小型 LLM 的实用提示工程

大语言模型的有效性在很大程度上取决于我们向其提供的指令。提示工程是指以某种方式精心设计问题,以便从 LLM 中获得最佳输出。 这是实现基于 LLM 的功能的关键一步。

提示工程是一个迭代过程。如果您尝试过不同的 LLM,可能已经注意到,您需要调整提示才能获得更好的结果。

不同尺寸的模型也是如此。

由大型 LLM(例如 Gemini 或 ChatGPT)驱动的对话界面通常只需极少的提示即可生成令人满意的结果。不过,如果使用未经微调的默认小型 LLM,您需要调整方法。

较小的 LLM 功能较弱,可从中提取信息的范围也较小。

“较小的 LLM”是什么意思?

定义 LLM 大小非常复杂,而且制造商并不总是会披露这些信息。

在本文档中,“较小的 LLM”是指任何参数少于 300 亿的模型。截至目前,参数数量从数百万到数十亿的模型可以在浏览器中或消费级设备上实际运行。

小型 LLM 的应用场景有哪些?

  • 设备端/浏览器端生成式 AI,例如,如果您将 Gemma 2B 与 MediaPipe 的 LLM 推理 API 搭配使用(甚至适用于仅支持 CPU 的设备),或者在浏览器中使用 Transformers.js 搭配 DistilBert。 只有使用这些较小的 LLM,才能在用户设备上下载模型并运行推理,这样才能使 Web 下载量保持在合理范围内,并满足设备的内存和 GPU/CPU 限制。
  • 自定义服务器端生成式 AI。您可以自行在服务器上运行 Gemma 2B、Gemma 7B 或 Gemma 27B 等小型开放式权重模型(还可以选择性地进行微调)。

开始使用

由 Gemini 或 ChatGPT 等大型 LLM 提供支持的聊天界面通常只需极少的提示即可生成令人满意的结果。不过,在使用较小的 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(例如 MediaPipe LLM Inference APITransformers.js)访问的较小 LLM。虽然这不一定是一种技术限制,但浏览器内 AI API 往往更精简。

注意 token 数量上限

由于您针对较小 LLM 的提示需要包含示例或更详细的说明,因此提示可能会更长,并占用更多输入令牌限制(如果有)。

此外,较小的模型往往具有较低的输入 token 数量上限。例如,Gemini 1.5 Pro 的输入 token 数量上限为 100 万,而 Gemma 模型的上下文窗口大小为 8K

使用令牌计数函数可避免达到此限制。

调整时间估算值

在工程时间估算中考虑提示设计和测试。

由于 API 差异和 token 限制,与大型 LLM 相比,您可能需要花费更多时间和精力来为小型 LLM 精心设计提示。测试和验证 LLM 的输出可能也需要付出更多精力。

提示工程与微调?

对于 Web 开发者,我们更倾向于使用提示工程来利用生成式 AI,而不是自定义训练和微调。但在某些使用场景中,即使是高级提示工程也可能不够,尤其是在使用较小的 LLM 时。

在以下情况下使用微调:

  • 您需要为特定任务提供出色的准确性和性能。微调可直接调整模型的内部参数,以获得最佳结果。
  • 您拥有与任务相关且已标记首选输出的精心整理的数据。您需要这些数据才能进行有效的微调。
  • 您会出于同一目的反复使用该模型。微调可以进行一次,然后重复用于特定任务。