首页 / 资料库 / 提示工程指南

资料库14 分钟读完MIT提示词示例文本摘要信息抽取文本分类对话代码生成推理

提示词的基本用法

译自《Basic Prompting》 · 查看英文原文

原文出处Basic Prompting 原作者:DAIR.AI · Elvis Saravia · 许可证:MIT License 中文译本由诸葛AI学院整理,仅供学习参考,版权归原作者与 DAIR.AI 所有。

提示词的基本用法

上一篇指南介绍了提示词,给了一个基础示例。

这一篇给出更多使用提示词的例子,并引入几个关键概念,后面的进阶指南会建立在这些概念之上。

学概念最好的办法往往是看例子。下面几组示例展示的是:一条写得好的提示词,能完成各种各样有意思的任务。

本篇目录:

  • 文本摘要
  • 信息抽取
  • 问答
  • 文本分类
  • 对话
  • 代码生成
  • 推理
  • Python 笔记本

文本摘要

文本摘要是自然语言生成的标准任务之一,横跨很多种类和领域。语言模型最有前景的应用之一,就是把文章和概念压缩成简短、易读的摘要。我们用提示词试一个基础摘要任务。

假如你想了解抗生素(antibiotics),可以试这样的提示词:

提示词 ``` Explain antibiotics

A: ```

这段提示词要求模型解释"抗生素是什么",末尾的 "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:" 是问答场景常用的一种显式提示格式,我在这里用它暗示模型:后面该给出答案了。这个例子里,写 "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,natural language processing)任务。

下面是一条从给定段落里抽取信息的提示词示例:

提示词 ``` 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: five priorities for research


问答

想让模型给出针对性的回答,最好的办法之一是改进提示词的格式。前面讲过,提示词可以把指令、上下文、输入和输出指示组合起来,拿到更好的结果。这些成分不是必需的,但组合起来是个好习惯:指令写得越具体,结果越好。下面是一条结构更完整的提示词长什么样:

提示词 ``` Answer the question based on the context below. Keep the answer short. 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: ```

指令要求"根据下文回答,答案要短,没把握就回 'Unsure about answer'",上下文是一段关于 Teplizumab 药物历史的文字,问题问 OKT3 最初提取自什么动物。

输出 Mice.

Mice,即小鼠。模型按"短答案"的要求只回了一个词。

上下文来自 Nature


文本分类

到目前为止,我们都只用简单指令来完成任务。身为提示工程师(prompt engineer),你得学会把指令写得更好。但这还不够:碰上更难的使用场景,光有指令不够用,你还得琢磨上下文,琢磨一条提示词里能用的各种成分,比如 input data(输入数据)和 examples(示例)。

用文本分类来演示一下。

提示词 ``` Classify the text into neutral, negative or positive.

Text: I think the food was okay. Sentiment: ```

要求把文本分成中性、负面、正面三类,"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

很好!这次模型返回 neutral,正是我要的那种写法。看来提示词里的那个示例帮模型把输出对准了。为了说明"具体"为什么重要,看下面这个例子,找出其中的问题:

提示词 ``` Classify the text into nutral, negative or positive.

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

输出 Neutral

问题出在哪?


对话

提示工程(prompt engineering)能做到的更有意思的事之一,是给 LLM 系统规定行为方式、意图和身份。搭建客服机器人这类对话系统时,这招特别有用。

比如,造一个对话系统,要求它回答技术问题时走学术路线。注意我们是怎么通过指令明确告诉它该有什么样表现的,这种手法有时被称为角色提示(role prompting)

提示词 ``` 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: ```

开头两行给 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" */

注释里写着需求:问用户的名字,然后说 "Hello"。

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

可以看到,我们连用哪种语言都没指定,模型自己选了 JavaScript。

再升一级。我想让你见识一下:在提示词设计上多花一点心思,LLM 能有多强。

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

给出两张表的结构(departments 和 students 及各自字段),要求生成一条查出计算机系全体学生的 MySQL 查询。

输出 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.

好多了,对吧?顺便说一句,这个例子我试了几次,系统有时还是会出错。把指令写得更好、再配上示例,准确率还能再往上走。

本指南这一节会继续补充更多常见应用的例子。

接下来的几篇指南会讲更多进阶的提示工程概念,用来改善模型在上述任务以及更难任务上的表现。


Python 笔记本

说明 笔记本
学习用 openaiLangChain 库完成各种常见任务 Getting Started with Prompt Engineering

上一节:提示词导论

下一节:进阶提示技巧

这篇在讲什么,跟咱们的课怎么对?

资料库是大厂公开教材的中文译本,偏原理和工程做法。想看面向中小企业的白话版本,去入门课场景课