🚀 在我们的全新课程中掌握提示工程并构建 AI Agent!使用 PROMPTING20 享 8 折优惠 ➜ 立即报名
提示词示例

提示词示例

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

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

主题


🎓

在我们的全新 AI 课程中了解更多提示技术和示例。立即加入! (opens in a new tab) 使用代码 PROMPTING20 可额外享受 8 折优惠。

文本摘要

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

假设你对了解抗生素感兴趣,可以尝试这样的提示词

提示词

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:研究的五个优先事项 (opens in a new tab)


问答

让模型给出具体答案的最佳方法之一是改进提示词的格式。如前所述,提示词可以结合指令、上下文、输入和输出指示符来获得更好的结果。虽然这些组成部分不是必需的,但它们是一种很好的做法,因为你的指令越具体,得到的结果就越好。以下是一个遵循更结构化提示词的示例。

提示词

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 (opens in a new tab)


文本分类

到目前为止,你都使用了简单的指令来执行任务。作为一名提示工程师,你需要更好地提供更优质的指令。但这还不够!你还会发现,对于更困难的用例,仅提供指令是不够的。这时你需要更多地考虑上下文以及你可以在提示词中使用的不同元素。你可以提供的其他元素包括输入数据示例

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

提示词

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.

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


代码生成

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

首先,让我们尝试一个向用户打招呼的简单程序。

提示词

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

输出

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

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

让我们稍微提升一下难度。下面的示例展示了只要在提示词设计上多花一点功夫,LLM 可以变得多么强大。

提示词

"""
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 来说,最困难的任务之一可能就是需要某种形式推理的任务。由于 LLM 可以催生出各种复杂的应用,推理是其中最有趣的领域之一。

在涉及数学能力的任务方面已经取得了一些进展。话虽如此,重要的是要注意到当前的 LLM 在执行推理任务时仍面临困难,因此这需要更高级的提示工程技术。我们将在下一份指南中介绍这些高级技术。现在,我们将介绍几个基本示例来展示算术能力。

提示词

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.

好多了,对吗?顺便说一句,我们尝试执行这项任务几次,模型有时会出错。如果你提供更好的指令并结合示例,这可能会有助于获得更准确的结果。

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

Notebook

如果你想使用 Python 练习上面的提示词,我们准备了一个 Notebook 来测试一些使用 OpenAI 模型的提示词。