服务粉丝

我们一直在努力
当前位置:首页 > 财经 >

【第2906期】Prompt Engineering: 循循善诱

日期: 来源:前端早读课收集编辑:LMJ1995

前言

有了 AI,将来交互可能会有另一种形式了。今日前端早读课文章由 @LMJ1995 翻译授权转载分享。

正文从这开始~~

自从 GPT3 问世以来,“预训练 + 微调” 的模式被广泛应用在各类大模型的下游任务中 --- 而这套模式最突出的问题在于微调的代价还是太高,对于中小型企业来讲,技术难度和成本都太大,不仅需要大量的数据去进行支持,同时最终的效果也是难以保证的。

【第2892期】用 JavaScript,五分钟将 Siri 接入 ChatGPT

Prompt engineering 的技术日趋成熟,提供了解决这种问题的另一个思路,本文是翻译一篇国外的关于 Prompt Learning 的一篇文章,就关于手工设计 prompt 以及实际的一些测试结果提供参考,译者旨在借以此文从工业界的角度,而非学术界的角度解读 prompt engineering 的实际应用场景。

Prompt Engineering

在接触任何一个新概念的时候,很多人可能有说文解字的强迫症 ---engineering 倒是好说,prompt 在中文语境下作何理解比较好呢?

直接翻译的话,prompt 可以被理解为 “指示,提示”,但是所谓 “指示工程” 或者 “提示工程” 这种字眼也是不知所云,与其死抠字眼,不如通过下图理解 prompt engineering 背后的逻辑:简单的来说,prompt 其实就是一种对于特定任务去精心构造的输入。

在传统的下游任务微调中,如果我们希望构造一个文本摘要的任务,那么我们需要标记相当规模的样本(原始文本 + 对应的摘要 label),重新训练 Language Model,并微调得到一个文本摘要的新模型,模型输入是待摘要的文本,输出是摘要后的文本。

而有了 prompt engineering,一种更加简单、轻量且易于泛化的策略是,我们只需要构造一个特定任务的 prompt 就可以了。还是以文本摘要的场景为例,我们仅需要设定特定的 prompt,比如原始输入时待摘要的文本 X,那么我们构造这个任务下的 prompt 为 [x].In summary,随后便可以直接通过 Language Model 获得结果。

【第2893期】ChatGPT 提示模式:提高代码质量、重构、需求获取和软件设计

Try multiple formulations of your prompt to get the best generations

在 generation task 中,尝试多种不同的 prompts 来解决问题通常是非常有帮助的。

对于人类来讲,同一个 prompt 的不同表达方式区别并不大,但对于模型来说,最终得到的 generation 可能千差万别 --- 在模型执行训练的数据中,不同表达可能来自于不同的文本语境并用于不同目的,最终的结果往往是非常难以预测的。

比如在上面的这个文本摘要的任务,我们可以尝试

  • In summary, ...

  • To summarize in plain language, ...

  • The main point to take from this article is that ...

预测 prompt 的不同表达方式产生的结果是比较困难的,但是尽管如此,事实上也还是有一些手段可以在手工漫无目的的尝试 prompt 之外,提供具体 prompt 改进的方向和参考,本文中不过多展示,可以参考文末链接的相关文章自行查阅。

Describe the task and the general setting

在输入结尾提供特定的任务描述固然是一种 prompt 的设计思路,但 prompt 的设计确不止如此。在整个输入的前后添加任务的更多描述和信息通常也是极其有帮助的 --- 还是对于上面的文本摘要任务,我们甚至可以进一步来添加更多细节

我们这里考略一些更复杂的场景,比如智能问答中,我们希望模型能够为顾客提供通情达理而又彬彬有礼的详细服务,客户的问题可能是

 Hi, I'd like a refund for the coffee maker I ordered. Would that be possible?

基于类似这样的输入,要如何去设计 prompt 呢?直觉上,首先最容易想到的是提供一个合适的 “人设”---polite 且 helpful 的 service agent。

 This is a conversation between a customer and a polite, helpful customer service agent.

随后我们将顾客实际输入的问题填充进去,同时通过这种格式,我们向模型进一步明确了场景,客户当前抛出了一个问题

 Question of the customer: Hi, I'd like a refund for the coffee maker I ordered. Would that be

随后我们告知模型,需要反馈给客户一个肯定的回答

 Response by the customer service agent: Hello, thank you for reaching out to us. Yes,

通过这种 prompt,我们建立了一个问答的完整上下文逻辑关系,提供了建立在用户和服务生之间的语境,同时我们希望给与肯定的答复。将上述全部内容组装起来之后,我们最终得到了如下的 prompt

 This is a conversation between a customer and a polite, helpful customer service agent.
Question of the customer: Hi, I'd like a refund for the coffee maker I ordered. Would that be possible?
Response by the customer service agent: Hello, thank you for reaching out to us. Yes,

将上述的模型提供进入预演模型的输入中,我们可以得到如下的答复内容

  • Yes, we are able to accept returns if the product is unused and unopened.

  • Yes, we are happy to refund your purchase. However, we do require you to return the item to our store for a full refund.

  • Yes, we can do that. Please send us a message with your name, phone number, and the reason for the refund. We will get back to you as soon as possible.

通过上述的这种方法和 prompt 设计,我们得到了几乎以假乱真的文本生成结果!

【第2898期】3D Bubble Tea created by GPT-4

Show the model what you would like to see

除此之外,另外一个设计 prompt 的思路是在输入中提供期望生成的内容的范例 --- 这种方法被称为 few-shot learning。

我们来看如下示例,对于一个文本分类任务,我们希望模型能将不同的文本分成 postive/negative/neutral 三类,比如给定以下输入

 Review: "I really enjoyed this movie!"
This sentiment of this review is

一个真实的输出是(基于一个真实模型得到的结果)

 This sentiment of this review is apt, considering the movie's plot,

这显然不是我们所期待的结果,对于这个场景,我们希望只需要做一个 positive/negative/neutral 三选一的选择题就可以了,而 Language Model 生成了一大堆话 --- 我们当然可以外接一个全连接层,按照 BERT 之类的思路去进行微调,但是我们来看 prompt learning 会怎么做:

通过设置这样的 prompt,在 prompt 中包含我们期望的输入和输出,以这种方式告诉模型我们想要的输出格式是什么样的 --- 经过 prompt 扩展之后,输入为

 This is a movie review sentiment classifier.
Review: "I loved this movie!"
This review is positive.
Review: "I don't know, it was ok I guess.."
This review is neutral.
Review: "What a waste of time, would not recommend this movie."
This review is negative.
Review: "I really enjoyed this movie!"
This review is

模型很容易就输出了 positive 这样的结果。

Examples

我们通过下面的两个任务来进一步应用 prompt。

Keyword Generation

在这个示例中,我们希望通过 prompt engineering 来完成 keyword generation(关键字摘要)任务,即给定一段话,找到一个词来代表一段话中最核心的概念。

我们定义了这样的一个 prompt,在 prompt 的开头进行简单的声明,随后提供了两个范例(来自于 John von Neumann / Feminism, Wikipedia),并最终输入 Wikipedia 中对 Python 的一段描述进行测试

 This is a bot that automatically finds the most important keyword for a given text passage.

Text: "John von Neumann (/vɒn ˈnɔɪmən/; Hungarian: Neumann János Lajos, pronounced [ˈnɒjmɒn ˈjaːnoʃ ˈlɒjoʃ]; December 28, 1903 – February 8, 1957) was a
Hungarian-American mathematician, physicist, computer scientist, engineer and polymath. Von Neumann was generally regarded as the foremost mathematician of his
time[2] and said to be "the last representative of the great mathematicians".[3] He integrated pure and applied sciences."

Most important key word: "John von Neumann"

Text: "Some scholars consider feminist campaigns to be a main force behind major historical societal changes for women's rights, particularly in the West, where
they are near-universally credited with achieving women's suffrage, gender-neutral language, reproductive rights for women (including access to contraceptives
and abortion), and the right to enter into contracts and own property.[9] Although feminist advocacy is, and has been, mainly focused on women's rights, some
feminists argue for the inclusion of men's liberation within its aims, because they believe that men are also harmed by traditional gender roles.[10] Feminist
theory, which emerged from feminist movements, aims to understand the nature of gender inequality by examining women's social roles and lived experience;
feminist theorists have developed theories in a variety of disciplines in order to respond to issues concerning gender."

Most important key word: "Feminism"

Text: "Guido van Rossum began working on Python in the late 1980s, as a successor to the ABC programming language, and first released it in 1991 as Python
0.9.0.[31] Python 2.0 was released in 2000 and introduced new features, such as list comprehensions and a garbage collection system using reference counting and
was discontinued with version 2.7.18 in 2020.[32] Python 3.0 was released in 2008 and was a major revision of the language that is not completely
backward-compatible and much Python 2 code does not run unmodified on Python 3."

Most important key word:

Bingo!模型很快的反馈 “Python” 作为最终的结果 --- 当然模型也偶尔返回 “Guido van Rossum”,倒也情有可原,毕竟答案不是唯一的嘛~

Example Generation

另一个常见的任务是根据给定的描述进行扩写,对于这一任务设计 prompt 也是很轻松的

 This is a list of ideas for blog posts for tourists visiting Toronto:

1. The best sights to see in Toronto
2. My favourite walks in Toronto

我们最终很容易就得到了如下的这样内容

 3. An overview of Toronto
4. Toronto events
5. Restaurants in Toronto
6. Shopping in Toronto
7. Travel tips for Toronto
8. Sightseeing in Toronto
9. What to do in Toronto

Have fun!

参考链接

关于 prompt engineering,如果希望获得更深入的理解,可以看这两篇文章

  • NLP Prompt 系列 ——Prompt Engineering 方法详细梳理:https://zhuanlan.zhihu.com/p/488279606

  • Fine-tune 之后的 NLP 新范式:Prompt 越来越火,CMU 华人博士后出了篇综述文章:https://zhuanlan.zhihu.com/p/395795968

关于本文
译者:@LMJ1995
译文:https://zhuanlan.zhihu.com/p/526299013
作者:@cohere
原文:https://docs.cohere.ai/prompt-engineering-wiki/

这期前端早读课
对你有帮助,帮” 赞 “一下,
期待下一期,帮” 在看” 一下 。

相关阅读

  • 开箱即用,完整版 ChatGPT 克隆方案,开源了!

  • 公众号关注 “GitHubDaily”设为 “星标”,每天带你逛 GitHub!在过去的短短几个月,以 ChatGPT、GPT4 为代表的 AI 应用和大模型火爆全球,被视为开启了新的科技工业革命和 AGI (通
  • 点燃通用人工智能的火花: GPT-4的早期实验

  • 导语今天介绍的这篇文章提供了对 GPT-4 早期版本的研究报告。作者认为,GPT-4 比 ChatGPT 以及其他相关 LLMs 表现出更多的通用智能。作者发现,除了对语言的掌握,GPT-4 还可以解
  • 从NLP算法世界走来的AI“社牛”们

  • 引言我们都羡慕过多啦a梦身上的四次元口袋,每次心里默念一个期待,想要的东西触手可达;我们也惊叹过斯派克·琼斯在大银幕为我们呈现的《HER》,耳机那头的温柔女声,似乎可以成为我
  • 美公布首次载人绕月试飞宇航员阵容

  • 美国航天局3日公布了执行“阿耳忒弥斯2号”首次载人绕月飞行测试任务的4名宇航员名单。这4名宇航员分别是美航天局宇航员雷德·怀斯曼、维克托·格洛韦尔和克里斯蒂娜·科克

热门文章

  • “复活”半年后 京东拍拍二手杀入公益事业

  • 京东拍拍二手“复活”半年后,杀入公益事业,试图让企业捐的赠品、家庭闲置品变成实实在在的“爱心”。 把“闲置品”变爱心 6月12日,“益心一益·守护梦想每一步”2018年四

最新文章

  • 痛惜!3名少年不幸遇难

  • 今日#女孩跳江获救3名救援小伙溺亡#引起很多网友的关注4月2日凌晨5时许,四川阆中嘉陵江边,发生一起悲剧。一名女孩跳江轻生,三名小伙下水救援。遗憾的是,女孩获救,三名参与救援的
  • 快速入门 GraphQL:一个接口实现 CRUD

  • 作为前端开发,想必经常做的事情就是:调接口、画页面、调接口、画页面...调用的接口大概率是 restful 的,也就是类似这种:/students 查询所有学生信息/student/1 查询 id 为 1 的
  • 品读∣人到中年,方懂清明

  • 作者:景桥来源:你的景和我的桥(ID:jing6qiao)梨花风起正清明。转眼,又是一年雨纷纷时节。年少,“听雨歌楼上,红烛昏罗帐”,还不懂清明的庄严肃穆、清洁明净。人到中年,上有老、下有小,
  • 【第2906期】Prompt Engineering: 循循善诱

  • 前言有了 AI,将来交互可能会有另一种形式了。今日前端早读课文章由 @LMJ1995 翻译授权转载分享。正文从这开始~~自从 GPT3 问世以来,“预训练 + 微调” 的模式被广泛应用在各类
  • 泽连斯基首次正式访问,有人“搞破坏”?

  • 据外媒报道,当地时间5日,乌克兰总统泽连斯基抵达波兰。这是泽连斯基第一次正式访问波兰,此前仅在波兰过境。报道称,泽连斯基访问波兰之际,乌克兰正计划反攻。波兰是乌克兰的亲密