生成式 AI 的提示工程

提示工程是指提出正确的问题以获得 LLM 给出的最佳输出。它支持使用 LLM 直接与 LLM 交互, 只使用简单的语言提示。

过去,使用机器学习模型通常需要深度 具备数据集、统计学和建模技术方面的知识。如今,LLM 可以 “已编程”英语版本, 其他语言

优秀的提示工程师并不要求具备编码经验。创造力和 不过,坚持在你的旅程中大有裨益。继续阅读 来学习一些实用的提示技巧

提示最佳实践

  1. 清楚地传达什么内容或信息最为重要。

  2. 设计提示的结构:首先定义其角色,提供上下文/输入数据, 然后提供指令。

  3. 使用具体多样的样本来帮助模型缩小关注范围并生成 获得更准确的结果。

  4. 使用限制条件来限制模型输出的范围。这有助于避免 迷失方向,转而离开说明,变成与事实不符的情形。

  5. 将复杂的任务分解成一系列更简单的提示。

  6. 在生成之前指示模型评估或检查自己的回答 。(“请务必将回答限制在 3 句话以内”,“请为您在 范围为 1-10 的简洁性”“您认为这是正确的吗?”)。

而最重要的可能是:

发挥创意!广告越有创意 思想开诚布公,结果就会越好。LLM 和提示 工程仍处于起步阶段,且每天都在不断发展。

提示类型

直接提示(零样本)

直接提示(也称为零样本)是最简单的提示类型。它 只向模型提供指令,而不是向模型提供任何示例。你还可以使用 或者给模型分配一个“角色”,就像在第二张幻灯片中提到的那样, 示例。

提供:

  1. 指令
  2. 一些背景信息

创意发掘:

Prompt: Can you give me a list of ideas for blog posts for tourists visiting
New York City for the first time?

角色提示:

Prompt: You are a mighty and powerful prompt-generating robot. You need to
understand my goals and objectives and then design a prompt. The prompt should
include all the relevant information context and data that was provided to you.
You must continue asking questions until you are confident that you can produce
the best prompt for the best outcome. Your final prompt must be optimized for
chat interactions. Start by asking me to describe my goal, then continue with
follow-up questions to design the best prompt.

数据整理:

Prompt: Create a four-column spreadsheet of 10 highly-rated science fiction
movies, year of release, average audience rating, and top 3 keywords from
audience reviews.

Make sure to cite the source of the audience rating.

使用样本提示(单样本、少样本和多样本)

单样本提示会向模型展示一个清晰的描述性示例,

使用一个示例生成想法:

Prompt:

Come up with a list of ideas for blog posts for tourists visiting
New York City for the first time.

1. Fuggedaboutit! Where to Stay in New York City On Your First Visit

少样本和多样本提示可以向模型提供更多所需示例 。对于较复杂的任务来说,这种方法比零样本好, 或者需要以 难以描述的具体方式

少样本情感分类:

Prompt:

Great product, 10/10: Positive
Didn't work very well: Negative
Super helpful, worth it: Positive
It doesn't work!:

运行此提示时,模型的回答将是分类“它没有 单位正值或负值,如示例中所示。

多样本表情符号响应预测器:

Prompt: Predict up to 5 emojis as a response to a text chat message. The output
should only include emojis.

input: The new visual design is blowing my mind 🤯
output: ➕,💘, ❤‍🔥

input: Well that looks great regardless
output: ❤️,🪄

input: Unfortunately this won't work
output: 💔,😔

input: sounds good, I'll look into that
output: 🙏,👍

input: 10hr cut of jeff goldblum laughing URL
output: 😂,💀,⚰️

input: Woo! Launch time!

这里的过程与此相同,但由于提示更加复杂,因此模型一直 给出了更多可模拟的示例。

思维链提示

思维链 (CoT) 提示会鼓励 LLM 解释其推理过程。 将其与少样本提示相结合,在处理更复杂的任务时获得更好的结果 在做出回答之前需要推理的用例。

Prompt:

The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:

零样本 CoT

回顾前面的零样本提示,这种方法采用零样本提示 然后添加一条指令:“我们来逐步进行思考。”LLM 能够 根据此指令生成思维链,通常能生成更准确的 答案。这是让 LLM 生成正确的 文字题等问题的解答。

Prompt:

I went to the market and bought 10 apples. I gave 2 apples to the neighbor and
2 to the repairman. I then went and bought 5 more apples and ate 1. How many
apples was I left with?

Let's think step by step.

提示迭代策略

学会爱上重写提示数(可能是数十个)的现实 次。如果遇到问题,可以参考下面这些提示来优化提示:

注意:随着时间的推移,这些策略的实用性或必要性可能会降低 模型改进。

  1. 重复关键字、词组或提示

  2. 指定所需的输出格式(CSV、JSON 等)

  3. 使用全大写形式来强调要点或说明。您还可以尝试 夸大性或夸张用语;例如:“您的说明应该是 绝对不可能被误解的。每一个字词都必须表达清晰!”

  4. 使用同义词或替代措辞(例如,使用“总结”,尝试 附加“tldr”添加一些输入文本)。交换不同的字词或词组,然后 并记录哪些效果较好,哪些效果较差。

  5. 用较长的提示尝试三明治法:将相同的陈述添加到 不同位置。

  6. 使用提示库寻找灵感。提示主图 以及此提示库 是两个很好的着手点

其他资源

提示最佳实践

了解提示(外部)