From 98a723075dd8c04614cd87899f0369dc4464f08a Mon Sep 17 00:00:00 2001 From: nowadays0421 Date: Mon, 24 Jul 2023 15:19:02 +0800 Subject: [PATCH 1/2] Finish PE-123 end edit --- .../2. 提示原则 Guidelines.ipynb | 1235 ++++++++++++++++- .../3. 迭代优化 Iterative.ipynb | 57 +- docs/content/环境配置.ipynb | 9 +- 3 files changed, 1270 insertions(+), 31 deletions(-) diff --git a/docs/content/C1 Prompt Engineering for Developer/2. 提示原则 Guidelines.ipynb b/docs/content/C1 Prompt Engineering for Developer/2. 提示原则 Guidelines.ipynb index c536d12..db0a492 100644 --- a/docs/content/C1 Prompt Engineering for Developer/2. 提示原则 Guidelines.ipynb +++ b/docs/content/C1 Prompt Engineering for Developer/2. 提示原则 Guidelines.ipynb @@ -1 +1,1234 @@ -{"cells":[{"attachments":{},"cell_type":"markdown","metadata":{},"source":["# 第二章 提示原则 Guidelines"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["本章讨论了设计高效Prompt的两个关键原则:**清晰具体**和**给予充足思考时间**。掌握这两点,对创建可靠的语言模型交互尤为重要。\n","\n","首先,Prompt需要清晰明确地表达需求,提供充足上下文,使语言模型准确理解我们的意图,就像向一个外星人详细解释人类世界一样。过于简略的Prompt往往难以把握所要完成的具体任务。\n","\n","其次,让语言模型有充足时间推理也极为关键。就像人类解题一样,匆忙得出的结论多有失误。因此Prompt应加入逐步推理的要求,给模型留出充分思考时间,这样生成的结果才更准确可靠。\n","\n","如果Prompt在这两点上都作了优化,语言模型就能够尽可能发挥潜力,完成复杂的推理和生成任务。掌握这些Prompt设计原则,是开发者取得语言模型应用成功的重要一步。"]},{"cell_type":"markdown","metadata":{},"source":["## 一、原则一 编写清晰、具体的指令\n","亲爱的读者,在与语言模型交互时,您需要牢记一点:以**清晰、具体**的方式表达您的需求。假设您面前坐着一位来自外星球的新朋友,其对人类语言和常识都一无所知。在这种情况下,您需要把想表达的意图讲得非常明确,不要有任何歧义。同样的,在提供Prompt的时候,也要以足够详细和容易理解的方式,把您的需求与上下文说清楚。 \n","\n","并不是说Prompt就必须非常短小简洁。事实上,在许多情况下,更长、更复杂的Prompt反而会让语言模型更容易抓住关键点,给出符合预期的回复。原因在于,复杂的Prompt提供了更丰富的上下文和细节,让模型可以更准确地把握所需的操作和响应方式。\n","\n","所以,记住用清晰、详尽的语言表达Prompt,就像在给外星人讲解人类世界一样,”*Adding more context helps the model understand you better.*“。"]},{"cell_type":"markdown","metadata":{},"source":["### 1.1 使用分隔符清晰地表示输入的不同部分"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["在编写Prompt时,我们可以使用各种标点符号作为“分隔符”,将不同的文本部分区分开来。\n","\n","分隔符就像是Prompt中的墙,将不同的指令、上下文、输入隔开,避免意外的混淆。你可以选择用 ` ```,\"\"\",< >, ,: ` 等做分隔符,只要能明确起到隔断作用即可。\n","\n","使用分隔符尤其重要的是可以防止 **“提示词注入” ( Prompt injection )**。什么是提示词注入?就是用户输入的文本可能包含与你的预设Prompt相冲突的内容,如果不加分隔,这些输入就可能“注入”并操纵语言模型,导致模型产生毫无关联的乱七八糟的输出。\n","\n","在以下的例子中,我们给出一段话并要求 GPT 进行总结,在该示例中我们使用 ``` 来作为分隔符。"]},{"cell_type":"code","execution_count":11,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["为了获得所需的输出,您应该提供清晰、具体的指示,避免与简短的提示词混淆,并使用更长的提示词来提供更多的清晰度和上下文信息。\n"]}],"source":["from tool import get_completion\n","\n","text = f\"\"\"\n","您应该提供尽可能清晰、具体的指示,以表达您希望模型执行的任务。\\\n","这将引导模型朝向所需的输出,并降低收到无关或不正确响应的可能性。\\\n","不要将写清晰的提示词与写简短的提示词混淆。\\\n","在许多情况下,更长的提示词可以为模型提供更多的清晰度和上下文信息,从而导致更详细和相关的输出。\n","\"\"\"\n","# 需要总结的文本内容\n","prompt = f\"\"\"\n","把用三个反引号括起来的文本总结成一句话。\n","```{text}```\n","\"\"\"\n","# 指令内容,使用 ``` 来分隔指令和待总结的内容\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"markdown","metadata":{},"source":["### 1.2 寻求结构化的输出"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["有时候我们需要语言模型给我们一些**结构化的输出**,而不仅仅是连续的文本。\n","\n","什么是结构化输出呢?就是按照某种格式组织的内容,例如JSON、HTML等。这种输出非常适合在代码中进一步解析和处理。例如,您可以在 Python 中将其读入字典或列表中。\n","\n","在以下示例中,我们要求 GPT 生成三本书的标题、作者和类别,并要求 GPT 以 JSON 的格式返回给我们,为便于解析,我们指定了 Json 的键。"]},{"cell_type":"code","execution_count":15,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["{\n"," \"books\": [\n"," {\n"," \"book_id\": 1,\n"," \"title\": \"迷失的时光\",\n"," \"author\": \"张三\",\n"," \"genre\": \"科幻\"\n"," },\n"," {\n"," \"book_id\": 2,\n"," \"title\": \"幻境之门\",\n"," \"author\": \"李四\",\n"," \"genre\": \"奇幻\"\n"," },\n"," {\n"," \"book_id\": 3,\n"," \"title\": \"虚拟现实\",\n"," \"author\": \"王五\",\n"," \"genre\": \"科幻\"\n"," }\n"," ]\n","}\n"]}],"source":["prompt = f\"\"\"\n","请生成包括书名、作者和类别的三本虚构的、非真实存在的中文书籍清单,\\\n","并以 JSON 格式提供,其中包含以下键:book_id、title、author、genre。\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)\n"]},{"cell_type":"markdown","metadata":{},"source":["### 1.3 要求模型检查是否满足条件"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["如果任务包含不一定能满足的假设(条件),我们可以告诉模型先检查这些假设,如果不满足,则会指出并停止执行后续的完整流程。您还可以考虑可能出现的边缘情况及模型的应对,以避免意外的结果或错误发生。\n","\n","在如下示例中,我们将分别给模型两段文本,分别是制作茶的步骤以及一段没有明确步骤的文本。我们将要求模型判断其是否包含一系列指令,如果包含则按照给定格式重新编写指令,不包含则回答“未提供步骤”。"]},{"cell_type":"code","execution_count":16,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Text 1 的总结:\n","第一步 - 把水烧开。\n","第二步 - 拿一个杯子并把茶包放进去。\n","第三步 - 把烧开的水倒在茶包上。\n","第四步 - 等待几分钟,让茶叶浸泡。\n","第五步 - 取出茶包。\n","第六步 - 如果需要,加入糖或牛奶调味。\n","第七步 - 就这样,您可以享受一杯美味的茶了。\n"]}],"source":["# 满足条件的输入(text中提供了步骤)\n","text_1 = f\"\"\"\n","泡一杯茶很容易。首先,需要把水烧开。\\\n","在等待期间,拿一个杯子并把茶包放进去。\\\n","一旦水足够热,就把它倒在茶包上。\\\n","等待一会儿,让茶叶浸泡。几分钟后,取出茶包。\\\n","如果您愿意,可以加一些糖或牛奶调味。\\\n","就这样,您可以享受一杯美味的茶了。\n","\"\"\"\n","prompt = f\"\"\"\n","您将获得由三个引号括起来的文本。\\\n","如果它包含一系列的指令,则需要按照以下格式重新编写这些指令:\n","\n","第一步 - ...\n","第二步 - …\n","…\n","第N步 - …\n","\n","如果文本中不包含一系列的指令,则直接写“未提供步骤”。\"\n","\\\"\\\"\\\"{text_1}\\\"\\\"\\\"\n","\"\"\"\n","response = get_completion(prompt)\n","print(\"Text 1 的总结:\")\n","print(response)"]},{"cell_type":"code","execution_count":17,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Text 2 的总结:\n","未提供步骤。\n"]}],"source":["# 不满足条件的输入(text中未提供预期指令)\n","text_2 = f\"\"\"\n","今天阳光明媚,鸟儿在歌唱。\\\n","这是一个去公园散步的美好日子。\\\n","鲜花盛开,树枝在微风中轻轻摇曳。\\\n","人们外出享受着这美好的天气,有些人在野餐,有些人在玩游戏或者在草地上放松。\\\n","这是一个完美的日子,可以在户外度过并欣赏大自然的美景。\n","\"\"\"\n","prompt = f\"\"\"\n","您将获得由三个引号括起来的文本。\\\n","如果它包含一系列的指令,则需要按照以下格式重新编写这些指令:\n","\n","第一步 - ...\n","第二步 - …\n","…\n","第N步 - …\n","\n","如果文本中不包含一系列的指令,则直接写“未提供步骤”。\"\n","\\\"\\\"\\\"{text_2}\\\"\\\"\\\"\n","\"\"\"\n","response = get_completion(prompt)\n","print(\"Text 2 的总结:\")\n","print(response)"]},{"cell_type":"markdown","metadata":{},"source":["### 1.4 提供少量示例"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["\"Few-shot\" prompting,即在要求模型执行实际任务之前,给模型一两个已完成的样例,让模型了解我们的要求和期望的输出样式。\n","\n","例如,在以下的样例中,我们先给了一个祖孙对话样例,然后要求模型用同样的隐喻风格回答关于“韧性”的问题。这就是一个少样本样例,它能帮助模型快速抓住我们要的语调和风格。\n","\n","利用少样本样例,我们可以轻松“预热”语言模型,让它为新的任务做好准备。这是一个让模型快速上手新任务的有效策略。"]},{"cell_type":"code","execution_count":22,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["<祖父母>: 韧性是一种坚持不懈的品质,就像一棵顽强的树在风雨中屹立不倒。它是面对困难和挑战时不屈不挠的精神,能够适应变化和克服逆境。韧性是一种内在的力量,让我们能够坚持追求目标,即使面临困难和挫折也能坚持不懈地努力。\n"]}],"source":["prompt = f\"\"\"\n","您的任务是以一致的风格回答问题。\n","\n","<孩子>: 请教我何为耐心。\n","\n","<祖父母>: 挖出最深峡谷的河流源于一处不起眼的泉眼;最宏伟的交响乐从单一的音符开始;最复杂的挂毯以一根孤独的线开始编织。\n","\n","<孩子>: 请教我何为韧性。\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["## 二、原则二 给模型时间去思考\n","\n","在设计Prompt时,给予语言模型充足的推理时间非常重要。语言模型与人类一样,需要时间来思考并解决复杂问题。如果让语言模型匆忙给出结论,其结果很可能不准确。\n","\n","例如,若要语言模型推断一本书的主题,仅提供简单的书名和一句简介是不足够的。这就像让一个人在极短时间内解决困难的数学题,错误在所难免。\n","\n","相反,我们应通过Prompt指引语言模型进行深入思考。可以要求其先列出对问题的各种看法,说明推理依据,然后再得出最终结论。在Prompt中添加逐步推理的要求,能让语言模型投入更多时间逻辑思维,输出结果也将更可靠准确。\n","\n","综上所述,给予语言模型充足的推理时间,是Prompt工程中一个非常重要的设计原则。这将大大提高语言模型处理复杂问题的效果,也是构建高质量Prompt的关键之处。开发者应注意给Prompt留出思考空间,以发挥语言模型的最大潜力。"]},{"cell_type":"markdown","metadata":{},"source":["### 2.1 指定完成任务所需的步骤"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["\n","\n","接下来我们将通过给定一个复杂任务,给出完成该任务的一系列步骤,来展示这一策略的效果。"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["首先我们描述了杰克和吉尔的故事,并给出提示词执行以下操作:首先,用一句话概括三个反引号限定的文本。第二,将摘要翻译成英语。第三,在英语摘要中列出每个名称。第四,输出包含以下键的 JSON 对象:英语摘要和人名个数。要求输出以换行符分隔。"]},{"cell_type":"code","execution_count":28,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["prompt 1:\n","1-两个兄妹在山上打水时发生意外,但最终平安回家。\n","2-In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. While singing joyfully, they climbed up, but unfortunately, Jack tripped on a stone and rolled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back to their cozy home. Despite the mishap, their adventurous spirit remained undiminished as they continued to explore with delight.\n","3-Jack, Jill\n","4-{\"english_summary\": \"In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. While singing joyfully, they climbed up, but unfortunately, Jack tripped on a stone and rolled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back to their cozy home. Despite the mishap, their adventurous spirit remained undiminished as they continued to explore with delight.\", \"num_names\": 2}\n"]}],"source":["text = f\"\"\"\n","在一个迷人的村庄里,兄妹杰克和吉尔出发去一个山顶井里打水。\\\n","他们一边唱着欢乐的歌,一边往上爬,\\\n","然而不幸降临——杰克绊了一块石头,从山上滚了下来,吉尔紧随其后。\\\n","虽然略有些摔伤,但他们还是回到了温馨的家中。\\\n","尽管出了这样的意外,他们的冒险精神依然没有减弱,继续充满愉悦地探索。\n","\"\"\"\n","# example 1\n","prompt_1 = f\"\"\"\n","执行以下操作:\n","1-用一句话概括下面用三个反引号括起来的文本。\n","2-将摘要翻译成英语。\n","3-在英语摘要中列出每个人名。\n","4-输出一个 JSON 对象,其中包含以下键:english_summary,num_names。\n","\n","请用换行符分隔您的答案。\n","\n","Text:\n","```{text}```\n","\"\"\"\n","response = get_completion(prompt_1)\n","print(\"prompt 1:\")\n","print(response)"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["上述输出仍然存在一定问题,例如,键“姓名”会被替换为法语(译注:在英文原版中,要求从英语翻译到法语,对应指令第三步的输出为 'Noms:',为Name的法语,这种行为难以预测,并可能为导出带来困难)\n","\n","因此,我们将Prompt加以改进,该 Prompt 前半部分不变,同时**确切指定了输出的格式**。"]},{"cell_type":"code","execution_count":29,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["\n","prompt 2:\n","Summary: 在一个迷人的村庄里,兄妹杰克和吉尔在山顶井里打水时发生了意外,但他们的冒险精神依然没有减弱,继续充满愉悦地探索。\n","\n","Translation: In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. Unfortunately, Jack tripped on a rock and tumbled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back home safely. Despite the mishap, their adventurous spirit remained strong as they continued to explore joyfully.\n","\n","Names: Jack, Jill\n","\n","JSON Output: {\"English_summary\": \"In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. Unfortunately, Jack tripped on a rock and tumbled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back home safely. Despite the mishap, their adventurous spirit remained strong as they continued to explore joyfully.\", \"num_names\": 2}\n"]}],"source":["prompt_2 = f\"\"\"\n","1-用一句话概括下面用<>括起来的文本。\n","2-将摘要翻译成英语。\n","3-在英语摘要中列出每个名称。\n","4-输出一个 JSON 对象,其中包含以下键:English_summary,num_names。\n","\n","请使用以下格式:\n","文本:<要总结的文本>\n","摘要:<摘要>\n","翻译:<摘要的翻译>\n","名称:<英语摘要中的名称列表>\n","输出 JSON:<带有 English_summary 和 num_names 的 JSON>\n","\n","Text: <{text}>\n","\"\"\"\n","response = get_completion(prompt_2)\n","print(\"\\nprompt 2:\")\n","print(response)"]},{"cell_type":"markdown","metadata":{},"source":["### 2.2 指导模型在下结论之前找出一个自己的解法"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["在设计Prompt时,我们还可以通过明确指导语言模型进行自主思考,来获得更好的效果。\n","\n","举个例子,假设我们要语言模型判断一个数学问题的解答是否正确。仅仅提供问题和解答是不够的,语言模型可能会匆忙做出错误判断。\n","\n","相反,我们可以在Prompt中先要求语言模型自己尝试解决这个问题,思考出自己的解法,然后再与提供的解答进行对比,判断正确性。这种先让语言模型自主思考的方式,能帮助它更深入理解问题,做出更准确的判断。\n","\n","接下来我们会给出一个问题和一份来自学生的解答,要求模型判断解答是否正确:"]},{"cell_type":"code","execution_count":30,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["学生的解决方案是正确的。他正确地计算了土地费用、太阳能电池板费用和维护费用,并将它们相加得到了总费用。\n"]}],"source":["prompt = f\"\"\"\n","判断学生的解决方案是否正确。\n","\n","问题:\n","我正在建造一个太阳能发电站,需要帮助计算财务。\n","\n"," 土地费用为 100美元/平方英尺\n"," 我可以以 250美元/平方英尺的价格购买太阳能电池板\n"," 我已经谈判好了维护合同,每年需要支付固定的10万美元,并额外支付每平方英尺10美元\n"," 作为平方英尺数的函数,首年运营的总费用是多少。\n","\n","学生的解决方案:\n","设x为发电站的大小,单位为平方英尺。\n","费用:\n","\n"," 土地费用:100x\n"," 太阳能电池板费用:250x\n"," 维护费用:100,000美元+100x\n"," 总费用:100x+250x+100,000美元+100x=450x+100,000美元\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["但是注意,学生的解决方案实际上是错误的。(*维护费用项100x应为10x,总费用450x应为360x*)\n","\n","我们可以通过指导模型先自行找出一个解法来解决这个问题。\n","\n","在接下来这个 Prompt 中,我们要求模型先自行解决这个问题,再根据自己的解法与学生的解法进行对比,从而判断学生的解法是否正确。同时,我们给定了输出的格式要求。通过拆分任务、明确步骤,让模型有更多时间思考,有时可以获得更准确的结果。在这个例子中,学生的答案是错误的,但如果我们没有先让模型自己计算,那么可能会被误导以为学生是正确的。"]},{"cell_type":"code","execution_count":40,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["实际解决方案和步骤:\n","\n"," 1. 土地费用:每平方英尺100美元,所以总费用为100x美元。\n"," 2. 太阳能电池板费用:每平方英尺250美元,所以总费用为250x美元。\n"," 3. 维护费用:固定费用为10万美元,额外费用为每平方英尺10美元,所以总费用为10万美元+10x美元。\n"," 4. 总费用:将上述三项费用相加,得到总费用为100x美元+250x美元+10万美元+10x美元=360x+10万美元。\n","\n","学生计算的总费用:450x+10万美元\n","实际计算的总费用:360x+10万美元\n","学生计算的费用和实际计算的费用是否相同:否\n","学生的解决方案和实际解决方案是否相同:否\n","学生的成绩:不正确\n"]}],"source":["prompt = f\"\"\"\n","请判断学生的解决方案是否正确,请通过如下步骤解决这个问题:\n","\n","步骤:\n","\n"," 首先,自己解决问题。\n"," 然后将您的解决方案与学生的解决方案进行比较,对比计算得到的总费用与学生计算的总费用是否一致,并评估学生的解决方案是否正确。\n"," 在自己完成问题之前,请勿决定学生的解决方案是否正确。\n","\n","使用以下格式:\n","\n"," 问题:问题文本\n"," 学生的解决方案:学生的解决方案文本\n"," 实际解决方案和步骤:实际解决方案和步骤文本\n"," 学生计算的总费用:学生计算得到的总费用\n"," 实际计算的总费用:实际计算出的总费用\n"," 学生计算的费用和实际计算的费用是否相同:是或否\n"," 学生的解决方案和实际解决方案是否相同:是或否\n"," 学生的成绩:正确或不正确\n","\n","问题:\n","\n"," 我正在建造一个太阳能发电站,需要帮助计算财务。 \n"," - 土地费用为每平方英尺100美元\n"," - 我可以以每平方英尺250美元的价格购买太阳能电池板\n"," - 我已经谈判好了维护合同,每年需要支付固定的10万美元,并额外支付每平方英尺10美元;\n","\n"," 作为平方英尺数的函数,首年运营的总费用是多少。\n","\n","学生的解决方案:\n","\n"," 设x为发电站的大小,单位为平方英尺。\n"," 费用:\n"," 1. 土地费用:100x美元\n"," 2. 太阳能电池板费用:250x美元\n"," 3. 维护费用:100,000+100x=10万美元+10x美元\n"," 总费用:100x美元+250x美元+10万美元+100x美元=450x+10万美元\n","\n","实际解决方案和步骤:\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["## 三、局限性"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["**开发大模型相关应用时请务必铭记:**\n","\n","\n","**虚假知识**:模型偶尔会生成一些看似真实实则编造的知识\n","\n","虽然模型在训练过程中接触了大量的知识,但它并没有*完全*记住所见的信息,因此它不甚清楚自己知识的边界。这意味着它可能会尝试回答主题晦涩难懂的问题,并编造听起来合理但实际上并不正确的答案。我们称这些编造的想法为幻觉(Hallucination)。\n","\n","如下示例展示了大模型的幻觉。我们要求告诉我们华为公司生产的 *GT Watch 运动手表* 产品的信息,事实上,这个公司是真实存在的,但产品是编造的,而模型一本正经地提供了它编造的知识,而且迷惑性很强。\n","\n","在开发与应用语言模型时,需要注意它们可能生成虚假信息的风险。尽管模型经过大规模预训练,掌握了丰富知识,但它实际上并没有*完全*记住所见的信息,难以准确判断自己的知识边界,可能做出错误推断。若让语言模型描述一个不存在的产品,它可能会自行构造出似是而非的细节。这被称为“幻觉”(Hallucination),是语言模型的一大缺陷。\n","\n","如下示例展示了大模型的幻觉。我们要求告诉我们华为公司生产的 *GT Watch 运动手表* 产品的信息,事实上,这个公司是真实存在的,但产品是编造的,而模型一本正经地提供了它编造的知识,而且迷惑性很强。"]},{"cell_type":"code","execution_count":44,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["华为公司生产的GT Watch运动手表是一款智能手表,具有多种功能和特点。以下是相关信息:\n","\n","1. 设计和外观:GT Watch采用圆形表盘设计,具有精致的外观和高质量的材料制造。它有多种颜色和表带选择,可以根据个人喜好进行定制。\n","\n","2. 显示屏:GT Watch配备了1.39英寸的AMOLED显示屏,具有高清分辨率和良好的可视性。用户可以通过触摸屏幕进行操作和导航。\n","\n","3. 运动追踪:GT Watch具有全天候的运动追踪功能,可以监测用户的步数、跑步距离、卡路里消耗和心率等数据。它还支持多种运动模式,如跑步、骑行、游泳等。\n","\n","4. 健康监测:GT Watch可以监测用户的心率、血氧饱和度和睡眠质量等健康指标。它还提供健康建议和提醒,帮助用户保持良好的健康状态。\n","\n","5. 通知和连接:GT Watch可以与用户的手机进行连接,通过蓝牙技术实现通知推送和电话提醒。用户可以在手表上查看短信、电话和社交媒体通知,无需拿出手机。\n","\n","6. 长续航时间:GT Watch具有较长的续航时间,一次充电可以使用数天。它还支持快速充电技术,可以在短时间内充满电。\n","\n","7. 其他功能:GT Watch还具有其他功能,如天气预报、闹钟、计时器、计步器等。它还支持NFC支付和音乐控制等便利功能。\n","\n","总体而言,华为GT Watch是一款功能强大、外观精致的智能运动手表,适合那些注重健康和运动的用户使用。\n"]}],"source":["prompt = f\"\"\"\n","告诉我华为公司生产的GT Watch运动手表的相关信息\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["语言模型生成虚假信息的“幻觉”问题,是使用与开发语言模型时需要高度关注的风险。由于幻觉信息往往令人无法辨别真伪,开发者必须警惕并尽量避免它的产生。\n","\n","目前 OpenAI 等公司正在积极研究解决语言模型的幻觉问题。在技术得以进一步改进之前,开发者可以通过Prompt设计减少幻觉发生的可能。例如,可以先让语言模型直接引用文本中的原句,然后再进行解答。这可以追踪信息来源,降低虚假内容的风险。\n","\n","综上,语言模型的幻觉问题事关应用的可靠性与安全性。开发者有必要认识到这一缺陷(注:截至2023年7月),并采取Prompt优化等措施予以缓解,以开发出更加可信赖的语言模型应用。这也将是未来语言模型进化的重要方向之一。"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["**注意**:\n"," \n","关于反斜杠使用的说明:在本教程中,我们使用反斜杠 \\ 来使文本适应屏幕大小以提高阅读体验,而没有用换行符 \\n 。GPT-3 并不受换行符(newline characters)的影响,但在您调用其他大模型时,需额外考虑换行符是否会影响模型性能。"]},{"cell_type":"markdown","metadata":{},"source":["## 四、英文原版 Prompt"]},{"cell_type":"markdown","metadata":{},"source":["**1.1 使用分隔符清晰地表示输入的不同部分**"]},{"cell_type":"code","execution_count":45,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["To guide a model towards the desired output and reduce irrelevant or incorrect responses, it is important to provide clear and specific instructions, which can be achieved through longer prompts that offer more clarity and context.\n"]}],"source":["text = f\"\"\"\n","You should express what you want a model to do by \\ \n","providing instructions that are as clear and \\ \n","specific as you can possibly make them. \\ \n","This will guide the model towards the desired output, \\ \n","and reduce the chances of receiving irrelevant \\ \n","or incorrect responses. Don't confuse writing a \\ \n","clear prompt with writing a short prompt. \\ \n","In many cases, longer prompts provide more clarity \\ \n","and context for the model, which can lead to \\ \n","more detailed and relevant outputs.\n","\"\"\"\n","prompt = f\"\"\"\n","Summarize the text delimited by triple backticks \\ \n","into a single sentence.\n","```{text}```\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"markdown","metadata":{},"source":["**1.2**寻求结构化的输出"]},{"cell_type":"code","execution_count":46,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["{\n"," \"books\": [\n"," {\n"," \"book_id\": 1,\n"," \"title\": \"The Enigma of Elysium\",\n"," \"author\": \"Evelyn Sinclair\",\n"," \"genre\": \"Mystery\"\n"," },\n"," {\n"," \"book_id\": 2,\n"," \"title\": \"Whispers in the Wind\",\n"," \"author\": \"Nathaniel Blackwood\",\n"," \"genre\": \"Fantasy\"\n"," },\n"," {\n"," \"book_id\": 3,\n"," \"title\": \"Echoes of the Past\",\n"," \"author\": \"Amelia Hart\",\n"," \"genre\": \"Romance\"\n"," }\n"," ]\n","}\n"]}],"source":["prompt = f\"\"\"\n","Generate a list of three made-up book titles along \\ \n","with their authors and genres. \n","Provide them in JSON format with the following keys: \n","book_id, title, author, genre.\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)\n"]},{"cell_type":"markdown","metadata":{},"source":["**1.3 要求模型检查是否满足条件**"]},{"cell_type":"code","execution_count":56,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Completion for Text 1:\n","Step 1 - Get some water boiling.\n","Step 2 - Grab a cup and put a tea bag in it.\n","Step 3 - Once the water is hot enough, pour it over the tea bag.\n","Step 4 - Let it sit for a bit so the tea can steep.\n","Step 5 - After a few minutes, take out the tea bag.\n","Step 6 - If you like, add some sugar or milk to taste.\n","Step 7 - Enjoy your delicious cup of tea.\n"]}],"source":["text_1 = f\"\"\"\n","Making a cup of tea is easy! First, you need to get some \\ \n","water boiling. While that's happening, \\ \n","grab a cup and put a tea bag in it. Once the water is \\ \n","hot enough, just pour it over the tea bag. \\ \n","Let it sit for a bit so the tea can steep. After a \\ \n","few minutes, take out the tea bag. If you \\ \n","like, you can add some sugar or milk to taste. \\ \n","And that's it! You've got yourself a delicious \\ \n","cup of tea to enjoy.\n","\"\"\"\n","prompt = f\"\"\"\n","You will be provided with text delimited by triple quotes. \n","If it contains a sequence of instructions, \\ \n","re-write those instructions in the following format:\n","\n","Step 1 - ...\n","Step 2 - …\n","…\n","Step N - …\n","\n","If the text does not contain a sequence of instructions, \\ \n","then simply write \\\"No steps provided.\\\"\n","\n","\\\"\\\"\\\"{text_1}\\\"\\\"\\\"\n","\"\"\"\n","response = get_completion(prompt)\n","print(\"Completion for Text 1:\")\n","print(response)"]},{"cell_type":"code","execution_count":48,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Completion for Text 2:\n","No steps provided.\n"]}],"source":["text_2 = f\"\"\"\n","The sun is shining brightly today, and the birds are \\\n","singing. It's a beautiful day to go for a \\ \n","walk in the park. The flowers are blooming, and the \\ \n","trees are swaying gently in the breeze. People \\ \n","are out and about, enjoying the lovely weather. \\ \n","Some are having picnics, while others are playing \\ \n","games or simply relaxing on the grass. It's a \\ \n","perfect day to spend time outdoors and appreciate the \\ \n","beauty of nature.\n","\"\"\"\n","prompt = f\"\"\"You will be provided with text delimited by triple quotes. \n","If it contains a sequence of instructions, \\ \n","re-write those instructions in the following format:\n","Step 1 - ...\n","Step 2 - …\n","…\n","Step N - …\n","\n","If the text does not contain a sequence of instructions, \\ \n","then simply write \\\"No steps provided.\\\"\n","\n","\\\"\\\"\\\"{text_2}\\\"\\\"\\\"\n","\"\"\"\n","response = get_completion(prompt)\n","print(\"Completion for Text 2:\")\n","print(response)"]},{"cell_type":"markdown","metadata":{},"source":["**1.4 提供少量示例**(少样本提示词,Few-shot prompting)"]},{"cell_type":"code","execution_count":49,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":[": Resilience is like a mighty oak tree that withstands the strongest storms, bending but never breaking. It is the unwavering determination to rise again after every fall, and the ability to find strength in the face of adversity. Just as a diamond is formed under immense pressure, resilience is forged through challenges and hardships, making us stronger and more resilient in the process.\n"]}],"source":["prompt = f\"\"\"\n","Your task is to answer in a consistent style.\n","\n",": Teach me about patience.\n","\n",": The river that carves the deepest \\ \n","valley flows from a modest spring; the \\ \n","grandest symphony originates from a single note; \\ \n","the most intricate tapestry begins with a solitary thread.\n","\n",": Teach me about resilience.\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"markdown","metadata":{},"source":["**2.1 指定完成任务所需的步骤**"]},{"cell_type":"code","execution_count":50,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Completion for prompt 1:\n","1 - Jack and Jill, siblings, go on a quest to fetch water from a hilltop well, but encounter misfortune when Jack trips on a stone and tumbles down the hill, with Jill following suit, yet they return home and remain undeterred in their adventurous spirits.\n","\n","2 - Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils rentrent chez eux et restent déterminés dans leur esprit d'aventure.\n","\n","3 - Jack, Jill\n","\n","4 - {\n"," \"french_summary\": \"Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils rentrent chez eux et restent déterminés dans leur esprit d'aventure.\",\n"," \"num_names\": 2\n","}\n"]}],"source":["text = f\"\"\"\n","In a charming village, siblings Jack and Jill set out on \\ \n","a quest to fetch water from a hilltop \\ \n","well. As they climbed, singing joyfully, misfortune \\ \n","struck—Jack tripped on a stone and tumbled \\ \n","down the hill, with Jill following suit. \\ \n","Though slightly battered, the pair returned home to \\ \n","comforting embraces. Despite the mishap, \\ \n","their adventurous spirits remained undimmed, and they \\ \n","continued exploring with delight.\n","\"\"\"\n","# example 1\n","prompt_1 = f\"\"\"\n","Perform the following actions: \n","1 - Summarize the following text delimited by triple \\\n","backticks with 1 sentence.\n","2 - Translate the summary into French.\n","3 - List each name in the French summary.\n","4 - Output a json object that contains the following \\\n","keys: french_summary, num_names.\n","\n","Separate your answers with line breaks.\n","\n","Text:\n","```{text}```\n","\"\"\"\n","response = get_completion(prompt_1)\n","print(\"Completion for prompt 1:\")\n","print(response)"]},{"cell_type":"code","execution_count":51,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["\n","Completion for prompt 2:\n","Summary: Jack and Jill, siblings from a charming village, go on a quest to fetch water from a hilltop well, but encounter misfortune when Jack trips on a stone and tumbles down the hill, with Jill following suit, yet they remain undeterred and continue exploring with delight.\n","\n","Translation: Jack et Jill, frère et sœur d'un charmant village, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils restent déterminés et continuent à explorer avec joie.\n","\n","Names: Jack, Jill\n","\n","Output JSON: \n","{\n"," \"french_summary\": \"Jack et Jill, frère et sœur d'un charmant village, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils restent déterminés et continuent à explorer avec joie.\",\n"," \"num_names\": 2\n","}\n"]}],"source":["prompt_2 = f\"\"\"\n","Your task is to perform the following actions: \n","1 - Summarize the following text delimited by <> with 1 sentence.\n","2 - Translate the summary into French.\n","3 - List each name in the French summary.\n","4 - Output a json object that contains the \n","following keys: french_summary, num_names.\n","\n","Use the following format:\n","Text: \n","Summary: \n","Translation: \n","Names: \n","Output JSON: \n","\n","Text: <{text}>\n","\"\"\"\n","response = get_completion(prompt_2)\n","print(\"\\nCompletion for prompt 2:\")\n","print(response)"]},{"cell_type":"markdown","metadata":{},"source":["**2.2 指导模型在下结论之前找出一个自己的解法**"]},{"cell_type":"code","execution_count":52,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["The student's solution is correct. They correctly identified the costs for land, solar panels, and maintenance, and calculated the total cost for the first year of operations as a function of the number of square feet.\n"]}],"source":["prompt = f\"\"\"\n","Determine if the student's solution is correct or not.\n","\n","Question:\n","I'm building a solar power installation and I need \\\n"," help working out the financials. \n","- Land costs $100 / square foot\n","- I can buy solar panels for $250 / square foot\n","- I negotiated a contract for maintenance that will cost \\ \n","me a flat $100k per year, and an additional $10 / square \\\n","foot\n","What is the total cost for the first year of operations \n","as a function of the number of square feet.\n","\n","Student's Solution:\n","Let x be the size of the installation in square feet.\n","Costs:\n","1. Land cost: 100x\n","2. Solar panel cost: 250x\n","3. Maintenance cost: 100,000 + 100x\n","Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"code","execution_count":53,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["To calculate the total cost for the first year of operations, we need to add up the costs of land, solar panels, and maintenance.\n","\n","1. Land cost: $100 / square foot\n","The cost of land is $100 multiplied by the number of square feet.\n","\n","2. Solar panel cost: $250 / square foot\n","The cost of solar panels is $250 multiplied by the number of square feet.\n","\n","3. Maintenance cost: $100,000 + $10 / square foot\n","The maintenance cost is a flat fee of $100,000 per year, plus $10 multiplied by the number of square feet.\n","\n","Total cost: Land cost + Solar panel cost + Maintenance cost\n","\n","So the actual solution is:\n","Total cost = (100 * x) + (250 * x) + (100,000 + (10 * x))\n","\n","Is the student's solution the same as the actual solution just calculated:\n","No\n","\n","Student grade:\n","Incorrect\n"]}],"source":["prompt = f\"\"\"\n","Your task is to determine if the student's solution \\\n","is correct or not.\n","To solve the problem do the following:\n","- First, work out your own solution to the problem. \n","- Then compare your solution to the student's solution \\ \n","and evaluate if the student's solution is correct or not. \n","Don't decide if the student's solution is correct until \n","you have done the problem yourself.\n","\n","Use the following format:\n","Question:\n","```\n","question here\n","```\n","Student's solution:\n","```\n","student's solution here\n","```\n","Actual solution:\n","```\n","steps to work out the solution and your solution here\n","```\n","Is the student's solution the same as actual solution \\\n","just calculated:\n","```\n","yes or no\n","```\n","Student grade:\n","```\n","correct or incorrect\n","```\n","\n","Question:\n","```\n","I'm building a solar power installation and I need help \\\n","working out the financials. \n","- Land costs $100 / square foot\n","- I can buy solar panels for $250 / square foot\n","- I negotiated a contract for maintenance that will cost \\\n","me a flat $100k per year, and an additional $10 / square \\\n","foot\n","What is the total cost for the first year of operations \\\n","as a function of the number of square feet.\n","``` \n","Student's solution:\n","```\n","Let x be the size of the installation in square feet.\n","Costs:\n","1. Land cost: 100x\n","2. Solar panel cost: 250x\n","3. Maintenance cost: 100,000 + 100x\n","Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000\n","```\n","Actual solution:\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"markdown","metadata":{},"source":["**3.1 幻觉**"]},{"cell_type":"code","execution_count":54,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["The AeroGlide UltraSlim Smart Toothbrush by Boie is a technologically advanced toothbrush designed to provide a superior brushing experience. Boie is a company known for its innovative oral care products, and the AeroGlide UltraSlim Smart Toothbrush is no exception.\n","\n","One of the standout features of this toothbrush is its ultra-slim design. The brush head is only 2mm thick, making it much thinner than traditional toothbrushes. This slim profile allows for better access to hard-to-reach areas of the mouth, ensuring a thorough and effective clean.\n","\n","The AeroGlide UltraSlim Smart Toothbrush also incorporates smart technology. It connects to a mobile app via Bluetooth, allowing users to track their brushing habits and receive personalized recommendations for improving their oral hygiene routine. The app provides real-time feedback on brushing technique, duration, and coverage, helping users to achieve optimal oral health.\n","\n","The toothbrush features soft, antimicrobial bristles made from a durable thermoplastic elastomer. These bristles are gentle on the gums and teeth, while also being effective at removing plaque and debris. The antimicrobial properties help to keep the brush head clean and hygienic between uses.\n","\n","Another notable feature of the AeroGlide UltraSlim Smart Toothbrush is its long battery life. It can last up to 30 days on a single charge, making it convenient for travel or everyday use without the need for frequent recharging.\n","\n","Overall, the AeroGlide UltraSlim Smart Toothbrush by Boie offers a combination of advanced technology, slim design, and effective cleaning capabilities. It is a great option for those looking to upgrade their oral care routine and achieve a healthier smile.\n"]}],"source":["prompt = f\"\"\"\n","Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.9"},"latex_envs":{"LaTeX_envs_menu_present":true,"autoclose":false,"autocomplete":true,"bibliofile":"biblio.bib","cite_by":"apalike","current_citInitial":1,"eqLabelWithNumbers":true,"eqNumInitial":1,"hotkeys":{"equation":"Ctrl-E","itemize":"Ctrl-I"},"labels_anchors":false,"latex_user_defs":false,"report_style_numbering":false,"user_envs_cfg":false},"toc":{"base_numbering":1,"nav_menu":{},"number_sections":true,"sideBar":true,"skip_h1_title":false,"title_cell":"Table of Contents","title_sidebar":"Contents","toc_cell":false,"toc_position":{},"toc_section_display":true,"toc_window_display":true}},"nbformat":4,"nbformat_minor":4} +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 第二章 提示原则 Guidelines" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "如何去使用 Prompt,以充分发挥 LLM 的性能?首先我们需要知道设计 Prompt 的原则,它们是每一个开发者设计 Prompt 所必须知道的基础概念。本章讨论了设计高效 Prompt 的两个关键原则:**编写清晰、具体的指令**和**给予模型充足思考时间**。掌握这两点,对创建可靠的语言模型交互尤为重要。\n", + "\n", + "首先,Prompt 需要清晰明确地表达需求,提供充足上下文,使语言模型准确理解我们的意图,就像向一个外星人详细解释人类世界一样。过于简略的 Prompt 往往使模型难以把握所要完成的具体任务。\n", + "\n", + "其次,让语言模型有充足时间推理也极为关键。就像人类解题一样,匆忙得出的结论多有失误。因此 Prompt 应加入逐步推理的要求,给模型留出充分思考时间,这样生成的结果才更准确可靠。\n", + "\n", + "如果 Prompt 在这两点上都作了优化,语言模型就能够尽可能发挥潜力,完成复杂的推理和生成任务。掌握这些 Prompt 设计原则,是开发者取得语言模型应用成功的重要一步。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 一、原则一 编写清晰、具体的指令\n", + "亲爱的读者,在与语言模型交互时,您需要牢记一点:以**清晰、具体**的方式表达您的需求。假设您面前坐着一位来自外星球的新朋友,其对人类语言和常识都一无所知。在这种情况下,您需要把想表达的意图讲得非常明确,不要有任何歧义。同样的,在提供 Prompt 的时候,也要以足够详细和容易理解的方式,把您的需求与上下文说清楚。 \n", + "\n", + "并不是说 Prompt 就必须非常短小简洁。事实上,在许多情况下,更长、更复杂的 Prompt 反而会让语言模型更容易抓住关键点,给出符合预期的回复。原因在于,复杂的 Prompt 提供了更丰富的上下文和细节,让模型可以更准确地把握所需的操作和响应方式。\n", + "\n", + "所以,记住用清晰、详尽的语言表达 Prompt,就像在给外星人讲解人类世界一样,“*Adding more context helps the model understand you better.*”。\n", + "\n", + "从该原则出发,我们提供几个设计 Prompt 的技巧。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1.1 使用分隔符清晰地表示输入的不同部分" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在编写 Prompt 时,我们可以使用各种标点符号作为“分隔符”,将不同的文本部分区分开来。\n", + "\n", + "分隔符就像是 Prompt 中的墙,将不同的指令、上下文、输入隔开,避免意外的混淆。你可以选择用 ` ```,\"\"\",< >, ,: ` 等做分隔符,只要能明确起到隔断作用即可。\n", + "\n", + "使用分隔符尤其重要的是可以防止 **“提示词注入” ( Prompt injection )**。什么是提示词注入?就是用户输入的文本可能包含与你的预设 Prompt 相冲突的内容,如果不加分隔,这些输入就可能“注入”并操纵语言模型,导致模型产生毫无关联的乱七八糟的输出。\n", + "\n", + "在以下的例子中,我们给出一段话并要求 GPT 进行总结,在该示例中我们使用 ``` 来作为分隔符。" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "为了获得所需的输出,您应该提供清晰、具体的指示,避免与简短的提示词混淆,并使用更长的提示词来提供更多的清晰度和上下文信息。\n" + ] + } + ], + "source": [ + "from tool import get_completion\n", + "\n", + "text = f\"\"\"\n", + "您应该提供尽可能清晰、具体的指示,以表达您希望模型执行的任务。\\\n", + "这将引导模型朝向所需的输出,并降低收到无关或不正确响应的可能性。\\\n", + "不要将写清晰的提示词与写简短的提示词混淆。\\\n", + "在许多情况下,更长的提示词可以为模型提供更多的清晰度和上下文信息,从而导致更详细和相关的输出。\n", + "\"\"\"\n", + "# 需要总结的文本内容\n", + "prompt = f\"\"\"\n", + "把用三个反引号括起来的文本总结成一句话。\n", + "```{text}```\n", + "\"\"\"\n", + "# 指令内容,使用 ``` 来分隔指令和待总结的内容\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1.2 寻求结构化的输出" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "有时候我们需要语言模型给我们一些**结构化的输出**,而不仅仅是连续的文本。\n", + "\n", + "什么是结构化输出呢?就是按照某种格式组织的内容,例如JSON、HTML等。这种输出非常适合在代码中进一步解析和处理。例如,您可以在 Python 中将其读入字典或列表中。\n", + "\n", + "在以下示例中,我们要求 GPT 生成三本书的标题、作者和类别,并要求 GPT 以 JSON 的格式返回给我们,为便于解析,我们指定了 Json 的键。" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"books\": [\n", + " {\n", + " \"book_id\": 1,\n", + " \"title\": \"迷失的时光\",\n", + " \"author\": \"张三\",\n", + " \"genre\": \"科幻\"\n", + " },\n", + " {\n", + " \"book_id\": 2,\n", + " \"title\": \"幻境之门\",\n", + " \"author\": \"李四\",\n", + " \"genre\": \"奇幻\"\n", + " },\n", + " {\n", + " \"book_id\": 3,\n", + " \"title\": \"虚拟现实\",\n", + " \"author\": \"王五\",\n", + " \"genre\": \"科幻\"\n", + " }\n", + " ]\n", + "}\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "请生成包括书名、作者和类别的三本虚构的、非真实存在的中文书籍清单,\\\n", + "并以 JSON 格式提供,其中包含以下键:book_id、title、author、genre。\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1.3 要求模型检查是否满足条件" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "如果任务包含不一定能满足的假设(条件),我们可以告诉模型先检查这些假设,如果不满足,则会指出并停止执行后续的完整流程。您还可以考虑可能出现的边缘情况及模型的应对,以避免意外的结果或错误发生。\n", + "\n", + "在如下示例中,我们将分别给模型两段文本,分别是制作茶的步骤以及一段没有明确步骤的文本。我们将要求模型判断其是否包含一系列指令,如果包含则按照给定格式重新编写指令,不包含则回答“未提供步骤”。" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Text 1 的总结:\n", + "第一步 - 把水烧开。\n", + "第二步 - 拿一个杯子并把茶包放进去。\n", + "第三步 - 把烧开的水倒在茶包上。\n", + "第四步 - 等待几分钟,让茶叶浸泡。\n", + "第五步 - 取出茶包。\n", + "第六步 - 如果需要,加入糖或牛奶调味。\n", + "第七步 - 就这样,您可以享受一杯美味的茶了。\n" + ] + } + ], + "source": [ + "# 满足条件的输入(text中提供了步骤)\n", + "text_1 = f\"\"\"\n", + "泡一杯茶很容易。首先,需要把水烧开。\\\n", + "在等待期间,拿一个杯子并把茶包放进去。\\\n", + "一旦水足够热,就把它倒在茶包上。\\\n", + "等待一会儿,让茶叶浸泡。几分钟后,取出茶包。\\\n", + "如果您愿意,可以加一些糖或牛奶调味。\\\n", + "就这样,您可以享受一杯美味的茶了。\n", + "\"\"\"\n", + "prompt = f\"\"\"\n", + "您将获得由三个引号括起来的文本。\\\n", + "如果它包含一系列的指令,则需要按照以下格式重新编写这些指令:\n", + "\n", + "第一步 - ...\n", + "第二步 - …\n", + "…\n", + "第N步 - …\n", + "\n", + "如果文本中不包含一系列的指令,则直接写“未提供步骤”。\"\n", + "\\\"\\\"\\\"{text_1}\\\"\\\"\\\"\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(\"Text 1 的总结:\")\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "上述示例中,模型可以很好地识别一系列的指令并进行输出。在接下来一个示例中,我们将提供给模型没有预期指令的输入,模型将判断未提供步骤。" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Text 2 的总结:\n", + "未提供步骤。\n" + ] + } + ], + "source": [ + "# 不满足条件的输入(text中未提供预期指令)\n", + "text_2 = f\"\"\"\n", + "今天阳光明媚,鸟儿在歌唱。\\\n", + "这是一个去公园散步的美好日子。\\\n", + "鲜花盛开,树枝在微风中轻轻摇曳。\\\n", + "人们外出享受着这美好的天气,有些人在野餐,有些人在玩游戏或者在草地上放松。\\\n", + "这是一个完美的日子,可以在户外度过并欣赏大自然的美景。\n", + "\"\"\"\n", + "prompt = f\"\"\"\n", + "您将获得由三个引号括起来的文本。\\\n", + "如果它包含一系列的指令,则需要按照以下格式重新编写这些指令:\n", + "\n", + "第一步 - ...\n", + "第二步 - …\n", + "…\n", + "第N步 - …\n", + "\n", + "如果文本中不包含一系列的指令,则直接写“未提供步骤”。\"\n", + "\\\"\\\"\\\"{text_2}\\\"\\\"\\\"\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(\"Text 2 的总结:\")\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1.4 提供少量示例" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Few-shot\" prompting,即在要求模型执行实际任务之前,给模型一两个已完成的样例,让模型了解我们的要求和期望的输出样式。\n", + "\n", + "例如,在以下的样例中,我们先给了一个祖孙对话样例,然后要求模型用同样的隐喻风格回答关于“韧性”的问题。这就是一个少样本样例,它能帮助模型快速抓住我们要的语调和风格。\n", + "\n", + "利用少样本样例,我们可以轻松“预热”语言模型,让它为新的任务做好准备。这是一个让模型快速上手新任务的有效策略。" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<祖父母>: 韧性是一种坚持不懈的品质,就像一棵顽强的树在风雨中屹立不倒。它是面对困难和挑战时不屈不挠的精神,能够适应变化和克服逆境。韧性是一种内在的力量,让我们能够坚持追求目标,即使面临困难和挫折也能坚持不懈地努力。\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "您的任务是以一致的风格回答问题。\n", + "\n", + "<孩子>: 请教我何为耐心。\n", + "\n", + "<祖父母>: 挖出最深峡谷的河流源于一处不起眼的泉眼;最宏伟的交响乐从单一的音符开始;最复杂的挂毯以一根孤独的线开始编织。\n", + "\n", + "<孩子>: 请教我何为韧性。\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 二、原则二 给模型时间去思考\n", + "\n", + "在设计 Prompt 时,给予语言模型充足的推理时间非常重要。语言模型与人类一样,需要时间来思考并解决复杂问题。如果让语言模型匆忙给出结论,其结果很可能不准确。例如,若要语言模型推断一本书的主题,仅提供简单的书名和一句简介是不足够的。这就像让一个人在极短时间内解决困难的数学题,错误在所难免。\n", + "\n", + "相反,我们应通过 Prompt 指引语言模型进行深入思考。可以要求其先列出对问题的各种看法,说明推理依据,然后再得出最终结论。在 Prompt 中添加逐步推理的要求,能让语言模型投入更多时间逻辑思维,输出结果也将更可靠准确。\n", + "\n", + "综上所述,给予语言模型充足的推理时间,是 Prompt Engineering 中一个非常重要的设计原则。这将大大提高语言模型处理复杂问题的效果,也是构建高质量 Prompt 的关键之处。开发者应注意给模型留出思考空间,以发挥语言模型的最大潜力。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2.1 指定完成任务所需的步骤" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "接下来我们将通过给定一个复杂任务,给出完成该任务的一系列步骤,来展示这一策略的效果。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "首先我们描述了杰克和吉尔的故事,并给出提示词执行以下操作:首先,用一句话概括三个反引号限定的文本。第二,将摘要翻译成英语。第三,在英语摘要中列出每个名称。第四,输出包含以下键的 JSON 对象:英语摘要和人名个数。要求输出以换行符分隔。" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "prompt 1:\n", + "1-两个兄妹在山上打水时发生意外,但最终平安回家。\n", + "2-In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. While singing joyfully, they climbed up, but unfortunately, Jack tripped on a stone and rolled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back to their cozy home. Despite the mishap, their adventurous spirit remained undiminished as they continued to explore with delight.\n", + "3-Jack, Jill\n", + "4-{\"english_summary\": \"In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. While singing joyfully, they climbed up, but unfortunately, Jack tripped on a stone and rolled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back to their cozy home. Despite the mishap, their adventurous spirit remained undiminished as they continued to explore with delight.\", \"num_names\": 2}\n" + ] + } + ], + "source": [ + "text = f\"\"\"\n", + "在一个迷人的村庄里,兄妹杰克和吉尔出发去一个山顶井里打水。\\\n", + "他们一边唱着欢乐的歌,一边往上爬,\\\n", + "然而不幸降临——杰克绊了一块石头,从山上滚了下来,吉尔紧随其后。\\\n", + "虽然略有些摔伤,但他们还是回到了温馨的家中。\\\n", + "尽管出了这样的意外,他们的冒险精神依然没有减弱,继续充满愉悦地探索。\n", + "\"\"\"\n", + "# example 1\n", + "prompt_1 = f\"\"\"\n", + "执行以下操作:\n", + "1-用一句话概括下面用三个反引号括起来的文本。\n", + "2-将摘要翻译成英语。\n", + "3-在英语摘要中列出每个人名。\n", + "4-输出一个 JSON 对象,其中包含以下键:english_summary,num_names。\n", + "\n", + "请用换行符分隔您的答案。\n", + "\n", + "Text:\n", + "```{text}```\n", + "\"\"\"\n", + "response = get_completion(prompt_1)\n", + "print(\"prompt 1:\")\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "上述输出仍然存在一定问题,例如,键“姓名”会被替换为法语(译注:在英文原版中,要求从英语翻译到法语,对应指令第三步的输出为 'Noms:',为Name的法语,这种行为难以预测,并可能为导出带来困难)\n", + "\n", + "因此,我们将Prompt加以改进,该 Prompt 前半部分不变,同时**确切指定了输出的格式**。" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "prompt 2:\n", + "Summary: 在一个迷人的村庄里,兄妹杰克和吉尔在山顶井里打水时发生了意外,但他们的冒险精神依然没有减弱,继续充满愉悦地探索。\n", + "\n", + "Translation: In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. Unfortunately, Jack tripped on a rock and tumbled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back home safely. Despite the mishap, their adventurous spirit remained strong as they continued to explore joyfully.\n", + "\n", + "Names: Jack, Jill\n", + "\n", + "JSON Output: {\"English_summary\": \"In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. Unfortunately, Jack tripped on a rock and tumbled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back home safely. Despite the mishap, their adventurous spirit remained strong as they continued to explore joyfully.\", \"num_names\": 2}\n" + ] + } + ], + "source": [ + "prompt_2 = f\"\"\"\n", + "1-用一句话概括下面用<>括起来的文本。\n", + "2-将摘要翻译成英语。\n", + "3-在英语摘要中列出每个名称。\n", + "4-输出一个 JSON 对象,其中包含以下键:English_summary,num_names。\n", + "\n", + "请使用以下格式:\n", + "文本:<要总结的文本>\n", + "摘要:<摘要>\n", + "翻译:<摘要的翻译>\n", + "名称:<英语摘要中的名称列表>\n", + "输出 JSON:<带有 English_summary 和 num_names 的 JSON>\n", + "\n", + "Text: <{text}>\n", + "\"\"\"\n", + "response = get_completion(prompt_2)\n", + "print(\"\\nprompt 2:\")\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2.2 指导模型在下结论之前找出一个自己的解法" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在设计 Prompt 时,我们还可以通过明确指导语言模型进行自主思考,来获得更好的效果。\n", + "\n", + "举个例子,假设我们要语言模型判断一个数学问题的解答是否正确。仅仅提供问题和解答是不够的,语言模型可能会匆忙做出错误判断。\n", + "\n", + "相反,我们可以在 Prompt 中先要求语言模型自己尝试解决这个问题,思考出自己的解法,然后再与提供的解答进行对比,判断正确性。这种先让语言模型自主思考的方式,能帮助它更深入理解问题,做出更准确的判断。\n", + "\n", + "接下来我们会给出一个问题和一份来自学生的解答,要求模型判断解答是否正确:" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "学生的解决方案是正确的。他正确地计算了土地费用、太阳能电池板费用和维护费用,并将它们相加得到了总费用。\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "判断学生的解决方案是否正确。\n", + "\n", + "问题:\n", + "我正在建造一个太阳能发电站,需要帮助计算财务。\n", + "\n", + " 土地费用为 100美元/平方英尺\n", + " 我可以以 250美元/平方英尺的价格购买太阳能电池板\n", + " 我已经谈判好了维护合同,每年需要支付固定的10万美元,并额外支付每平方英尺10美元\n", + " 作为平方英尺数的函数,首年运营的总费用是多少。\n", + "\n", + "学生的解决方案:\n", + "设x为发电站的大小,单位为平方英尺。\n", + "费用:\n", + "\n", + " 土地费用:100x\n", + " 太阳能电池板费用:250x\n", + " 维护费用:100,000美元+100x\n", + " 总费用:100x+250x+100,000美元+100x=450x+100,000美元\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "但是注意,学生的解决方案实际上是错误的。(*维护费用项100x应为10x,总费用450x应为360x*)\n", + "\n", + "我们可以通过指导模型先自行找出一个解法来解决这个问题。\n", + "\n", + "在接下来这个 Prompt 中,我们要求模型先自行解决这个问题,再根据自己的解法与学生的解法进行对比,从而判断学生的解法是否正确。同时,我们给定了输出的格式要求。通过拆分任务、明确步骤,让模型有更多时间思考,有时可以获得更准确的结果。在这个例子中,学生的答案是错误的,但如果我们没有先让模型自己计算,那么可能会被误导以为学生是正确的。" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "实际解决方案和步骤:\n", + "\n", + " 1. 土地费用:每平方英尺100美元,所以总费用为100x美元。\n", + " 2. 太阳能电池板费用:每平方英尺250美元,所以总费用为250x美元。\n", + " 3. 维护费用:固定费用为10万美元,额外费用为每平方英尺10美元,所以总费用为10万美元+10x美元。\n", + " 4. 总费用:将上述三项费用相加,得到总费用为100x美元+250x美元+10万美元+10x美元=360x+10万美元。\n", + "\n", + "学生计算的总费用:450x+10万美元\n", + "实际计算的总费用:360x+10万美元\n", + "学生计算的费用和实际计算的费用是否相同:否\n", + "学生的解决方案和实际解决方案是否相同:否\n", + "学生的成绩:不正确\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "请判断学生的解决方案是否正确,请通过如下步骤解决这个问题:\n", + "\n", + "步骤:\n", + "\n", + " 首先,自己解决问题。\n", + " 然后将您的解决方案与学生的解决方案进行比较,对比计算得到的总费用与学生计算的总费用是否一致,并评估学生的解决方案是否正确。\n", + " 在自己完成问题之前,请勿决定学生的解决方案是否正确。\n", + "\n", + "使用以下格式:\n", + "\n", + " 问题:问题文本\n", + " 学生的解决方案:学生的解决方案文本\n", + " 实际解决方案和步骤:实际解决方案和步骤文本\n", + " 学生计算的总费用:学生计算得到的总费用\n", + " 实际计算的总费用:实际计算出的总费用\n", + " 学生计算的费用和实际计算的费用是否相同:是或否\n", + " 学生的解决方案和实际解决方案是否相同:是或否\n", + " 学生的成绩:正确或不正确\n", + "\n", + "问题:\n", + "\n", + " 我正在建造一个太阳能发电站,需要帮助计算财务。 \n", + " - 土地费用为每平方英尺100美元\n", + " - 我可以以每平方英尺250美元的价格购买太阳能电池板\n", + " - 我已经谈判好了维护合同,每年需要支付固定的10万美元,并额外支付每平方英尺10美元;\n", + "\n", + " 作为平方英尺数的函数,首年运营的总费用是多少。\n", + "\n", + "学生的解决方案:\n", + "\n", + " 设x为发电站的大小,单位为平方英尺。\n", + " 费用:\n", + " 1. 土地费用:100x美元\n", + " 2. 太阳能电池板费用:250x美元\n", + " 3. 维护费用:100,000+100x=10万美元+10x美元\n", + " 总费用:100x美元+250x美元+10万美元+100x美元=450x+10万美元\n", + "\n", + "实际解决方案和步骤:\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 三、局限性" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**开发大模型相关应用时请务必铭记:**\n", + "\n", + "\n", + "**虚假知识**:模型偶尔会生成一些看似真实实则编造的知识\n", + "\n", + "在开发与应用语言模型时,需要注意它们可能生成虚假信息的风险。尽管模型经过大规模预训练,掌握了丰富知识,但它实际上并没有*完全*记住所见的信息,难以准确判断自己的知识边界,可能做出错误推断。若让语言模型描述一个不存在的产品,它可能会自行构造出似是而非的细节。这被称为“幻觉”(Hallucination),是语言模型的一大缺陷。\n", + "\n", + "如下示例展示了大模型的幻觉。我们要求告诉我们华为公司生产的 *GT Watch 运动手表* 产品的信息,事实上,这个公司是真实存在的,但产品是编造的,而模型一本正经地提供了它编造的知识,而且迷惑性很强。" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "华为公司生产的GT Watch运动手表是一款智能手表,具有多种功能和特点。以下是相关信息:\n", + "\n", + "1. 设计和外观:GT Watch采用圆形表盘设计,具有精致的外观和高质量的材料制造。它有多种颜色和表带选择,可以根据个人喜好进行定制。\n", + "\n", + "2. 显示屏:GT Watch配备了1.39英寸的AMOLED显示屏,具有高清分辨率和良好的可视性。用户可以通过触摸屏幕进行操作和导航。\n", + "\n", + "3. 运动追踪:GT Watch具有全天候的运动追踪功能,可以监测用户的步数、跑步距离、卡路里消耗和心率等数据。它还支持多种运动模式,如跑步、骑行、游泳等。\n", + "\n", + "4. 健康监测:GT Watch可以监测用户的心率、血氧饱和度和睡眠质量等健康指标。它还提供健康建议和提醒,帮助用户保持良好的健康状态。\n", + "\n", + "5. 通知和连接:GT Watch可以与用户的手机进行连接,通过蓝牙技术实现通知推送和电话提醒。用户可以在手表上查看短信、电话和社交媒体通知,无需拿出手机。\n", + "\n", + "6. 长续航时间:GT Watch具有较长的续航时间,一次充电可以使用数天。它还支持快速充电技术,可以在短时间内充满电。\n", + "\n", + "7. 其他功能:GT Watch还具有其他功能,如天气预报、闹钟、计时器、计步器等。它还支持NFC支付和音乐控制等便利功能。\n", + "\n", + "总体而言,华为GT Watch是一款功能强大、外观精致的智能运动手表,适合那些注重健康和运动的用户使用。\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "告诉我华为公司生产的GT Watch运动手表的相关信息\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "语言模型生成虚假信息的“幻觉”问题,是使用与开发语言模型时需要高度关注的风险。由于幻觉信息往往令人无法辨别真伪,开发者必须警惕并尽量避免它的产生。\n", + "\n", + "目前 OpenAI 等公司正在积极研究解决语言模型的幻觉问题。在技术得以进一步改进之前,开发者可以通过Prompt设计减少幻觉发生的可能。例如,可以先让语言模型直接引用文本中的原句,然后再进行解答。这可以追踪信息来源,降低虚假内容的风险。\n", + "\n", + "综上,语言模型的幻觉问题事关应用的可靠性与安全性。开发者有必要认识到这一缺陷(注:截至2023年7月),并采取Prompt优化等措施予以缓解,以开发出更加可信赖的语言模型应用。这也将是未来语言模型进化的重要方向之一。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**注意**:\n", + " \n", + "关于反斜杠使用的说明:在本教程中,我们使用反斜杠 \\ 来使文本适应屏幕大小以提高阅读体验,而没有用换行符 \\n 。GPT-3 并不受换行符(newline characters)的影响,但在您调用其他大模型时,需额外考虑换行符是否会影响模型性能。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 四、英文原版 Prompt" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**1.1 使用分隔符清晰地表示输入的不同部分**" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "To guide a model towards the desired output and reduce irrelevant or incorrect responses, it is important to provide clear and specific instructions, which can be achieved through longer prompts that offer more clarity and context.\n" + ] + } + ], + "source": [ + "text = f\"\"\"\n", + "You should express what you want a model to do by \\ \n", + "providing instructions that are as clear and \\ \n", + "specific as you can possibly make them. \\ \n", + "This will guide the model towards the desired output, \\ \n", + "and reduce the chances of receiving irrelevant \\ \n", + "or incorrect responses. Don't confuse writing a \\ \n", + "clear prompt with writing a short prompt. \\ \n", + "In many cases, longer prompts provide more clarity \\ \n", + "and context for the model, which can lead to \\ \n", + "more detailed and relevant outputs.\n", + "\"\"\"\n", + "prompt = f\"\"\"\n", + "Summarize the text delimited by triple backticks \\ \n", + "into a single sentence.\n", + "```{text}```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**1.2 寻求结构化的输出**" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"books\": [\n", + " {\n", + " \"book_id\": 1,\n", + " \"title\": \"The Enigma of Elysium\",\n", + " \"author\": \"Evelyn Sinclair\",\n", + " \"genre\": \"Mystery\"\n", + " },\n", + " {\n", + " \"book_id\": 2,\n", + " \"title\": \"Whispers in the Wind\",\n", + " \"author\": \"Nathaniel Blackwood\",\n", + " \"genre\": \"Fantasy\"\n", + " },\n", + " {\n", + " \"book_id\": 3,\n", + " \"title\": \"Echoes of the Past\",\n", + " \"author\": \"Amelia Hart\",\n", + " \"genre\": \"Romance\"\n", + " }\n", + " ]\n", + "}\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Generate a list of three made-up book titles along \\ \n", + "with their authors and genres. \n", + "Provide them in JSON format with the following keys: \n", + "book_id, title, author, genre.\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**1.3 要求模型检查是否满足条件**" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Completion for Text 1:\n", + "Step 1 - Get some water boiling.\n", + "Step 2 - Grab a cup and put a tea bag in it.\n", + "Step 3 - Once the water is hot enough, pour it over the tea bag.\n", + "Step 4 - Let it sit for a bit so the tea can steep.\n", + "Step 5 - After a few minutes, take out the tea bag.\n", + "Step 6 - If you like, add some sugar or milk to taste.\n", + "Step 7 - Enjoy your delicious cup of tea.\n" + ] + } + ], + "source": [ + "text_1 = f\"\"\"\n", + "Making a cup of tea is easy! First, you need to get some \\ \n", + "water boiling. While that's happening, \\ \n", + "grab a cup and put a tea bag in it. Once the water is \\ \n", + "hot enough, just pour it over the tea bag. \\ \n", + "Let it sit for a bit so the tea can steep. After a \\ \n", + "few minutes, take out the tea bag. If you \\ \n", + "like, you can add some sugar or milk to taste. \\ \n", + "And that's it! You've got yourself a delicious \\ \n", + "cup of tea to enjoy.\n", + "\"\"\"\n", + "prompt = f\"\"\"\n", + "You will be provided with text delimited by triple quotes. \n", + "If it contains a sequence of instructions, \\ \n", + "re-write those instructions in the following format:\n", + "\n", + "Step 1 - ...\n", + "Step 2 - …\n", + "…\n", + "Step N - …\n", + "\n", + "If the text does not contain a sequence of instructions, \\ \n", + "then simply write \\\"No steps provided.\\\"\n", + "\n", + "\\\"\\\"\\\"{text_1}\\\"\\\"\\\"\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(\"Completion for Text 1:\")\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Completion for Text 2:\n", + "No steps provided.\n" + ] + } + ], + "source": [ + "text_2 = f\"\"\"\n", + "The sun is shining brightly today, and the birds are \\\n", + "singing. It's a beautiful day to go for a \\ \n", + "walk in the park. The flowers are blooming, and the \\ \n", + "trees are swaying gently in the breeze. People \\ \n", + "are out and about, enjoying the lovely weather. \\ \n", + "Some are having picnics, while others are playing \\ \n", + "games or simply relaxing on the grass. It's a \\ \n", + "perfect day to spend time outdoors and appreciate the \\ \n", + "beauty of nature.\n", + "\"\"\"\n", + "prompt = f\"\"\"You will be provided with text delimited by triple quotes. \n", + "If it contains a sequence of instructions, \\ \n", + "re-write those instructions in the following format:\n", + "Step 1 - ...\n", + "Step 2 - …\n", + "…\n", + "Step N - …\n", + "\n", + "If the text does not contain a sequence of instructions, \\ \n", + "then simply write \\\"No steps provided.\\\"\n", + "\n", + "\\\"\\\"\\\"{text_2}\\\"\\\"\\\"\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(\"Completion for Text 2:\")\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**1.4 提供少量示例**(少样本提示词,Few-shot prompting)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + ": Resilience is like a mighty oak tree that withstands the strongest storms, bending but never breaking. It is the unwavering determination to rise again after every fall, and the ability to find strength in the face of adversity. Just as a diamond is formed under immense pressure, resilience is forged through challenges and hardships, making us stronger and more resilient in the process.\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Your task is to answer in a consistent style.\n", + "\n", + ": Teach me about patience.\n", + "\n", + ": The river that carves the deepest \\ \n", + "valley flows from a modest spring; the \\ \n", + "grandest symphony originates from a single note; \\ \n", + "the most intricate tapestry begins with a solitary thread.\n", + "\n", + ": Teach me about resilience.\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**2.1 指定完成任务所需的步骤**" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Completion for prompt 1:\n", + "1 - Jack and Jill, siblings, go on a quest to fetch water from a hilltop well, but encounter misfortune when Jack trips on a stone and tumbles down the hill, with Jill following suit, yet they return home and remain undeterred in their adventurous spirits.\n", + "\n", + "2 - Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils rentrent chez eux et restent déterminés dans leur esprit d'aventure.\n", + "\n", + "3 - Jack, Jill\n", + "\n", + "4 - {\n", + " \"french_summary\": \"Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils rentrent chez eux et restent déterminés dans leur esprit d'aventure.\",\n", + " \"num_names\": 2\n", + "}\n" + ] + } + ], + "source": [ + "text = f\"\"\"\n", + "In a charming village, siblings Jack and Jill set out on \\ \n", + "a quest to fetch water from a hilltop \\ \n", + "well. As they climbed, singing joyfully, misfortune \\ \n", + "struck—Jack tripped on a stone and tumbled \\ \n", + "down the hill, with Jill following suit. \\ \n", + "Though slightly battered, the pair returned home to \\ \n", + "comforting embraces. Despite the mishap, \\ \n", + "their adventurous spirits remained undimmed, and they \\ \n", + "continued exploring with delight.\n", + "\"\"\"\n", + "# example 1\n", + "prompt_1 = f\"\"\"\n", + "Perform the following actions: \n", + "1 - Summarize the following text delimited by triple \\\n", + "backticks with 1 sentence.\n", + "2 - Translate the summary into French.\n", + "3 - List each name in the French summary.\n", + "4 - Output a json object that contains the following \\\n", + "keys: french_summary, num_names.\n", + "\n", + "Separate your answers with line breaks.\n", + "\n", + "Text:\n", + "```{text}```\n", + "\"\"\"\n", + "response = get_completion(prompt_1)\n", + "print(\"Completion for prompt 1:\")\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Completion for prompt 2:\n", + "Summary: Jack and Jill, siblings from a charming village, go on a quest to fetch water from a hilltop well, but encounter misfortune when Jack trips on a stone and tumbles down the hill, with Jill following suit, yet they remain undeterred and continue exploring with delight.\n", + "\n", + "Translation: Jack et Jill, frère et sœur d'un charmant village, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils restent déterminés et continuent à explorer avec joie.\n", + "\n", + "Names: Jack, Jill\n", + "\n", + "Output JSON: \n", + "{\n", + " \"french_summary\": \"Jack et Jill, frère et sœur d'un charmant village, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils restent déterminés et continuent à explorer avec joie.\",\n", + " \"num_names\": 2\n", + "}\n" + ] + } + ], + "source": [ + "prompt_2 = f\"\"\"\n", + "Your task is to perform the following actions: \n", + "1 - Summarize the following text delimited by <> with 1 sentence.\n", + "2 - Translate the summary into French.\n", + "3 - List each name in the French summary.\n", + "4 - Output a json object that contains the \n", + "following keys: french_summary, num_names.\n", + "\n", + "Use the following format:\n", + "Text: \n", + "Summary: \n", + "Translation: \n", + "Names: \n", + "Output JSON: \n", + "\n", + "Text: <{text}>\n", + "\"\"\"\n", + "response = get_completion(prompt_2)\n", + "print(\"\\nCompletion for prompt 2:\")\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**2.2 指导模型在下结论之前找出一个自己的解法**" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The student's solution is correct. They correctly identified the costs for land, solar panels, and maintenance, and calculated the total cost for the first year of operations as a function of the number of square feet.\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Determine if the student's solution is correct or not.\n", + "\n", + "Question:\n", + "I'm building a solar power installation and I need \\\n", + " help working out the financials. \n", + "- Land costs $100 / square foot\n", + "- I can buy solar panels for $250 / square foot\n", + "- I negotiated a contract for maintenance that will cost \\ \n", + "me a flat $100k per year, and an additional $10 / square \\\n", + "foot\n", + "What is the total cost for the first year of operations \n", + "as a function of the number of square feet.\n", + "\n", + "Student's Solution:\n", + "Let x be the size of the installation in square feet.\n", + "Costs:\n", + "1. Land cost: 100x\n", + "2. Solar panel cost: 250x\n", + "3. Maintenance cost: 100,000 + 100x\n", + "Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "To calculate the total cost for the first year of operations, we need to add up the costs of land, solar panels, and maintenance.\n", + "\n", + "1. Land cost: $100 / square foot\n", + "The cost of land is $100 multiplied by the number of square feet.\n", + "\n", + "2. Solar panel cost: $250 / square foot\n", + "The cost of solar panels is $250 multiplied by the number of square feet.\n", + "\n", + "3. Maintenance cost: $100,000 + $10 / square foot\n", + "The maintenance cost is a flat fee of $100,000 per year, plus $10 multiplied by the number of square feet.\n", + "\n", + "Total cost: Land cost + Solar panel cost + Maintenance cost\n", + "\n", + "So the actual solution is:\n", + "Total cost = (100 * x) + (250 * x) + (100,000 + (10 * x))\n", + "\n", + "Is the student's solution the same as the actual solution just calculated:\n", + "No\n", + "\n", + "Student grade:\n", + "Incorrect\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Your task is to determine if the student's solution \\\n", + "is correct or not.\n", + "To solve the problem do the following:\n", + "- First, work out your own solution to the problem. \n", + "- Then compare your solution to the student's solution \\ \n", + "and evaluate if the student's solution is correct or not. \n", + "Don't decide if the student's solution is correct until \n", + "you have done the problem yourself.\n", + "\n", + "Use the following format:\n", + "Question:\n", + "```\n", + "question here\n", + "```\n", + "Student's solution:\n", + "```\n", + "student's solution here\n", + "```\n", + "Actual solution:\n", + "```\n", + "steps to work out the solution and your solution here\n", + "```\n", + "Is the student's solution the same as actual solution \\\n", + "just calculated:\n", + "```\n", + "yes or no\n", + "```\n", + "Student grade:\n", + "```\n", + "correct or incorrect\n", + "```\n", + "\n", + "Question:\n", + "```\n", + "I'm building a solar power installation and I need help \\\n", + "working out the financials. \n", + "- Land costs $100 / square foot\n", + "- I can buy solar panels for $250 / square foot\n", + "- I negotiated a contract for maintenance that will cost \\\n", + "me a flat $100k per year, and an additional $10 / square \\\n", + "foot\n", + "What is the total cost for the first year of operations \\\n", + "as a function of the number of square feet.\n", + "``` \n", + "Student's solution:\n", + "```\n", + "Let x be the size of the installation in square feet.\n", + "Costs:\n", + "1. Land cost: 100x\n", + "2. Solar panel cost: 250x\n", + "3. Maintenance cost: 100,000 + 100x\n", + "Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000\n", + "```\n", + "Actual solution:\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**3.1 幻觉**" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The AeroGlide UltraSlim Smart Toothbrush by Boie is a technologically advanced toothbrush designed to provide a superior brushing experience. Boie is a company known for its innovative oral care products, and the AeroGlide UltraSlim Smart Toothbrush is no exception.\n", + "\n", + "One of the standout features of this toothbrush is its ultra-slim design. The brush head is only 2mm thick, making it much thinner than traditional toothbrushes. This slim profile allows for better access to hard-to-reach areas of the mouth, ensuring a thorough and effective clean.\n", + "\n", + "The AeroGlide UltraSlim Smart Toothbrush also incorporates smart technology. It connects to a mobile app via Bluetooth, allowing users to track their brushing habits and receive personalized recommendations for improving their oral hygiene routine. The app provides real-time feedback on brushing technique, duration, and coverage, helping users to achieve optimal oral health.\n", + "\n", + "The toothbrush features soft, antimicrobial bristles made from a durable thermoplastic elastomer. These bristles are gentle on the gums and teeth, while also being effective at removing plaque and debris. The antimicrobial properties help to keep the brush head clean and hygienic between uses.\n", + "\n", + "Another notable feature of the AeroGlide UltraSlim Smart Toothbrush is its long battery life. It can last up to 30 days on a single charge, making it convenient for travel or everyday use without the need for frequent recharging.\n", + "\n", + "Overall, the AeroGlide UltraSlim Smart Toothbrush by Boie offers a combination of advanced technology, slim design, and effective cleaning capabilities. It is a great option for those looking to upgrade their oral care routine and achieve a healthier smile.\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + }, + "latex_envs": { + "LaTeX_envs_menu_present": true, + "autoclose": false, + "autocomplete": true, + "bibliofile": "biblio.bib", + "cite_by": "apalike", + "current_citInitial": 1, + "eqLabelWithNumbers": true, + "eqNumInitial": 1, + "hotkeys": { + "equation": "Ctrl-E", + "itemize": "Ctrl-I" + }, + "labels_anchors": false, + "latex_user_defs": false, + "report_style_numbering": false, + "user_envs_cfg": false + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/content/C1 Prompt Engineering for Developer/3. 迭代优化 Iterative.ipynb b/docs/content/C1 Prompt Engineering for Developer/3. 迭代优化 Iterative.ipynb index 5bf5ed4..2c23ad9 100644 --- a/docs/content/C1 Prompt Engineering for Developer/3. 迭代优化 Iterative.ipynb +++ b/docs/content/C1 Prompt Engineering for Developer/3. 迭代优化 Iterative.ipynb @@ -6,15 +6,13 @@ "source": [ "# 第三章 迭代优化\n", "\n", - "在开发大语言模型应用时,很难通过第一次尝试就得到完美适用的Prompt。但关键是要有一个**良好的迭代优化过程**,以不断改进Prompt。相比训练机器学习模型,Prompt的一次成功率可能更高,但仍需要通过多次迭代找到最适合应用的形式。\n", + "在开发大语言模型应用时,很难通过第一次尝试就得到完美适用的 Prompt。但关键是要有一个**良好的迭代优化过程**,以不断改进 Prompt。相比训练机器学习模型,Prompt 的一次成功率可能更高,但仍需要通过多次迭代找到最适合应用的形式。\n", "\n", - "本章以产品说明书生成营销文案为例,展示Prompt迭代优化的思路。这与吴恩达在机器学习课程中演示的机器学习模型开发流程相似:有了想法后,编写代码、获取数据、训练模型、查看结果。通过分析错误找出适用领域,调整方案后再次训练。Prompt开发也采用类似循环迭代的方式,逐步逼近最优。\n", - "\n", - "具体来说,有了任务想法后,可以先编写初版Prompt,注意清晰明确并给模型充足思考时间。运行后检查结果,如果不理想,则分析Prompt不够清楚或思考时间不够等原因,做出改进,再次运行。如此循环多次,终将找到适合应用的Prompt。\n", + "本章以产品说明书生成营销文案为例,展示 Prompt 迭代优化的思路。这与吴恩达在机器学习课程中演示的机器学习模型开发流程相似:有了想法后,编写代码、获取数据、训练模型、查看结果。通过分析错误找出适用领域,调整方案后再次训练。Prompt 开发也采用类似循环迭代的方式,逐步逼近最优。具体来说,有了任务想法后,可以先编写初版 Prompt,注意清晰明确并给模型充足思考时间。运行后检查结果,如果不理想,则分析 Prompt 不够清楚或思考时间不够等原因,做出改进,再次运行。如此循环多次,终将找到适合应用的 Prompt。\n", " \n", "![1](../../../figures/docs/C1/Iterative-Prompt-Develelopment.png)\n", - " \n", - "总之,很难有适用于世间万物的所谓“最佳 Prompt ”,开发高效Prompt的关键在于找到一个好的迭代优化过程,而非一开始就要求完美。通过快速试错迭代,可有效确定符合特定应用的最佳Prompt形式。\n" + "\n", + "总之,很难有适用于世间万物的所谓“最佳 Prompt ”,开发高效 Prompt 的关键在于找到一个好的迭代优化过程,而非一开始就要求完美。通过快速试错迭代,可有效确定符合特定应用的最佳 Prompt 形式。\n" ] }, { @@ -135,7 +133,7 @@ "\n", "它似乎很好地完成了要求,即从技术说明书开始编写产品描述,介绍了一个精致的中世纪风格办公椅。但是当我看到这个生成的内容时,我会觉得它**太长了**。\n", "\n", - "在看到语言模型根据产品说明生成的第一个版本营销文案后,我们注意到文本长度过长,不太适合用作简明的电商广告语。所以这时候就需要对Prompt进行优化改进。具体来说,第一版结果满足了从技术说明转换为营销文案的要求,描写了中世纪风格办公椅的细节。但是过于冗长的文本不太适合电商场景。这时我们就可以**在Prompt中添加长度限制**,要求生成更简洁的文案。\n" + "在看到语言模型根据产品说明生成的第一个版本营销文案后,我们注意到文本长度过长,不太适合用作简明的电商广告语。所以这时候就需要对 Prompt 进行优化改进。具体来说,第一版结果满足了从技术说明转换为营销文案的要求,描写了中世纪风格办公椅的细节。但是过于冗长的文本不太适合电商场景。这时我们就可以**在 Prompt 中添加长度限制**,要求生成更简洁的文案。\n" ] }, { @@ -173,6 +171,13 @@ "print(response)\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "我们可以计算一下输出的长度。" + ] + }, { "cell_type": "code", "execution_count": 11, @@ -198,8 +203,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "\n", - "当在Prompt中设置长度限制要求时,语言模型生成的输出长度不总能精确符合要求,但基本能控制在可接受的误差范围内。比如要求生成50词的文本,语言模型有时会生成60词左右的输出,但总体接近预定长度。\n", + "当在 Prompt 中设置长度限制要求时,语言模型生成的输出长度不总能精确符合要求,但基本能控制在可接受的误差范围内。比如要求生成50词的文本,语言模型有时会生成60词左右的输出,但总体接近预定长度。\n", "\n", "这是因为**语言模型在计算和判断文本长度时依赖于分词器**,而分词器在字符统计方面不具备完美精度。目前存在多种方法可以尝试控制语言模型生成输出的长度,比如指定语句数、词数、汉字数等。\n", "\n", @@ -212,13 +216,11 @@ "source": [ "### 1.3 提示优化2: 处理抓错文本细节\n", "\n", - "在迭代优化Prompt的过程中,我们还需要注意语言模型生成文本的细节是否符合预期。\n", + "在迭代优化 Prompt 的过程中,我们还需要注意语言模型生成文本的细节是否符合预期。\n", "\n", - "比如在这个案例中,进一步分析会发现,该椅子面向的其实是家具零售商,而不是终端消费者。所以生成的文案中过多强调风格、氛围等方面,而较少涉及产品技术细节,与目标受众的关注点不太吻合。这时候我们就可以继续调整Prompt,明确要求语言模型生成面向家具零售商的描述,更多关注材质、工艺、结构等技术方面的表述。\n", + "比如在这个案例中,进一步分析会发现,该椅子面向的其实是家具零售商,而不是终端消费者。所以生成的文案中过多强调风格、氛围等方面,而较少涉及产品技术细节,与目标受众的关注点不太吻合。这时候我们就可以继续调整 Prompt,明确要求语言模型生成面向家具零售商的描述,更多关注材质、工艺、结构等技术方面的表述。\n", "\n", - "通过迭代地分析结果,检查是否捕捉到正确的细节,我们可以逐步优化Prompt,使语言模型生成的文本更加符合预期的样式和内容要求。\n", - "\n", - "细节的精准控制是语言生成任务中非常重要的一点。我们需要训练语言模型**根据不同目标受众关注不同的方面,输出风格和内容上都适合的文本**。" + "通过迭代地分析结果,检查是否捕捉到正确的细节,我们可以逐步优化 Prompt,使语言模型生成的文本更加符合预期的样式和内容要求。细节的精准控制是语言生成任务中非常重要的一点。我们需要训练语言模型**根据不同目标受众关注不同的方面,输出风格和内容上都适合的文本**。" ] }, { @@ -257,7 +259,7 @@ "source": [ "可见,通过修改 Prompt ,模型的关注点倾向了具体特征与技术细节。\n", "\n", - "我可能进一步想要在描述的结尾展示出产品ID。因此,我可以进一步改进这个 Prompt ,要求在描述的结尾,展示出说明书中的7位产品ID。" + "我可能进一步想要在描述的结尾展示出产品 ID。因此,我可以进一步改进这个 Prompt ,要求在描述的结尾,展示出说明书中的7位产品 ID。" ] }, { @@ -296,11 +298,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "通过上面的示例,我们可以看到Prompt迭代优化的一般过程。与训练机器学习模型类似,设计高效Prompt也需要多个版本的试错调整。\n", + "通过上面的示例,我们可以看到 Prompt 迭代优化的一般过程。与训练机器学习模型类似,设计高效 Prompt 也需要多个版本的试错调整。\n", "\n", - "具体来说,第一版Prompt应该满足明确和给模型思考时间两个原则。在此基础上,一般的迭代流程是:首先尝试一个初版,分析结果,然后继续改进Prompt,逐步逼近最优。许多成功的Prompt都是通过这种多轮调整得出的。\n", + "具体来说,第一版 Prompt 应该满足明确和给模型思考时间两个原则。在此基础上,一般的迭代流程是:首先尝试一个初版,分析结果,然后继续改进 Prompt,逐步逼近最优。许多成功的Prompt 都是通过这种多轮调整得出的。\n", "\n", - "后面我会展示一个更复杂的Prompt案例,让大家更深入地了解语言模型的强大能力。但在此之前,我想强调Prompt设计是一个循序渐进的过程。开发者需要做好多次尝试和错误的心理准备,通过不断调整和优化,才能找到最符合具体场景需求的Prompt形式。这需要智慧和毅力,但结果往往是值得的。\n", + "后面我会展示一个更复杂的 Prompt 案例,让大家更深入地了解语言模型的强大能力。但在此之前,我想强调 Prompt 设计是一个循序渐进的过程。开发者需要做好多次尝试和错误的心理准备,通过不断调整和优化,才能找到最符合具体场景需求的 Prompt 形式。这需要智慧和毅力,但结果往往是值得的。\n", "\n", "让我们继续探索提示工程的奥秘,开发出令人惊叹的大语言模型应用吧!" ] @@ -379,6 +381,13 @@ "print(response)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "上述输出为 HTML 代码,我们可以使用 Python 的 IPython 库将 HTML 代码加载出来。" + ] + }, { "cell_type": "code", "execution_count": 20, @@ -445,15 +454,13 @@ "metadata": {}, "source": [ "\n", - "本章重点讲解了在开发大语言模型应用时,采用迭代方式不断优化Prompt的过程。作为Prompt工程师,关键不是一开始就要求完美的Prompt,而是掌握有效的Prompt开发流程。\n", + "本章重点讲解了在开发大语言模型应用时,采用迭代方式不断优化 Prompt 的过程。作为 Prompt 工程师,关键不是一开始就要求完美的 Prompt,而是掌握有效的 Prompt 开发流程。\n", "\n", - "具体来说,首先编写初版Prompt,然后通过多轮调整逐步改进,直到生成了满意的结果。对于更复杂的应用,可以在多个样本上进行迭代训练,评估Prompt的平均表现。\n", + "具体来说,首先编写初版 Prompt,然后通过多轮调整逐步改进,直到生成了满意的结果。对于更复杂的应用,可以在多个样本上进行迭代训练,评估 Prompt 的平均表现。在应用较为成熟后,才需要采用在多个样本集上评估 Prompt 性能的方式来进行细致优化。因为这需要较高的计算资源。\n", "\n", - "在应用较为成熟后,才需要采用在多个样本集上评估Prompt性能的方式来进行细致优化。因为这需要较高的计算资源。\n", + "总之,Prompt 工程师的核心是掌握 Prompt 的迭代开发和优化技巧,而非一开始就要求100%完美。通过不断调整试错,最终找到可靠适用的 Prompt 形式才是设计 Prompt 的正确方法。\n", "\n", - "总之,Prompt工程师的核心是掌握Prompt的迭代开发和优化技巧,而非一开始就要求100%完美。通过不断调整试错,最终找到可靠适用的Prompt形式才是设计Prompt的正确方法。\n", - "\n", - "读者可以在Jupyter Notebook上,对本章给出的示例进行实践,修改Prompt并观察不同输出,以深入理解Prompt迭代优化的过程。这会对进一步开发复杂语言模型应用提供很好的实践准备。\n" + "读者可以在 Jupyter Notebook 上,对本章给出的示例进行实践,修改 Prompt 并观察不同输出,以深入理解 Prompt 迭代优化的过程。这会对进一步开发复杂语言模型应用提供很好的实践准备。\n" ] }, { @@ -899,7 +906,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.8.10" }, "latex_envs": { "LaTeX_envs_menu_present": true, diff --git a/docs/content/环境配置.ipynb b/docs/content/环境配置.ipynb index e6ee46f..bbebe3a 100644 --- a/docs/content/环境配置.ipynb +++ b/docs/content/环境配置.ipynb @@ -221,7 +221,7 @@ ], "metadata": { "kernelspec": { - "display_name": "gpt_flask", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -235,10 +235,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" - }, - "orig_nbformat": 4 + "version": "3.10.11" + } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } From b1990bf20c03980f19725dc86dc4c903d76eedc8 Mon Sep 17 00:00:00 2001 From: nowadays0421 Date: Mon, 24 Jul 2023 16:39:16 +0800 Subject: [PATCH 2/2] Finish PE-final --- .../4. 文本概括 Summarizing.ipynb | 743 +++++++++++++++++- .../5. 推断 Inferring.ipynb | 16 +- .../6. 文本转换 Transforming.ipynb | 60 +- .../7. 文本扩展 Expanding.ipynb | 25 +- .../8. 聊天机器人 Chatbot.ipynb | 20 +- 5 files changed, 789 insertions(+), 75 deletions(-) diff --git a/docs/content/C1 Prompt Engineering for Developer/4. 文本概括 Summarizing.ipynb b/docs/content/C1 Prompt Engineering for Developer/4. 文本概括 Summarizing.ipynb index cef2062..6c2f9ea 100644 --- a/docs/content/C1 Prompt Engineering for Developer/4. 文本概括 Summarizing.ipynb +++ b/docs/content/C1 Prompt Engineering for Developer/4. 文本概括 Summarizing.ipynb @@ -1 +1,742 @@ -{"cells":[{"attachments":{},"cell_type":"markdown","id":"b58204ea","metadata":{},"source":["# 第四章 文本概括\n"]},{"attachments":{},"cell_type":"markdown","id":"12fa9ea4","metadata":{},"source":["在繁忙的信息时代,小明是一名热心的开发者,面临着海量的文本信息处理的挑战。他需要通过研究无数的文献资料来为他的项目找到关键的信息,但是时间却远远不够。在他焦头烂额之际,他发现了大型语言模型(LLM)的文本摘要功能。\n","\n","这个功能对小明来说如同灯塔一样,照亮了他处理信息海洋的道路。LLM的强大能力在于它可以将复杂的文本信息简化,提炼出关键的观点,这对于他来说无疑是巨大的帮助。他不再需要花费大量的时间去阅读所有的文档,只需要用LLM将它们概括,就可以快速获取到他所需要的信息。\n","\n","通过编程调用API接口,小明成功实现了这个文本摘要的功能。他感叹道:“这简直就像一道魔法,将无尽的信息海洋变成了清晰的信息源泉。”小明的经历,展现了LLM文本摘要功能的巨大优势:**节省时间**,**提高效率**,以及**精准获取信息**。这就是我们本章要介绍的内容,让我们一起来探索如何利用编程和调用API接口,掌握这个强大的工具。"]},{"attachments":{},"cell_type":"markdown","id":"9cca835b","metadata":{},"source":["## 一、单一文本概括"]},{"attachments":{},"cell_type":"markdown","id":"0c1e1b92","metadata":{},"source":["以商品评论的总结任务为例:对于电商平台来说,网站上往往存在着海量的商品评论,这些评论反映了所有客户的想法。如果我们拥有一个工具去概括这些海量、冗长的评论,便能够快速地浏览更多评论,洞悉客户的偏好,从而指导平台与商家提供更优质的服务。"]},{"attachments":{},"cell_type":"markdown","id":"aad5bd2a","metadata":{},"source":["**输入文本**"]},{"cell_type":"markdown","id":"11c360ae","metadata":{},"source":["这是一段在线商品评价,可能来自于一个在线购物平台,例如亚马逊、淘宝、京东等。评价者为一款熊猫公仔进行了点评,评价内容包括商品的质量、大小、价格和物流速度等因素,以及他的女儿对该商品的喜爱程度。"]},{"cell_type":"code","execution_count":2,"id":"43b5dd25","metadata":{},"outputs":[],"source":["prod_review = \"\"\"\n","这个熊猫公仔是我给女儿的生日礼物,她很喜欢,去哪都带着。\n","公仔很软,超级可爱,面部表情也很和善。但是相比于价钱来说,\n","它有点小,我感觉在别的地方用同样的价钱能买到更大的。\n","快递比预期提前了一天到货,所以在送给女儿之前,我自己玩了会。\n","\"\"\""]},{"attachments":{},"cell_type":"markdown","id":"662c9cd2","metadata":{},"source":["### 1.1 限制输出文本长度"]},{"attachments":{},"cell_type":"markdown","id":"a6d10814","metadata":{},"source":["我们尝试将文本的长度限制在30个字以内。"]},{"cell_type":"code","execution_count":5,"id":"bf4b39f9","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["熊猫公仔软可爱,女儿喜欢,但有点小。快递提前一天到货。\n"]}],"source":["from tool import get_completion\n","\n","prompt = f\"\"\"\n","您的任务是从电子商务网站上生成一个产品评论的简短摘要。\n","\n","请对三个反引号之间的评论文本进行概括,最多30个字。\n","\n","评论: ```{prod_review}```\n","\"\"\"\n","\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"markdown","id":"fce32884","metadata":{},"source":["我们可以看到语言模型给了我们一个符合要求的结果。\n","\n","注意:在上一节中我们提到了语言模型在计算和判断文本长度时依赖于分词器,而分词器在字符统计方面不具备完美精度。"]},{"attachments":{},"cell_type":"markdown","id":"e9ab145e","metadata":{},"source":["### 1.2 设置关键角度侧重"]},{"attachments":{},"cell_type":"markdown","id":"f84d0123","metadata":{},"source":["在某些情况下,我们会针对不同的业务场景对文本的侧重会有所不同。例如,在商品评论文本中,物流部门可能更专注于运输的时效性,商家则更关注价格和商品质量,而平台则更看重整体的用户体验。\n","\n","我们可以通过增强输入提示(Prompt),来强调我们对某一特定视角的重视。"]},{"attachments":{},"cell_type":"markdown","id":"d6f8509a","metadata":{},"source":["#### 1.2.1 侧重于快递服务"]},{"cell_type":"code","execution_count":7,"id":"80636c3e","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["快递提前到货,公仔可爱但有点小。\n"]}],"source":["prompt = f\"\"\"\n","您的任务是从电子商务网站上生成一个产品评论的简短摘要。\n","\n","请对三个反引号之间的评论文本进行概括,最多30个字,并且侧重在快递服务上。\n","\n","评论: ```{prod_review}```\n","\"\"\"\n","\n","response = get_completion(prompt)\n","print(response)"]},{"attachments":{},"cell_type":"markdown","id":"76c97fea","metadata":{},"source":["通过输出结果,我们可以看到,文本以“快递提前到货”开头,体现了对于快递效率的侧重。"]},{"attachments":{},"cell_type":"markdown","id":"83275907","metadata":{},"source":["#### 1.2.2 侧重于价格与质量"]},{"cell_type":"code","execution_count":8,"id":"728d6c57","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["可爱的熊猫公仔,质量好但有点小,价格稍高。快递提前到货。\n"]}],"source":["prompt = f\"\"\"\n","您的任务是从电子商务网站上生成一个产品评论的简短摘要。\n","\n","请对三个反引号之间的评论文本进行概括,最多30个词汇,并且侧重在产品价格和质量上。\n","\n","评论: ```{prod_review}```\n","\"\"\"\n","\n","response = get_completion(prompt)\n","print(response)"]},{"attachments":{},"cell_type":"markdown","id":"972dbb1b","metadata":{},"source":["通过输出的结果,我们可以看到,文本以“可爱的熊猫公仔,质量好但有点小,价格稍高”开头,体现了对于产品价格与质量的侧重。"]},{"attachments":{},"cell_type":"markdown","id":"b3ed53d2","metadata":{},"source":["### 1.3 关键信息提取"]},{"attachments":{},"cell_type":"markdown","id":"ba6f5c25","metadata":{},"source":["在1.2节中,虽然我们通过添加关键角度侧重的 Prompt ,确实让文本摘要更侧重于某一特定方面,然而,我们可以发现,在结果中也会保留一些其他信息,比如偏重价格与质量角度的概括中仍保留了“快递提前到货”的信息。如果我们只想要提取某一角度的信息,并过滤掉其他所有信息,则可以要求 LLM 进行“**文本提取( Extract )**”而非“概括( Summarize )”。"]},{"cell_type":"markdown","id":"da39760c","metadata":{},"source":["下面让我们来一起来对文本进行提取信息吧!"]},{"cell_type":"code","execution_count":9,"id":"c845ccab","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["产品运输相关的信息:快递提前一天到货。\n"]}],"source":["prompt = f\"\"\"\n","您的任务是从电子商务网站上的产品评论中提取相关信息。\n","\n","请从以下三个反引号之间的评论文本中提取产品运输相关的信息,最多30个词汇。\n","\n","评论: ```{prod_review}```\n","\"\"\"\n","\n","response = get_completion(prompt)\n","print(response)"]},{"attachments":{},"cell_type":"markdown","id":"50498a2b","metadata":{},"source":["## 二、同时概括多条文本"]},{"attachments":{},"cell_type":"markdown","id":"a291541a","metadata":{},"source":["在实际的工作流中,我们往往要处理大量的评论文本,下面的示例将多条用户评价集合在一个列表中,并利用 ```for``` 循环和文本概括(Summarize)提示词,将评价概括至小于 20 个词以下,并按顺序打印。当然,在实际生产中,对于不同规模的评论文本,除了使用 ```for``` 循环以外,还可能需要考虑整合评论、分布式等方法提升运算效率。您可以搭建主控面板,来总结大量用户评论,以及方便您或他人快速浏览,还可以点击查看原评论。这样,您就能高效掌握顾客的所有想法。"]},{"cell_type":"code","execution_count":3,"id":"ef606961","metadata":{},"outputs":[],"source":["review_1 = prod_review\n","\n","# 一盏落地灯的评论\n","review_2 = \"\"\"\n","我需要一盏漂亮的卧室灯,这款灯不仅具备额外的储物功能,价格也并不算太高。\n","收货速度非常快,仅用了两天的时间就送到了。\n","不过,在运输过程中,灯的拉线出了问题,幸好,公司很乐意寄送了一根全新的灯线。\n","新的灯线也很快就送到手了,只用了几天的时间。\n","装配非常容易。然而,之后我发现有一个零件丢失了,于是我联系了客服,他们迅速地给我寄来了缺失的零件!\n","对我来说,这是一家非常关心客户和产品的优秀公司。\n","\"\"\"\n","\n","# 一把电动牙刷的评论\n","review_3 = \"\"\"\n","我的牙科卫生员推荐了电动牙刷,所以我就买了这款。\n","到目前为止,电池续航表现相当不错。\n","初次充电后,我在第一周一直将充电器插着,为的是对电池进行条件养护。\n","过去的3周里,我每天早晚都使用它刷牙,但电池依然维持着原来的充电状态。\n","不过,牙刷头太小了。我见过比这个牙刷头还大的婴儿牙刷。\n","我希望牙刷头更大一些,带有不同长度的刷毛,\n","这样可以更好地清洁牙齿间的空隙,但这款牙刷做不到。\n","总的来说,如果你能以50美元左右的价格购买到这款牙刷,那是一个不错的交易。\n","制造商的替换刷头相当昂贵,但你可以购买价格更为合理的通用刷头。\n","这款牙刷让我感觉就像每天都去了一次牙医,我的牙齿感觉非常干净!\n","\"\"\"\n","\n","# 一台搅拌机的评论\n","review_4 = \"\"\"\n","在11月份期间,这个17件套装还在季节性促销中,售价约为49美元,打了五折左右。\n","可是由于某种原因(我们可以称之为价格上涨),到了12月的第二周,所有的价格都上涨了,\n","同样的套装价格涨到了70-89美元不等。而11件套装的价格也从之前的29美元上涨了约10美元。\n","看起来还算不错,但是如果你仔细看底座,刀片锁定的部分看起来没有前几年版本的那么漂亮。\n","然而,我打算非常小心地使用它\n","(例如,我会先在搅拌机中研磨豆类、冰块、大米等坚硬的食物,然后再将它们研磨成所需的粒度,\n","接着切换到打蛋器刀片以获得更细的面粉,如果我需要制作更细腻/少果肉的食物)。\n","在制作冰沙时,我会将要使用的水果和蔬菜切成细小块并冷冻\n","(如果使用菠菜,我会先轻微煮熟菠菜,然后冷冻,直到使用时准备食用。\n","如果要制作冰糕,我会使用一个小到中号的食物加工器),这样你就可以避免添加过多的冰块。\n","大约一年后,电机开始发出奇怪的声音。我打电话给客户服务,但保修期已经过期了,\n","所以我只好购买了另一台。值得注意的是,这类产品的整体质量在过去几年里有所下降\n",",所以他们在一定程度上依靠品牌认知和消费者忠诚来维持销售。在大约两天内,我收到了新的搅拌机。\n","\"\"\"\n","\n","reviews = [review_1, review_2, review_3, review_4]\n"]},{"cell_type":"code","execution_count":4,"id":"eb878522","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["评论1: 熊猫公仔是生日礼物,女儿喜欢,软可爱,面部表情和善。价钱有点小,快递提前一天到货。 \n","\n","评论2: 漂亮卧室灯,储物功能,快速送达,灯线问题,快速解决,容易装配,关心客户和产品。 \n","\n","评论3: 这款电动牙刷电池续航好,但牙刷头太小,价格合理,清洁效果好。 \n","\n","评论4: 该评论提到了一个17件套装的产品,在11月份有折扣销售,但在12月份价格上涨。评论者提到了产品的外观和使用方法,并提到了产品质量下降的问题。最后,评论者提到他们购买了另一台搅拌机。 \n","\n"]}],"source":["for i in range(len(reviews)):\n"," prompt = f\"\"\"\n"," 你的任务是从电子商务网站上的产品评论中提取相关信息。\n","\n"," 请对三个反引号之间的评论文本进行概括,最多20个词汇。\n","\n"," 评论文本: ```{reviews[i]}```\n"," \"\"\"\n"," response = get_completion(prompt)\n"," print(f\"评论{i+1}: \", response, \"\\n\")\n"]},{"cell_type":"markdown","id":"f118c0cc","metadata":{},"source":["## 三、英文版"]},{"cell_type":"markdown","id":"a08635df","metadata":{},"source":["**1.1 单一文本概括**"]},{"cell_type":"code","execution_count":12,"id":"e55327d5","metadata":{},"outputs":[],"source":["prod_review = \"\"\"\n","Got this panda plush toy for my daughter's birthday, \\\n","who loves it and takes it everywhere. It's soft and \\ \n","super cute, and its face has a friendly look. It's \\ \n","a bit small for what I paid though. I think there \\ \n","might be other options that are bigger for the \\ \n","same price. It arrived a day earlier than expected, \\ \n","so I got to play with it myself before I gave it \\ \n","to her.\n","\"\"\""]},{"cell_type":"code","execution_count":13,"id":"30c2ef51","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["This panda plush toy is loved by the reviewer's daughter, but they feel it is a bit small for the price.\n"]}],"source":["prompt = f\"\"\"\n","Your task is to generate a short summary of a product \\\n","review from an ecommerce site. \n","\n","Summarize the review below, delimited by triple \n","backticks, in at most 30 words. \n","\n","Review: ```{prod_review}```\n","\"\"\"\n","\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"markdown","id":"9bdcfc1b","metadata":{},"source":["**1.2 设置关键角度侧重**"]},{"cell_type":"markdown","id":"5dd0534f","metadata":{},"source":["1.2.1 侧重于快递服务"]},{"cell_type":"code","execution_count":14,"id":"b354cc3f","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["The customer is happy with the product but suggests offering larger options for the same price. They were pleased with the early delivery.\n"]}],"source":["prompt = f\"\"\"\n","Your task is to generate a short summary of a product \\\n","review from an ecommerce site to give feedback to the \\\n","Shipping deparmtment. \n","\n","Summarize the review below, delimited by triple \n","backticks, in at most 30 words, and focusing on any aspects \\\n","that mention shipping and delivery of the product. \n","\n","Review: ```{prod_review}```\n","\"\"\"\n","\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"markdown","id":"af6aaf3a","metadata":{},"source":["1.2.2 侧重于价格和质量"]},{"cell_type":"code","execution_count":15,"id":"1b5358fd","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["The customer loves the panda plush toy for its softness and cuteness, but feels it is overpriced compared to other options available.\n"]}],"source":["prompt = f\"\"\"\n","Your task is to generate a short summary of a product \\\n","review from an ecommerce site to give feedback to the \\\n","pricing deparmtment, responsible for determining the \\\n","price of the product. \n","\n","Summarize the review below, delimited by triple \n","backticks, in at most 30 words, and focusing on any aspects \\\n","that are relevant to the price and perceived value. \n","\n","Review: ```{prod_review}```\n","\"\"\"\n","\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"markdown","id":"0f582677","metadata":{},"source":["**1.3 关键信息提取**"]},{"cell_type":"code","execution_count":16,"id":"32c87014","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["The shipping department should take note that the product arrived a day earlier than expected.\n"]}],"source":["prompt = f\"\"\"\n","Your task is to extract relevant information from \\ \n","a product review from an ecommerce site to give \\\n","feedback to the Shipping department. \n","\n","From the review below, delimited by triple quotes \\\n","extract the information relevant to shipping and \\ \n","delivery. Limit to 30 words. \n","\n","Review: ```{prod_review}```\n","\"\"\"\n","\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"markdown","id":"2043d100","metadata":{},"source":["**2.1 同时概括多条文本**"]},{"cell_type":"code","execution_count":17,"id":"cff48486","metadata":{},"outputs":[],"source":["review_1 = prod_review \n","\n","# review for a standing lamp\n","review_2 = \"\"\"\n","Needed a nice lamp for my bedroom, and this one \\\n","had additional storage and not too high of a price \\\n","point. Got it fast - arrived in 2 days. The string \\\n","to the lamp broke during the transit and the company \\\n","happily sent over a new one. Came within a few days \\\n","as well. It was easy to put together. Then I had a \\\n","missing part, so I contacted their support and they \\\n","very quickly got me the missing piece! Seems to me \\\n","to be a great company that cares about their customers \\\n","and products. \n","\"\"\"\n","\n","# review for an electric toothbrush\n","review_3 = \"\"\"\n","My dental hygienist recommended an electric toothbrush, \\\n","which is why I got this. The battery life seems to be \\\n","pretty impressive so far. After initial charging and \\\n","leaving the charger plugged in for the first week to \\\n","condition the battery, I've unplugged the charger and \\\n","been using it for twice daily brushing for the last \\\n","3 weeks all on the same charge. But the toothbrush head \\\n","is too small. I’ve seen baby toothbrushes bigger than \\\n","this one. I wish the head was bigger with different \\\n","length bristles to get between teeth better because \\\n","this one doesn’t. Overall if you can get this one \\\n","around the $50 mark, it's a good deal. The manufactuer's \\\n","replacements heads are pretty expensive, but you can \\\n","get generic ones that're more reasonably priced. This \\\n","toothbrush makes me feel like I've been to the dentist \\\n","every day. My teeth feel sparkly clean! \n","\"\"\"\n","\n","# review for a blender\n","review_4 = \"\"\"\n","So, they still had the 17 piece system on seasonal \\\n","sale for around $49 in the month of November, about \\\n","half off, but for some reason (call it price gouging) \\\n","around the second week of December the prices all went \\\n","up to about anywhere from between $70-$89 for the same \\\n","system. And the 11 piece system went up around $10 or \\\n","so in price also from the earlier sale price of $29. \\\n","So it looks okay, but if you look at the base, the part \\\n","where the blade locks into place doesn’t look as good \\\n","as in previous editions from a few years ago, but I \\\n","plan to be very gentle with it (example, I crush \\\n","very hard items like beans, ice, rice, etc. in the \\\n","blender first then pulverize them in the serving size \\\n","I want in the blender then switch to the whipping \\\n","blade for a finer flour, and use the cross cutting blade \\\n","first when making smoothies, then use the flat blade \\\n","if I need them finer/less pulpy). Special tip when making \\\n","smoothies, finely cut and freeze the fruits and \\\n","vegetables (if using spinach-lightly stew soften the \\\n","spinach then freeze until ready for use-and if making \\\n","sorbet, use a small to medium sized food processor) \\\n","that you plan to use that way you can avoid adding so \\\n","much ice if at all-when making your smoothie. \\\n","After about a year, the motor was making a funny noise. \\\n","I called customer service but the warranty expired \\\n","already, so I had to buy another one. FYI: The overall \\\n","quality has gone done in these types of products, so \\\n","they are kind of counting on brand recognition and \\\n","consumer loyalty to maintain sales. Got it in about \\\n","two days.\n","\"\"\"\n","\n","reviews = [review_1, review_2, review_3, review_4]"]},{"cell_type":"code","execution_count":18,"id":"3f61080b","metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["0 Soft and cute panda plush toy loved by daughter, but small for the price. Arrived early. \n","\n","1 Great lamp with storage, fast delivery, excellent customer service, and easy assembly. Highly recommended. \n","\n","2 Impressive battery life, but toothbrush head is too small. Good deal if bought around $50. \n","\n","3 The reviewer found the price increase after the sale disappointing and noticed a decrease in quality over time. \n","\n"]}],"source":["for i in range(len(reviews)):\n"," prompt = f\"\"\"\n"," Your task is to generate a short summary of a product \\\n"," review from an ecommerce site. \n","\n"," Summarize the review below, delimited by triple \\\n"," backticks in at most 20 words. \n","\n"," Review: ```{reviews[i]}```\n"," \"\"\"\n"," response = get_completion(prompt)\n"," print(i, response, \"\\n\")"]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.11"},"latex_envs":{"LaTeX_envs_menu_present":true,"autoclose":false,"autocomplete":true,"bibliofile":"biblio.bib","cite_by":"apalike","current_citInitial":1,"eqLabelWithNumbers":true,"eqNumInitial":1,"hotkeys":{"equation":"Ctrl-E","itemize":"Ctrl-I"},"labels_anchors":false,"latex_user_defs":false,"report_style_numbering":false,"user_envs_cfg":false},"toc":{"base_numbering":1,"nav_menu":{},"number_sections":true,"sideBar":true,"skip_h1_title":false,"title_cell":"Table of Contents","title_sidebar":"Contents","toc_cell":false,"toc_position":{},"toc_section_display":true,"toc_window_display":true}},"nbformat":4,"nbformat_minor":5} +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b58204ea", + "metadata": {}, + "source": [ + "# 第四章 文本概括\n" + ] + }, + { + "cell_type": "markdown", + "id": "12fa9ea4", + "metadata": {}, + "source": [ + "在繁忙的信息时代,小明是一名热心的开发者,面临着海量的文本信息处理的挑战。他需要通过研究无数的文献资料来为他的项目找到关键的信息,但是时间却远远不够。在他焦头烂额之际,他发现了大型语言模型(LLM)的文本摘要功能。\n", + "\n", + "这个功能对小明来说如同灯塔一样,照亮了他处理信息海洋的道路。LLM 的强大能力在于它可以将复杂的文本信息简化,提炼出关键的观点,这对于他来说无疑是巨大的帮助。他不再需要花费大量的时间去阅读所有的文档,只需要用 LLM 将它们概括,就可以快速获取到他所需要的信息。\n", + "\n", + "通过编程调用 AP I接口,小明成功实现了这个文本摘要的功能。他感叹道:“这简直就像一道魔法,将无尽的信息海洋变成了清晰的信息源泉。”小明的经历,展现了LLM文本摘要功能的巨大优势:**节省时间**,**提高效率**,以及**精准获取信息**。这就是我们本章要介绍的内容,让我们一起来探索如何利用编程和调用API接口,掌握这个强大的工具。" + ] + }, + { + "cell_type": "markdown", + "id": "9cca835b", + "metadata": {}, + "source": [ + "## 一、单一文本概括" + ] + }, + { + "cell_type": "markdown", + "id": "0c1e1b92", + "metadata": {}, + "source": [ + "以商品评论的总结任务为例:对于电商平台来说,网站上往往存在着海量的商品评论,这些评论反映了所有客户的想法。如果我们拥有一个工具去概括这些海量、冗长的评论,便能够快速地浏览更多评论,洞悉客户的偏好,从而指导平台与商家提供更优质的服务。" + ] + }, + { + "cell_type": "markdown", + "id": "11c360ae", + "metadata": {}, + "source": [ + "接下来我们提供一段在线商品评价作为示例,可能来自于一个在线购物平台,例如亚马逊、淘宝、京东等。评价者为一款熊猫公仔进行了点评,评价内容包括商品的质量、大小、价格和物流速度等因素,以及他的女儿对该商品的喜爱程度。" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "43b5dd25", + "metadata": {}, + "outputs": [], + "source": [ + "prod_review = \"\"\"\n", + "这个熊猫公仔是我给女儿的生日礼物,她很喜欢,去哪都带着。\n", + "公仔很软,超级可爱,面部表情也很和善。但是相比于价钱来说,\n", + "它有点小,我感觉在别的地方用同样的价钱能买到更大的。\n", + "快递比预期提前了一天到货,所以在送给女儿之前,我自己玩了会。\n", + "\"\"\"" + ] + }, + { + "cell_type": "markdown", + "id": "662c9cd2", + "metadata": {}, + "source": [ + "### 1.1 限制输出文本长度" + ] + }, + { + "cell_type": "markdown", + "id": "a6d10814", + "metadata": {}, + "source": [ + "我们首先尝试将文本的长度限制在30个字以内。" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "bf4b39f9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "熊猫公仔软可爱,女儿喜欢,但有点小。快递提前一天到货。\n" + ] + } + ], + "source": [ + "from tool import get_completion\n", + "\n", + "prompt = f\"\"\"\n", + "您的任务是从电子商务网站上生成一个产品评论的简短摘要。\n", + "\n", + "请对三个反引号之间的评论文本进行概括,最多30个字。\n", + "\n", + "评论: ```{prod_review}```\n", + "\"\"\"\n", + "\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "fce32884", + "metadata": {}, + "source": [ + "我们可以看到语言模型给了我们一个符合要求的结果。\n", + "\n", + "注意:在上一节中我们提到了语言模型在计算和判断文本长度时依赖于分词器,而分词器在字符统计方面不具备完美精度。" + ] + }, + { + "cell_type": "markdown", + "id": "e9ab145e", + "metadata": {}, + "source": [ + "### 1.2 设置关键角度侧重" + ] + }, + { + "cell_type": "markdown", + "id": "f84d0123", + "metadata": {}, + "source": [ + "在某些情况下,我们会针对不同的业务场景对文本的侧重会有所不同。例如,在商品评论文本中,物流部门可能更专注于运输的时效性,商家则更关注价格和商品质量,而平台则更看重整体的用户体验。\n", + "\n", + "我们可以通过增强输入提示(Prompt),来强调我们对某一特定视角的重视。" + ] + }, + { + "cell_type": "markdown", + "id": "d6f8509a", + "metadata": {}, + "source": [ + "#### 1.2.1 侧重于快递服务" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "80636c3e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "快递提前到货,公仔可爱但有点小。\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "您的任务是从电子商务网站上生成一个产品评论的简短摘要。\n", + "\n", + "请对三个反引号之间的评论文本进行概括,最多30个字,并且侧重在快递服务上。\n", + "\n", + "评论: ```{prod_review}```\n", + "\"\"\"\n", + "\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "76c97fea", + "metadata": {}, + "source": [ + "通过输出结果,我们可以看到,文本以“快递提前到货”开头,体现了对于快递效率的侧重。" + ] + }, + { + "cell_type": "markdown", + "id": "83275907", + "metadata": {}, + "source": [ + "#### 1.2.2 侧重于价格与质量" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "728d6c57", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "可爱的熊猫公仔,质量好但有点小,价格稍高。快递提前到货。\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "您的任务是从电子商务网站上生成一个产品评论的简短摘要。\n", + "\n", + "请对三个反引号之间的评论文本进行概括,最多30个词汇,并且侧重在产品价格和质量上。\n", + "\n", + "评论: ```{prod_review}```\n", + "\"\"\"\n", + "\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "972dbb1b", + "metadata": {}, + "source": [ + "通过输出的结果,我们可以看到,文本以“可爱的熊猫公仔,质量好但有点小,价格稍高”开头,体现了对于产品价格与质量的侧重。" + ] + }, + { + "cell_type": "markdown", + "id": "b3ed53d2", + "metadata": {}, + "source": [ + "### 1.3 关键信息提取" + ] + }, + { + "cell_type": "markdown", + "id": "ba6f5c25", + "metadata": {}, + "source": [ + "在1.2节中,虽然我们通过添加关键角度侧重的 Prompt ,确实让文本摘要更侧重于某一特定方面,然而,我们可以发现,在结果中也会保留一些其他信息,比如偏重价格与质量角度的概括中仍保留了“快递提前到货”的信息。如果我们只想要提取某一角度的信息,并过滤掉其他所有信息,则可以要求 LLM 进行 **文本提取(Extract)** 而非概括( Summarize )。" + ] + }, + { + "cell_type": "markdown", + "id": "da39760c", + "metadata": {}, + "source": [ + "下面让我们来一起来对文本进行提取信息吧!" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "c845ccab", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "产品运输相关的信息:快递提前一天到货。\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "您的任务是从电子商务网站上的产品评论中提取相关信息。\n", + "\n", + "请从以下三个反引号之间的评论文本中提取产品运输相关的信息,最多30个词汇。\n", + "\n", + "评论: ```{prod_review}```\n", + "\"\"\"\n", + "\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "50498a2b", + "metadata": {}, + "source": [ + "## 二、同时概括多条文本" + ] + }, + { + "cell_type": "markdown", + "id": "a291541a", + "metadata": {}, + "source": [ + "在实际的工作流中,我们往往要处理大量的评论文本,下面的示例将多条用户评价集合在一个列表中,并利用 ```for``` 循环和文本概括(Summarize)提示词,将评价概括至小于 20 个词以下,并按顺序打印。当然,在实际生产中,对于不同规模的评论文本,除了使用 ```for``` 循环以外,还可能需要考虑整合评论、分布式等方法提升运算效率。您可以搭建主控面板,来总结大量用户评论,以及方便您或他人快速浏览,还可以点击查看原评论。这样,您就能高效掌握顾客的所有想法。" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "ef606961", + "metadata": {}, + "outputs": [], + "source": [ + "review_1 = prod_review\n", + "\n", + "# 一盏落地灯的评论\n", + "review_2 = \"\"\"\n", + "我需要一盏漂亮的卧室灯,这款灯不仅具备额外的储物功能,价格也并不算太高。\n", + "收货速度非常快,仅用了两天的时间就送到了。\n", + "不过,在运输过程中,灯的拉线出了问题,幸好,公司很乐意寄送了一根全新的灯线。\n", + "新的灯线也很快就送到手了,只用了几天的时间。\n", + "装配非常容易。然而,之后我发现有一个零件丢失了,于是我联系了客服,他们迅速地给我寄来了缺失的零件!\n", + "对我来说,这是一家非常关心客户和产品的优秀公司。\n", + "\"\"\"\n", + "\n", + "# 一把电动牙刷的评论\n", + "review_3 = \"\"\"\n", + "我的牙科卫生员推荐了电动牙刷,所以我就买了这款。\n", + "到目前为止,电池续航表现相当不错。\n", + "初次充电后,我在第一周一直将充电器插着,为的是对电池进行条件养护。\n", + "过去的3周里,我每天早晚都使用它刷牙,但电池依然维持着原来的充电状态。\n", + "不过,牙刷头太小了。我见过比这个牙刷头还大的婴儿牙刷。\n", + "我希望牙刷头更大一些,带有不同长度的刷毛,\n", + "这样可以更好地清洁牙齿间的空隙,但这款牙刷做不到。\n", + "总的来说,如果你能以50美元左右的价格购买到这款牙刷,那是一个不错的交易。\n", + "制造商的替换刷头相当昂贵,但你可以购买价格更为合理的通用刷头。\n", + "这款牙刷让我感觉就像每天都去了一次牙医,我的牙齿感觉非常干净!\n", + "\"\"\"\n", + "\n", + "# 一台搅拌机的评论\n", + "review_4 = \"\"\"\n", + "在11月份期间,这个17件套装还在季节性促销中,售价约为49美元,打了五折左右。\n", + "可是由于某种原因(我们可以称之为价格上涨),到了12月的第二周,所有的价格都上涨了,\n", + "同样的套装价格涨到了70-89美元不等。而11件套装的价格也从之前的29美元上涨了约10美元。\n", + "看起来还算不错,但是如果你仔细看底座,刀片锁定的部分看起来没有前几年版本的那么漂亮。\n", + "然而,我打算非常小心地使用它\n", + "(例如,我会先在搅拌机中研磨豆类、冰块、大米等坚硬的食物,然后再将它们研磨成所需的粒度,\n", + "接着切换到打蛋器刀片以获得更细的面粉,如果我需要制作更细腻/少果肉的食物)。\n", + "在制作冰沙时,我会将要使用的水果和蔬菜切成细小块并冷冻\n", + "(如果使用菠菜,我会先轻微煮熟菠菜,然后冷冻,直到使用时准备食用。\n", + "如果要制作冰糕,我会使用一个小到中号的食物加工器),这样你就可以避免添加过多的冰块。\n", + "大约一年后,电机开始发出奇怪的声音。我打电话给客户服务,但保修期已经过期了,\n", + "所以我只好购买了另一台。值得注意的是,这类产品的整体质量在过去几年里有所下降\n", + ",所以他们在一定程度上依靠品牌认知和消费者忠诚来维持销售。在大约两天内,我收到了新的搅拌机。\n", + "\"\"\"\n", + "\n", + "reviews = [review_1, review_2, review_3, review_4]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "eb878522", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "评论1: 熊猫公仔是生日礼物,女儿喜欢,软可爱,面部表情和善。价钱有点小,快递提前一天到货。 \n", + "\n", + "评论2: 漂亮卧室灯,储物功能,快速送达,灯线问题,快速解决,容易装配,关心客户和产品。 \n", + "\n", + "评论3: 这款电动牙刷电池续航好,但牙刷头太小,价格合理,清洁效果好。 \n", + "\n", + "评论4: 该评论提到了一个17件套装的产品,在11月份有折扣销售,但在12月份价格上涨。评论者提到了产品的外观和使用方法,并提到了产品质量下降的问题。最后,评论者提到他们购买了另一台搅拌机。 \n", + "\n" + ] + } + ], + "source": [ + "for i in range(len(reviews)):\n", + " prompt = f\"\"\"\n", + " 你的任务是从电子商务网站上的产品评论中提取相关信息。\n", + "\n", + " 请对三个反引号之间的评论文本进行概括,最多20个词汇。\n", + "\n", + " 评论文本: ```{reviews[i]}```\n", + " \"\"\"\n", + " response = get_completion(prompt)\n", + " print(f\"评论{i+1}: \", response, \"\\n\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "f118c0cc", + "metadata": {}, + "source": [ + "## 三、英文版" + ] + }, + { + "cell_type": "markdown", + "id": "a08635df", + "metadata": {}, + "source": [ + "**1.1 单一文本概括**" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "e55327d5", + "metadata": {}, + "outputs": [], + "source": [ + "prod_review = \"\"\"\n", + "Got this panda plush toy for my daughter's birthday, \\\n", + "who loves it and takes it everywhere. It's soft and \\ \n", + "super cute, and its face has a friendly look. It's \\ \n", + "a bit small for what I paid though. I think there \\ \n", + "might be other options that are bigger for the \\ \n", + "same price. It arrived a day earlier than expected, \\ \n", + "so I got to play with it myself before I gave it \\ \n", + "to her.\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "30c2ef51", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This panda plush toy is loved by the reviewer's daughter, but they feel it is a bit small for the price.\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Your task is to generate a short summary of a product \\\n", + "review from an ecommerce site. \n", + "\n", + "Summarize the review below, delimited by triple \n", + "backticks, in at most 30 words. \n", + "\n", + "Review: ```{prod_review}```\n", + "\"\"\"\n", + "\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "9bdcfc1b", + "metadata": {}, + "source": [ + "**1.2 设置关键角度侧重**" + ] + }, + { + "cell_type": "markdown", + "id": "5dd0534f", + "metadata": {}, + "source": [ + "1.2.1 侧重于快递服务" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "b354cc3f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The customer is happy with the product but suggests offering larger options for the same price. They were pleased with the early delivery.\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Your task is to generate a short summary of a product \\\n", + "review from an ecommerce site to give feedback to the \\\n", + "Shipping deparmtment. \n", + "\n", + "Summarize the review below, delimited by triple \n", + "backticks, in at most 30 words, and focusing on any aspects \\\n", + "that mention shipping and delivery of the product. \n", + "\n", + "Review: ```{prod_review}```\n", + "\"\"\"\n", + "\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "af6aaf3a", + "metadata": {}, + "source": [ + "1.2.2 侧重于价格和质量" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "1b5358fd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The customer loves the panda plush toy for its softness and cuteness, but feels it is overpriced compared to other options available.\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Your task is to generate a short summary of a product \\\n", + "review from an ecommerce site to give feedback to the \\\n", + "pricing deparmtment, responsible for determining the \\\n", + "price of the product. \n", + "\n", + "Summarize the review below, delimited by triple \n", + "backticks, in at most 30 words, and focusing on any aspects \\\n", + "that are relevant to the price and perceived value. \n", + "\n", + "Review: ```{prod_review}```\n", + "\"\"\"\n", + "\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "0f582677", + "metadata": {}, + "source": [ + "**1.3 关键信息提取**" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "32c87014", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The shipping department should take note that the product arrived a day earlier than expected.\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Your task is to extract relevant information from \\ \n", + "a product review from an ecommerce site to give \\\n", + "feedback to the Shipping department. \n", + "\n", + "From the review below, delimited by triple quotes \\\n", + "extract the information relevant to shipping and \\ \n", + "delivery. Limit to 30 words. \n", + "\n", + "Review: ```{prod_review}```\n", + "\"\"\"\n", + "\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "2043d100", + "metadata": {}, + "source": [ + "**2.1 同时概括多条文本**" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "cff48486", + "metadata": {}, + "outputs": [], + "source": [ + "review_1 = prod_review \n", + "\n", + "# review for a standing lamp\n", + "review_2 = \"\"\"\n", + "Needed a nice lamp for my bedroom, and this one \\\n", + "had additional storage and not too high of a price \\\n", + "point. Got it fast - arrived in 2 days. The string \\\n", + "to the lamp broke during the transit and the company \\\n", + "happily sent over a new one. Came within a few days \\\n", + "as well. It was easy to put together. Then I had a \\\n", + "missing part, so I contacted their support and they \\\n", + "very quickly got me the missing piece! Seems to me \\\n", + "to be a great company that cares about their customers \\\n", + "and products. \n", + "\"\"\"\n", + "\n", + "# review for an electric toothbrush\n", + "review_3 = \"\"\"\n", + "My dental hygienist recommended an electric toothbrush, \\\n", + "which is why I got this. The battery life seems to be \\\n", + "pretty impressive so far. After initial charging and \\\n", + "leaving the charger plugged in for the first week to \\\n", + "condition the battery, I've unplugged the charger and \\\n", + "been using it for twice daily brushing for the last \\\n", + "3 weeks all on the same charge. But the toothbrush head \\\n", + "is too small. I’ve seen baby toothbrushes bigger than \\\n", + "this one. I wish the head was bigger with different \\\n", + "length bristles to get between teeth better because \\\n", + "this one doesn’t. Overall if you can get this one \\\n", + "around the $50 mark, it's a good deal. The manufactuer's \\\n", + "replacements heads are pretty expensive, but you can \\\n", + "get generic ones that're more reasonably priced. This \\\n", + "toothbrush makes me feel like I've been to the dentist \\\n", + "every day. My teeth feel sparkly clean! \n", + "\"\"\"\n", + "\n", + "# review for a blender\n", + "review_4 = \"\"\"\n", + "So, they still had the 17 piece system on seasonal \\\n", + "sale for around $49 in the month of November, about \\\n", + "half off, but for some reason (call it price gouging) \\\n", + "around the second week of December the prices all went \\\n", + "up to about anywhere from between $70-$89 for the same \\\n", + "system. And the 11 piece system went up around $10 or \\\n", + "so in price also from the earlier sale price of $29. \\\n", + "So it looks okay, but if you look at the base, the part \\\n", + "where the blade locks into place doesn’t look as good \\\n", + "as in previous editions from a few years ago, but I \\\n", + "plan to be very gentle with it (example, I crush \\\n", + "very hard items like beans, ice, rice, etc. in the \\\n", + "blender first then pulverize them in the serving size \\\n", + "I want in the blender then switch to the whipping \\\n", + "blade for a finer flour, and use the cross cutting blade \\\n", + "first when making smoothies, then use the flat blade \\\n", + "if I need them finer/less pulpy). Special tip when making \\\n", + "smoothies, finely cut and freeze the fruits and \\\n", + "vegetables (if using spinach-lightly stew soften the \\\n", + "spinach then freeze until ready for use-and if making \\\n", + "sorbet, use a small to medium sized food processor) \\\n", + "that you plan to use that way you can avoid adding so \\\n", + "much ice if at all-when making your smoothie. \\\n", + "After about a year, the motor was making a funny noise. \\\n", + "I called customer service but the warranty expired \\\n", + "already, so I had to buy another one. FYI: The overall \\\n", + "quality has gone done in these types of products, so \\\n", + "they are kind of counting on brand recognition and \\\n", + "consumer loyalty to maintain sales. Got it in about \\\n", + "two days.\n", + "\"\"\"\n", + "\n", + "reviews = [review_1, review_2, review_3, review_4]" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "3f61080b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 Soft and cute panda plush toy loved by daughter, but small for the price. Arrived early. \n", + "\n", + "1 Great lamp with storage, fast delivery, excellent customer service, and easy assembly. Highly recommended. \n", + "\n", + "2 Impressive battery life, but toothbrush head is too small. Good deal if bought around $50. \n", + "\n", + "3 The reviewer found the price increase after the sale disappointing and noticed a decrease in quality over time. \n", + "\n" + ] + } + ], + "source": [ + "for i in range(len(reviews)):\n", + " prompt = f\"\"\"\n", + " Your task is to generate a short summary of a product \\\n", + " review from an ecommerce site. \n", + "\n", + " Summarize the review below, delimited by triple \\\n", + " backticks in at most 20 words. \n", + "\n", + " Review: ```{reviews[i]}```\n", + " \"\"\"\n", + " response = get_completion(prompt)\n", + " print(i, response, \"\\n\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + }, + "latex_envs": { + "LaTeX_envs_menu_present": true, + "autoclose": false, + "autocomplete": true, + "bibliofile": "biblio.bib", + "cite_by": "apalike", + "current_citInitial": 1, + "eqLabelWithNumbers": true, + "eqNumInitial": 1, + "hotkeys": { + "equation": "Ctrl-E", + "itemize": "Ctrl-I" + }, + "labels_anchors": false, + "latex_user_defs": false, + "report_style_numbering": false, + "user_envs_cfg": false + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/content/C1 Prompt Engineering for Developer/5. 推断 Inferring.ipynb b/docs/content/C1 Prompt Engineering for Developer/5. 推断 Inferring.ipynb index b3a2e59..8e01279 100644 --- a/docs/content/C1 Prompt Engineering for Developer/5. 推断 Inferring.ipynb +++ b/docs/content/C1 Prompt Engineering for Developer/5. 推断 Inferring.ipynb @@ -17,9 +17,9 @@ "\n", "让我们先想象一下,你是一名初创公司的数据分析师,你的任务是从各种产品评论和新闻文章中提取出关键的情感和主题。这些任务包括了标签提取、实体提取、以及理解文本的情感等等。在传统的机器学习流程中,你需要收集标签化的数据集、训练模型、确定如何在云端部署模型并进行推断。尽管这种方式可能会产生不错的效果,但完成这一全流程需要耗费大量的时间和精力。而且,每一个任务,比如情感分析、实体提取等等,都需要训练和部署单独的模型。\n", "\n", - "然而,就在你准备投入繁重工作的时候,你发现了大型语言模型(LLM)。LLM的一个明显优点是,对于许多这样的任务,你只需要编写一个 Prompt,就可以开始生成结果,大大减轻了你的工作负担。这个发现像是找到了一把神奇的钥匙,让应用程序开发的速度加快了许多。最令你兴奋的是,你可以仅仅使用一个模型和一个API来执行许多不同的任务,无需再纠结如何训练和部署许多不同的模型。\n", + "然而,就在你准备投入繁重工作的时候,你发现了大型语言模型(LLM)。LLM 的一个明显优点是,对于许多这样的任务,你只需要编写一个 Prompt,就可以开始生成结果,大大减轻了你的工作负担。这个发现像是找到了一把神奇的钥匙,让应用程序开发的速度加快了许多。最令你兴奋的是,你可以仅仅使用一个模型和一个 API 来执行许多不同的任务,无需再纠结如何训练和部署许多不同的模型。\n", "\n", - "让我们开始这一章的学习,一起探索如何利用LLM加快我们的工作进程,提高我们的工作效率。" + "让我们开始这一章的学习,一起探索如何利用 LLM 加快我们的工作进程,提高我们的工作效率。" ] }, { @@ -102,7 +102,7 @@ "id": "76be2320", "metadata": {}, "source": [ - "如果你想要给出更简洁的答案,以便更容易进行后处理,可以在上述 Prompt 基础上添加另一个指令:*用一个单词回答:「正面」或「负面」*。这样就只会打印出 “正面” 这个单词,这使得输出更加统一,方便后续处理。" + "如果你想要给出更简洁的答案,以便更容易进行后期处理,可以在上述 Prompt 基础上添加另一个指令:*用一个单词回答:「正面」或「负面」*。这样就只会打印出 “正面” 这个单词,这使得输出更加统一,方便后续处理。" ] }, { @@ -136,7 +136,7 @@ "id": "81d2a973-1fa4-4a35-ae35-a2e746c0e91b", "metadata": {}, "source": [ - "### 2.2 识别情感类型" + "### 1.2 识别情感类型" ] }, { @@ -456,7 +456,7 @@ "id": "95b636f1", "metadata": {}, "source": [ - "假设我们有一个新闻网站或类似的平台,这是我们感兴趣的主题:美国航空航天局、当地政府、工程、员工满意度、联邦政府等。我们想要分析一篇新闻文章,理解其包含了哪些主题。可以使用这样的prompt:确定以下主题列表中的每个项目是否是以下文本中的主题。以 0 或 1 的形式给出答案列表。" + "假设我们有一个新闻网站或类似的平台,这是我们感兴趣的主题:美国航空航天局、当地政府、工程、员工满意度、联邦政府等。我们想要分析一篇新闻文章,理解其包含了哪些主题。可以使用这样的 Prompt:确定以下主题列表中的每个项目是否是以下文本中的主题。以 0 或 1 的形式给出答案列表。" ] }, { @@ -499,7 +499,7 @@ "id": "08247dbf", "metadata": {}, "source": [ - "从输出结果来看,这个`story`与关于“美国航空航天局”、“员工满意度”、“联邦政府”、“当地政府”有关,而与“工程”无关。这种能力在机器学习领域被称为零样本(Zero-Shot)学习。这是因为我们并没有提供任何带标签的训练数据,仅凭 Prompt ,它便能判定哪些主题在新闻文章中被包含。\n", + "从输出结果来看,这个 `story` 与关于“美国航空航天局”、“员工满意度”、“联邦政府”、“当地政府”有关,而与“工程”无关。这种能力在机器学习领域被称为零样本(Zero-Shot)学习。这是因为我们并没有提供任何带标签的训练数据,仅凭 Prompt ,它便能判定哪些主题在新闻文章中被包含。\n", "\n", "如果我们希望制定一个新闻提醒,我们同样可以运用这种处理新闻的流程。假设我对“美国航空航天局”的工作深感兴趣,那么你就可以构建一个如此的系统:每当出现与'美国宇航局'相关的新闻,系统就会输出提醒。" ] @@ -961,7 +961,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -975,7 +975,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.8.10" }, "latex_envs": { "LaTeX_envs_menu_present": true, diff --git a/docs/content/C1 Prompt Engineering for Developer/6. 文本转换 Transforming.ipynb b/docs/content/C1 Prompt Engineering for Developer/6. 文本转换 Transforming.ipynb index baa9c47..219459a 100644 --- a/docs/content/C1 Prompt Engineering for Developer/6. 文本转换 Transforming.ipynb +++ b/docs/content/C1 Prompt Engineering for Developer/6. 文本转换 Transforming.ipynb @@ -9,7 +9,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "2fac57c2", "metadata": {}, @@ -22,7 +21,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "bf3733d4", "metadata": {}, @@ -35,7 +33,7 @@ "id": "06ba53ba", "metadata": {}, "source": [ - "文本翻译是大语言模型的典型应用场景之一。相比于传统统计机器翻译系统,大语言模型翻译更加流畅自然,还原度更高。通过在大规模高质量平行语料上进行fine-tuning训练,大语言模型可以深入学习不同语言间的词汇、语法、语义等层面的对应关系,模拟双语者的转换思维,进行意义传递的精准转换,而非简单的逐词替换。\n", + "文本翻译是大语言模型的典型应用场景之一。相比于传统统计机器翻译系统,大语言模型翻译更加流畅自然,还原度更高。通过在大规模高质量平行语料上进行 Fine-Tune,大语言模型可以深入学习不同语言间的词汇、语法、语义等层面的对应关系,模拟双语者的转换思维,进行意义传递的精准转换,而非简单的逐词替换。\n", "\n", "以英译汉为例,传统统计机器翻译多倾向直接替换英文词汇,语序保持英语结构,容易出现中文词汇使用不地道、语序不顺畅的现象。而大语言模型可以学习英汉两种语言的语法区别,进行动态的结构转换。同时,它还可以通过上下文理解原句意图,选择合适的中文词汇进行转换,而非生硬的字面翻译。\n", "\n", @@ -43,7 +41,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "1b418e32", "metadata": {}, @@ -79,7 +76,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "e3e922b4", "metadata": {}, @@ -111,7 +107,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "c1841354", "metadata": {}, @@ -146,7 +141,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "68723ba5", "metadata": {}, @@ -154,31 +148,6 @@ "### 1.4 同时进行语气转换" ] }, - { - "cell_type": "code", - "execution_count": 13, - "id": "a4770dcc", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Formal: ¿Le gustaría ordenar una almohada?\n", - "Informal: ¿Te gustaría ordenar una almohada?\n" - ] - } - ], - "source": [ - "prompt = f\"\"\"\n", - "Translate the following text to Spanish in both the \\\n", - "formal and informal forms: \n", - "'Would you like to order a pillow?'\n", - "\"\"\"\n", - "response = get_completion(prompt)\n", - "print(response)\n" - ] - }, { "cell_type": "code", "execution_count": 14, @@ -204,7 +173,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "b2dc4c56", "metadata": {}, @@ -213,7 +181,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "54b00aa4", "metadata": {}, @@ -297,7 +264,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "6ab558a2", "metadata": {}, @@ -306,7 +272,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "b85ae847", "metadata": {}, @@ -350,7 +315,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "98df9009", "metadata": {}, @@ -359,14 +323,13 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "0bf9c074", "metadata": {}, "source": [ - "大语言模型如ChatGPT在不同数据格式之间转换方面表现出色。它可以轻松实现JSON到HTML、XML、Markdown等格式的相互转化。下面是一个示例,展示如何使用大语言模型**将JSON数据转换为HTML格式**:\n", + "大语言模型如 ChatGPT 在不同数据格式之间转换方面表现出色。它可以轻松实现 JSON 到 HTML、XML、Markdown 等格式的相互转化。下面是一个示例,展示如何使用大语言模型**将 JSON 数据转换为 HTML 格式**:\n", "\n", - "假设我们有一个JSON数据,包含餐厅员工的姓名和邮箱信息。现在我们需要将这个JSON转换为HTML表格格式,以便在网页中展示。在这个案例中,我们就可以使用大语言模型,直接输入JSON数据,并给出需要转换为HTML表格的要求。语言模型会自动解析JSON结构,并以HTML表格形式输出,完成格式转换的任务。\n", + "假设我们有一个 JSON 数据,包含餐厅员工的姓名和邮箱信息。现在我们需要将这个 JSON 转换为 HTML 表格格式,以便在网页中展示。在这个案例中,我们就可以使用大语言模型,直接输入JSON 数据,并给出需要转换为 HTML 表格的要求。语言模型会自动解析 JSON 结构,并以 HTML 表格形式输出,完成格式转换的任务。\n", "\n", "利用大语言模型强大的格式转换能力,我们可以快速实现各种结构化数据之间的相互转化,大大简化开发流程。掌握这一转换技巧将有助于读者**更高效地处理结构化数据**。\n" ] @@ -429,6 +392,14 @@ "print(response)" ] }, + { + "cell_type": "markdown", + "id": "99c8114e", + "metadata": {}, + "source": [ + "将上述 HTML 代码展示出来如下:" + ] + }, { "cell_type": "code", "execution_count": 11, @@ -476,7 +447,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "29b7167b", "metadata": {}, @@ -485,7 +455,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "22776140", "metadata": {}, @@ -552,7 +521,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "538181e0", "metadata": {}, @@ -602,7 +570,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "2e2d1f6a", "metadata": {}, @@ -657,7 +624,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "3ee5d487", "metadata": {}, @@ -1343,7 +1309,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -1357,7 +1323,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.8.10" }, "latex_envs": { "LaTeX_envs_menu_present": true, diff --git a/docs/content/C1 Prompt Engineering for Developer/7. 文本扩展 Expanding.ipynb b/docs/content/C1 Prompt Engineering for Developer/7. 文本扩展 Expanding.ipynb index 6ed2d37..12c83ed 100644 --- a/docs/content/C1 Prompt Engineering for Developer/7. 文本扩展 Expanding.ipynb +++ b/docs/content/C1 Prompt Engineering for Developer/7. 文本扩展 Expanding.ipynb @@ -13,11 +13,9 @@ "source": [ "**文本扩展**是大语言模型的一个重要应用方向,它可以输入简短文本,生成更加丰富的长文。这为创作提供了强大支持,但也可能被滥用。因此开发者在使用时,必须谨记社会责任,避免生成有害内容。\n", "\n", - "在本章中,我们将学习*基于OpenAI API实现一个客户邮件自动生成的示例*,用于*根据客户反馈优化客服邮件*。这里还会介绍“温度”(temperature)这一超参数,它可以**控制文本生成的多样性**。\n", + "在本章中,我们将学习*基于 OpenAI API 实现一个客户邮件自动生成的示例*,用于*根据客户反馈优化客服邮件*。这里还会介绍“温度”(temperature)这一超参数,它可以**控制文本生成的多样性**。\n", "\n", - "需要注意,扩展功能只应用来辅助人类创作,而非大规模自动生成内容。开发者应审慎使用,避免产生负面影响。只有以负责任和有益的方式应用语言模型,才能发挥其最大价值。\n", - "\n", - "相信践行社会责任的开发者可以利用语言模型的扩展功能,开发出真正造福人类的创新应用。\n" + "需要注意,扩展功能只应用来辅助人类创作,而非大规模自动生成内容。开发者应审慎使用,避免产生负面影响。只有以负责任和有益的方式应用语言模型,才能发挥其最大价值。相信践行社会责任的开发者可以利用语言模型的扩展功能,开发出真正造福人类的创新应用。\n" ] }, { @@ -33,7 +31,7 @@ "source": [ "在这个客户邮件自动生成的示例中,我们**将根据客户的评价和其中的情感倾向,使用大语言模型针对性地生成回复邮件**。\n", "\n", - "具体来说,我们先输入客户的评论文本和对应的情感分析结果(正面或者负面)。然后构造一个Prompt,要求大语言模型基于这些信息来生成一封定制的回复电子邮件。\n", + "具体来说,我们先输入客户的评论文本和对应的情感分析结果(正面或者负面)。然后构造一个 Prompt,要求大语言模型基于这些信息来生成一封定制的回复电子邮件。\n", "\n", "下面先给出一个实例,包括一条客户评价和这个评价表达的情感。这为后续的语言模型生成回复邮件提供了关键输入信息。通过输入客户反馈的具体内容和情感态度,语言模型可以生成针对这个特定客户、考虑其具体情感因素的个性化回复。这种**针对个体客户特点的邮件生成方式,将大大提升客户满意度**。" ] @@ -129,7 +127,7 @@ "source": [ "## 二、引入温度系数\n", "\n", - "大语言模型中的 “温度”(temperature) 参数可以控制生成文本的随机性和多样性。temperature的值越大,语言模型输出的多样性越大;temperature的值越小,输出越倾向高概率的文本。\n", + "大语言模型中的 “温度”(temperature) 参数可以控制生成文本的随机性和多样性。temperature 的值越大,语言模型输出的多样性越大;temperature 的值越小,输出越倾向高概率的文本。\n", "\n", "举个例子,在某一上下文中,语言模型可能认为“比萨”是接下来最可能的词,其次是“寿司”和“塔可”。若 temperature 为0,则每次都会生成“比萨”;而当 temperature 越接近 1 时,生成结果是“寿司”或“塔可”的可能性越大,使文本更加多样。\n", "\n", @@ -192,6 +190,13 @@ "print(response)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "第二次运行输出结果会发生变化:" + ] + }, { "cell_type": "code", "execution_count": 5, @@ -241,9 +246,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**温度(temperature)参数可以控制语言模型生成文本的随机性**。温度为0时,每次使用同样的Prompt,得到的结果总是一致的。而在上面的样例中,当温度设为0.7时,则每次执行都会生成不同的文本。\n", + "**温度(temperature)参数可以控制语言模型生成文本的随机性**。温度为0时,每次使用同样的 Prompt,得到的结果总是一致的。而在上面的样例中,当温度设为0.7时,则每次执行都会生成不同的文本。\n", "\n", - "所以,这次的结果与之前得到的邮件就不太一样了。再次执行同样的Prompt,邮件内容还会有变化。因此。我建议读者朋友们可以自己尝试不同的 temperature ,来观察输出的变化。总体来说,temperature 越高,语言模型的文本生成就越具有随机性。可以想象,高温度下,语言模型就像心绪更加活跃,但也可能更有创造力。\n", + "所以,这次的结果与之前得到的邮件就不太一样了。再次执行同样的 Prompt,邮件内容还会有变化。因此。我建议读者朋友们可以自己尝试不同的 temperature ,来观察输出的变化。总体来说,temperature 越高,语言模型的文本生成就越具有随机性。可以想象,高温度下,语言模型就像心绪更加活跃,但也可能更有创造力。\n", "\n", "适当调节这个超参数,可以让语言模型的生成更富有多样性,也更能意外惊喜。希望这些经验可以帮助你在不同场景中找到最合适的温度设置。\n" ] @@ -411,7 +416,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -425,7 +430,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.8.10" }, "latex_envs": { "LaTeX_envs_menu_present": true, diff --git a/docs/content/C1 Prompt Engineering for Developer/8. 聊天机器人 Chatbot.ipynb b/docs/content/C1 Prompt Engineering for Developer/8. 聊天机器人 Chatbot.ipynb index 17e29ef..52689ac 100644 --- a/docs/content/C1 Prompt Engineering for Developer/8. 聊天机器人 Chatbot.ipynb +++ b/docs/content/C1 Prompt Engineering for Developer/8. 聊天机器人 Chatbot.ipynb @@ -15,14 +15,8 @@ "id": "f0bdc2c9", "metadata": {}, "source": [ - "大型语言模型带给我们的激动人心的一种可能性是,我们可以通过它构建定制的聊天机器人(Chatbot),而且只需很少的工作量。在这一章节的探索中,我们将带你了解如何利用会话形式,与具有个性化特性(或专门为特定任务或行为设计)的聊天机器人进行深度对话。" - ] - }, - { - "cell_type": "markdown", - "id": "e6fae355", - "metadata": {}, - "source": [ + "大型语言模型带给我们的激动人心的一种可能性是,我们可以通过它构建定制的聊天机器人(Chatbot),而且只需很少的工作量。在这一章节的探索中,我们将带你了解如何利用会话形式,与具有个性化特性(或专门为特定任务或行为设计)的聊天机器人进行深度对话。\n", + "\n", "像 ChatGPT 这样的聊天模型实际上是组装成以一系列消息作为输入,并返回一个模型生成的消息作为输出的。这种聊天格式原本的设计目标是简便多轮对话,但我们通过之前的学习可以知道,它对于不会涉及任何对话的**单轮任务**也同样有用。" ] }, @@ -380,6 +374,14 @@ "!pip install panel" ] }, + { + "cell_type": "markdown", + "id": "b61e475a", + "metadata": {}, + "source": [ + "如果你还没有安装 panel 库(用于可视化界面),请运行上述指令以安装该第三方库。" + ] + }, { "cell_type": "code", "execution_count": null, @@ -850,7 +852,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.8.10" }, "latex_envs": { "LaTeX_envs_menu_present": true,