mirror of
https://github.com/dair-ai/Prompt-Engineering-Guide
synced 2024-11-06 09:20:31 +00:00
a458efc27b
Signed-off-by: liyun <liyun@yunify.com>
325 lines
12 KiB
Plaintext
325 lines
12 KiB
Plaintext
# 提示词示例
|
||
|
||
在上一节中,我们介绍并给出了如何赋能大语言模型的基本示例。
|
||
|
||
在本节中,我们会提供更多示例,介绍如何使用提示词来完成不同的任务,并介绍其中涉及的重要概念。通常,学习概念的最佳方法是通过示例进行学习。下面,我们将通过示例介绍说明如何使用精细的提示词来执行不同类型的任务。
|
||
|
||
主题:
|
||
|
||
- [文本概括](#文本概括)
|
||
- [信息提取](#信息提取)
|
||
- [问答](#问答)
|
||
- [文本分类](#文本分类)
|
||
- [对话](#对话)
|
||
- [代码生成](#代码生成)
|
||
- [推理](#推理)
|
||
|
||
* * *
|
||
|
||
## 文本概括
|
||
|
||
自然语言生成的标准任务之一是文本概括。文本概括可能涉及到不同的风格和领域。事实上,语言模型最前景的应用场景之一就是能够快速概括出易于理解的文章大意和相关概念。 我们可以使用提示词尝试一个简单的概括任务。
|
||
|
||
假设我想了解抗生素的相关信息,我可以给出这样的提示:
|
||
|
||
_提示词_
|
||
|
||
```
|
||
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 研发人员也利用这种能力来构建强大的产品和体验。
|
||
|
||
Paragraph source: [ChatGPT: five priorities for research](https://www.nature.com/articles/d41586-023-00288-7)
|
||
|
||
* * *
|
||
|
||
## 问答
|
||
|
||
提高模型响应精确度的最佳方法之一是改进提示词的格式。 如前所述,提示词可以通过指令、上下文、输入和输出指示以改进响应结果。 虽然这些要素不是必需的,但如果您的指示越明确,响应的结果就会越好。 以下示例可以说明结构化提示词的重要性。
|
||
|
||
_提示词_
|
||
|
||
```
|
||
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](https://www.nature.com/articles/d41586-023-00400-x)。
|
||
|
||
* * *
|
||
|
||
## 文本分类
|
||
|
||
目前,我们已经会使用简单的指令来执行任务。 作为提示工程师,您需要提供更好的指令。 此外, 您也会发现,对于更负责的使用场景,仅提供指令是远远不够的。 所以,您需要思考如何在提示词中包含相关语境和其他不同要素。 同样,你还可以提供其他的信息,如`输入数据`和`示例`。
|
||
|
||
可以通过以下示例体验文本分类:
|
||
|
||
_提示词_
|
||
|
||
```
|
||
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
|
||
```
|
||
|
||
这时候你知道给出具体指令的重要性了吧?
|
||
|
||
* * *
|
||
|
||
## 对话
|
||
|
||
你可以通过提示工程进行更有趣的实验,比如指导大语言模型系统如何表现,指定它的行为意图和身份。 如果你正在构建客服聊天机器人之类的对话系统时,这项功能尤其有用。
|
||
|
||
比如,可以通过以下示例创建一个对话系统,该系统能够基于问题给出技术性和科学的回答。 你可以关注我们是如何通过指令明确地告诉模型应该如何表现。 这种应用场景有时也被称为_角色提示(Role Prompting)_。
|
||
|
||
_提示词_
|
||
|
||
```
|
||
The following is a conversation with an AI research assistant. The assistant tone is technical and scientific. // 以下是与 AI 助理的对话,语气应该专业、技术性强。
|
||
|
||
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. // 以下是与 AI 助理的对话。请给出易于理解的答案,最好是小学生都能看懂的那种。
|
||
|
||
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.
|
||
```
|
||
|
||
我想我们应该取得了一些进展, 你也可以继续改进它。 如果你再添加更多的示例,可能会得到更好的结果。
|
||
|
||
* * *
|
||
|
||
## 代码生成
|
||
|
||
大语言模型另外一个有效的应用场景是代码生成。 在此方面,Copilot 就是一个很好的示例。 你可以通过一些有效的提示词执行代码生成任务。 让我们来看一下下面的例子。
|
||
|
||
我们先用它写个简单的用户欢迎程序:
|
||
|
||
_提示词_
|
||
|
||
```
|
||
/*
|
||
询问用户的姓名并说“ Hello”
|
||
*/
|
||
```
|
||
|
||
_输出结果_
|
||
|
||
```
|
||
let name = prompt("What is your name?");
|
||
console.log(`Hello, ${name}!`);
|
||
```
|
||
|
||
你看,我们甚至都不需要指定要使用的编程语言。
|
||
|
||
来,我们再稍微升级一下。 下面的例子会向你展示提示词会让大语言模型变得多么强大。
|
||
|
||
_提示词_
|
||
|
||
```
|
||
"""
|
||
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 查询。
|
||
|
||
* * *
|
||
|
||
## 推理
|
||
|
||
目前对于大语言模型来说,推理任务算是最具有挑战性的了。 推理任务最让人兴奋的地方就是可以促使各种复杂的应用程序从大语言模型中诞生。
|
||
|
||
目前,涉及数学能力的推理任务已经有了一些改进。 对于当前的大型语言模型来说,执行推理任务可能会有一些难度,因此就需要更高级的提示词工程技术。 我们会在后面的指南中介绍这些高级技术。 现在,我们将介绍几个基本示例来展示算术功能。
|
||
|
||
_提示词_
|
||
|
||
```
|
||
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.
|
||
```
|
||
|
||
好多了吧? 顺便说一句,我试过几次,有时还是会失败。 如果你可以用示例给模型说明一下,可能会获得更准确的结果。
|
||
|
||
我们后面还会在本章节中介绍更多常见应用示例。
|
||
|
||
在后面的章节,我们将介绍更高级的提示工程概念和技术,以完成更困难任务。
|