Examples of Prompts

提示词示例

上一节介绍了如何提示LLMs的基本示例。

本节将提供更多关于如何使用提示来实现不同任务的示例,并在此过程中介绍关键概念。通常,学习概念的最佳方式是通过示例。下面的几个示例说明了如何使用精心设计的提示来执行不同类型的任务。

主题:


文本摘要

自然语言生成中的标准任务之一是文本摘要。文本摘要可以包括许多不同的风格和领域。事实上,语言模型最有前途的应用之一是将文章和概念总结为快速且易于阅读的摘要。让我们尝试使用提示进行一个基本的摘要任务。

假设你对学习抗生素感兴趣,你可以尝试这样的提示:

提示:

Explain antibiotics

A:

输出:

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

"A:" 是一个明确的提示格式,用于问答中。你在这里使用它是为了告诉模型后面会有一个答案。在这个例子中,不清楚使用它和不使用它相比有什么用处,但我们将在后面的例子中讨论这个问题。让我们假设这是过多的信息,你想进一步总结它。事实上,你可以指示模型将其总结为一句话,如下所示:

提示:

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

Explain the above in one sentence:

输出:

Antibiotics are medications used to treat bacterial infections by either killing the bacteria or stopping them from reproducing, but they are not effective against viruses and overuse can lead to antibiotic resistance.

不必过于关注上述输出的准确性,这一点我们将在后续指南中讨论,模型试图用一句话总结段落。你可以巧妙地使用指令,但我们将把这一点留到后面的章节。请随时在此暂停并尝试,看看是否能得到更好的结果。


信息提取

虽然语言模型被训练来执行自然语言生成和相关任务,但它也非常擅长执行分类和一系列其他自然语言处理(NLP)任务。

这是一个从给定段落中提取信息的提示示例。

提示:

Author-contribution statements and acknowledgements in research papers should state clearly and specifically whether, and to what extent, the authors used AI technologies such as ChatGPT in the preparation of their manuscript and analysis. They should also indicate which LLMs were used. This will alert editors and reviewers to scrutinize manuscripts more carefully for potential biases, inaccuracies and improper source crediting. Likewise, scientific journals should be transparent about their use of LLMs, for example when selecting submitted manuscripts.

Mention the large language model based product mentioned in the paragraph above:

输出:

The large language model based product mentioned in the paragraph above is ChatGPT.

有很多方法可以改进上述结果,但这已经非常有用。

到现在为止,你应该已经明白,你可以通过简单地指示模型做什么来让它执行不同的任务。这是一个强大的能力,AI产品开发者已经在利用它来构建强大的产品和体验。

段落来源:ChatGPT: 研究的五个优先事项 (在新标签页中打开)


问答

让模型以特定答案回应的最佳方法之一是改进提示的格式。如前所述,提示可以结合指令、上下文、输入和输出指示器以获得更好的结果。虽然这些组件不是必需的,但随着指令的更加具体,结果也会更好,因此这成为一种良好的实践。以下是一个遵循更结构化提示的示例。

提示:

Answer the question based on the context below. Keep the answer short and concise. Respond "Unsure about answer" if not sure about the answer.

Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.

Question: What was OKT3 originally sourced from?

Answer:

输出:

Mice.

上下文从Nature(在新标签页中打开)获得。


文本分类

到目前为止,您已经使用了简单的指令来执行任务。作为提示工程师,您需要更好地提供更好的指令。但这还不是全部!您还会发现,对于更复杂的用例,仅仅提供指令是不够的。这就是您需要更多地考虑上下文和提示中可以使用的不同元素的地方。您可以提供的其他元素包括input dataexamples

让我们通过提供一个文本分类的例子来尝试演示这一点。

提示:

Classify the text into neutral, negative or positive. 

Text: I think the food was okay. 
Sentiment:

输出:

Neutral

你给出了分类文本的指令,模型响应为'Neutral',这是正确的。这没有问题,但假设你真正需要的是模型以你想要的精确格式给出标签。所以,你希望它返回neutral,而不是Neutral。你如何实现这一点?有几种方法可以做到这一点。你在这里关心的是特异性,所以你能提供的提示信息越多,结果就越好。你可以尝试提供示例来指定正确的行为。让我们再试一次:

提示:

Classify the text into neutral, negative or positive. 

Text: I think the vacation is okay.
Sentiment: neutral 

Text: I think the food was okay. 
Sentiment:

输出:

neutral

完美!这次模型返回了neutral,这是你正在寻找的特定标签。看来提示中提供的示例帮助模型在输出中更加具体。

为了强调有时具体性为何重要,请查看下面的示例并找出问题:

提示:

Classify the text into nutral, negative or positive. 

Text: I think the vacation is okay.
Sentiment:

输出:

Neutral

这里的问题是什么?作为提示,虚构的nutral标签被模型完全忽略了。相反,模型输出Neutral,因为它对该标签有一定的偏见。但假设你真正想要的是nutral。你会如何解决这个问题?也许你可以尝试为标签添加描述或在提示中添加更多示例?如果你不确定,我们将在接下来的部分讨论一些想法。


对话

也许通过提示工程可以实现的一个更有趣的事情是指导LLM系统如何行为、其意图和身份。这在构建诸如客户服务聊天机器人等对话系统时特别有用。

例如,让我们创建一个能够生成更技术和科学回答的对话系统。请注意,您通过指令明确告诉它如何表现。这有时被称为角色提示

提示:

The following is a conversation with an AI research assistant. The assistant tone is technical and scientific.

Human: Hello, who are you?
AI: Greeting! I am an AI research assistant. How can I help you today?
Human: Can you tell me about the creation of blackholes?
AI:

输出:

Sure! Black holes are regions of spacetime where the gravitational force is so strong that nothing, not even light, can escape from it. They are created when a very massive star dies and its core collapses in on itself, forming a singularity of infinite density. The intense gravity of the singularity pulls in all the matter and radiation around it, creating the black hole.

我们的AI研究助手听起来有点太技术性了,对吧?好吧,让我们改变这种行为,并指示系统提供更易于理解的答案。

提示:

The following is a conversation with an AI research assistant. The assistant answers should be easy to understand even by primary school students.

Human: Hello, who are you?
AI: Greeting! I am an AI research assistant. How can I help you today?
Human: Can you tell me about the creation of black holes?
AI: 

输出:

Sure! Black holes are created when a very massive star runs out of fuel and collapses in on itself. The gravity of the star is so strong that nothing, not even light, can escape its pull. This creates a point in space-time with an intense gravitational force called a black hole.

你取得了一些进展。你可以继续改进它。如果你添加更多的例子,可能会得到更好的结果。


代码生成

LLMs 在代码生成方面非常有效的一个应用是代码生成。Copilot 就是一个很好的例子。通过巧妙的提示,你可以执行大量的代码生成任务。让我们看看下面的一些例子。

首先,让我们尝试一个简单的程序来问候用户。

提示:

/*
Ask the user for their name and say "Hello"
*/

输出:

let name = prompt("What is your name?");
console.log(`Hello, ${name}!`);

请注意,您甚至不需要指定要使用的语言。

让我们稍微改变一下层次。下面的例子展示了在设计提示时多花一点功夫,LLMs 可以有多么强大。

提示:

"""
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
Create a MySQL query for all students in the Computer Science Department
"""

输出:

SELECT StudentId, StudentName 
FROM students 
WHERE DepartmentId IN (SELECT DepartmentId FROM departments WHERE DepartmentName = 'Computer Science');

这非常令人印象深刻。在这种情况下,您提供了关于数据库模式的数据,并要求它生成一个有效的MySQL查询。


推理

对于今天的LLM来说,最困难的任务之一可能是需要某种形式的推理。推理是最有趣的领域之一,因为可以从LLMs中涌现出各种复杂的应用。

在涉及数学能力的任务中已经有了一些改进。也就是说,值得注意的是,当前的LLMs在执行推理任务时仍然存在困难,因此这需要更高级的提示工程技术。我们将在下一个指南中介绍这些高级技术。现在,我们将介绍一些基本示例来展示算术能力。

提示:

What is 9,000 * 9,000?

输出:

81,000,000

让我们尝试一些更困难的事情。

提示:

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. 

A: 

输出

No, the odd numbers in this group add up to an odd number: 119.

这是不正确的!让我们通过改进提示来尝试改进这一点。

提示:

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. 

Solve by breaking the problem into steps. First, identify the odd numbers, add them, and indicate whether the result is odd or even. 

输出:

Odd numbers: 15, 5, 13, 7, 1
Sum: 41 
41 is an odd number.

好多了,对吧?顺便说一下,我们尝试了几次这个任务,模型有时会失败。如果你能提供更好的指令并结合示例,可能会帮助获得更准确的结果。

在接下来的部分中,我们将介绍更高级的提示工程概念和技术,以提高所有这些以及更困难任务的性能。

笔记本

如果你想使用Python练习上述提示,我们准备了一个笔记本,用于使用OpenAI模型测试一些提示。