prompts-基础提示


文档摘要

基础提示 在上一个指南中,我们介绍了提示并给出了一个基本示例。 在这个指南中,我们将提供更多的提示使用示例,并介绍一些对更高级指南来说很重要的关键概念。通常,通过示例学习概念是最好的方法。下面我们将涵盖几个如何利用精心设计的提示来执行各种有趣且不同的任务的例子。 主题: 文本摘要 信息提取 问答 文本分类 对话 代码生成 推理 Python 笔记本 文本摘要 自然语言生成中最标准的任务之一就是文本摘要。文本摘要可以包括许多不同的风格和领域。语言模型最有前景的应用之一是能够将文章和概念总结成简洁易读的摘要。让我们尝试一个基本的摘要任务。 假设我对抗生素感兴趣,我可以用这样的提示: 提示: 输出: "A:" 是一种在问答中使用的显式提示格式。我在这里使用它是为了告诉模型有一个预期的后续内容。

基础提示

在上一个指南中,我们介绍了提示并给出了一个基本示例。

在这个指南中,我们将提供更多的提示使用示例,并介绍一些对更高级指南来说很重要的关键概念。通常,通过示例学习概念是最好的方法。下面我们将涵盖几个如何利用精心设计的提示来执行各种有趣且不同的任务的例子。

主题:

文本摘要

自然语言生成中最标准的任务之一就是文本摘要。文本摘要可以包括许多不同的风格和领域。语言模型最有前景的应用之一是能够将文章和概念总结成简洁易读的摘要。让我们尝试一个基本的摘要任务。

假设我对抗生素感兴趣,我可以用这样的提示:

提示:

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:研究的五大优先事项

问答

让模型响应特定答案的最佳方式之一是改进提示的格式。正如之前所提到的,提示可以结合指令、上下文、输入和输出指示符以获得更好的结果。虽然这些组件不是必需的,但随着你提供的指令越具体,结果越好。下面是一个遵循更结构化提示的示例。

提示:

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:

输出:

Mice.

上下文来自Nature

文本分类

到目前为止,我们使用简单的指令来完成任务。作为一名提示工程师,你需要更好地提供指令。但这还不够!你会发现对于更复杂的用例,仅仅提供指令是不够的。这时你需要更多地考虑上下文和提示中可以使用的不同元素。其他可以提供的元素包括输入数据 or 示例

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

提示:

Classify the text into neutral, negative or positive. Text: I think the food was okay. Sentiment:

输出:

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. So instead of 中立 we want it to return 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

这里的问题是什么?

对话

也许你能通过提示工程实现的最有趣的事情之一是指导大型语言模型系统如何表现、它的意图和身份。当你构建像客户服务聊天机器人这样的对话系统时,这一点尤为重要。

例如,让我们创建一个能够生成更具技术性和科学性回答的对话系统。请注意,我们是如何通过指令明确告知其如何表现的。这有时被称为“角色提示”。

提示:

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.

更好了吧?顺便说一句,我尝试了几次,系统有时会失败。如果你提供更好的指令并结合示例,可能有助于获得更准确的结果。

我们将在本指南的这一部分继续包含更多常见的应用示例。

在即将发布的指南中,我们将涵盖更多高级的提示工程技术,以改善所有这些任务及更复杂任务的表现。

Python 笔记本

描述 笔记本
了解如何使用 openai and LangChain 库执行许多不同类型的基本任务 开始提示工程

上一节(提示简介)

下一节(高级提示技术)

声明:
本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。我们不对因使用此翻译而产生的任何误解或误译负责。


发布者: 作者: 转发
评论区 (0)
U