P
Prompt Master
Prompt Engineering 教程与提示词实战

从任务定义、示例和工作流到评测与安全边界

Prompt Engineering 教程与提示词实战Introduction

Tips

从简单开始、specificity、Do vs Don't 等通用技巧

本章 Prompt 决定
01要解决的 Prompt 问题

从简单开始、specificity、Do vs Don't 等通用技巧

02完成后带走

一份保留任务边界的可复用 Prompt、示例集、评测记录或安全规则。

03做到什么算完成

至少跑一个代表性样本,检查结果,并记录仍需人工复核的部分。

以下是设计 prompt 时需要记住的一些技巧:

从简单开始

在开始设计 prompt 时,你应该记住,这实际上是一个迭代过程,需要大量实验才能获得最佳结果。使用来自 OpenAI 或 Cohere 的简单 playground 是一个很好的起点。

你可以从简单的 prompt 开始,并逐渐添加更多 elements 和 context(因为你想要更好的结果)。因此,在这个过程中不断迭代 prompt 是至关重要的。阅读本指南时,你会看到许多示例,其中 specificity、simplicity 和 conciseness 通常会带来更好的结果。

当你有一个涉及许多不同子任务的大任务时,可以尝试将任务分解为更简单的子任务,并随着结果的改善逐步构建。这避免了在 prompt 设计过程中一开始就添加过多复杂性。

instruction

你可以使用命令来指示模型执行各种简单任务,例如“写入”、“分类”、“总结”、“翻译”、“排序”等,从而为各种简单任务设计有效的 prompt。

记住,你还需要大量实验以找出最有效的方法。以不同的 keywords、contexts 和 data 试验不同的 instruction,看看什么样最适合你特定用例和任务。通常,context 越具体且与任务越相关则效果越好。

有些人建议将 instruction 放在 prompt 的开头。另有人则建议使用像 “###” 这样的清晰分隔符来分隔 instruction 和 context。

例如:

Prompt:

### Instruction ###
Translate the following text to Spanish:

Text: "hello!"

Output:

¡Hola!

具体性(specificity)

要非常具体地说明你希望模型执行的 instruction 和 task。prompt 越具描述性和详细,结果越好。特别是当你对生成的结果或风格有要求时,这一点尤为重要。不存在什么特定 token 或关键词能保证更好的结果;更重要的是一个有良好格式和描述性的 prompt。事实上,在 prompt 中提供 examples 对获得特定格式的期望输出非常有效。

在设计 prompt 时,还应注意 prompt 的长度,因为上下文长度是有限制的。包含太多不必要的细节不一定是好方法;细节应该相关,并有助于完成任务。我们鼓励大量实验和迭代,以优化适用于你应用的 prompt。

例如,让我们尝试从一段文本中提取特定信息:

Prompt:

Extract location names from the text below.

Desired format:
Locations: <comma-separated list>

Input: "Although these developments are encouraging for researchers, many mysteries remain. Neuroimmunologist Henrique Veiga-Fernandes at Lisbon's Champalimaud Centre said: 'We often have a black box between the brain and the effects we observe. If we want to use it in a therapeutic context, we need to understand the mechanism.'"

Output:

Locations: Lisbon, Champalimaud Centre

避免不明确

给定上述关于详细描述和改进格式的建议,很容易陷入陷阱:想在 prompt 上过于聪明,从而可能创造出不明确的描述。通常来说,具体和直接会更好。类比有效沟通:越直接,信息传达得越有效。

例如,你可能有兴趣了解 prompt engineering 的概念:

Explain the concept of prompt engineering. Keep the explanation short, only a few sentences.

上面的 prompt 并不清楚要使用多少句子以及什么风格。更好的 prompt 应当非常具体、简洁并切中要点,例如:

Explain the concept of prompt engineering to a high school student in 2-3 sentences.

Do vs Don’t

设计 prompt 时的另一个常见技巧是避免说 “不要做什么”,而应该说 “要做什么”。这样更具体,并且更聚焦于有利于模型生成良好回复的细节。

例如,一个电影推荐 agent(bad prompt):

Prompt:

You are a movie recommendation agent. Do not ask about interests. Do not ask for personal information.

Customer: Recommend a movie based on my interests.
Agent:

Output:

Sure, I can recommend a movie based on your interests. What genres do you like—action, comedy, romance, or something else?

更好的 prompt:

Prompt:

You are a movie recommendation agent. Recommend a movie from globally trending movies. Avoid asking for user preferences and avoid asking for personal information. If you cannot recommend a movie, respond with "Sorry, I can't find a movie recommendation today.".

Customer: Recommend a movie based on my interests.
Agent:

Output:

Sorry, I don't have any information about your interests. Here is a list of globally trending movies: [movie list]. I hope you find something you like!

📚 相关资源

常见问题

点击问题,查看本章对应的实践答案。

刚开始写 Prompt,从哪种结构起步?

从最简单的 instruction + input 开始,结果不行再叠 context、再叠输出格式。一上来就堆 system role + 多 example + JSON schema,调试时不知道是哪一层出错。OpenAI / Cohere 的 playground 是最好的迭代环境,每改一项跑一次。

把 instruction 放在 prompt 开头还是结尾?

推荐放开头,并用 `### Instruction ###` 这类显式分隔符把指令和数据隔开。这是 OpenAI 早期文档的建议,至今仍稳——模型看到分隔符后认知「这一段是要执行的命令」。指令埋在最后,长上下文里很容易被忽略。

Specificity 到底要具体到什么程度?

具体到「受众 + 句数 + 风格」这层。「Explain prompt engineering」太空,「Explain prompt engineering to a high school student in 2-3 sentences」就稳了——给了受众(高中生)、长度(2-3 句)、隐含的风格(简洁)。但别堆无关细节,context window 有限。

提供示例对输出格式控制有多大作用?

很大。文档原话:「在 prompt 中提供 examples 对获得特定格式的期望输出非常有效。」想拿 `Locations: <comma-separated list>` 这种受限格式,最稳的写法就是写一遍「Desired format」加一段示范输入输出,比反复用文字描述格式更有效。

电影推荐 agent 那个反例为什么会翻车?

因为 prompt 全是否定句:「Do not ask about interests. Do not ask for personal information.」模型仍然反问「What genres do you like」。换成正向指令「Recommend a movie from globally trending movies. 找不到就回 Sorry, I can't find a movie recommendation today.」立刻给出热门片单——告诉模型走哪条路径,比禁止哪条路径有效得多。