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

Maud Nalpas
Maud Nalpas

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

提示工程是一个迭代过程。如果您尝试过 那么你可能已经注意到,你需要对提示进行一些调整, 以取得更理想的结果。

这一点也适用于不同体型的模特。

Gemini 等大型 LLM 提供支持的聊天界面 或 ChatGPT,通常只需极少的提示工作即可产生令人满意的结果。 不过,在使用未微调的默认小型 LLM 时, 您需要调整方法。

体量较小的 LLM 功能不那么强大,可供借鉴的信息池也较小。

什么是“较小的 LLM”?

定义 LLM 大小十分复杂,而且有时也并非如此 由制作商披露

在本文档中,我们将介绍“小型 LLM”指参数低于 30B 的任何模型。 截至目前,只有几百万到几十亿个参数的模型 在消费级设备上运行在浏览器中。

小型 LLM 适用于哪些情况?

  • 设备端/浏览器内生成式 AI,例如:如果您使用的是 Gemma 2B 使用 MediaPipe 的 LLM Inference API (甚至适用于仅支持 CPU 的设备)或在浏览器中使用 DistilBert(若有) Transformers.js。 只能在用户设备上下载模型并运行推断 大型语言模型 (LLM),以确保网页下载保持合理 设备内存和 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 差异

LLM 云 API,例如 Gemini API 或 OpenAI,它们通常是 指向大型 LLM,提供便捷的提示功能。例如 Gemini 1.5 Pro 提供系统说明JSON 模式

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

Mind Token 限制

因为针对较小的 LLM 给出的提示需要包含样本或更详细 那么它可能会较长,并且会占用更多的输入词元 限制(如果有)。

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

使用词元数量函数 以免达到上限

调整您的估算时间

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

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

提示工程师还是微调?

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

在以下情况下使用微调:

  • 在完成特定任务时,您需要达到出色的准确性和性能。细发质 直接调整模型的内部参数以获得最佳结果。
  • 您拥有精心挑选、与您的任务相关的数据,且已带有 首选输出。您需要此数据才能进行有效微调。
  • 您可以反复出于相同目的使用模型。你可以 一次,并重复用于特定任务。