发挥LLM大语言模型的潜力:快速工程指南

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

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


LLM是什么?

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


哪些LLM可用?

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


LLM的缺点

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


可以执行哪些任务?

以下是这些任务的列表以及每个任务的示例。

LLM 可以生成长文本的摘要,使用提示模板来总结一段文本:


Summarize the text above: / Explain the text above in  sentences:

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

Prompt

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:

给出以下输出:

Output

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.

从大量文本中识别和提取特定信息。这在新闻和商业等领域很有用。


Mention X in the text above: / List X in the text above:

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

Prompt

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:

输出是:

Output

Le Locle, Geneva.

根据提示中提供的信息回答问题。

Template

Answer the question based on the context below. . Respond "Not sure" if not sure about the answer.
Context: 
Q: 
A:

Prompt

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:

ChatGPT 回应:

Output

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.

LLM 可以将文本分类到预定义的类别中,例如情绪分析或垃圾邮件检测。

Template

Classify the text into :
Text: 
Classification:

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

Prompt

Classify the text into positive, neutral or negative:
Text: That shot selection was awful
Classification:

Output

Negative

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

Template

The following is a conversation with . The  should answer .
Person: 
: 
Person: 
:

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

Prompt

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:

Output

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.

Prompt

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:

Output

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.

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

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

Template


Method: 
Language: 
Description: 
Parameters: 
Context: 
Output: 

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

Prompt

/* Method: fetchFirst5Pages
 * Language: Javascript
 * Description: Fetch the first 5 pages from this paginated API: http://example.com/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
 */

Output

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.com/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.

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

Template

Q: 
A: Let's work this out in a step by step way to be sure we have the right answer. First, .

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

Prompt

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]

Output

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".

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

Prompt

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 pided by 3.

Output

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 pided 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.

攻略

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

零次提示

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

Prompt

Classify the text into positive, neutral or negative:
Text: That shot selection was awful
Classification:

Output

Negative

对于模型来说,这可能不足以准确回答我们想要的结果。那我们还有别的办法吗?


小提示

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

Prompt

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:

Output

Neg

思维链提示

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

Prompt

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:

Output

The prime numbers in the list are 5 and 7. Their sum is 12. 12 % 3 = 0. The answer is yes.

最后说明

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

展开阅读全文

页面更新:2024-05-14

标签:模型   示例   样本   潜力   例子   缺点   文本   模板   答案   提示   语言   快速   代码   指南   工程

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top