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

Maud Nalpas
Maud Nalpas

大型语言模型的有效性在很大程度上取决于我们给出的指令。提示工程是指以从 LLM 获得最佳输出的方式设计问题的过程。这是实现基于 LLM 的功能的关键步骤。

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

这同样适用于不同尺寸的模型。

由大型 LLM(例如 Gemini 或 ChatGPT)提供支持的聊天界面通常只需极少的提示工作即可生成令人满意的结果。但是,在使用未经微调的默认较小 LLM 时,您需要调整您的方法。

较小的 LLM 功能较少,可供选择的信息池也较小。

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

定义 LLM 尺寸很复杂,制作者不一定会披露这些尺寸。

在本文档中,“较小的 LLM”是指参数超过 300 亿个模型。 就目前而言,具有数百万到几十亿个参数的模型实际上可以在消费级设备的浏览器中运行。

在哪里使用较小的 LLM?

  • 设备端/浏览器内的生成式 AI,例如,结合使用 Gemma 2B 和 MediaPipe 的 LLM Inference API(即使适用于仅支持 CPU 的设备),或者在浏览器中使用 Transformers.js 使用 DistilBert。 只有使用这些较小的 LLM,才能在用户设备上下载模型并进行推断,以确保网页下载合理且适合设备的内存和 GPU/CPU 限制。
  • 自定义服务器端生成式 AI。Gemma 2B、Gemma 7B 或 Gemma 27B 等小型开放式模型可让您在自己的服务器上运行(并可根据需要进行微调)。

开始使用

由大型 LLM(如 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 差异

Gemini API 或 OpenAI 等 LLM 云 API 通常是大型 LLM 的入口点,可提供便捷的提示功能。例如,Gemini 1.5 Pro 提供系统说明JSON 模式

目前,这些功能并非始终可用于自定义模型,也不适用于使用浏览器内 AI API(例如 MediaPipe LLM Inference APITransformers.js)访问的较小 LLM。虽然这不一定是技术限制,但浏览器内置 AI API 往往更精简。

Mind 令牌限制

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

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

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

调整估算的时间

在估算工程时间时,请考虑及时的设计和测试。

由于 API 差异和令牌限制,您可能需要投入更多的时间和精力来为较小的 LLM 编写提示,而不是为较大的 LLM 编写提示。测试和验证 LLM 的输出可能也会花费更多精力。

提示工程师还是微调?

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

在以下情况下使用微调:

  • 您对特定任务要求出色的准确率和性能。微调可直接调整模型的内部参数,以获得最佳结果。
  • 您拥有与您的任务相关的精心挑选的数据,并且这些数据已经带有首选输出标签。您需要此数据才能进行有效微调。
  • 您反复使用该模型来达到相同的目的。一次微调可以完成一次,然后便可针对特定任务重复使用。