近年来,大型语言模型的发展使得自然语言处理领域得到了极大的发展。这些模型,如GPT-3、BERT等,可以处理自然语言生成、阅读理解、文本分类等任务。然而,尽管这些模型能够处理各种自然语言任务,但是它们仍然需要正确的输入才能准确地回答问题。正确向大模型提问,不仅可以提高模型的准确度,还能使得我们更好地理解和利用这些模型。因此,正确向大语言模型提问变得越来越重要。基于目前的AI技术,考验的不再是人们解决问题的能力,而是提问能力。

Prompt engineering (PE) 是与 AI 进行有效沟通已实现预期结果的过程,Prompt engineering (PE) 技术可以应用于各种各样的任务,使其成为任何寻求提高日常和创新活动效率的人的有用工具。越来越多的人将成为Prompt engineer。

提示过程 (Prompting)

如何向AI大模型正确提问?_ai

指导人工智能执行任务的过程称为提示过程 (Prompting)¹。我们向 AI 提供一组指令(提示Prompt),然后它执行任务。提示可以简单到一个问题,也可以复杂到多个段落,如文章摘要、解决数学问题等

指令提问

如何向AI大模型正确提问?_agi_02

最简单的提示方法之一就是直接给出指令。比如我们可以给出指令

Prompt:

A user has input their first and last name into a form. We don't know in which order 
their first/last name is, but we need it to be in the format 'Last, First'. Convert the following:

john doe
  • 1.
  • 2.
  • 3.
  • 4.

Output:

Doe, John
  • 1.

给出明确的指令,AI就能很容易的解决该问题。我们还可以提问更复杂的问题,比如以下例子

Prompt:

请阅读以下销售邮件。删除任何可用于识别个人身份的信息 (PII),并用相应的占位符替换它。例如,用"[姓名]"替换"John Doe"。

你好 John,

我写信给你是因为我注意到你最近买了一辆新车。我是一家当地经销商 (Cheap Dealz) 的销售员,我想让你知道我们有一辆新车的超值优惠。如果你有兴趣,请告诉我。

谢谢,

Jimmy Smith

电话:
电子邮件:
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Output:

你好 [姓名],

我写信给你是因为我注意到你最近买了一辆新车。我是一家当地经销商 ([经销商名称]) 的销售员,我想让你知道我们有一辆新车的超值优惠。如果你有兴趣,请告诉我。

谢谢,

[销售员姓名]

电话:[电话号码]
电子邮件:[电子邮件地址]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

可以看到模型从我们给出指令中推断出正确的输出。例如,它知道将Cheap Dealz替换为[经销商名称],将Jimmy Smith替换为[销售员姓名],即使我们没有明确告诉它这样做。

角色提示提问

如何向AI大模型正确提问?_人工智能_03

另一种提示(Prompt)技术是给 AI 分配一个角色。例如,您的提示可以以"你是一名医生"或"你是一名律师"开始,然后要求 AI 回答一些医学或法律问题。举个例子:

Prompt:

你是一个能解决世界上任何问题的杰出数学家。
试着解决下面的问题:

100*100/400*56 是多少?
  • 1.
  • 2.
  • 3.
  • 4.

Output:

答案是 1400。
  • 1.

显然答案是正确的,但是如果 AI 只是被提示100100/40056等于几?,它可能会回答280(错误)。

通过为 AI 分配一个角色,我们给它提供了一些上下文。这个上下文有助于 AI 更好地理解问题。通过更好地理解问题,AI 往往可以给出更好的答案。

Note:目前可以最新的AI大模型,直接提问可能已经可以得到正确答案

多范例提示问题

如何向AI大模型正确提问?_ide_04

另一个提示策略是_多范例提示(few shot prompting)_ , 这种策略将为模型展示一些例子(shots),从而更形象地描述你的需求。

在上图的例子中,我们尝试对用户反馈进行正面(positive)或反面(negative)的分类。我们向模型展示了 3 个例子,然后我们输入一个不在例子里面的反馈(It doesnt work!:)。模型发现头三个例子都被分类到 positive 或者 negative ,进而通过这些信息将我们最后输入的反馈分类到了 negative

我们如何结构化地设计例子也是非常重要的。由于我们已经将头三个例子结构化成: input: classification,因此模型最终也跟着同样只输出分类的结果,而不是一段完整的句子,例如 this review is positive

Prompt:

Great product, 10/10: positive
Didn't work very well: negative
Super helpful, worth it: positive
It doesnt work!:
  • 1.
  • 2.
  • 3.
  • 4.

Output:

negative
  • 1.

多范例提示的一个关键场景是当我们需要以特定的结构输出结果,但是又很难向模型进行描述的时候。为了理解这一点,让我们看这样一个例子:假设您需要通过分析当地的报纸文章来汇编附近城镇公民的姓名和职业。您希望模型读取每篇文章并以Last, First [OCCUPATION]格式输出姓名和职业列表。为了让模型做到这一点,您可以向它展示一些示例:

Prompt:

In the bustling town of Emerald Hills, a diverse group of individuals made their mark. Sarah Martinez, a dedicated nurse, was known for her compassionate care at the local hospital. David Thompson, an innovative software engineer, worked tirelessly on groundbreaking projects that would revolutionize the tech industry. Meanwhile, Emily Nakamura, a talented artist and muralist, painted vibrant and thought-provoking pieces that adorned the walls of buildings and galleries alike. Lastly, Michael O'Connell, an ambitious entrepreneur, opened a unique, eco-friendly cafe that quickly became the town's favorite meeting spot. Each of these individuals contributed to the rich tapestry of the Emerald Hills community.
1. Sarah Martinez [NURSE]
2. David Thompson [SOFTWARE ENGINEER]
3. Emily Nakamura [ARTIST]
4. Michael O'Connell [ENTREPRENEUR]

At the heart of the town, Chef Oliver Hamilton has transformed the culinary scene with his farm-to-table restaurant, Green Plate. Oliver's dedication to sourcing local, organic ingredients has earned the establishment rave reviews from food critics and locals alike.

Just down the street, you'll find the Riverside Grove Library, where head librarian Elizabeth Chen has worked diligently to create a welcoming and inclusive space for all. Her efforts to expand the library's offerings and establish reading programs for children have had a significant impact on the town's literacy rates.

As you stroll through the charming town square, you'll be captivated by the beautiful murals adorning the walls. These masterpieces are the work of renowned artist, Isabella Torres, whose talent for capturing the essence of Riverside Grove has brought the town to life.

Riverside Grove's athletic achievements are also worth noting, thanks to former Olympic swimmer-turned-coach, Marcus Jenkins. Marcus has used his experience and passion to train the town's youth, leading the Riverside Grove Swim Team to several regional championships.
1. Oliver Hamilton [CHEF]
2. Elizabeth Chen [LIBRARIAN]
3. Isabella Torres [ARTIST]
4. Marcus Jenkins [COACH]

Oak Valley, a charming small town, is home to a remarkable trio of individuals whose skills and dedication have left a lasting impact on the community.

At the town's bustling farmer's market, you'll find Laura Simmons, a passionate organic farmer known for her delicious and sustainably grown produce. Her dedication to promoting healthy eating has inspired the town to embrace a more eco-conscious lifestyle.

In Oak Valley's community center, Kevin Alvarez, a skilled dance instructor, has brought the joy of movement to people of all ages. His inclusive dance classes have fostered a sense of unity and self-expression among residents, enriching the local arts scene.

Lastly, Rachel O'Connor, a tireless volunteer, dedicates her time to various charitable initiatives. Her commitment to improving the lives of others has been instrumental in creating a strong sense of community within Oak Valley.

Through their unique talents and unwavering dedication, Laura, Kevin, and Rachel have woven themselves into the fabric of Oak Valley, helping to create a vibrant and thriving small town.
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

Output:

1. Laura Simmons [FARMER]
2. Kevin Alvarez [DANCE INSTRUCTOR]
3. Rachel O'Connor [VOLUNTEER]
  • 1.
  • 2.
  • 3.

通过向模型展示正确的输出格式示例,它就能够为新的文章生成正确的输出结果。

Note: 尽管我们可以通过使用指令提示符的方式来生成相同的输出,但多范例提示在输出结果的一致性上会更好。

不同类型的范例提示

单词 shot 在该场景下与 example(范例) 一致。除了多范例提示(few-shot prompting)之外,还有另外两种不同的类型。它们之间唯一的区别就是你向模型展示了多少范例。

类型:

  • 无范例提示(0 shot prompting): 不展示范例
  • 单范例提示(1 shot prompting): 只展示 1 条范例
  • 多范例提示(few shot prompting): 展示 2 条及以上的范例
  1. 无范例提示
    无范例提示是最基本的提示形式。它仅仅是向模型展示提示信息,没有提供任何示例,并要求其生成回答。因此,你到目前为止看到的所有指令和角色提示都属于无范例提示。无范例提示的另一个例子是:
Add 2+2:
  • 1.

这是无范例提示,因为我们没有向模型展示任何完整的示例。

  1. 单范例提示
    单范例提示是向模型展示一个示例。例如:
Add 3+3: 6
Add 2+2:
  • 1.
  • 2.

我们仅向模型展示了一个完整的示例(“Add 3+3: 6”),因此这是一个单范例提示。

  1. 多范例提示
    多范例提示是向模型展示2个或更多示例。例如:
Add 3+3: 6
Add 5+5: 10
Add 2+2:
  • 1.
  • 2.
  • 3.

这是我们向模型展示了至少2个完整的示例(“Add 3+3: 6”和“Add 5+5: 10”)。通常,展示给模型的示例越多,输出结果就越好,因此在大多数情况下,多范例提示比另外两种提示更受欢迎。

结论

多范例提示是让模型生成准确且格式正确的输出的强大技术!

组合提示提问

如何向AI大模型正确提问?_agi_05

前面我们介绍了三种独立的提示提问类别,指令提问、角色提示提问、多范例提��问题,将这些不同的提问技巧组合起来可以产生更强大提示。

以下是一个包含指令、角色以及多示例提示的例子:

Prompt:

Twitter是一个社交媒体平台,用户可以发布称为“推文”的短消息。推文可以是积极的或消极的,我们希望能够将推文分类为积极或消极。以下是一些积极和消极推文的例子。请确保正确分类最后一个推文。

Q: 推文: "今天真是美好的一天!"
这条推文是积极的还是消极的?

A: 积极的

Q: 推文: 我讨厌这个班级"
这条推文是积极的还是消极的?

A: 消极的

Q: 推文: "我喜欢牛仔裤上的口袋"

A:
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

通过这种组合提示提问,我们通常可以提高AI在不同任务上的表现。

总结

一共介绍4种基础的向AI大模型的提问题方式

  • 指令提问
  • 角色提示提问
  • 多范例提示问题
  • 组合提示提问

各种提问方式需要结合具体应用,来选择适合的提问方式,大家可以多进行练习,学习用AI的方式去学习和利用AI大模型。