提示工程之基本提示【二】

2023-03-24 09:26:53 浏览数 (2)

文章目录

  • 基本提示
  • Basic Prompting
    • 文本摘要
    • Text Summarization
    • 信息提取
    • Information Extraction
    • 问题回答
    • Question Answering
    • 文本分类
    • Text Classification
    • 谈话
    • Conversation
    • 代码生成
    • Code Generation
    • 推理
    • Reasoning
    • Python笔记本
    • Python Notebooks

基本提示

Basic Prompting

在之前的指南中,我们介绍并给出了提示符的基本示例。 In the previous guide, we introduced and gave a basic example of a prompt.

在本指南中,我们将提供更多关于如何使用提示的示例,并介绍对更高级指南很重要的关键概念。 In this guide, we will provide more examples of how prompts are used and introduce key concepts that will be important for the more advanced guides.

通常,学习概念的最好方法是通过例子。 Often, the best way to learn concepts is by going through examples.

下面我们将介绍几个例子,说明如何使用精心设计的提示来执行各种有趣和不同的任务。 Below we cover a few examples of how well-crafted prompts can be used to perform all types of interesting and different tasks.

主题: Topics:

-[文本摘要](# Text - summary)

  • Text Summarization

-[信息提取](# Information - Extraction)

  • Information Extraction

-[问题回答](# Question -answer)

  • Question Answering

-[文本分类](# Text - Classification)

  • Text Classification

——(对话)(#对话)

  • Conversation

-[代码生成](# Code - Generation)

  • Code Generation

——(推理)(#推理)

  • Reasoning
  • [Python笔记本](# Python -笔记本)
  • Python Notebooks


文本摘要

Text Summarization

自然语言生成的标准任务之一是文本摘要。 One of the standard tasks in natural language generation is text summarization.

文本摘要可以包括许多不同的风格和领域。 Text summarization can include many different flavors and domains.

语言模型最有前途的应用之一是能够将文章和概念总结为快速且易于阅读的摘要。 One of the most promising applications of language models is the ability to summarize articles and concepts into quick and easy-to-read summaries.

让我们用提示来做一个基本的总结任务。 Let’s try a basic summarization task using prompts.

假设我对抗生素很感兴趣,我可以尝试这样的提示: Let’s say I am interested to learn about antibiotics, I could try a prompt like this:

提示: Prompt:

’ ’ ’

代码语言:javascript复制
解释抗生素
Explain antibiotics

答:
A:

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
抗生素是一种用于治疗细菌感染的药物。
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:”是在问题回答中使用的显式提示格式。 The “A:” is an explicit prompt format that’s used in question answering.

我在这里用它来告诉模型,有一个预期的进一步。 I used it here to tell the model that there is an expected further.

在这个例子中,不清楚这个是如何有用还是不使用它,但我们将把它留给后面的例子。 In this example, it’s not clear how this is useful vs not using it but we will leave it that for later examples.

让我们假设这是太多的信息,并希望进一步总结。 Let’s just assume that this is too much information and want to summarize it further.

我们可以指导模型像这样总结成一句话: We can instruct the model to summarize into one sentence like so:

提示: Prompt:

’ ’ ’

代码语言:javascript复制
抗生素是一种用于治疗细菌感染的药物。
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:

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
抗生素是一种通过杀死细菌或阻止细菌繁殖来治疗细菌感染的药物,但它们对病毒无效,过度使用会导致抗生素耐药性。
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.

' ' '

没有过多关注上面输出的准确性,这是我们将在后面的指南中讨论的问题,模型试图用一句话概括这段话。 Without paying too much attention to the accuracy of the output above, which is something we will touch on in a later guide, the model tried to summarize the paragraph in one sentence.

你可以巧妙地使用这些说明,但我们将把它留到后面的章节。 You can get clever with the instructions but we will leave that for a later chapter.

请随意在此暂停并进行试验,看看是否会得到更好的结果。 Feel free to pause here and experiment to see if you get better results.



信息提取

Information Extraction

虽然语言模型被训练为执行自然语言生成和相关任务,但它也非常能够执行分类和一系列其他自然语言处理(NLP)任务。 While language models are trained to perform natural language generation and related tasks, it’s also very capable of performing classification and a range of other natural language processing (NLP) tasks.

下面是一个从给定段落中提取信息的提示符示例。 Here is an example of a prompt that extracts information from a given paragraph.

提示: Prompt:

’ ’ ’

代码语言:javascript复制
研究论文中的作者贡献声明和致谢应清楚而具体地说明作者是否以及在多大程度上使用了ChatGPT等人工智能技术来准备他们的手稿和分析。
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.

它们还应该指出使用了哪些llm。
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:

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
上面一段提到的基于大型语言模型的产品是ChatGPT。
The large language model based product mentioned in the paragraph above is ChatGPT.

' ' '

我们有很多方法可以改善上述结果,但这已经非常有用了。 There are many ways we can improve the results above, but this is already very useful.

到目前为止,很明显,您可以通过简单地指示模型做什么来要求它执行不同的任务。 By now it should be obvious that you can ask the model to perform different tasks by simply instructing it what to do.

这是一种强大的能力,人工智能产品开发人员已经在使用它来构建强大的产品和体验。 That’s a powerful capability that AI product builders are already using to build powerful products and experiences.

段落来源:【ChatGPT:研究的五大重点】(https://www.nature.com/articles/d41586-023-00288-7) Paragraph source: ChatGPT: five priorities for research

问题回答

Question Answering

让模型对特定的答案做出回应的最好方法之一是改进提示的格式。 One of the best ways to get the model to respond to specific answers is to improve the format of the prompt.

如前所述,提示符可以组合指令、上下文、输入和输出指示器,以获得改进的结果。 As covered before, a prompt could combine instructions, context, input, and output indicators to get improved results.

虽然这些组件不是必需的,但这是一种很好的实践,因为您的指导越具体,得到的结果就越好。 While these components are not required, it becomes a good practice as the more specific you are with instruction, the better results you will get.

下面是一个例子,说明在一个更有条理的提示符之后会是什么样子。 Below is an example of how this would look following a more structured prompt.

提示: Prompt:

’ ’ ’

代码语言:javascript复制
根据下面的上下文回答问题。
Answer the question based on the context below.

回答要简短。
Keep the answer short.

如果不确定答案,回答“不确定”。
Respond "Unsure about answer" if not sure about the answer.

背景:Teplizumab起源于新泽西州一家名为Ortho Pharmaceutical的制药公司。
Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical.

在那里,科学家们产生了这种抗体的早期版本,被称为OKT3。
There, scientists generated an early version of the antibody, dubbed OKT3.

这种分子最初来源于小鼠,能够结合到T细胞表面,限制它们杀死细胞的潜力。
Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential.

1986年,它被批准用于预防肾移植后的器官排斥反应,这使它成为第一个被允许用于人体的治疗性抗体。
In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.

问:OKT3最初的来源是什么?
Question: What was OKT3 originally sourced from?

答:
Answer:

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
老鼠。
Mice.

' ' '

背景信息来源于自然。 Context obtained from Nature.



文本分类

Text Classification

到目前为止,我们使用简单的指令来执行任务。 So far, we have used simple instructions to perform a task.

作为一名提示工程师,您需要更好地提供更好的指示。 As a prompt engineer, you will need to get better at providing better instructions.

但这还不是全部! But that’s not all!

您还会发现,对于更难的用例,仅仅提供说明是不够的。 You will also find that for harder use cases, just providing instructions won’t be enough.

这就是你需要更多地考虑上下文和你可以在提示符中使用的不同元素的地方。 This is where you need to think more about the context and the different elements you can use in a prompt.

你可以提供的其他元素是“输入数据”或“示例”。 Other elements you can provide are input data or examples.

让我们通过提供一个文本分类示例来演示这一点。 Let’s try to demonstrate this by providing an example of text classification.

提示: Prompt:

’ ’ ’

代码语言:javascript复制
把文章分为中性的、消极的和积极的。
Classify the text into neutral, negative or positive.

我觉得食物还可以。
Text: I think the food was okay.

情绪:
Sentiment:

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
中性
Neutral

' ' '

我们给出了对文本进行分类的指令,模型的回答是“中性”,这是正确的。 We gave the instruction to classify the text and the model responded with 'Neutral' which is correct.

这并没有什么问题,但是让我们假设我们真正需要的是模型以我们想要的确切格式给出标签。 Nothing is wrong with this but let’s say that what we really need is for the model to give the label in the exact format we want.

因此,我们希望它返回’ Neutral ‘而不是’ Neutral '。 So instead of Neutral we want it to return neutral.

我们如何做到这一点呢? How do we achieve this.

有不同的方法可以做到这一点。 There are different ways to do this.

这里我们关心的是特异性,所以我们能提供的信息越多,结果就越好。 We care about specificity here, so the more information we can provide the prompt the better results.

我们可以试着提供例子来说明正确的行为。 We can try providing examples to specify the correct behavior.

让我们再试一次: Let’s try again:

提示: Prompt:

’ ’ ’

代码语言:javascript复制
把文章分为中性的、消极的和积极的。
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:

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
中性
neutral

' ' '

完美! Perfect!

这一次,模型返回“中性”,这是我正在寻找的特定标签。 This time the model returned neutral which is the specific label I was looking for.

提示中提供的示例似乎有助于模型的输出更加具体。 It seems that the example provided in the prompt helped the model to be specific in its output.

为了强调为什么有时具体是重要的,检查这个例子并找出问题: To highlight why sometimes being specific is important, checkout this example and spot the problem:

提示: Prompt:

’ ’ ’

代码语言:javascript复制
把文章分为中性的、消极的和积极的。
Classify the text into nutral, negative or positive.

我觉得假期还行。
Text: I think the vacation is okay.

情绪:
Sentiment:

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
中性
Neutral

' ' '

这里的问题是什么? What is the problem here?



谈话

Conversation

也许通过快速工程可以实现的更有趣的事情之一是指导LLM系统如何表现、它的意图和它的身份。 Perhaps one of the more interesting things you can achieve with prompt engineering is instructing the LLM system on how to behave, its intent, and its identity.

这在构建会话系统(如客户服务聊天机器人)时特别有用。 This is particularly useful when you are building conversational systems like customer service chatbots.

例如,让我们创建一个对话系统,能够对问题产生更多技术和科学的回答。 For instance, let’s create a conversational system that’s able to generate more technical and scientific responses to questions.

注意我们是如何通过指令显式地告诉它如何行为的。 Note how we are explicitly telling it how to behave through the instruction.

这有时被称为“角色提示”。 This is sometimes referred to as role prompting.

提示: Prompt:

’ ’ ’

代码语言:javascript复制
以下是与人工智能研究助理的对话。
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:

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
当然!
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.

' ' '

我们的人工智能研究助理听起来有点太专业了,对吧? Our AI research assistant sounds a bit too technical, right?

好吧,让我们改变这种行为,让系统给出更容易理解的答案。 Okay, let’s change this behavior and instruct the system to give more accessible answers.

提示: Prompt:

’ ’ ’

代码语言:javascript复制
以下是与人工智能研究助理的对话。
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:

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
当然!
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.

' ' '

我们的人工智能研究助理听起来有点太专业了,对吧? Our AI research assistant sounds a bit too technical, right?

好吧,让我们改变这种行为,让系统给出更容易理解的答案。 Okay, let’s change this behavior and instruct the system to give more accessible answers.

提示: Prompt:

’ ’ ’

代码语言:javascript复制
以下是与人工智能研究助理的对话。
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:

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
当然!
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.

' ' '

我认为我们取得了一些进展。 I think we made some progress.

你可以继续改进它。 You can continue improving it.

我相信如果你添加更多的例子,你可能会得到更好的结果。 I am sure if you add more examples you might get even better results.



代码生成

Code Generation

llm非常有效的一个应用程序是代码生成。 One application where LLMs are quite effective is code generation.

副驾驶就是一个很好的例子。 Copilot is a great example of this.

您可以使用巧妙的提示符执行大量的代码生成任务。 There are a vast number of code-generation tasks you can perform with clever prompts.

让我们看看下面的几个例子。 Let’s look at a few examples below.

首先,让我们尝试一个欢迎用户的简单程序。 First, let’s try a simple program that greets the user.

提示: Prompt:

’ ’ ’

代码语言:javascript复制
/*
/*

询问用户的名字并说“Hello”
Ask the user for their name and say "Hello"

* /
*/

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
let name = prompt(“你叫什么名字?”);
let name = prompt("What is your name?");

console.log(“你好,${名称}!”);
console.log(`Hello, ${name}!`);

' ' '

您可以看到,我们甚至不需要指定要使用的语言。 You can see that we didn’t even need to specify the language to use.

让我们转换一下级别。 Let’s switch levels a bit.

我想向您展示llm在设计提示时的强大功能。 I want to show you how powerful LLMs can be with a little more effort in designing the prompts.

提示: Prompt:

’ ’ ’

代码语言:javascript复制
”“”
"""

表部门,列= [departmentd, DepartmentName]
Table departments, columns = [DepartmentId, DepartmentName]

表students, columns = [departmentd, studententid, StudentName]
Table students, columns = [DepartmentId, StudentId, StudentName]

为计算机系的所有学生创建一个MySQL查询
Create a MySQL query for all students in the Computer Science Department

”“”
"""

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
SELECT studententid, StudentName
SELECT StudentId, StudentName

从学生
FROM students

WHERE departmentd IN (SELECT departmentd FROM DepartmentName = 'Computer Science');
WHERE DepartmentId IN (SELECT DepartmentId FROM departments WHERE DepartmentName = 'Computer Science');

' ' '

这令人印象深刻。 This is very impressive.

在本例中,我们提供了关于数据库模式的数据,并要求它生成有效的MySQL查询。 In this case, we provided data about the database schema and asked it to generate a valid MySQL query.

推理

Reasoning

也许当今法学硕士最困难的任务之一需要某种形式的推理。 Perhaps one of the most difficult tasks for an LLM today requires some form of reasoning.

推理是我最感兴趣的领域之一,因为llm可以产生复杂的应用程序类型。 Reasoning is one of the areas that I am most excited about due to the types of complex applications that can emerge from LLMs.

在涉及数学能力的任务中有了一些改进。 There have been some improvements in tasks involving mathematical capabilities.

也就是说,需要注意的是,目前的llm很难执行推理任务,因此这需要更先进的即时工程技术。 That said, it’s important to note that current LLMs struggle to perform reasoning tasks so this requires even more advanced prompt engineering techniques.

我们将在下一篇指南中介绍这些高级技术。 We will cover these advanced techniques in the next guide.

现在,我们将介绍几个基本示例来展示算术能力。 For now, we will cover a few basic examples to show arithmetic capabilities.

提示: Prompt:

’ ’ ’

代码语言:javascript复制
9000 * 9000等于多少?
What is 9,000 * 9,000?

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
81000000年
81,000,000

' ' '

让我们试试更难的。 Let’s try something more difficult.

提示: Prompt:

’ ’ ’

代码语言:javascript复制
这组奇数加起来是偶数:15,32,5,13,82,7,1。
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.

答:
A:

' ' '

输出 Output

’ ’ ’

代码语言:javascript复制
不,这组中的奇数加起来是一个奇数:119。
No, the odd numbers in this group add up to an odd number: 119.

' ' '

这是不正确的! That’s incorrect!

让我们试着通过改进提示来改进这一点。 Let’s try to improve this by improving the prompt.

提示: Prompt:

’ ’ ’

代码语言:javascript复制
这组奇数加起来是偶数:15,32,5,13,82,7,1。
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.

' ' '

输出: Output:

’ ’ ’

代码语言:javascript复制
奇数:15,5,13,7,1
Odd numbers: 15, 5, 13, 7, 1

总结:41
Sum: 41

41是个奇数。
41 is an odd number.

' ' '

好多了,对吧? Much better, right?

顺便说一下,我试了几次,系统有时会失败。 By the way, I tried this a couple of times and the system sometimes fails.

如果你结合例子提供更好的指导,可能会得到更准确的结果。 If you provide better instruction combined with examples, it might help get more accurate results.

在本部分中,我们将继续包括更多常见应用程序的示例。 We will continue to include more examples of common applications in this section of the guide.

在接下来的指南中,我们将介绍更高级的即时工程概念,以提高所有这些和更困难的任务的性能。 In the upcoming guides, we will cover even more advanced prompt engineering concepts for improving performance on all these and more difficult tasks.



Python笔记本

Python Notebooks

| | |笔记本描述 |Description|Notebook|

学习如何使用“openai”和“LangChain”库执行许多不同类型的常见任务[提示工程入门](…/notebook /pe-lecture.ipynb |Learn how to perform many different types of common tasks using the openai and LangChain library|Getting Started with Prompt Engineering|



[上一节(提示简介)](./ Prompts - Introduction .md) Previous Section (Prompts Introduction)

下一节(高级提示技术) Next Section (Advanced Prompting Techniques)

0 人点赞