Tips
从简单开始、specificity、Do vs Don’t 等通用技巧
以下是设计 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:
code### Instruction ### Translate the following text to Spanish: Text: "hello!"
Output:
code¡Hola!
#具体性(specificity)
要非常具体地说明你希望模型执行的 instruction 和 task。prompt 越具描述性和详细,结果越好。特别是当你对生成的结果或风格有要求时,这一点尤为重要。不存在什么特定 token 或关键词能保证更好的结果;更重要的是一个有良好格式和描述性的 prompt。事实上,在 prompt 中提供 examples 对获得特定格式的期望输出非常有效。
在设计 prompt 时,还应注意 prompt 的长度,因为上下文长度是有限制的。包含太多不必要的细节不一定是好方法;细节应该相关,并有助于完成任务。我们鼓励大量实验和迭代,以优化适用于你应用的 prompt。
例如,让我们尝试从一段文本中提取特定信息:
Prompt:
codeExtract 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:
codeLocations: Lisbon, Champalimaud Centre
#避免不明确
给定上述关于详细描述和改进格式的建议,很容易陷入陷阱:想在 prompt 上过于聪明,从而可能创造出不明确的描述。通常来说,具体和直接会更好。类比有效沟通:越直接,信息传达得越有效。
例如,你可能有兴趣了解 prompt engineering 的概念:
codeExplain the concept of prompt engineering. Keep the explanation short, only a few sentences.
上面的 prompt 并不清楚要使用多少句子以及什么风格。更好的 prompt 应当非常具体、简洁并切中要点,例如:
codeExplain the concept of prompt engineering to a high school student in 2-3 sentences.
#Do vs Don’t
设计 prompt 时的另一个常见技巧是避免说 “不要做什么”,而应该说 “要做什么”。这样更具体,并且更聚焦于有利于模型生成良好回复的细节。
例如,一个电影推荐 agent(bad prompt):
Prompt:
codeYou 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:
codeSure, I can recommend a movie based on your interests. What genres do you like—action, comedy, romance, or something else?
更好的 prompt:
Prompt:
codeYou 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:
codeSorry, 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!