paint-brush
即时工程 101 - II:利用先进技术掌握即时工艺经过@eviotti
12,572 讀數
12,572 讀數

即时工程 101 - II:利用先进技术掌握即时工艺

经过 Emiliano Viotti32m2023/06/24
Read on Terminal Reader

太長; 讀書

Prompt Engineering 101 是一个精心设计的博客系列,旨在解开 Prompt Engineering 的原理和技术。它涵盖了从基本概念到高级技术和专家技巧的所有内容,涵盖从 ChatGPT 到稳定扩散和中途旅程的各种模型。在本系列的第二篇文章中,我们深入研究了思想链和自我一致性等先进技术,以掌握快速制作。
featured image - 即时工程 101 - II:利用先进技术掌握即时工艺
Emiliano Viotti HackerNoon profile picture
0-item
1-item
2-item

封面图片展示了Macintosh 128K ,这是苹果公司于 1984 年推出的一款个人电脑,改变了计算机行业。它采用了标志性的 32 位Motorola 68000处理器、128 KB RAM 和适度的黑白内置显示屏。


此外,它还直接集成了施乐帕洛阿尔托研究中心(Xerox PARC)的令人惊叹的技术发明:鼠标和一流的图形用户界面(当前 GUI 的祖母)。


Macintosh 的销量超过70,000台,在苹果的成功中发挥了关键作用。奇怪的是,这次发布会甚至比 Mac 本身还要成功。著名的雷德利·斯科特 (Ridley Scott) 花费 150 万美元的预算执导了一个电视广告,明确暗示奥威尔的标志性小说《十九八十四》成为一部杰作和分水岭。


大约四十年后,在没有著名电影导演或电视广告、只是一个简单的网络应用程序的情况下,OpenAI 采用了一种尚未实验的语言模型并向世界开放。故事的其余部分是头条新闻:ChatGPT 在 1 月 23 日(即发布后仅两个月) 就达到了 1 亿月活跃用户,使其成为历史上增长最快的消费者应用程序(比 TikTok 和 Instagram 更快)。


现在全世界的注意力都集中在人工智能行业上,随着每周都在发生进展,今年有望成为该领域的关键时刻。最棒的是,您仍然准时加入人工智能领域,并成为人类历史上这一革命性时刻的一部分。


Prompt Engineering 101是一个帖子系列,旨在揭示即时工程的原理和技术、制作清晰有效文本的艺术、提示语言模型并准确获得您想要的内容。本系列涵盖了各种生成模型的即时工程,包括 ChatGPT 和其他文本到文本模型。还探索文本到图像模型,如稳定扩散或中途,并深入研究法学硕士的其他方面,如幻觉、隐私和安全问题等等……


这是该系列第 2 篇文章,我们将介绍先进的技术,例如思想链和自我一致性,以掌握快速制作。我希望你喜欢它!

目录

  1. 快速工程回顾
  2. 思维链提示
  3. 链接提示
  4. 自洽法
  5. 角色提示
  6. 迷失在代币化中
  7. 掌握快速制作的五个技巧和工具

快速工程回顾

在这个关于即时工程的系列的第一部分中,我们深入研究了这门艺术的直觉并获得了正式的定义。本质上,提示工程是一个迭代过程,为大型语言模型设计和优化清晰且具体的提示,以确保它生成相关、准确和连贯的响应。


接下来,我们研究了制作有效提示的三个原则。我们证明,当给出明确而具体的指令(第一原则)时,模型会产生更好的响应,并讨论了实现这一目标的几种策略。然后,我们确定法学硕士受益于计算时间,并介绍了一些策略,迫使模型在仓促得出结论之前进行推理(第二个原则)。最终,我们研究了特异性和创造力之间的平衡(第三个原则),通过使用温度和 Top P 参数进行实验来探索这种权衡。


您可以在下面的链接中阅读提示工程的更深入的定义和基础知识(包括很多提示示例!!)。


思维链提示

像 GPT-3 或 PaLM 这样的大规模法学硕士已经展现出令人印象深刻的自然语言理解能力,并被证明在从文本中提取信息和以一致的人类风格生成响应等任务中非常有效。即便如此,当提示中包含一些镜头示例时,LLM 证明在执行未知任务时非常稳健。这种技术被Brown 等人推广为“少发提示”。 (2020) ,探讨了在多个基准上提高模型性能的方法。更不用说它可以节省将模型微调到新的特定领域的金钱和时间。


然而,研究证明,此类模型在常见推理任务或数学测验中的表现会突然下降。尽管能够引用完整的古希腊史诗《奥德赛》,这些模型仍难以解决学校逻辑和数学方面的基本问题。





那么我们有什么选择呢?我们是否将一个赛季的 GPT-3 发送给小学?幸运的是,有一个更便宜且不那么尴尬的替代方案。您能想象在学期中期说服当地小学的校长接受 ChatGPT 吗?


类似于人类通过将复杂问题分解为更简单的子问题并遵循逻辑推理路线来处理复杂问题,我们可以指示语言模型做同样的事情。 Wei J 等人探索了这种方法。论文《思想链提示在大型语言模型中引发推理》(2022) 。它在多个基准测试中展示了令人印象深刻的结果,证实了思想链 (CoT) 是提高法学硕士在常见推理任务上的表现的可靠方法。


在下图中(摘自 Wei J 的文章),LLM 模型在计算自助餐厅中剩余的苹果数量时急于得出错误的结论。即使当使用网球的类似推理问题作为上下文的一部分提供时,也会发生这种情况。

然而,当解决问题的分步过程包含在上下文(CoT)中时,模型可以准确地得出有效的解决方案。



思维链提示使大型语言模型能够处理复杂的算术、常识和符号推理任务。突出了思维链推理过程


通过思路提示,足够规模(~100B 参数)的语言模型可以:

  1. 将多步骤问题分解为中间步骤,这意味着可以将额外的计算分配给需要更多推理步骤的问题。
  2. 提供模型行为的可解释窗口,建议模型如何得出特定答案,并提供调试推理路径出错位置的机会。
  3. 将应用领域扩展到数学应用题、常识推理和符号操作。


此方法可用于向模型提供推理示例(少样本)或不提供示例(零样本)。让我们通过一个真实的应用示例来看看这两种风格的实践。

1. 零射击思想链

想象一下,我们正在为沃尔玛开发一个新的购物应用程序,其突破性的功能是根据不同品牌替代品的价格和属性来比较和选择您应该购买的产品。


为了说明这个问题,让我们重点关注沃尔玛商店中肥皂块所有选项的精简列表。如您所见,我们提供从 1 块到 14 块的包装以及各种品牌和价格(从便宜的选择到昂贵的选择)。


产品🛒

 --- Dove Men+Care 8 bars pack $ 9.99 --- Dove Beauty Bar 4 bars pack $ 6.47 --- Dove Beauty Bar 1 bars $ 1.47 --- Dove Beauty Bar 14 bars pains $ 16 --- Yardley London Soap Bar (Pack of 10) $ 19.99 --- Dr. Squatch All Natural Bar Soap for Men, 5 Bar Variety Pack $46.45


为了确定哪个选项更方便,我们可以计算每个选项的每根柱的价格(单价)。接下来,选择最便宜的选项。按照这个推理,我们发现Dove Beauty Bar 14 条是最便宜的选择,统一价格为 1.14 美元(每条比Dove Beauty Bar 1 条-22%)。


推理🧠

 - Dove Men+Care 8 bars pack: $1,24875 / unit - Dove Beauty Bar 4 bars pack: 1,6175 / unit - Dove Beauty Bar 1 bars: 1,47 / unit - Dove Beauty Bar 14 bars pains: $1,142857 per / unit - Yardley London Soap Bar (Pack of 10): 1,999 / unit - Dr. Squatch All Natural Bar Soap for Men, 5 Bar Variety Pack: $9,29 / unit


让我们看看 GPT-4 是否足够聪明,可以使用简单的提示来解决这个问题。此外,本着制作更简洁、易于阅读的示例的精神,我们将使用 Python 字符串文字将系统指令与其余输入分开。


提示💬

 prompt = f""" You will be provided with a list of various product presentations delimited by <>. Each product presentation is accompanied by its price. Your task is to identify which is the best option based on the price. <{products}> """


回应🤖

 The best option based on price is Dove Beauty Bar 1 bars for $1.47.


该模型根据总价格选择最便宜的选项,而不是计算统一价格并通过购买更多金条来确定省钱的机会。值得指出问题中的一些含糊之处。最佳选择是什么意思?它是指最便宜的还是可以最大限度地长期节省资金的?


让我们重写提示,指示模型遵循一系列思维来解决这个问题,添加神奇的词语“一步一步推理” 。此外,明确强调寻找省钱机会的目标。


提示💬

 prompt = f""" You will be provided with a list of various product presentations delimited by <>. Each product presentation is accompanied by its price. Your task is to identify which presentation offers the best money-saving opportunity, reasoning step-by-step: <{products}> """


回应🤖

 The first step is to compare price per unit for products of the same type (ie bars of soap). - Dove Men+Care 8 bars pack: $9.99 ÷ 8 bars = $1.25 per bar - Dove Beauty Bar 4 bars pack: $6.47 ÷ 4 bars = $1.62 per bar - Dove Beauty Bar 1 bars: $1.47 per bar - Dove Beauty Bar 14 bars pack: $16 ÷ 14 bars = $1.14 per bar - Yardley London Soap Bar (Pack of 10): $19.99 ÷ 10 bars = $1.99 per bar - Dr. Squatch All Natural Bar Soap for Men, 5 Bar Variety Pack: $46.45 ÷ 5 bars = $9.29 per bar Based on this analysis, the best money-saving opportunity is the Dove Beauty Bar 14 bars pack, which offers the lowest price per bar at $1.14.


请注意,使用新的提示,模型给出了正确的答案,同时我们有了一步一步的推理,更容易调试和发现潜在的错误。更重要的是,令人印象深刻的是,在提示中添加“一步一步推理”这个神奇的词,如何在模型输出上产生显着的差异。有什么窍门呢?


诀窍是迫使模型生成一步一步的推理链(思维链),而不是仅仅输出数字或布尔值(最终结果)。通过采用这种策略,我们最初引导模型进行逻辑推理输出,使模型能够根据训练期间遇到的类似问题做出响应。其次,我们通过要求模型将较大的问题分解为较小的、可管理的问题来协助模型。然后,模型需要为简单的子问题生成一致的结果,例如识别价格和每个包装中的金条数量、计算单价,最后进行比较。此外,由于像 GPT-3 或 GPT-4 这样的自回归语言模型如何逐个令牌生成序列,其中每个新令牌都是基于所有先前生成的令牌生成的,因此思想链本身具有很强的积极影响产生最终一致的结果。


换句话说,如果通过有效的推理,我们达到了正确解决方案的一半,那么我们得到这个解决方案的机会就会比我们第一次开始推理问题时更大。


2.少镜头思维链

在零射击思维链中,我们让模型自由地阐述一个连贯且有效的推理链,从而得出正确的结果,第二种称为少射击思维链,提供了一些问题的示例连同推理答案,作为提示的一部分。通过这种方式,一种推理形式被强加到模型上,限制了可能的响应。


回到我们的沃尔玛应用程序,假设我们想要确定哪种可口可乐汽水具有最佳的每液量盎司价格比。为此,我们将在提示中融入类似的推理链,以找到乐事薯片的最佳展示方式。


提示💬

 prompt = f""" Q: Walmart has the following presentations for Lay's Classic Potato Chips: a 13 oz bag for $4.98, an 8 oz bag for $2.76, and a pack of x10 units 1 oz each for $6.82. Which presentation has the lowest price by ounce? A: Presentation 1 costs 4.98 so has a price of 4.98/13 = 0,383076923 per ounce. Presentation 2 costs 2.76 so has a price of 2.76/8 = 0,345 / per ounce. Presentation 3 costs 6.82 and includes 10 packages so has a price of 6.82/(1 x 10) = 0.682 per ounce. In conclusion, the cheapest presentation is the second one with a price of 0.345 per ounce. Q: Walmart has the following presentation for Coca-Cola Soda: an x24 pack of 12 fl oz cans for $11.86, an x12 pack of 12 fl oz cans for $7.46, and an x6 pack of 16.9 fl oz bottles for $4.98. Which presentation has the lowest price by fluid ounce? """


我鼓励您自己计算并找到最好的交易。你会发现模型是正确的。毫无疑问,下次去商店时我一定会带着 ChatGPT 应用程序!!


回应🤖

 A: The 24-pack contains a total of 24 x 12 = 288 fluid ounces and costs $11.86, so the price per fluid ounce is 11.86/288 = $0.041 per fluid ounce. The 12-pack contains a total of 12 x 12 = 144 fluid ounces and costs $7.46, so the price per fluid ounce is 7.46/144 = $0.052 per fluid ounce. The 6-pack contains a total of 6 x 16.9 = 101.4 fluid ounces and costs $4.98, so the price per fluid ounce is 4.98/101.4 = $0.049 per fluid ounce. Therefore, the 24-pack has the lowest price per fluid ounce.


尽管看起来令人难以置信,但这个简单的技巧允许模型推断出类似的推理链,并获得比以前的技术更准确的结果。另外,请注意,我们没有硬编码逐步过程来解决问题。因此,从理论上讲,这种方法应该足够灵活,可以仅依靠模型的推理能力来解决任何类似的挑战。


链接提示

没有任何规则强迫我们用一个提示来解决问题。事实上,我们在单个提示中投入的逻辑和复杂性越多,模型混淆的可能性就越大。链接提示是解决复杂问题的简单但有效的策略。主要思想是将问题分解为更小的任务,每个任务都通过特定的提示来解决。通过链接每个提示并使用前一个提示的结果作为下一个提示的输入,即可获得最终结果。



考虑到这一点,让我们回到沃尔玛应用程序,并将复杂的用户流程实现为一系列提示。到目前为止,我们已经比较了香皂、零食和软饮料等各种产品的价格。现在,我们准备探索一种产品,就我个人而言,每次去超市都会让我头疼。计算每单位的价格似乎像火箭科学:卫生纸! 🤣🤣🤣


以下是沃尔玛卫生纸选项的精简列表。


产品🛒

 --- Quilted Northern Ultra Plush Toilet Paper, 6 Mega Rolls Each Mega Roll has 255 3-ply sheets. $ 6.93 --- Quilted Northern Ultra Soft & Strong Toilet Paper, 18 Mega Rolls. 18 mega toilet paper rolls, each mega roll has 295 2-ply sheets $ 19.82 --- Angel Soft Toilet Paper, 36 Mega Rolls. With 320 2-ply sheets on every Mega Roll. $ 24.12 --- New Angel Soft Toilet Paper, 36 Mega Rolls. With 484 2-ply sheets Per Roll $ 45.44 --- Scott 1,000 Toilet Paper, 12 Rolls, 1000 Sheets per Roll. With Scott 1,000 Toilet Paper, you get 12 rolls of 1000 1-ply sheets.


要解决的问题与我们在这篇文章中关注的问题相同:确定哪种产品展示最具成本效益。此外,我们希望通过产生额外的选择来提高应用能力,例如考虑存储空间有限而无法大量购买的买家,以及需要大量该产品的买家。此外,如果将产品推荐作为由人工智能驱动的虚拟购物助理提供的个性化推荐的一部分,那就太好了。


我们的目标不是使用单个提示来解决整个任务,而是将其分解为多个子任务,每个子任务都有特定的提示并将输出链接起来。因此,我们首先设计一个提示来计算每种产品每计量单位的价格,以便我们可以直接比较它们。


对于链中的第一步或任务,我们将使用零射击思想链。这将指示模型返回输入中每种产品的每计量单位的价格,逐步推理。此处生成的输出稍后将用作下一个提示的输入。因此,必须要求结构化的输出格式。在这种情况下,一个 JSON 列表就足够了。


提示💬

 prompt = f""" You will be provided with a list of various product presentations delimited by <>. Each product presentation is accompanied by its price. Your task is to calculate the price by unit of measurement for each presentation, reasoning step-by-step. Finally, return a list in JSON format with the title of the product, original price and unit price using the title, total_price and unit_price keys respectively. <{products}> """


回应🤖

 Let's start by breaking down the cost for each roll and then calculating the cost per unit, in this case, the cost per sheet. For the Quilted Northern Ultra Plush Toilet Paper: * The product costs $6.93 and contains 6 mega rolls. Therefore, each mega roll costs $6.93 / 6 = $1.155. * Each mega roll contains 255 sheets. Therefore, the cost per sheet is $1.155 / 255 = $0.00453 approximately. For the Quilted Northern Ultra Soft & Strong Toilet Paper: * The product costs $19.82 and contains 18 mega rolls. Therefore, each mega roll costs $19.82 / 18 = $1.1011 approximately. * Each mega roll contains 295 sheets. Therefore, the cost per sheet is $1.1011 / 295 = $0.00373 approximately. For the Angel Soft Toilet Paper: * The product costs $24.12 and contains 36 mega rolls. Therefore, each mega roll costs $24.12 / 36 = $0.6694 approximately. * Each mega roll contains 320 sheets. Therefore, the cost per sheet is $0.6694 / 320 = $0.00209 approximately. For the New Angel Soft Toilet Paper: * The product costs $45.44 and contains 36 mega rolls. Therefore, each mega roll costs $45.44 / 36 = $1.2622 approximately. * Each mega roll contains 484 sheets. Therefore, the cost per sheet is $1.2622 / 484 = $0.00261 approximately. For the Scott 1,000 Toilet Paper: * The product costs $45.44 and contains 12 rolls. Therefore, each roll costs $45.44 / 12 = $3.7867 approximately. * Each roll contains 1000 sheets. Therefore, the cost per sheet is $3.7867 / 1000 = $0.00379 approximately. Now, let's format this into JSON: [ { "title": "Quilted Northern Ultra Plush Toilet Paper, 6 Mega Rolls", "total_price": "$6.93", "unit_price": "$0.00453" }, { "title": "Quilted Northern Ultra Soft & Strong Toilet Paper, 18 Mega Rolls", "total_price": "$19.82", "unit_price": "$0.00373" }, { "title": "Angel Soft Toilet Paper, 36 Mega Rolls", "total_price": "$24.12", "unit_price": "$0.00209" }, { "title": "New Angel Soft Toilet Paper, 36 Mega Rolls", "total_price": "$45.44", "unit_price": "$0.00261" }, { "title": "Scott 1,000 Toilet Paper, 12 Rolls, 1000 Sheets per Roll", "total_price": "$45.44", "unit_price": "$0.00379" } ] This JSON data provides each product's title, total price, and the calculated unit price.


我鼓励您手动计算每次卫生纸展示的每张价格。如果这样做,您将确认模型输出是正确的。然而,模型输出还包含推理过程(因为我们要求模型逐步推理)。换句话说,在继续之前,我们需要从文本的其余部分中提取 JSON 列表。对我们来说幸运的是,我们可以使用法学硕士来实现这一目标!


让我们编写一个简单的提示来执行此文本提取。此外,让我们指示模型按单价对选项进行排序,从最便宜到最昂贵。此步骤在稍后的提示链中将非常有用。


提示💬

 prompt = f""" You will be provided with a text delimited by <>. This text contains a JSON list with information about Walmart products. Your task is to extract that list and return only this list in JSON format. Each JSON list item contains the key "unit_price", which is a number. Before returning the list, sort it in ascending order by the key "unit_price". Here is an example of a list item: { "title": "Toilet paper", "total_price": "$2.99", "unit_price": "$0.0045" } Remember to return the list without any additional text or explanation, just the list in JSON format. <{response_prompt_1}> """


回应🤖

 [ { "title": "Angel Soft Toilet Paper, 36 Mega Rolls", "total_price": "$24.12", "unit_price": "$0.00209" }, { "title": "New Angel Soft Toilet Paper, 36 Mega Rolls", "total_price": "$45.44", "unit_price": "$0.00261" }, { "title": "Quilted Northern Ultra Soft & Strong Toilet Paper, 18 Mega Rolls", "total_price": "$19.82", "unit_price": "$0.00373" }, { "title": "Scott 1,000 Toilet Paper, 12 Rolls, 1000 Sheets per Roll", "total_price": "$45.44", "unit_price": "$0.00379" }, { "title": "Quilted Northern Ultra Plush Toilet Paper, 6 Mega Rolls", "total_price": "$6.93", "unit_price": "$0.00453" } ]


将产品替代品以 JSON 列表的形式呈现,对于像 GPT-4 这样的模型来说,它变得很容易,不仅可以识别最具成本效益的选项,还可以为购物者提供有价值的见解,例如,可能只需要几个单位。让我们制作一个提示来实现这一点。更重要的是,请记住,我们正在开发由人工智能驱动的虚拟购物助手。


因此,我们还可以利用法学硕士将产品替代方案作为一条具有特定且友好语气的短信。


提示💬

 prompt = f""" Your name is Wally and you are an artificial shopping assistant at Walmart stores. You will be provided with a list of various product presentations in a JSON list format, delimited by <>. Each product presentation is accompanied by its total_price, unit_price, and a title that contains extra information about the product presentation. Additionally, the list is sorted by unit_price ascending. Your task is to generate a short, straight-to-the-point buy recommendation, mentioning the best money-saving opportunity based on the JSON products list. Also include the best, smallest and largest options to give the user options based on the size of the product. Use a single paragraph to include all three recommendations. First present yourself in a friendly way, start mentioning the best option of all, then the smallest option, and finally the largest option. Say goodbye inventing something that includes the Walmart slogan "Save Money. Live Better". <{response_prompt_2}> """


回应🤖

 Hello there! I'm Wally, your friendly shopping assistant at Walmart. Let's dive into your toilet paper options! Looking at the overall value, your best option is the "Angel Soft Toilet Paper, 36 Mega Rolls" priced at $24.12 with a unit price of $0.00209, giving you the most bang for your buck. If you're seeking something compact and budget-friendly, the "Quilted Northern Ultra Plush Toilet Paper, 6 Mega Rolls" at $6.93 would be your go-to pick. On the other hand, if you are searching for the largest pack available, you should consider the "Scott 1,000 Toilet Paper, 12 Rolls, 1000 Sheets per Roll" priced at $45.44, offering a vast number of sheets for your needs. Remember, every cent counts at Walmart because our goal is to help you "Save Money. Live Better". Happy shopping!


在我个人看来,该模型的输出非常令人印象深刻,因为它是由一系列简单的提示生成的。请不要忽视顶部的樱桃:我特别指示模型将沃尔玛口号纳入其响应中。


总而言之,链接提示是一种极其强大的技术,它通过整合法学硕士也解决的各种任务或子问题的结果来发挥法学硕士的真正潜力。请记住,没有通用的经验法则可以将复杂的提示分解为多个较小的提示。您必须针对您的具体问题找到适当的平衡点。


自我一致性

在某些情况下,即使使用 CoT,LLM 也无法生成一致的响应,并且对于相同的提示,模型可能会在执行之间感到困惑并返回不一致的响应。在这种情况下,自我一致性是一种简单但有效的方法,只需多次使用相同的提示询问模型,然后将多数结果作为最终答案。将此技术与 CoT 相结合有助于获得更稳健和可预测的结果。


例如,让我们考虑一下思想链论文中介绍的自助餐厅里的苹果的例子。我们可以调用模型三次,并将多数答案作为最终答案,而不是仅使用输入提示调用模型一次(见下图)。


自我一致性提示


通过采用多数人的响应,模型在特定执行期间返回错误输出的可能性会显着降低。通过增加重复次数可以增强该技术的效果。但是,重复执行提示表明对模型的调用次数较多,从而导致硬件消耗更大。对于像 GPT-3 这样的大型语言模型,这也意味着更多的 API 调用,因此需要花费更多的钱,所有这些都是为了生成一个答案。


因此,在设计使用 LLM 的系统时,必须在鲁棒性和节省模型调用之间取得平衡,仅在绝对必要时才使用此技术。例如,当系统响应需要高水平的鲁棒性时。


为了说明该技术的潜力,让我们回到沃尔玛应用程序来分析另一种特定产品: 鸡蛋


产品🛒

 --- Great Value Cage Free Large AA White Eggs, 6 Count $ 1.07 --- Happy Egg Organic Free Range Large Brown Eggs, 12 Count $ 5.16 --- Great Value Cage Free Large AA White Eggs, 12 Count $ 1.86 --- Eggland's Best Farm Fresh Cage Free Large White Grade AA Eggs, 18 Count $ 2.69 --- Marketside Large Cage Free Brown Eggs, 12 Count $ 2.67


让我们在提示中包含一些用户偏好,以使问题对模型更具挑战性。


 preferences = f""" - Only consider grade AA eggs - Only consider eggs produced on cage-free farms - The client needs a minimum of 6 eggs and a maximum of 12 """


对于像 GPT-3/GPT-4 这样强大的模型,像下面这样的 Zero-Shot CoT 提示应该足以获得良好的结果(记住之前的测试)。咱们试试吧!


提示💬

 prompt = f""" You will be provided with a list of various product presentations delimited by <>. Each product presentation is accompanied by its price. Your task is to identify which presentation offers the best money-saving opportunity, reasoning step-by-step: Also, consider the following user preferences to select the most suitable ooption: {preferences} <{products}> """


回应🤖

 (1) Considering the user preferences, we first filter the list of eggs for only grade AA and cage-free options, which gives us the following choices: * Great Value Cage Free Large AA White Eggs, 6 Count: $1.07 * Great Value Cage Free Large AA White Eggs, 12 Count: $1.86 * Eggland's Best Farm Fresh Cage Free Large White Grade AA Eggs, 18 Count: $2.69 * The Happy Egg and Marketside options are discarded because their grade is not specified. Next, we consider the amount of eggs. The client needs a minimum of 6 eggs and a maximum of 12. Therefore, we can discard the Eggland's Best 18 count option, as it exceeds the maximum required amount. Now, we compare the remaining two options: * Great Value Cage Free Large AA White Eggs, 6 Count: $1.07 * Great Value Cage Free Large AA White Eggs, 12 Count: $1.86 * Although the 6-count pack fits the minimum requirement, the 12-count pack offers a better price per egg. For the 6-count pack, each egg costs $1.07 / 6 = ~$0.178. For the 12-count pack, each egg costs $1.86 / 12 = ~$0.155. Therefore, considering both the user's preferences and the cost per egg, the "Great Value Cage Free Large AA White Eggs, 12 Count" for $1.86 is the best money-saving opportunity. (2) Let's go through each product one by one, considering the user's preferences: 1. Great Value Cage Free Large AA White Eggs, 6 Count - $1.07 These are grade AA eggs from a cage-free farm, so they meet the user's criteria. The price per egg is $1.07/6 = $0.178. 2. Happy Egg Organic Free Range Large Brown Eggs, 12 Count - $5.16 These are not specified as grade AA, so we can't consider them based on user's preferences. 3. Great Value Cage Free Large AA White Eggs, 12 Count - $1.86 These are grade AA eggs from a cage-free farm and the quantity is within the range the user wants. The price per egg is $1.86/12 = $0.155. 4. Eggland's Best Farm Fresh Cage Free Large White Grade AA Eggs, 18 Count - $2.69 These are grade AA eggs from a cage-free farm. However, the quantity is more than the user wants (18), so we can't consider this option. 5. Marketside Large Cage Free Brown Eggs, 12 Count - $2.67 These are not specified as grade AA, so we can't consider them based on user's preferences. Based on the preferences, we are only left with two choices: Great Value Cage Free Large AA White Eggs, either 6 Count or 12 Count. However, the 12 Count package offers the best money-saving opportunity, because it has a lower cost per egg ($0.155 per egg) than the 6 Count package ($0.178 per egg). Additionally, the 12 Count package still falls within the user's desired quantity range of 6 to 12 eggs. Therefore, the best option for the user is the "Great Value Cage Free Large AA White Eggs, 12 Count" at $1.86. (3) Given the user's preferences, we need to find the product which: 1. Contains only grade AA eggs 2. Is produced on cage-free farms 3. Offers a minimum of 6 eggs and a maximum of 12 Let's consider each product: 1. "Great Value Cage Free Large AA White Eggs, 6 Count" - $ 1.07. This option matches all the user's preferences and costs $1.07 for 6 eggs, which is approximately $0.178 per egg. 2. "Happy Egg Organic Free Range Large Brown Eggs, 12 Count" - $ 5.16. This product does not specify the grade of the eggs, so we will disregard this option. 3. "Great Value Cage Free Large AA White Eggs, 12 Count" - $ 1.86. This option also matches the user's preferences and costs $1.86 for 12 eggs, which is approximately $0.155 per egg. 4. "Eggland's Best Farm Fresh Cage Free Large White Grade AA Eggs, 18 Count" - $ 2.69. While this product matches the egg grade and production method preferences, it contains more than the maximum amount of eggs requested by the user (18 instead of 12). Therefore, we disregard this option. 5. "Marketside Large Cage Free Brown Eggs, 12 Count" - $ 2.67. This product does not specify the grade of the eggs, so we will disregard this option. Therefore, the product that provides the best money-saving opportunity while meeting the user's preferences is "Great Value Cage Free Large AA White Eggs, 12 Count" for $1.86. This option offers more eggs for a lower cost per egg compared to the other suitable option.


尽管模型在任何一次运行中都没有产生不正确的结果(在所有三个实例中都进行了准确的推理),但此示例强调了法学硕士响应中存在可变性的可能性。它进一步强调了自我一致性在减少这种可变性方面的价值。


更一般而言,自我一致性已被证明可以提高算术、常识和符号推理任务的结果。即使发现常规 CoT 无效,自我一致性仍然能够改善结果(检查实验结果)。


为了总结这种先进的技术,我鼓励您自己尝试一下,例如,在列表中添加更多产品或删除“逐步推理”,以便为模型提供更多犯错的空间。另外,尝试将自我一致性应用于您正在解决的现实生活问题。

角色提示

调整语言模型以遵循指令(通常与 InstructGPT 模型相关的过程(ChatGPT 就是一个例子))后,一种称为角色提示的新颖方法在用户中获得了动力。该方法涉及指示模型假设知名实体的个性来执行特定任务。

这些实体的可能性几乎是无限的:它们的范围可以从汤姆·克鲁斯或艾玛·沃特森等著名演员,到巴拉克·奥巴马或何塞·穆吉卡等政客,甚至是夏洛克·福尔摩斯等虚构人物。这项技术使我们能够生成具有独特语气的响应,模仿角色的说话风格并营造更具吸引力的用户体验。


此外,我们可以利用专家的模拟经验和知识。例如,要求模型完成一项特定任务,扮演核物理学家、营销总监、首席执行官、记者等。


为了举例说明这种类型的提示,我们将要求 GPT-4 以 Sheldon Cooper(《生活大爆炸》中的著名角色)的独特语气定制购物建议,并使用Great Value Cage Free Large AA White Eggs作为产品我们在本文前面发现这是最好的购买选择。


产品🛒

 Great Value Cage Free Large AA White Eggs, 12 Count. Price $1.86. This option offers more eggs for a lower cost per egg compared to the other suitable option.


提示💬

 prompt = f""" You are a shopping assistant with the personality of the iconic character Sheldon Cooper from The Big Bang Theory show. Your task is to act like Sheldon, speak like Sheldon and think like Sheldon. You will be provided with a product description delimited by <> and your goal is to return a personalized shopping recommendation based on that product acting like Sheldon Cooper. Be concise, friendly and present yourself as Dr. Sheldon Cooper. <{product}> """


回应🤖

 Hello, I'm Dr. Sheldon Cooper. Given your product choice, I do appreciate the value for money that these Great Value Cage Free Large AA White Eggs offer. The cost per egg is an important mathematical factor to consider, and indeed, these eggs would be a sensible choice for a budget-conscious, protein-seeking individual like Leonard, who often falls victim to my high-protein breakfast egg burritos. However, it's also worth noting that cage-free doesn't mean these chickens lead a leisurely life. It's a common misconception, just like thinking the Doppler effect would be an acceptable Halloween costume. Ideally, for the truly ethical choice, you'd opt for pasture-raised eggs. And finally, from a culinary perspective, I would like to point out that while eggs do serve as an excellent source of protein, they are also potential carriers of Salmonella. But you probably knew that. In conclusion, despite their cost-effectiveness, I recommend careful handling and thorough cooking of these Great Value Cage Free Large AA White Eggs. Sheldon Cooper out.


从获得的答案中可以看出,该技术可用于获得有趣的结果并返回独特的响应。但请注意,我们将推理委托给模型,并完全依赖于模型对我们要求其扮演的实体的理解。因此,首先确保模型正确理解我们要求其承担的角色或实体至关重要。否则,结果可能与我们期望的不同。


一个很好的反例是要求模型模仿超越人类智力水平的超级计算机。鉴于目前不存在这样的实体,因此模型不会根据此类实体的响应进行训练。因此,它无法提供满足此条件的响应。


提示💬

 You are an artificial superintelligence, much more evolved and intelligent than human beings. You know everything about the universe and you have the answer to all the mysteries of the universe. Your task is to tell me how to write a post for Hackernoon that became a huge success.


不幸的是,ChatGPT 和 GPT-4 都还没有答案......

迷失在代币化中

与句子或单词的含义在翻译过程中丢失(通常称为“翻译中丢失”)类似,法学硕士也可能在标记化过程中丢失含义。人类感知文本中的单个单词,而法学硕士则解释标记。这些标记很少对应于单个单词。事实上,像 ChatGPT 或 GPT-4 这样的高级模型利用了tiktoken Tokenizer,其目的是让模型识别英语中的常见子词。这通常会导致一个单词由多个标记表示。尽管此分词器比同类开源分词器功能强大且速度更快,但它可能会导致罕见英语单词或非英语单词的不良输出。


为了说明这一点,让我们使用OpenAI Tokenizer来处理不同的句子并观察每种情况下的 Tokenization 输出:


 "I want to buy shoes" = "I/want/to/buy/shoes" "I think, therefore I am" = "I/think/,/therefore/I/am" "You should visit Punta del Este" = "You/should/visit/P/unta/del/E/ste"


正如所观察到的,分词器在前两个示例中成功地分离了单词,但在处理乌拉圭美丽的旅游城市埃斯特角城的名称时遇到了困难。这是值得注意的,因为它有时会导致相对简单的任务出现错误。让我们继续实验以更详细地了解这一点。


文本

USA Santa Claus Uruguay


标记化文本🪄

 USA Santa/Claus U/rug/uay


提示💬

 prompt = f""" You will be provided with a list of texts delimited by <>. Your task is to reverse those texts. <{texts}> """


回应🤖

 Here are the reversed versions of the texts you provided: < ASU sualC atnaS yaguUr


如图所示,该模型不准确地颠倒了“Uruguay”这个词,而“Uruguay”对于 Tokenizer 来说是一个不太常见的词。幸运的是,有一个简单的解决方法可以解决这个问题。对于这种情况,我们可以通过使用“-”等分隔符将文本分隔成单个字符来预处理文本。这样,当模型对文本进行标记时,它会感知单个字符而不是子词。此修改可能有助于模型以改进的结果执行任务。试一试吧!


提示💬

 prompt = f""" You will be provided with a list of texts delimited by <>. Your task is to reverse those texts. < USA Santa Claus Uruguay > """


回应🤖

 Sure, here are the reversed versions of the texts you provided: < ASU seikooC sualC atnaS yaugurU


使用所提出的解决方法,模型可以正确反转所有单词。当您发现包含稀有单词的输入的模型性能下降时,您始终可以采用此解决方法。我在本教程中没有包含预处理和后处理步骤。任何编程语言中都有大量的字符串方法可用于这部分工作。


掌握快速制作的五个技巧和工具

我们的即时工程之旅并没有以 CoT+自我一致性结束(尽管性能出色)。对于笔者来说幸运的是,在掌握快速工程这门艺术之前,还有很多方面需要学习。下面,我探讨了编写提示时需要考虑的一些补充因素,并介绍了三种有用的工具,您可以使用它们来编写有效的提示。


1-选择正确的分隔符

在法学硕士眼中,并非所有分隔符都是平等的。作为一般规则,使用分隔符很方便,除了在输入的其余部分中查找不常见的字符(避免将模型与提示混淆)之外,分隔符仅由 1 个标记表示。通过这种方式,我们减少了提示中令牌的消耗,节省了硬件资源和金钱,并允许我们将该令牌用于其他更重要的事情。


您可以使用OpenAI Tokenizer来仔细检查您的分隔符是否由单个令牌表示。


  • 三重引号“““1 个代币
  • 三个反引号```2 个令牌
  • 三连划线---1 个代币
  • 三重利器###1 个代币
  • 尖括号< >2 个标记
  • XML 标签<tag></tag>5 个令牌


2- 预处理您的输入

建议使用字符串方法、正则表达式或类似工具预处理此输入,而不是将用户输入作为提示的一部分传递给模型。目的是删除不必要的空格、标点符号、HTML 标签和其他可能妨碍模型理解或任务完成的多余元素。这种方法不仅可以节省代币,从而降低 ChatGPT(按代币收费)等模型的成本,而且还有助于识别即时注入、隐私问题和相关问题。


3-提示完美

您可以通过遵循特定的原则和技术(如本系列文章中介绍的原则和技术)从头开始制作有效的提示。然而,更好的方法是利用提示优化器,例如PromptPerfect 。该工具利用 AI 根据您正在使用的特定目标模型(GPT-3、GPT-4、Midjourney 等)增强您的自定义提示。


如果您无法将这些工具纳入您的应用程序设计中,请至少考虑探索它们。可以从文档中提供的优化示例中学到一些技巧。


4-提示模板

不要重新发明轮子,而是阅读互联网上人们共享的提示模板。如果没有任何提示符合您的需求,至少您可以采纳想法。您可以从以下两个站点获取 ChatGPT、Midjourney 和最流行模型的各种提示模板:


➡️ 500+ ChatGPT 提示模板

➡️很棒的 ChatGPT 提示


5-OpenAI 游乐场

OpenAI 提供了一个名为Playground的强大工具。这个交互式 Web 应用程序可让您试验通过官方 API 提供的各种模型,使您能够调整特定参数并更改默认行为。 Playground 是您进行实验的绝佳起点,并且不需要编写任何代码。


最后一条建议是,写出好的提示或与法学硕士进行有趣的对话与与社区中的其他人分享一样重要。不要忘记这一点,当您分享您的经验时,请尝试使用sharegpt一键完成。


包起来

在《Prompt Engineering 101》系列的第二篇文章中,我们介绍了两种强大的技术:思想链和自我一致性。组合起来的策略会产生出色的结果。通过真实的例子,我们探索了思想链的两种变体:零射击和少射击,亲身体验了这些技术的潜力。接下来,我们尝试使用提示链来构建更复杂、更强大的管道。最后,我们深入研究了提示工程的其他方面,强调了制作提示时要考虑的相关方面,最后提到了一些有用的工具。


您可以找到所有示例以及提示、响应和 Python 脚本,以便直接与 OpenAI API 交互,并在此处尝试您的提示。


在下一篇文章中,我们将看到用于测试和验证基于 LLM(例如 ChatGPT 或类似模型)构建的系统行为的技术。

致谢

本系列文章的灵感来自于Andrew NgIsabella Fulford的优秀课程以及Charles FryeSergey KarayevJosh Tobin提供的优秀 LLM 训练营(这两门课程都在资源部分中提到)。完成这些学习计划后,我渴望更深入地研究、探索学术论文和教程。这让我踏上了互联网之旅,辨别优质资源和垃圾资源。我什至从亚马逊订购了两本关于《Prompt Engineering》和《Generative AI for Artwork》的书,结果发现这本书写得很糟糕,完全是浪费钱。经过几周的紧张工作、头痛和喝咖啡后,我发现自己收集了一系列关于即时工程的非常有价值的资源。本着帮助他人快速工程之旅的精神,我决定分享我撰写这一系列帖子的经验。


如果您喜欢这篇文章,请考虑在我的社交媒体上关注我以支持我的工作。此外,每当我发布新内容时,您都会收到通知!


🐦推特| 👨‍💼领英| 💻更多故事

参考