生成式 AI 提示工程

提示工程是指提出正确的问题以获得 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”,而不是“Summarize”)。替换不同的字词或短语,并记录哪些字词效果较好,哪些效果较差。

  5. 在长时间提示时,不妨尝试三明治方法:在不同的位置添加相同的语句。

  6. 通过提示库获取灵感。Prompt Hero 和此提示图库是很不错的入手点。

其他资源

提示最佳实践

了解提示(外部)