Examples
summarization / extraction / Q&A 等 prompt 示例合集
上一节介绍了如何 prompting LLM 的基本示例。
在本节中,我们会提供更多示例,介绍如何使用
prompt主题:
#文本概括
自然语言生成中的一个标准任务是 text summarization。它可以涵盖许多不同的风格和领域。LLM 最有前景的应用之一就是将文章和概念概括成简洁易读的摘要。让我们尝试使用 prompt 做一个基本的 summarization task。
Prompt:
codeExplain antibiotics A:
Output:
codeAntibiotics 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:现在假设我们感觉模型给了太多信息,想要进一步提炼它。我们可以指导模型用一句话总结相关内容:
Prompt:
codeAntibiotics 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:
可理解为 “用一句话解释上面的信息”。codeExplain the above in one sentence
Output:
codeAntibiotics 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.
在不过多关注上面输出的准确性的情况下(这会在后续章节讨论),模型尝试将段落总结成一句话。你可以对 instruction 进行更精细的设计,也可以停下来自行实验,看看能否获得更好的结果。
#信息提取
虽然 LLM 主要被训练用于自然语言生成及相关任务,但它同样非常擅长 classification 以及一系列其他 NLP task。
这是一个从给定段落中提取信息的 prompt 示例:
Prompt:
codeAuthor-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:
Output:
codeThe large language model based product mentioned in the paragraph above is ChatGPT.
现在应该很明显,你可以通过简单指示模型做什么来让它执行不同的任务。这是一个强大的能力,AI 产品开发人员已经在利用它来构建强大的产品和体验。
段落来源:https://www.nature.com/articles/d41586-023-00288-7
#问答
让模型给出具体回答的最佳方法之一是改善 prompt 的格式。如前所述,一个 prompt 可以结合 instruction、context、input data 和 output indicator 来获得更好的结果。虽然这些组成部分不是必需的,但随着你给出的 instruction 越具体,你将获得越好的结果。下面是一个例子,展示一个更结构化的 prompt 会是什么样子:
Prompt:
codeAnswer 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:
Output:
codeMice.
上下文来源:https://www.nature.com/articles/d41586-023-00400-x
#文本分类
到目前为止,你已经使用了简单 instruction 来执行任务。作为 prompt engineer,你需要更擅长提供更好的 instruction。但这还不是全部:在更困难的用例中,仅仅提供 instruction 并不够,这时你需要更多考虑 context 和其他元素(比如 input data 或 examples)。
让我们通过一个文本分类的示例来演示这一点。
Prompt:
codeClassify the text into neutral, negative or positive. Text: I think the food was okay. Sentiment:
Output:
codeNeutral
你给出了对文本进行 classification 的 instruction,模型以
'Neutral'neutralNeutral有几种不同的方法可以做到这一点。这里你关心的是 specificity:你给 prompt 提供的信息越多,结果就越可控。你可以尝试提供 examples 来指定正确的行为。让我们再试一次:
Prompt:
codeClassify 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:
codeneutral
完美:这次模型返回了
neutral为了强调有时 specificity 为什么很重要,请查看下面的例子并找出问题所在:
Prompt:
codeClassify the text into nutral, negative or positive. Text: I think the vacation is okay. Sentiment:
Output:
codeNeutral
这里的问题是什么?作为 prompt,模型完全忽略了虚构的
nutralNeutralnutral#对话
你可以通过 prompt engineering 做更有趣的实验,比如指导 LLM 应该如何表现,指定它的行为意图和身份。这在构建对话系统(例如 customer support chatbot)时尤其有用。
比如,可以通过以下示例创建一个对话系统,该系统能够基于问题给出技术性和科学的回答。你可以关注我们是如何通过 instruction 明确告诉模型应该如何表现。这种应用场景有时也被称为 role prompting。
Prompt:
codeThe 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:
codeSure! 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 research assistant 听起来有点太技术性了。我们改变这种行为,并指示系统给出更易于理解的回答:
Prompt:
codeThe 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:
codeSure! 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.
如果你再添加更多 examples,可能会得到更好的结果。
#代码生成
LLM 在 code generation 方面非常有效。通过巧妙的 prompt,你可以执行大量 code generation tasks。下面是几个示例。
我们先让它写一个简单的用户欢迎程序:
Prompt:
code/* Ask the user for their name and say "Hello" */
Output:
codelet name = prompt("What is your name?"); console.log(`Hello, ${name}!`);
你看,我们甚至都不需要指定要使用的编程语言。
我们再升级一下。下面的示例展示了通过稍微多一点的 prompt design,LLM 可以变得多么强大:
Prompt:
code""" Table departments, columns = [DepartmentId, DepartmentName] Table students, columns = [DepartmentId, StudentId, StudentName] Create a MySQL query for all students in the Computer Science Department """
Output:
codeSELECT StudentId, StudentName FROM students WHERE DepartmentId IN (SELECT DepartmentId FROM departments WHERE DepartmentName = 'Computer Science');
在这种情况下,你提供了关于 database schema 的 data,并要求它生成一个有效的 MySQL query。
#推理
也许,当今 LLM 面临的最困难任务之一是 reasoning tasks。推理是最具吸引力的领域之一,因为有了推理,就可以从 LLM 中涌现出各种复杂的应用类型。
目前,涉及数学能力的 reasoning task 已经有了一些改进。对于当前的 LLM 来说,执行 reasoning task 仍可能有一些难度,因此需要更高级的 prompt engineering techniques。这里先看几个基础示例,展示 arithmetic ability。
Prompt:
codeWhat is 9,000 * 9,000?
Output:
code81,000,000
我们加大难度:
Prompt:
codeThe odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. A:
Output:
codeNo, the odd numbers in this group add up to an odd number: 119.
这不对。我们用更明确的 instruction 去改进它:
Prompt:
codeThe 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:
codeOdd numbers: 15, 5, 13, 7, 1 Sum: 41 41 is an odd number.
效果好多了。顺便提一下,我们尝试了几次这个任务,模型有时仍会失败。如果你提供更好的 instruction 并结合 examples,可能有助于获得更准确的结果。
#Notebook
如果你想使用 Python 练习上述 prompts,可以参考这个 notebook: