近年来,语言模型得到了迅速改进,GPT-3 和 GPT-4 等大型语言模型 (LLM) 占据了中心舞台。这些模型因其以令人难以置信的技能执行各种任务的能力而变得流行。此外,随着这些模型的参数数量(数十亿!)增加,这些模型也意外地获得了新的能力。

在本文中,我们将探讨 LLM、它们可以执行的任务、它们的缺点以及各种提示工程策略。

什么是LLM?

LLM 是经过大量文本数据训练的神经网络。训练过程允许模型学习文本中的模式,包括语法、句法和单词关联。这些模型使用这些学习到的模式来生成类似人类的文本,使其成为自然语言处理 (NLP) 任务的理想选择。

哪些LLM可用?

有多种 LLM 可用,其中 GPT-4 最受欢迎。其他模型包括 LLaMA、PaLM、BERT 和 T5。每个模型都有其优点和缺点,其中一些是开放的,另一些是封闭的,只能通过 API 使用。

LLM的缺点

尽管表现出色,但法学硕士有一些局限性。一个显着的缺点是他们无法超越提示中提供的信息进行推理。此外,LLM 可以根据他们接受培训的数据生成有偏见的文本。控制 LLM 的输出也具有挑战性,因此有必要使用及时的工程策略来实现所需的输出。

您可以执行哪些任务?

我们可以通过格式化每个案例的提示来指导 LLM 为我们执行特定任务。以下是这些任务的列表以及每个任务的提示模板和示例。

文本摘要

LLM 可以生成长文本的摘要,从而更容易理解和消化内容。我们可以要求使用以下提示模板来总结一段文本:

Template

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt"><Full text>
Summarize the text above: / Explain the text above in <N> sentences:</code></span></span>

例如,我们可以要求使用以下提示来总结这段文本:

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">In a mechanical watch the watch's gears are turned by a spiral spring called a mainspring. In a manual watch energy is stored in the mainspring by turning a knob, the crown on the side of the watch. Then the energy from the mainspring powers the watch movement until it runs down, requiring the spring to be wound again.
A self-winding watch movement has a mechanism which winds the mainspring using the natural motions of the wearer's body. The watch contains an oscillating weight that turns on a pivot. The normal movements of the watch in the user's pocket (for a pocketwatch) or on the user's arm (for a wristwatch) cause the rotor to pivot on its staff, which is attached to a ratcheted winding mechanism. The motion of the watch is thereby translated into circular motion of the weight which, through a series of reverser and reducing gears, eventually winds the mainspring. There are many different designs for modern self-winding mechanisms. Some designs allow winding of the watch to take place while the weight swings in only one direction while other, more advanced, mechanisms have two ratchets and wind the mainspring during both clockwise and anti-clockwise weight motions.
Explain the text above in 2 sentences:</code></span></span>

给出以下输出:

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">A mechanical watch uses a mainspring to power the movement of the watch's gears. In a manual watch, the mainspring is wound by turning a knob, while a self-winding watch uses the natural motion of the wearer's body to wind the mainspring through an oscillating weight and ratcheted winding mechanism.</code></span></span>

信息提取

LLM可以从大量文本中识别和提取特定信息。这在新闻和商业智能等领域很有用。以下模板指示模型提取信息:

Template

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt"><Full text>
Mention X in the text above: / List X in the text above:</code></span></span>

例如,我们可以要求从以下文本中提取地名:

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">The earliest credible evidence for a successful design is the watch made by the Swiss watchmaker Abraham-Louis Perrelet, who lived in Le Locle. In late 1776 or early 1777, he invented a self-winding mechanism for pocket watches using an oscillating weight inside the watch that moved up and down. The Geneva Society of Arts, reporting on this watch in 1777, stated that 15 minutes walking was necessary to fully wind the watch.
List the names of places in the text above:</code></span></span>

输出是:

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Le Locle, Geneva.</code></span></span>

问答

LLM还可以根据提示中提供的信息回答问题。以下模板可用于指导 LLM 回答问题:

Template

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Answer the question based on the context below. <Other instructions>. Respond "Not sure" if not sure about the answer.
Context: <Context>
Q: <Question>
A:</code></span></span>

例如,使用以下提示:

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Answer the question based on the context below. Answer in a single sentence. Respond "Not sure" if not sure about the answer.
Context: An automatic watch, also known as a self-winding watch or simply an automatic, is a mechanical watch where the natural motion of the wearer provides energy to wind the mainspring, making manual winding unnecessary if worn enough. It is distinguished from a manual watch in that a manual watch must have its mainspring wound by hand at regular intervals.
Q: Do automatic watches need to be manually wound?
A:</code></span></span>

ChatGPT 回应:

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">No, automatic watches do not need to be manually wound if worn enough because the natural motion of the wearer provides energy to wind the mainspring.</code></span></span>

文本分类

LLM 可以将文本分类到预定义的类别中,例如情绪分析或垃圾邮件检测。您可以按如下方式格式化文本分类指令:

Template

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Classify the text into <List of choices>:
Text: <Full text>
Classification:</code></span></span>

例如,我们可以尝试根据其情绪是正面、中性还是负面来对文本进行分类:

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Classify the text into positive, neutral or negative:
Text: That shot selection was awful
Classification:</code></span></span>

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Negative</code></span></span>

对话

LLM 可以生成类似人类的对话,使它们在聊天机器人和客户服务应用程序中很有用。我们可以指示 LLM 以特定方式跟踪对话。

Template

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">The following is a conversation with <Actor>. The <Actor> should answer <Further instructions>.
Person: <Conversation>
<Actor>: <Response>
Person: <Conversation>
<Actor>:</code></span></span>

LLM 将填写我们指示的参与者类型的响应,我们可以通过将整个对话与新的响应再次传递给它来继续对话。例如:

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">The following is a conversation with a lawyer. The lawyer should answer simply and in layman terms.
Person: Hello, I would like to know how to dispute a parking ticket
Lawyer:</code></span></span>

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Sure, I can help. The first step is to check the ticket for any errors or mistakes. If you find any, you can use those as grounds to dispute the ticket. If there are no mistakes, you can still dispute the ticket by providing evidence to support your case, such as photos or witness statements. You can also request a hearing to present your case in person.</code></span></span>

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">The following is a conversation with a lawyer. The lawyer should answer simply and in layman terms.
Person: Hello, I would like to know how to dispute a parking ticket
Lawyer: Sure, I can help. The first step is to check the ticket for any errors or mistakes. If you find any, you can use those as grounds to dispute the ticket. If there are no mistakes, you can still dispute the ticket by providing evidence to support your case, such as photos or witness statements. You can also request a hearing to present your case in person.
Person: How do I request a hearing?
Lawyer:</code></span></span>

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">To request a hearing, you'll need to follow the instructions on the back of the ticket or on the website provided. Usually, you'll need to submit a written request within a certain timeframe, along with any evidence or supporting documents you have. Once your request is received, you'll be notified of the date, time, and location of your hearing.</code></span></span>

代码生成

LLM 可以生成代码,使开发人员更容易创建复杂的软件程序。我们可以写一个代码注释来描述我们希望代码做什么,LLM 将能够尽最大努力为我们做这件事。我们希望代码做的事情越复杂,我们就需要在提示注释中提供越具体和描述性的信息。

模板可能因我们要生成的编程语言而异,但我建议大致遵循此模板:

Template

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt"><Comment symbol of your language of choice (/* or ''' or #)>
Method: <Name of the method>
Language: <Programming language>
Description: <Description of what the method must do>
Parameters: <Parameters that the method will receive>
Context: <Further information needed to implement the method (URLs, databases, etc.)>
Output: <Output of the method>
<Comment symbol of your language of choice (/* or ''' or #)></code></span></span>

例如,如果我们想获取带有分页的 API,我们可以编写以下注释:

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">/* Method: fetchFirst5Pages
 * Language: Javascript
 * Description: Fetch the first 5 pages from this paginated API: http://example/pages/?n=x
 * Context: This API accepts the following URL parameters:
 *   n: the number of the page to fetch
 * Output: the content of the first 5 pages
 */</code></span></span>

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Here's an example implementation of the fetchFirst5Pages method in JavaScript, assuming the use of the fetch function:

async function fetchFirst5Pages() {
  const baseUrl = 'http://example/pages/?n=';
  const pages = [];

  for (let i = 1; i <= 5; i++) {
    const url = baseUrl + i;
    const response = await fetch(url);

    if (!response.ok) {
      throw new Error(`Error fetching page ${i}`);
    }

    const pageData = await response.json();
    pages.push(pageData);
  }

  return pages;
}

This function uses a loop to fetch the first 5 pages from the API by appending the page number to the base URL. It checks if the response from the server is okay and throws an error if it's not. Then, it parses the JSON data from the response and adds it to an array of pages. Finally, it returns the array of pages.</code></span></span>

推理

LLM可能难以完成一些基本任务,例如算术问题。但是,如果我们要求 LLM 根据其响应进行推理,我们可以帮助它找到正确的答案。这是我推荐的模板:

Template

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Q: <Question>
A: Let's work this out in a step by step way to be sure we have the right answer. First, <Instructions>.</code></span></span>

在这个例子中,我们可以看到,如果我们不加推理地提出一个简单的算术问题,它就会失败:

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Answer the following question. Respond "Not sure" if not sure about the answer.
Q: The sum of prime numbers in the following list are a multiple of 3: [2, 4, 6, 7, 9, 14]
A: [Yes or No]</code></span></span>

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">No. The sum of prime numbers in the list [2, 4, 6, 7, 9, 14] is not a multiple of 3. Therefore, the answer is "No".</code></span></span>

但是,如果我们指示 LLM 对其答案进行推理,我们会得到截然不同的结果:

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Q: The sum of prime numbers in the following list are a multiple of 3: [2, 4, 6, 7, 9, 14]
A: Let's work this out in a step by step way to be sure we have the right answer. First, we find the prime numbers in the list and add them together. Then we check whether the resulting number can be divided by 3.</code></span></span>

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Sure, let's work this out step by step.

The prime numbers in the list are 2 and 7. So we can add them together:

2 + 7 = 9

The resulting number is 9. Now we need to check whether it is a multiple of 3. We can do this by checking if the remainder of 9 divided by 3 is equal to 0:

9 % 3 = 0

Since the remainder is 0, we know that 9 is indeed a multiple of 3.

Therefore, the answer is "Yes", the sum of prime numbers in the list [2, 4, 6, 7, 9, 14] is a multiple of 3.</code></span></span>

攻略

虽然这些模板将帮助您充分发挥 LLM 的潜力以满足您的需求,但有时它们仍然难以完成某些复杂的任务。已经开发了一些技术来帮助 LLM 获得满意的答案。到目前为止,我们只使用了零样本提示,我们现在将对其进行定义。但是,我们可以使用更复杂的技术。

零次提示

我们一直在对本文中的所有示例使用零样本提示。零样本提示意味着向 LLM 传递一个提示,只包含我们希望它回答的问题,而没有任何示例说明它在其他情况下应该如何响应。例如,在之前的例子中我们做了:

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Classify the text into positive, neutral or negative:
Text: That shot selection was awful
Classification:</code></span></span>

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Negative</code></span></span>

对于模型来说,这可能不足以准确回答我们想要的结果。让我们看看我们有什么选择。

小提示

与零样本提示相反,使用少样本提示,我们会​​在我们提出问题之前给 LLM 一些它应该如何回应的例子。例如,假设我们要像前面的示例一样对文本的情感进行分类。但我们希望它每次都能以非常具体的格式给我们答案。我们可以像这样预先给它举几个例子:

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Classify the text into positive, neutral or negative:
Text: Today the weather is fantastic
Classification: Pos
Text: The furniture is small.
Classification: Neu
Text: I don't like your attitude
Classification: Neg
Text: That shot selection was awful
Classification:</code></span></span>

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Neg</code></span></span>

思维链提示

对于更复杂的任务,我们可以将小样本提示与让模型推理其答案相结合。我们可以使用与之前相同的示例来查看推理和小样本提示如何影响结果:

Prompt

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">Q: The sum of prime numbers in the following list are a multiple of 3: [2, 4, 6, 7, 9, 14]
A: The prime numbers in the list are 2 and 7. Their sum is 9. 9 % 3 = 0. The answer is yes.
Q: The sum of prime numbers in the following list are a multiple of 3: [1, 2, 4, 7, 8, 10]
A: The prime numbers in the list are 1, 2 and 7. Their sum is 10. 10 % 3 = 1. The answer is no.
Q: The sum of prime numbers in the following list are a multiple of 3: [1, 2, 3, 4]
A: The prime numbers in the list are 1, 2 and 3. Their sum is 6. 6 % 3 = 0. The answer is yes.
Q: The sum of prime numbers in the following list are a multiple of 3: [5, 6, 7, 8]
A:</code></span></span>

Output

<span style="background-color:#272822"><span style="color:#f8f8f2"><code class="language-txt">The prime numbers in the list are 5 and 7. Their sum is 12. 12 % 3 = 0. The answer is yes.</code></span></span>

最后说明

LLM彻底改变了自然语言处理领域,但要最大限度地发挥其潜力,及时工程至关重要。通过了解 LLM 可以执行的任务、它们的缺点以及各种及时的工程策略,开发人员可以利用 LLM 的力量来创建创新和有效的解决方案。在不久的将来可能会开发出更多的策略和技术,因此请关注该领域的进一步发展,以最大限度地发挥 LLM 的潜力。此外,随着 LLM 继续变得越来越大,增加了数十亿个参数,我们现在甚至无法想象的更多任务很可能会成为可能。想想使用这些新工具可以做什么,以及它们将来会为我们服务哪些用例,真是令人惊叹。

更多推荐

最大限度地发挥LLM的潜力:快速工程指南