🚀 在我们的新课程中掌握提示工程并构建AI代理!使用 PROMPTING20 享受 20% 折扣 ➜ 立即报名
设计提示的一般技巧

设计提示的一般技巧

以下是一些设计提示时需要记住的技巧

从简单开始

当你开始设计提示时,应记住这是一个迭代过程,需要大量实验才能获得最佳结果。使用 OpenAI 或 Cohere 提供的简单 Playground 是一个不错的起点。

你可以从简单的提示开始,然后随着目标是获得更好的结果,不断添加更多元素和上下文。因此,在此过程中迭代你的提示至关重要。阅读本指南时,你会看到许多示例,其中特异性、简单性和简洁性通常会为你带来更好的结果。

当你有涉及许多不同子任务的大型任务时,可以尝试将任务分解为更简单的子任务,并随着获得更好的结果不断构建。这避免了在开始时为提示设计过程增加过多复杂性。

指令

你可以使用命令来指示模型完成各种简单任务,从而设计出有效的提示,例如“撰写”、“分类”、“摘要”、“翻译”、“排序”等。

记住,你还需要进行大量实验才能找到最佳方法。尝试使用不同的关键词、上下文和数据来运用不同的指令,看看哪种方法最适合你的特定用例和任务。通常,上下文越具体且与你尝试执行的任务越相关,效果越好。我们将在后续指南中探讨采样和添加更多上下文的重要性。

其他人建议将指令放在提示的开头。另一个建议是使用一些清晰的分隔符(例如“###”)来分隔指令和上下文。

例如

提示

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

Text: "hello!"

输出

¡Hola!

特异性

对于你希望模型执行的指令和任务,要非常具体。提示越具描述性且越详细,结果越好。当你寻求特定的结果或生成风格时,这一点尤为重要。并没有特定的 token 或关键词能带来更好的结果。更重要的是有一个好的格式和描述性的提示。事实上,在提示中提供示例对于获得特定格式的预期输出非常有效。

设计提示时,还应考虑提示的长度,因为提示的长度有限制。思考应该多具体、多详细。包含太多不必要的细节不一定是好的方法。细节应相关并有助于完成当前任务。这需要你进行大量实验。我们鼓励大量实验和迭代,以优化你的应用程序的提示。

举个例子,让我们尝试一个简单的提示,从一段文本中提取特定信息。

提示

Extract the name of places in the following text. 

Desired format:
Place: <comma_separated_list_of_places>

Input: "Although these developments are encouraging to researchers, much is still a mystery. “We often have a black box between the brain and the effect we see in the periphery,” says Henrique Veiga-Fernandes, a neuroimmunologist at the Champalimaud Centre for the Unknown in Lisbon. “If we want to use it in the therapeutic context, we actually need to understand the mechanism.“"

输出

Place: Champalimaud Centre for the Unknown, Lisbon

输入文本取自这篇 Nature 文章 (opens in a new tab)

🎓

在我们的新 AI 课程中了解更多关于提示技术和示例的信息。立即加入! (opens in a new tab)使用代码 PROMPTING20 即可额外享受 20% 折扣。

避免不精确

鉴于上面关于详细和改进格式的技巧,很容易陷入过度“聪明”设计提示并可能创建不精确描述的陷阱。通常最好具体和直接。这里的类比与有效沟通非常相似——越直接,信息传达越有效。

例如,你可能对学习提示工程的概念感兴趣。你可能会尝试这样的提示:

Explain the concept prompt engineering. Keep the explanation short, only a few sentences, and don't be too descriptive.

上面的提示不清楚需要多少句子以及什么风格。你可能仍然会用上面的提示获得不错的回答,但更好的提示会是非常具体、简洁、直指要点。比如这样:

Use 2-3 sentences to explain the concept of prompt engineering to a high school student.

应该做什么,不应该做什么?

设计提示时另一个常见的技巧是避免说“不要做什么”,而是说“要做什么”。这鼓励了更具体的描述,并专注于那些能让模型给出良好回应的细节。

这里有一个电影推荐聊天机器人的例子,它恰恰失败在我不想让它做的事情上,原因是我写指令的方式——侧重于“不要做什么”。

提示

The following is an agent that recommends movies to a customer. DO NOT ASK FOR INTERESTS. DO NOT ASK FOR PERSONAL INFORMATION.

Customer: Please recommend a movie based on my interests.
Agent: 

输出

Sure, I can recommend a movie based on your interests. What kind of movie would you like to watch? Do you prefer action, comedy, romance, or something else?

下面是一个更好的提示:

提示

The following is an agent that recommends movies to a customer. The agent is responsible to recommend a movie from the top global trending movies. It should refrain from asking users for their preferences and avoid asking for personal information. If the agent doesn't have a movie to recommend, it should respond "Sorry, couldn't find a movie to recommend today.".

Customer: Please recommend a movie based on my interests.
Agent:

输出

Sorry, I don't have any information about your interests. However, here's a list of the top global trending movies right now: [list of movies]. I hope you find something you like!

上面的一些示例改编自“使用 OpenAI API 进行提示工程的最佳实践”一文。 (opens in a new tab)