完成 Prompt Engineering 最终修订
This commit is contained in:
21
content/Prompt Engineering for Developer/1. 简介.md
Normal file
21
content/Prompt Engineering for Developer/1. 简介.md
Normal file
@ -0,0 +1,21 @@
|
||||
# 第一章 简介
|
||||
|
||||
**作者 吴恩达教授**
|
||||
|
||||
欢迎来到本课程,我们将为开发人员介绍 ChatGPT 提示词工程(Prompt Engineering)。本课程由 Isa Fulford 教授和我一起授课。Isa 是 OpenAI 的技术团队成员,曾开发过受欢迎的 ChatGPT 检索插件,并且在教授 LLM (Large Language Model, 大语言模型)技术在产品中的应用方面做出了很大贡献。她还参与编写了教授人们使用 Prompt 的 OpenAI cookbook。
|
||||
|
||||
互联网上有很多有关提示词(Prompt, 本教程中将保留该术语)的材料,例如《30 prompts everyone has to know》之类的文章。这些文章主要集中在 ChatGPT 的 Web 界面上,许多人在使用它执行特定的、通常是一次性的任务。但是,我认为对于开发人员,LLM 的更强大功能是能通过 API 调用,从而快速构建软件应用程序。我认为这方面还没有得到充分的重视。实际上,我们在 DeepLearning.AI 的姊妹公司 AI Fund 的团队一直在与许多初创公司合作,将这些技术应用于诸多应用程序上。很兴奋能看到 LLM API 能够让开发人员非常快速地构建应用程序。
|
||||
|
||||
在本课程中,我们将与您分享一些技巧,来挖掘 LLM 的潜力,也会提供应用上的最佳实践。过程中会涉及大量材料。首先,你会学习到用于软件开发的 Prompt 最佳实践,随后会涉及到几个常用使用例,包括概括、推断、转换与扩展,最后会利用 LLM 构建 chatbot(聊天机器人)。希望这能激发你的想象力,去开拓新应用。
|
||||
|
||||
随着 LLM 的发展,其大致可以分为两种类型,后续称为基础 LLM 和指令微调(Instruction Tuned)LLM。基础LLM是基于文本训练数据,训练出预测下一个单词能力的模型。其通常通过在互联网和其他来源的大量数据上训练,来确定紧接着出现的最可能的词。例如,如果你以“从前,有一只独角兽”作为 Prompt ,基础 LLM 可能会继续预测“她与独角兽朋友共同生活在一片神奇森林中”。但是,如果你以“法国的首都是什么”为 Prompt ,则基础 LLM 可能会根据互联网上的文章,将回答预测为“法国最大的城市是什么?法国的人口是多少?”,因为互联网上的文章很可能是有关法国国家的问答题目列表。
|
||||
|
||||
而对于指令微调的 LLM ,相关研究和实践正甚嚣尘上,训练它们来遵循指示。因此,如果你问它,“法国的首都是什么?”,它有极大可能输出“法国的首都是巴黎”。指令微调的LLM的训练通常是基于预训练好的LLM的,即模型已经在大量文本数据上进行了训练。然后对其进行进一步训练与微调(finetune),使用的数据包括输入和理想输出(输入是指令、输出是遵循这些指令的良好回答)。然后通常使用一种称为 RLHF(reinforcement learning from human feedback,人类反馈强化学习)的技术进行进一步改进,使系统更能够有帮助地遵循指令。
|
||||
|
||||
因为指令微调的 LLM 已经被训练成有益、诚实、无害的,所以与基础 LLM 相比,它们更不可能输出有问题的文本,如有害输出。许多实际使用场景已经转向指令微调的 LLM 。您在互联网上找到的一些最佳实践可能更适用于基础 LLM ,但对于今天的大多数实际应用,我们建议将注意力集中在指令微调的 LLM 上,这些 LLM 更容易使用,而且由于 OpenAI 和其他 LLM 公司的工作,它们变得更加安全,也更加协调。
|
||||
|
||||
因此,本课程将重点介绍**针对指令微调 LLM 的最佳实践**,我们也建议您将其用于大多数使用场景。在继续之前,我想感谢 OpenAI 和 DeepLearning.ai 团队为 Isa 和我所提供的材料作出的贡献。我非常感激 OpenAI 的 Andrew Main、Joe Palermo、Boris Power、Ted Sanders 和 Lillian Weng,他们参与了我们的头脑风暴材料的制定和审核,为这个短期课程编制了课程大纲。我也感激 Deep Learning 方面的 Geoff Ladwig、Eddy Shyu 和 Tommy Nelson 的工作。
|
||||
|
||||
当您使用指令微调 LLM 时,您可以类比为向另一个人提供指令(假设他很聪明但不知道您任务的具体细节)。因此,当 LLM 无法正常工作时,有时是因为指令不够清晰。例如,如果您想问“请为我写一些关于阿兰·图灵( Alan Turing )的东西”,在此基础上清楚表明您希望文本专注于他的科学工作、个人生活、历史角色或其他方面可能会更有帮助。另外您还可以指定回答的语调, 来更加满足您的需求,可选项包括*专业记者写作*,或者*向朋友写的随笔*等。
|
||||
|
||||
如果你将 LLM 视为一名新毕业的大学生,要求他完成这个任务,你甚至可以提前指定他们应该阅读哪些文本片段来写关于 Alan Turing 的文本,这样能够帮助这位新毕业的大学生更好地完成这项任务。下一章你会看到提示词创建的两个原则,一是**清晰明确**,二是**给LLM时间去思考**。
|
||||
1238
content/Prompt Engineering for Developer/2. 提示原则 Guidelines.ipynb
Normal file
1238
content/Prompt Engineering for Developer/2. 提示原则 Guidelines.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
923
content/Prompt Engineering for Developer/3. 迭代优化 Iterative.ipynb
Normal file
923
content/Prompt Engineering for Developer/3. 迭代优化 Iterative.ipynb
Normal file
@ -0,0 +1,923 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 第三章 迭代优化\n",
|
||||
"\n",
|
||||
"当使用 LLM 构建应用程序时,实践层面上很难*第一次尝试*就成功获得适合最终应用的 Prompt。但这并不重要,只要您有一个好的迭代过程来不断改进您的 Prompt,那么您就能够得到一个适合任务的 Prompt。虽然相比训练机器学习模型,在 Prompt 方面一次成功的几率可能会高一些,但正如上所说, Prompt 是否一次完善并不重要。最重要的是**层层迭代**为您的应用程序找到有效 Prompt 的过程。\n",
|
||||
"\n",
|
||||
"因此在本章中,我们将以产品说明书中生成营销文案为例,来展示一些流程框架,并提示您思考如何层层迭代地分析和完善您的 Prompt。\n",
|
||||
"\n",
|
||||
"在吴恩达(Andrew Ng,原教程作者)的机器学习课程中展示过一张图表,说明了机器学习开发的流程。通常是先有一个想法,然后再用以下流程实现:编写代码,获取数据,训练模型,获得实验结果。然后您可以查看结果,分析误差与错误,找出适用领域,甚至可以更改您对具体问题的具体思路或解决方法。此后再次更改实现,并运行另一个实验等,反复迭代,最终获得有效的机器学习模型。在编写基于 LLM 的应用程序的 Prompt 时,流程可能非常相似。您产生了关于要完成的任务的想法后,可以尝试编写第一个 Prompt ,注意要满足上一章说过的两个原则:**清晰明确,并且给系统足够的时间思考**。然后您可以运行并查看结果。如果第一次效果不好,那么迭代的过程就是找出为什么指令不够清晰或为什么没有给算法足够的时间思考,以便改进想法、改进 Prompt 等等,循环多次,直到找到适合您的应用程序的 Prompt。\n",
|
||||
"\n",
|
||||
"很难有适用于世间万物的所谓“最佳 Prompt ”,更好的方法是找到有效的迭代过程,以便您可以快速地找到一个适合您的应用程序的 Prompt 。\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<div class=\"toc\">\n",
|
||||
" <ul class=\"toc-item\">\n",
|
||||
" <li><span><a href=\"#一环境配置\" data-toc-modified-id=\"一、环境配置\">一、环境配置</a></span></li>\n",
|
||||
" <li>\n",
|
||||
" <span><a href=\"#二任务从产品说明书生成一份营销产品描述\" data-toc-modified-id=\"二、任务——从产品说明书生成一份营销产品描述\">二、任务——从产品说明书生成一份营销产品描述</a></span>\n",
|
||||
" <ul class=\"toc-item\">\n",
|
||||
" <li><span><a href=\"#21-问题一生成文本太长\" data-toc-modified-id=\"2.1 问题一:生成文本太长\">2.1 问题一:生成文本太长</a></span></li>\n",
|
||||
" <li><span><a href=\"#22-问题二抓错文本细节\" data-toc-modified-id=\"2.2 问题二:抓错文本细节\">2.2 问题二:抓错文本细节</a></span></li>\n",
|
||||
" <li><span><a href=\"#23-问题三添加表格描述\" data-toc-modified-id=\"2.3 问题三:添加表格描述\">2.3 问题三:添加表格描述</a></span></li>\n",
|
||||
" </ul>\n",
|
||||
" </li>\n",
|
||||
" </ul>\n",
|
||||
"</div>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 一、环境配置\n",
|
||||
"\n",
|
||||
"同上一章,我们首先需要配置使用 OpenAI API 的环境"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import openai\n",
|
||||
"# 导入第三方库\n",
|
||||
"\n",
|
||||
"openai.api_key = \"sk-...\"\n",
|
||||
"# 设置 API_KEY, 请替换成您自己的 API_KEY\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 一个封装 OpenAI 接口的函数,参数为 Prompt,返回对应结果\n",
|
||||
"def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n",
|
||||
" '''\n",
|
||||
" prompt: 对应的提示词\n",
|
||||
" model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT),有内测资格的用户可以选择 gpt-4\n",
|
||||
" '''\n",
|
||||
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
|
||||
" response = openai.ChatCompletion.create(\n",
|
||||
" model=model,\n",
|
||||
" messages=messages,\n",
|
||||
" temperature=0, # 模型输出的温度系数,控制输出的随机程度\n",
|
||||
" )\n",
|
||||
" # 调用 OpenAI 的 ChatCompletion 接口\n",
|
||||
" return response.choices[0].message[\"content\"]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 二、任务——从产品说明书生成一份营销产品描述"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"给定一份椅子的资料页。描述说它属于*中世纪灵感*系列,产自意大利,并介绍了材料、构造、尺寸、可选配件等参数。假设您想要使用这份说明书帮助营销团队为电商平台撰写营销描述稿:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 示例:产品说明书\n",
|
||||
"fact_sheet_chair = \"\"\"\n",
|
||||
"OVERVIEW\n",
|
||||
"- Part of a beautiful family of mid-century inspired office furniture, \n",
|
||||
"including filing cabinets, desks, bookcases, meeting tables, and more.\n",
|
||||
"- Several options of shell color and base finishes.\n",
|
||||
"- Available with plastic back and front upholstery (SWC-100) \n",
|
||||
"or full upholstery (SWC-110) in 10 fabric and 6 leather options.\n",
|
||||
"- Base finish options are: stainless steel, matte black, \n",
|
||||
"gloss white, or chrome.\n",
|
||||
"- Chair is available with or without armrests.\n",
|
||||
"- Suitable for home or business settings.\n",
|
||||
"- Qualified for contract use.\n",
|
||||
"\n",
|
||||
"CONSTRUCTION\n",
|
||||
"- 5-wheel plastic coated aluminum base.\n",
|
||||
"- Pneumatic chair adjust for easy raise/lower action.\n",
|
||||
"\n",
|
||||
"DIMENSIONS\n",
|
||||
"- WIDTH 53 CM | 20.87”\n",
|
||||
"- DEPTH 51 CM | 20.08”\n",
|
||||
"- HEIGHT 80 CM | 31.50”\n",
|
||||
"- SEAT HEIGHT 44 CM | 17.32”\n",
|
||||
"- SEAT DEPTH 41 CM | 16.14”\n",
|
||||
"\n",
|
||||
"OPTIONS\n",
|
||||
"- Soft or hard-floor caster options.\n",
|
||||
"- Two choices of seat foam densities: \n",
|
||||
"medium (1.8 lb/ft3) or high (2.8 lb/ft3)\n",
|
||||
"- Armless or 8 position PU armrests \n",
|
||||
"\n",
|
||||
"MATERIALS\n",
|
||||
"SHELL BASE GLIDER\n",
|
||||
"- Cast Aluminum with modified nylon PA6/PA66 coating.\n",
|
||||
"- Shell thickness: 10 mm.\n",
|
||||
"SEAT\n",
|
||||
"- HD36 foam\n",
|
||||
"\n",
|
||||
"COUNTRY OF ORIGIN\n",
|
||||
"- Italy\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Introducing our stunning mid-century inspired office chair, the perfect addition to any home or business setting. Part of a beautiful family of office furniture, including filing cabinets, desks, bookcases, meeting tables, and more, this chair is available in several options of shell color and base finishes to suit your style. Choose from plastic back and front upholstery (SWC-100) or full upholstery (SWC-110) in 10 fabric and 6 leather options.\n",
|
||||
"\n",
|
||||
"The chair is constructed with a 5-wheel plastic coated aluminum base and features a pneumatic chair adjust for easy raise/lower action. It is available with or without armrests and is qualified for contract use. The base finish options are stainless steel, matte black, gloss white, or chrome.\n",
|
||||
"\n",
|
||||
"Measuring at a width of 53 cm, depth of 51 cm, and height of 80 cm, with a seat height of 44 cm and seat depth of 41 cm, this chair is designed for ultimate comfort. You can also choose between soft or hard-floor caster options and two choices of seat foam densities: medium (1.8 lb/ft3) or high (2.8 lb/ft3). The armrests are available in either an armless or 8 position PU option.\n",
|
||||
"\n",
|
||||
"The materials used in the construction of this chair are of the highest quality. The shell base glider is made of cast aluminum with modified nylon PA6/PA66 coating and has a shell thickness of 10 mm. The seat is made of HD36 foam, ensuring maximum comfort and durability.\n",
|
||||
"\n",
|
||||
"This chair is made in Italy and is the perfect combination of style and functionality. Upgrade your workspace with our mid-century inspired office chair today!\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Prompt :基于说明书生成营销描述\n",
|
||||
"prompt = f\"\"\"\n",
|
||||
"Your task is to help a marketing team create a \n",
|
||||
"description for a retail website of a product based \n",
|
||||
"on a technical fact sheet.\n",
|
||||
"\n",
|
||||
"Write a product description based on the information \n",
|
||||
"provided in the technical specifications delimited by \n",
|
||||
"triple backticks.\n",
|
||||
"\n",
|
||||
"Technical specifications: ```{fact_sheet_chair}```\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 示例:产品说明书\n",
|
||||
"fact_sheet_chair = \"\"\"\n",
|
||||
"概述\n",
|
||||
"\n",
|
||||
" 美丽的中世纪风格办公家具系列的一部分,包括文件柜、办公桌、书柜、会议桌等。\n",
|
||||
" 多种外壳颜色和底座涂层可选。\n",
|
||||
" 可选塑料前后靠背装饰(SWC-100)或10种面料和6种皮革的全面装饰(SWC-110)。\n",
|
||||
" 底座涂层选项为:不锈钢、哑光黑色、光泽白色或铬。\n",
|
||||
" 椅子可带或不带扶手。\n",
|
||||
" 适用于家庭或商业场所。\n",
|
||||
" 符合合同使用资格。\n",
|
||||
"\n",
|
||||
"结构\n",
|
||||
"\n",
|
||||
" 五个轮子的塑料涂层铝底座。\n",
|
||||
" 气动椅子调节,方便升降。\n",
|
||||
"\n",
|
||||
"尺寸\n",
|
||||
"\n",
|
||||
" 宽度53厘米|20.87英寸\n",
|
||||
" 深度51厘米|20.08英寸\n",
|
||||
" 高度80厘米|31.50英寸\n",
|
||||
" 座椅高度44厘米|17.32英寸\n",
|
||||
" 座椅深度41厘米|16.14英寸\n",
|
||||
"\n",
|
||||
"选项\n",
|
||||
"\n",
|
||||
" 软地板或硬地板滚轮选项。\n",
|
||||
" 两种座椅泡沫密度可选:中等(1.8磅/立方英尺)或高(2.8磅/立方英尺)。\n",
|
||||
" 无扶手或8个位置PU扶手。\n",
|
||||
"\n",
|
||||
"材料\n",
|
||||
"外壳底座滑动件\n",
|
||||
"\n",
|
||||
" 改性尼龙PA6/PA66涂层的铸铝。\n",
|
||||
" 外壳厚度:10毫米。\n",
|
||||
" 座椅\n",
|
||||
" HD36泡沫\n",
|
||||
"\n",
|
||||
"原产国\n",
|
||||
"\n",
|
||||
" 意大利\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"产品描述:\n",
|
||||
"\n",
|
||||
"我们自豪地推出美丽的中世纪风格办公家具系列,其中包括文件柜、办公桌、书柜、会议桌等。我们的产品采用多种外壳颜色和底座涂层,以满足您的个性化需求。您可以选择塑料前后靠背装饰(SWC-100)或10种面料和6种皮革的全面装饰(SWC-110),以使您的办公室更加舒适和时尚。\n",
|
||||
"\n",
|
||||
"我们的底座涂层选项包括不锈钢、哑光黑色、光泽白色或铬,以满足您的不同需求。椅子可带或不带扶手,适用于家庭或商业场所。我们的产品符合合同使用资格,为您提供更加可靠的保障。\n",
|
||||
"\n",
|
||||
"我们的产品采用五个轮子的塑料涂层铝底座,气动椅子调节,方便升降。尺寸为宽度53厘米|20.87英寸,深度51厘米|20.08英寸,高度80厘米|31.50英寸,座椅高度44厘米|17.32英寸,座椅深度41厘米|16.14英寸,为您提供舒适的使用体验。\n",
|
||||
"\n",
|
||||
"我们的产品还提供软地板或硬地板滚轮选项,两种座椅泡沫密度可选:中等(1.8磅/立方英尺)或高(2.8磅/立方英尺),以及无扶手或8个位置PU扶手,以满足您的不同需求。\n",
|
||||
"\n",
|
||||
"我们的产品采用改性尼龙PA6/PA66涂层的铸铝外壳底座滑动件,外壳厚度为10毫米,座椅采用HD36泡沫,为您提供更加舒适的使用体验。我们的产品原产国为意大利,为您提供更加优质的品质保证。\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Prompt :基于说明书创建营销描述\n",
|
||||
"prompt = f\"\"\"\n",
|
||||
"您的任务是帮助营销团队基于技术说明书创建一个产品的营销描述。\n",
|
||||
"\n",
|
||||
"根据```标记的技术说明书中提供的信息,编写一个产品描述。\n",
|
||||
"\n",
|
||||
"技术说明: ```{fact_sheet_chair}```\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2.1 问题一:生成文本太长\n",
|
||||
"\n",
|
||||
"它似乎很好地完成了要求,即从技术说明书开始编写产品描述,介绍了一个精致的中世纪风格办公椅。但是当我看到这个时,我会觉得这个太长了。\n",
|
||||
"\n",
|
||||
"所以在上述过程中,我产生想法后写了一个 Prompt ,并得到了结果,但是我对它不是很满意,因为它太长了。所以我澄清我的 Prompt ,要求它限制生成文本长度,要求最多使用50个字。\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Introducing our beautiful medieval-style office furniture collection, including filing cabinets, desks, bookcases, and conference tables. Choose from a variety of shell colors and base coatings, with optional plastic or fabric/leather decoration. The chair features a plastic-coated aluminum base with five wheels and pneumatic height adjustment. Perfect for home or commercial use. Made in Italy.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 优化后的 Prompt,要求生成描述不多于 50 词\n",
|
||||
"prompt = f\"\"\"\n",
|
||||
"Your task is to help a marketing team create a \n",
|
||||
"description for a retail website of a product based \n",
|
||||
"on a technical fact sheet.\n",
|
||||
"\n",
|
||||
"Write a product description based on the information \n",
|
||||
"provided in the technical specifications delimited by \n",
|
||||
"triple backticks.\n",
|
||||
"\n",
|
||||
"Use at most 50 words.\n",
|
||||
"\n",
|
||||
"Technical specifications: ```{fact_sheet_chair}```\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"提取回答并根据空格拆分,答案为54个字,较好地完成了设计要求。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"54\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"lst = response.split()\n",
|
||||
"print(len(lst))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"中世纪风格办公家具系列,包括文件柜、办公桌、书柜、会议桌等。多种颜色和涂层可选,可带或不带扶手。底座涂层选项为不锈钢、哑光黑色、光泽白色或铬。适用于家庭或商业场所,符合合同使用资格。意大利制造。\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 优化后的 Prompt,要求生成描述不多于 50 词\n",
|
||||
"prompt = f\"\"\"\n",
|
||||
"您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。\n",
|
||||
"\n",
|
||||
"根据```标记的技术说明书中提供的信息,编写一个产品描述。\n",
|
||||
"\n",
|
||||
"使用最多50个词。\n",
|
||||
"\n",
|
||||
"技术规格:```{fact_sheet_chair}```\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"97"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 由于中文需要分词,此处直接计算整体长度\n",
|
||||
"len(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"LLM在能堪堪胜任严格的字数限制,但实现得并不精确。此例中,英文输出要求控制在50个词,但有时会输出60或65个单词的内容,但这也还算合理。原因是 LLM 使用分词器(tokenizer)解释文本,但它们往往在计算字符方面表现一般般。有很多不同的方法来尝试控制您得到的输出的长度(如若干句话/词/个汉字/个字母 (characters) 等)。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2.2 问题二:抓错文本细节\n",
|
||||
"\n",
|
||||
"我们继续完善这段推广词,会发现的第二个问题是,这个网站并不是直接向消费者销售,它实际上面向的是家具零售商,他们会更关心椅子的技术细节和材料。在这种情况下,您可以继续修改这个 Prompt ,让它更精确地描述椅子的技术细节。\n",
|
||||
"\n",
|
||||
"解决方法:要求它专注于与目标受众相关的方面。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Introducing our beautiful medieval-style office furniture collection, including file cabinets, desks, bookcases, and conference tables. Available in multiple shell colors and base coatings, with optional plastic or fabric/leather upholstery. Features a plastic-coated aluminum base with five wheels and pneumatic chair adjustment. Suitable for home or commercial use and made with high-quality materials, including cast aluminum with a modified nylon coating and HD36 foam. Made in Italy.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 优化后的 Prompt,说明面向对象,应具有什么性质且侧重于什么方面\n",
|
||||
"prompt = f\"\"\"\n",
|
||||
"Your task is to help a marketing team create a \n",
|
||||
"description for a retail website of a product based \n",
|
||||
"on a technical fact sheet.\n",
|
||||
"\n",
|
||||
"Write a product description based on the information \n",
|
||||
"provided in the technical specifications delimited by \n",
|
||||
"triple backticks.\n",
|
||||
"\n",
|
||||
"The description is intended for furniture retailers, \n",
|
||||
"so should be technical in nature and focus on the \n",
|
||||
"materials the product is constructed from.\n",
|
||||
"\n",
|
||||
"Use at most 50 words.\n",
|
||||
"\n",
|
||||
"Technical specifications: ```{fact_sheet_chair}```\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"这款中世纪风格办公家具系列包括文件柜、办公桌、书柜和会议桌等,适用于家庭或商业场所。可选多种外壳颜色和底座涂层,底座涂层选项为不锈钢、哑光黑色、光泽白色或铬。椅子可带或不带扶手,可选软地板或硬地板滚轮,两种座椅泡沫密度可选。外壳底座滑动件采用改性尼龙PA6/PA66涂层的铸铝,座椅采用HD36泡沫。原产国为意大利。\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 优化后的 Prompt,说明面向对象,应具有什么性质且侧重于什么方面\n",
|
||||
"prompt = f\"\"\"\n",
|
||||
"您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。\n",
|
||||
"\n",
|
||||
"根据```标记的技术说明书中提供的信息,编写一个产品描述。\n",
|
||||
"\n",
|
||||
"该描述面向家具零售商,因此应具有技术性质,并侧重于产品的材料构造。\n",
|
||||
"\n",
|
||||
"使用最多50个单词。\n",
|
||||
"\n",
|
||||
"技术规格: ```{fact_sheet_chair}```\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"可见,通过修改 Prompt ,模型的关注点倾向了具体特征与技术细节。\n",
|
||||
"\n",
|
||||
"我可能进一步想要在描述的结尾展示出产品ID。因此,我可以进一步改进这个 Prompt ,要求在描述的结尾,展示出说明书中的7位产品ID。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Introducing our beautiful medieval-style office furniture collection, featuring file cabinets, desks, bookshelves, and conference tables. Available in multiple shell colors and base coatings, with optional plastic or fabric/leather decorations. The chair comes with or without armrests and has a plastic-coated aluminum base with five wheels and pneumatic height adjustment. Suitable for home or commercial use. Made in Italy.\n",
|
||||
"\n",
|
||||
"Product IDs: SWC-100, SWC-110\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 更进一步,要求在描述末尾包含 7个字符的产品ID\n",
|
||||
"prompt = f\"\"\"\n",
|
||||
"Your task is to help a marketing team create a \n",
|
||||
"description for a retail website of a product based \n",
|
||||
"on a technical fact sheet.\n",
|
||||
"\n",
|
||||
"Write a product description based on the information \n",
|
||||
"provided in the technical specifications delimited by \n",
|
||||
"triple backticks.\n",
|
||||
"\n",
|
||||
"The description is intended for furniture retailers, \n",
|
||||
"so should be technical in nature and focus on the \n",
|
||||
"materials the product is constructed from.\n",
|
||||
"\n",
|
||||
"At the end of the description, include every 7-character \n",
|
||||
"Product ID in the technical specification.\n",
|
||||
"\n",
|
||||
"Use at most 50 words.\n",
|
||||
"\n",
|
||||
"Technical specifications: ```{fact_sheet_chair}```\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"这款中世纪风格的办公家具系列包括文件柜、办公桌、书柜和会议桌等,适用于家庭或商业场所。可选多种外壳颜色和底座涂层,底座涂层选项为不锈钢、哑光黑色、光泽白色或铬。椅子可带或不带扶手,可选塑料前后靠背装饰或10种面料和6种皮革的全面装饰。座椅采用HD36泡沫,可选中等或高密度,座椅高度44厘米,深度41厘米。外壳底座滑动件采用改性尼龙PA6/PA66涂层的铸铝,外壳厚度为10毫米。原产国为意大利。产品ID:SWC-100/SWC-110。\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 更进一步\n",
|
||||
"prompt = f\"\"\"\n",
|
||||
"您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。\n",
|
||||
"\n",
|
||||
"根据```标记的技术说明书中提供的信息,编写一个产品描述。\n",
|
||||
"\n",
|
||||
"该描述面向家具零售商,因此应具有技术性质,并侧重于产品的材料构造。\n",
|
||||
"\n",
|
||||
"在描述末尾,包括技术规格中每个7个字符的产品ID。\n",
|
||||
"\n",
|
||||
"使用最多50个单词。\n",
|
||||
"\n",
|
||||
"技术规格: ```{fact_sheet_chair}```\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"以上是许多开发人员通常会经历的 Prompt 开发的迭代过程简短示例。我的建议是,像上一章中所演示的那样,Prompt 应该保持清晰和明确,并在必要时给模型一些思考时间。在这些要求的基础上,常见流程是首先尝试编写一版 Prompt ,看看会发生什么,然后继续迭代完善 Prompt,以逐渐接近所需的结果。许多成功的 Prompt 都是通过这种迭代过程得出的。我将向您展示一个更复杂的 Prompt 示例,可能会让您对 ChatGPT 的能力有更深入的了解。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2.3 问题三:添加表格描述\n",
|
||||
"继续添加指引,要求提取产品尺寸信息并组织成表格,并指定表格的列、表名和格式;再将所有内容格式化为可以在网页使用的 HTML。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<div>\n",
|
||||
" <p>Introducing our beautiful collection of medieval-style office furniture, including file cabinets, desks, bookcases, and conference tables. Choose from a variety of shell colors and base coatings. You can opt for plastic front and backrest decoration (SWC-100) or full decoration with 10 fabrics and 6 leathers (SWC-110). Base coating options include stainless steel, matte black, glossy white, or chrome. The chair is available with or without armrests and is suitable for both home and commercial settings. It is contract eligible.</p>\n",
|
||||
" <p>The structure features a plastic-coated aluminum base with five wheels. The chair is pneumatically adjustable for easy height adjustment.</p>\n",
|
||||
" <p>Product IDs: SWC-100, SWC-110</p>\n",
|
||||
" <table>\n",
|
||||
" <caption>Product Dimensions</caption>\n",
|
||||
" <tr>\n",
|
||||
" <td>Width</td>\n",
|
||||
" <td>20.87 inches</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <td>Depth</td>\n",
|
||||
" <td>20.08 inches</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <td>Height</td>\n",
|
||||
" <td>31.50 inches</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <td>Seat Height</td>\n",
|
||||
" <td>17.32 inches</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <td>Seat Depth</td>\n",
|
||||
" <td>16.14 inches</td>\n",
|
||||
" </tr>\n",
|
||||
" </table>\n",
|
||||
" <p>Options include soft or hard floor casters. You can choose from two seat foam densities: medium (1.8 pounds/cubic foot) or high (2.8 pounds/cubic foot). The chair is available with or without 8-position PU armrests.</p>\n",
|
||||
" <p>Materials:</p>\n",
|
||||
" <ul>\n",
|
||||
" <li>Shell, base, and sliding parts: cast aluminum coated with modified nylon PA6/PA66. Shell thickness: 10mm.</li>\n",
|
||||
" <li>Seat: HD36 foam</li>\n",
|
||||
" </ul>\n",
|
||||
" <p>Made in Italy.</p>\n",
|
||||
"</div>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 要求它抽取信息并组织成表格,并指定表格的列、表名和格式\n",
|
||||
"prompt = f\"\"\"\n",
|
||||
"Your task is to help a marketing team create a \n",
|
||||
"description for a retail website of a product based \n",
|
||||
"on a technical fact sheet.\n",
|
||||
"\n",
|
||||
"Write a product description based on the information \n",
|
||||
"provided in the technical specifications delimited by \n",
|
||||
"triple backticks.\n",
|
||||
"\n",
|
||||
"The description is intended for furniture retailers, \n",
|
||||
"so should be technical in nature and focus on the \n",
|
||||
"materials the product is constructed from.\n",
|
||||
"\n",
|
||||
"At the end of the description, include every 7-character \n",
|
||||
"Product ID in the technical specification.\n",
|
||||
"\n",
|
||||
"After the description, include a table that gives the \n",
|
||||
"product's dimensions. The table should have two columns.\n",
|
||||
"In the first column include the name of the dimension. \n",
|
||||
"In the second column include the measurements in inches only.\n",
|
||||
"\n",
|
||||
"Give the table the title 'Product Dimensions'.\n",
|
||||
"\n",
|
||||
"Format everything as HTML that can be used in a website. \n",
|
||||
"Place the description in a <div> element.\n",
|
||||
"\n",
|
||||
"Technical specifications: ```{fact_sheet_chair}```\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
" <p>Introducing our beautiful collection of medieval-style office furniture, including file cabinets, desks, bookcases, and conference tables. Choose from a variety of shell colors and base coatings. You can opt for plastic front and backrest decoration (SWC-100) or full decoration with 10 fabrics and 6 leathers (SWC-110). Base coating options include stainless steel, matte black, glossy white, or chrome. The chair is available with or without armrests and is suitable for both home and commercial settings. It is contract eligible.</p>\n",
|
||||
" <p>The structure features a plastic-coated aluminum base with five wheels. The chair is pneumatically adjustable for easy height adjustment.</p>\n",
|
||||
" <p>Product IDs: SWC-100, SWC-110</p>\n",
|
||||
" <table>\n",
|
||||
" <caption>Product Dimensions</caption>\n",
|
||||
" <tr>\n",
|
||||
" <td>Width</td>\n",
|
||||
" <td>20.87 inches</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <td>Depth</td>\n",
|
||||
" <td>20.08 inches</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <td>Height</td>\n",
|
||||
" <td>31.50 inches</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <td>Seat Height</td>\n",
|
||||
" <td>17.32 inches</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <td>Seat Depth</td>\n",
|
||||
" <td>16.14 inches</td>\n",
|
||||
" </tr>\n",
|
||||
" </table>\n",
|
||||
" <p>Options include soft or hard floor casters. You can choose from two seat foam densities: medium (1.8 pounds/cubic foot) or high (2.8 pounds/cubic foot). The chair is available with or without 8-position PU armrests.</p>\n",
|
||||
" <p>Materials:</p>\n",
|
||||
" <ul>\n",
|
||||
" <li>Shell, base, and sliding parts: cast aluminum coated with modified nylon PA6/PA66. Shell thickness: 10mm.</li>\n",
|
||||
" <li>Seat: HD36 foam</li>\n",
|
||||
" </ul>\n",
|
||||
" <p>Made in Italy.</p>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 表格是以 HTML 格式呈现的,加载出来\n",
|
||||
"from IPython.display import display, HTML\n",
|
||||
"\n",
|
||||
"display(HTML(response))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<div>\n",
|
||||
"<h2>中世纪风格办公家具系列椅子</h2>\n",
|
||||
"<p>这款椅子是中世纪风格办公家具系列的一部分,适用于家庭或商业场所。它有多种外壳颜色和底座涂层可选,包括不锈钢、哑光黑色、光泽白色或铬。您可以选择带或不带扶手的椅子,以及软地板或硬地板滚轮选项。此外,您可以选择两种座椅泡沫密度:中等(1.8磅/立方英尺)或高(2.8磅/立方英尺)。</p>\n",
|
||||
"<p>椅子的外壳底座滑动件是改性尼龙PA6/PA66涂层的铸铝,外壳厚度为10毫米。座椅采用HD36泡沫,底座是五个轮子的塑料涂层铝底座,可以进行气动椅子调节,方便升降。此外,椅子符合合同使用资格,是您理想的选择。</p>\n",
|
||||
"<p>产品ID:SWC-100</p>\n",
|
||||
"</div>\n",
|
||||
"\n",
|
||||
"<table>\n",
|
||||
" <caption>产品尺寸</caption>\n",
|
||||
" <tr>\n",
|
||||
" <th>宽度</th>\n",
|
||||
" <td>20.87英寸</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>深度</th>\n",
|
||||
" <td>20.08英寸</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>高度</th>\n",
|
||||
" <td>31.50英寸</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>座椅高度</th>\n",
|
||||
" <td>17.32英寸</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>座椅深度</th>\n",
|
||||
" <td>16.14英寸</td>\n",
|
||||
" </tr>\n",
|
||||
"</table>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 要求它抽取信息并组织成表格,并指定表格的列、表名和格式\n",
|
||||
"prompt = f\"\"\"\n",
|
||||
"您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。\n",
|
||||
"\n",
|
||||
"根据```标记的技术说明书中提供的信息,编写一个产品描述。\n",
|
||||
"\n",
|
||||
"该描述面向家具零售商,因此应具有技术性质,并侧重于产品的材料构造。\n",
|
||||
"\n",
|
||||
"在描述末尾,包括技术规格中每个7个字符的产品ID。\n",
|
||||
"\n",
|
||||
"在描述之后,包括一个表格,提供产品的尺寸。表格应该有两列。第一列包括尺寸的名称。第二列只包括英寸的测量值。\n",
|
||||
"\n",
|
||||
"给表格命名为“产品尺寸”。\n",
|
||||
"\n",
|
||||
"将所有内容格式化为可用于网站的HTML格式。将描述放在<div>元素中。\n",
|
||||
"\n",
|
||||
"技术规格:```{fact_sheet_chair}```\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<h2>中世纪风格办公家具系列椅子</h2>\n",
|
||||
"<p>这款椅子是中世纪风格办公家具系列的一部分,适用于家庭或商业场所。它有多种外壳颜色和底座涂层可选,包括不锈钢、哑光黑色、光泽白色或铬。您可以选择带或不带扶手的椅子,以及软地板或硬地板滚轮选项。此外,您可以选择两种座椅泡沫密度:中等(1.8磅/立方英尺)或高(2.8磅/立方英尺)。</p>\n",
|
||||
"<p>椅子的外壳底座滑动件是改性尼龙PA6/PA66涂层的铸铝,外壳厚度为10毫米。座椅采用HD36泡沫,底座是五个轮子的塑料涂层铝底座,可以进行气动椅子调节,方便升降。此外,椅子符合合同使用资格,是您理想的选择。</p>\n",
|
||||
"<p>产品ID:SWC-100</p>\n",
|
||||
"</div>\n",
|
||||
"\n",
|
||||
"<table>\n",
|
||||
" <caption>产品尺寸</caption>\n",
|
||||
" <tr>\n",
|
||||
" <th>宽度</th>\n",
|
||||
" <td>20.87英寸</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>深度</th>\n",
|
||||
" <td>20.08英寸</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>高度</th>\n",
|
||||
" <td>31.50英寸</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>座椅高度</th>\n",
|
||||
" <td>17.32英寸</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>座椅深度</th>\n",
|
||||
" <td>16.14英寸</td>\n",
|
||||
" </tr>\n",
|
||||
"</table>"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 表格是以 HTML 格式呈现的,加载出来\n",
|
||||
"from IPython.display import display, HTML\n",
|
||||
"\n",
|
||||
"display(HTML(response))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"本章的主要内容是 LLM 在开发应用程序中的迭代式 Prompt 开发过程。开发者需要先尝试编写 Prompt ,然后通过迭代逐步完善它,直至得到需要的结果。作为一名高效的提示词工程师(Prompt Engineer),关键在于掌握有效的开发Prompt的过程,而不是去寻求得到“完美的”Prompt。对于一些更复杂的应用程序,可以对多个样本(如数百张说明书)进行 Prompt 的迭代开发,并在样本集上进行评估。\n",
|
||||
"\n",
|
||||
"最后,在更成熟的应用程序中,可以观察多个Prompt在多个样本集上的表现,测试平均或最差性能。但通常,**仅当**应用较成型之后,才推荐您通过这种评估方式,来精益求精。\n",
|
||||
"\n",
|
||||
"请使用 Jupyter Notebook,动手实践本节给出的示例,并尝试不同的变化,查看结果。"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.8.13"
|
||||
},
|
||||
"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
|
||||
}
|
||||
@ -0,0 +1,742 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "b58204ea",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 第四章 文本概括"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a190d6a1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<div class=\"toc\">\n",
|
||||
" <ul class=\"toc-item\">\n",
|
||||
" <li><span><a href=\"#一引言\" data-toc-modified-id=\"一、引言\">一、引言</a></span></li>\n",
|
||||
" <li>\n",
|
||||
" <span><a href=\"#二单一文本概括\" data-toc-modified-id=\"二、单一文本概括实验\">二、单一文本概括实验</a></span>\n",
|
||||
" <ul class=\"toc-item\">\n",
|
||||
" <li><span><a href=\"#21-限制输出文本长度\" data-toc-modified-id=\"2.1 限制输出文本长度\">2.1 限制输出文本长度</a></span></li> \n",
|
||||
" <li><span><a href=\"#22-设置关键角度侧重\" data-toc-modified-id=\"2.2 设置关键角度侧重\">2.2 设置关键角度侧重</a></span></li>\n",
|
||||
" <li><span><a href=\"#23-关键信息提取\" data-toc-modified-id=\"2.3 关键信息提取\">2.3 关键信息提取</a></span></li>\n",
|
||||
" </ul>\n",
|
||||
" </li>\n",
|
||||
" <li><span><a href=\"#三同时概括多条文本\" data-toc-modified-id=\"三、同时概括多条文本\">三、同时概括多条文本</a></span></li>\n",
|
||||
" </ul>\n",
|
||||
"</div>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "b70ad003",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 一、引言"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "12fa9ea4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"当今世界上文本信息浩如烟海,我们很难拥有足够的时间去阅读所有想了解的东西。但欣喜的是,目前LLM在文本概括任务上展现了强大的水准,也已经有不少团队将概括功能实现在多种应用中。\n",
|
||||
"\n",
|
||||
"本章节将介绍如何使用编程的方式,调用API接口来实现“文本概括”功能。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "1de4fd1e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"首先,我们需要引入 OpenAI 包,加载 API 密钥,定义 getCompletion 函数。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1b4bfa7f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import openai\n",
|
||||
"# 导入第三方库\n",
|
||||
"\n",
|
||||
"openai.api_key = \"sk-...\"\n",
|
||||
"# 设置 API_KEY, 请替换成您自己的 API_KEY\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "9f679f1f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_completion(prompt, model=\"gpt-3.5-turbo\"): \n",
|
||||
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
|
||||
" response = openai.ChatCompletion.create(\n",
|
||||
" model=model,\n",
|
||||
" messages=messages,\n",
|
||||
" temperature=0, # 值越低则输出文本随机性越低\n",
|
||||
" )\n",
|
||||
" return response.choices[0].message[\"content\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "9cca835b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 二、单一文本概括"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "0c1e1b92",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"以商品评论的总结任务为例:对于电商平台来说,网站上往往存在着海量的商品评论,这些评论反映了所有客户的想法。如果我们拥有一个工具去概括这些海量、冗长的评论,便能够快速地浏览更多评论,洞悉客户的偏好,从而指导平台与商家提供更优质的服务。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "9dc2e2bc",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**输入文本**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "4d9c0eeb",
|
||||
"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",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "aad5bd2a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**输入文本(中文翻译)**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "43b5dd25",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prod_review_zh = \"\"\"\n",
|
||||
"这个熊猫公仔是我给女儿的生日礼物,她很喜欢,去哪都带着。\n",
|
||||
"公仔很软,超级可爱,面部表情也很和善。但是相比于价钱来说,\n",
|
||||
"它有点小,我感觉在别的地方用同样的价钱能买到更大的。\n",
|
||||
"快递比预期提前了一天到货,所以在送给女儿之前,我自己玩了会。\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "662c9cd2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 2.1 限制输出文本长度"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "a6d10814",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"我们尝试限制文本长度为最多30词。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "02208fbc",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Soft and cute panda plush toy loved by daughter, but a bit small for the price. Arrived early.\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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "0df0eb90",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"中文翻译版本"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "bf4b39f9",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"可爱软熊猫公仔,女儿喜欢,面部表情和善,但价钱有点小贵,快递提前一天到货。\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = f\"\"\"\n",
|
||||
"您的任务是从电子商务网站上生成一个产品评论的简短摘要。\n",
|
||||
"\n",
|
||||
"请对三个反引号之间的评论文本进行概括,最多30个词汇。\n",
|
||||
"\n",
|
||||
"评论: ```{prod_review_zh}```\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "e9ab145e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 2.2 设置关键角度侧重"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "f84d0123",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"有时,针对不同的业务,我们对文本的侧重会有所不同。例如对于商品评论文本,物流会更关心运输时效,商家更加关心价格与商品质量,平台更关心整体服务体验。\n",
|
||||
"\n",
|
||||
"我们可以通过增加Prompt提示,来体现对于某个特定角度的侧重。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d6f8509a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 2.2.1 侧重于快递服务"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "9d8a32a6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"The panda plush toy arrived a day earlier than expected, but the customer felt it was a bit small for the price paid.\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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "0bd4243a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"中文翻译版本"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "80636c3e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"快递提前到货,熊猫公仔软可爱,但有点小,价钱不太划算。\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = f\"\"\"\n",
|
||||
"您的任务是从电子商务网站上生成一个产品评论的简短摘要。\n",
|
||||
"\n",
|
||||
"请对三个反引号之间的评论文本进行概括,最多30个词汇,并且聚焦在产品运输上。\n",
|
||||
"\n",
|
||||
"评论: ```{prod_review_zh}```\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": [
|
||||
"### 2.2.2 侧重于价格与质量"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "767f252c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"The panda plush toy is soft, cute, and loved by the recipient, but the price may be too high for its size compared to other options.\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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "cf54fac4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"中文翻译版本"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"id": "728d6c57",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"可爱软熊猫公仔,面部表情友好,但价钱有点高,尺寸较小。快递提前一天到货。\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = f\"\"\"\n",
|
||||
"您的任务是从电子商务网站上生成一个产品评论的简短摘要。\n",
|
||||
"\n",
|
||||
"请对三个反引号之间的评论文本进行概括,最多30个词汇,并且聚焦在产品价格和质量上。\n",
|
||||
"\n",
|
||||
"评论: ```{prod_review_zh}```\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": [
|
||||
"### 2.3 关键信息提取"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "ba6f5c25",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"在2.2节中,虽然我们通过添加关键角度侧重的 Prompt ,使得文本摘要更侧重于某一特定方面,但是可以发现,结果中也会保留一些其他信息,如偏重价格与质量角度的概括中仍保留了“快递提前到货”的信息。如果我们只想要提取某一角度的信息,并过滤掉其他所有信息,则可以要求 LLM 进行“文本提取( Extract )”而非“概括( Summarize )”"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "2d60dc58",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\"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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "0339b877",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"中文翻译版本"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"id": "c845ccab",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"快递比预期提前了一天到货。\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = f\"\"\"\n",
|
||||
"您的任务是从电子商务网站上的产品评论中提取相关信息。\n",
|
||||
"\n",
|
||||
"请从以下三个反引号之间的评论文本中提取产品运输相关的信息,最多30个词汇。\n",
|
||||
"\n",
|
||||
"评论: ```{prod_review_zh}```\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": 5,
|
||||
"id": "ee7caa78",
|
||||
"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": 10,
|
||||
"id": "9d1aa5ac",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0 Soft and cute panda plush toy loved by daughter, but a bit small for the price. Arrived early. \n",
|
||||
"\n",
|
||||
"1 Affordable lamp with storage, fast shipping, and excellent customer service. Easy to assemble and missing parts were quickly replaced. \n",
|
||||
"\n",
|
||||
"2 Good battery life, small toothbrush head, but effective cleaning. Good deal if bought around $50. \n",
|
||||
"\n",
|
||||
"3 The product was on sale for $49 in November, but the price increased to $70-$89 in December. The base doesn't look as good as previous editions, but the reviewer plans to be gentle with it. A special tip for making smoothies is to freeze the fruits and vegetables beforehand. The motor made a funny noise after a year, and the warranty had expired. Overall quality has decreased. \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\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "eb878522",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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(i, response, \"\\n\")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d757b389",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"0 概括:可爱的熊猫毛绒玩具,质量好,送货快,但有点小。 \n",
|
||||
"\n",
|
||||
"1 这个评论是关于一款具有额外储存空间的床头灯,价格适中。客户对公司的服务和产品表示满意。 \n",
|
||||
"\n",
|
||||
"2 评论概括:电动牙刷电池寿命长,但刷头太小,需要更长的刷毛。价格合理,使用后牙齿感觉干净。 \n",
|
||||
"\n",
|
||||
"3 评论概括:产品价格在12月份上涨,质量不如以前,但交付速度快。 "
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.8.13"
|
||||
},
|
||||
"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
|
||||
}
|
||||
1032
content/Prompt Engineering for Developer/5. 推断 Inferring.ipynb
Normal file
1032
content/Prompt Engineering for Developer/5. 推断 Inferring.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
1205
content/Prompt Engineering for Developer/6. 文本转换 Transforming.ipynb
Normal file
1205
content/Prompt Engineering for Developer/6. 文本转换 Transforming.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
440
content/Prompt Engineering for Developer/7. 文本扩展 Expanding.ipynb
Normal file
440
content/Prompt Engineering for Developer/7. 文本扩展 Expanding.ipynb
Normal file
@ -0,0 +1,440 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 第七章 文本扩展"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<div class=\"toc\">\n",
|
||||
" <ul class=\"toc-item\">\n",
|
||||
" <li><span><a href=\"#一引言\" data-toc-modified-id=\"一、引言\">一、引言</a></span></li>\n",
|
||||
" <li>\n",
|
||||
" <span><a href=\"#二定制客户邮件\" data-toc-modified-id=\"二、定制客户邮件\">二、定制客户邮件</a></span>\n",
|
||||
" </li>\n",
|
||||
" <li><span><a href=\"#三引入温度系数\" data-toc-modified-id=\"三、引入温度系数\">三、引入温度系数</a></span>\n",
|
||||
" </li>\n",
|
||||
" </ul>\n",
|
||||
"</div>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 一、引言\n",
|
||||
"\n",
|
||||
"扩展是将短文本(例如一组说明或主题列表)输入到大型语言模型中,让模型生成更长的文本(例如基于某个主题的电子邮件或论文)。这种应用是一把双刃剑,好处例如将大型语言模型用作头脑风暴的伙伴;但也存在问题,例如某人可能会使用它来生成大量垃圾邮件。因此,当你使用大型语言模型的这些功能时,请仅以**负责任** (responsible) 和**有益于人们** (helps people) 的方式使用它们。\n",
|
||||
"\n",
|
||||
"在本章中,你将学会如何基于 OpenAI API 生成*针对每位客户评价优化*的客服电子邮件。我们还将利用模型的另一个输入参数称为温度,这种参数允许您在模型响应中变化探索的程度和多样性。\n",
|
||||
"\n",
|
||||
"同以上几章,你需要类似的代码来配置一个可以使用 OpenAI API 的环境"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import openai\n",
|
||||
"# 导入第三方库\n",
|
||||
"\n",
|
||||
"openai.api_key = \"sk-...\"\n",
|
||||
"# 设置 API_KEY, 请替换成您自己的 API_KEY\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 一个封装 OpenAI 接口的函数,参数为 Prompt,返回对应结果\n",
|
||||
"def get_completion(prompt, model=\"gpt-3.5-turbo\", temperature=0):\n",
|
||||
" '''\n",
|
||||
" prompt: 对应的提示\n",
|
||||
" model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT),有内测资格的用户可以选择 gpt-4\n",
|
||||
" temperature: 温度系数\n",
|
||||
" '''\n",
|
||||
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
|
||||
" response = openai.ChatCompletion.create(\n",
|
||||
" model=model,\n",
|
||||
" messages=messages,\n",
|
||||
" temperature=temperature, # 模型输出的温度系数,控制输出的随机程度\n",
|
||||
" )\n",
|
||||
" # 调用 OpenAI 的 ChatCompletion 接口\n",
|
||||
" return response.choices[0].message[\"content\"]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 二、定制客户邮件"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"我们将根据客户评价和情感,针对性写自动回复邮件。因此,我们将给定客户评价和情感,使用 LLM 针对性生成响应,即根据客户评价和评论情感生成定制电子邮件。\n",
|
||||
"\n",
|
||||
"我们首先给出一个示例,包括一个评论及对应的情感。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# given the sentiment from the lesson on \"inferring\",\n",
|
||||
"# and the original customer message, customize the email\n",
|
||||
"sentiment = \"negative\"\n",
|
||||
"\n",
|
||||
"# review for a blender\n",
|
||||
"review = f\"\"\"\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",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 我们可以在推理那章学习到如何对一个评论判断其情感倾向\n",
|
||||
"sentiment = \"negative\"\n",
|
||||
"\n",
|
||||
"# 一个产品的评价\n",
|
||||
"review = f\"\"\"\n",
|
||||
"他们在11月份的季节性销售期间以约49美元的价格出售17件套装,折扣约为一半。\\\n",
|
||||
"但由于某些原因(可能是价格欺诈),到了12月第二周,同样的套装价格全都涨到了70美元到89美元不等。\\\n",
|
||||
"11件套装的价格也上涨了大约10美元左右。\\\n",
|
||||
"虽然外观看起来还可以,但基座上锁定刀片的部分看起来不如几年前的早期版本那么好。\\\n",
|
||||
"不过我打算非常温柔地使用它,例如,\\\n",
|
||||
"我会先在搅拌机中将像豆子、冰、米饭等硬物研磨,然后再制成所需的份量,\\\n",
|
||||
"切换到打蛋器制作更细的面粉,或者在制作冰沙时先使用交叉切割刀片,然后使用平面刀片制作更细/不粘的效果。\\\n",
|
||||
"制作冰沙时,特别提示:\\\n",
|
||||
"将水果和蔬菜切碎并冷冻(如果使用菠菜,则轻轻煮软菠菜,然后冷冻直到使用;\\\n",
|
||||
"如果制作果酱,则使用小到中号的食品处理器),这样可以避免在制作冰沙时添加太多冰块。\\\n",
|
||||
"大约一年后,电机发出奇怪的噪音,我打电话给客服,但保修已经过期了,所以我不得不再买一个。\\\n",
|
||||
"总的来说,这些产品的总体质量已经下降,因此它们依靠品牌认可和消费者忠诚度来维持销售。\\\n",
|
||||
"货物在两天内到达。\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"我们已经使用推断课程中所学方法提取了情感,这是一个关于搅拌机的客户评价,现在我们将根据情感定制回复。\n",
|
||||
"\n",
|
||||
"以下述 Prompt 为例:假设你是一个客户服务 AI 助手,你的任务是为客户发送电子邮件回复,根据通过三个反引号分隔的客户电子邮件,生成一封回复以感谢客户的评价。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Dear Valued Customer,\n",
|
||||
"\n",
|
||||
"Thank you for taking the time to leave a review about our product. We are sorry to hear that you experienced an increase in price and that the quality of the product did not meet your expectations. We apologize for any inconvenience this may have caused you.\n",
|
||||
"\n",
|
||||
"We would like to assure you that we take all feedback seriously and we will be sure to pass your comments along to our team. If you have any further concerns, please do not hesitate to reach out to our customer service team for assistance.\n",
|
||||
"\n",
|
||||
"Thank you again for your review and for choosing our product. We hope to have the opportunity to serve you better in the future.\n",
|
||||
"\n",
|
||||
"Best regards,\n",
|
||||
"\n",
|
||||
"AI customer agent\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = f\"\"\"\n",
|
||||
"You are a customer service AI assistant.\n",
|
||||
"Your task is to send an email reply to a valued customer.\n",
|
||||
"Given the customer email delimited by ```, \\\n",
|
||||
"Generate a reply to thank the customer for their review.\n",
|
||||
"If the sentiment is positive or neutral, thank them for \\\n",
|
||||
"their review.\n",
|
||||
"If the sentiment is negative, apologize and suggest that \\\n",
|
||||
"they can reach out to customer service. \n",
|
||||
"Make sure to use specific details from the review.\n",
|
||||
"Write in a concise and professional tone.\n",
|
||||
"Sign the email as `AI customer agent`.\n",
|
||||
"Customer review: ```{review}```\n",
|
||||
"Review sentiment: {sentiment}\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"尊敬的客户,\n",
|
||||
"\n",
|
||||
"非常感谢您对我们产品的评价。我们非常抱歉您在购买过程中遇到了价格上涨的问题。我们一直致力于为客户提供最优惠的价格,但由于市场波动,价格可能会有所变化。我们深表歉意,如果您需要任何帮助,请随时联系我们的客户服务团队。\n",
|
||||
"\n",
|
||||
"我们非常感谢您对我们产品的详细评价和使用技巧。我们将会把您的反馈传达给我们的产品团队,以便改进我们的产品质量和性能。\n",
|
||||
"\n",
|
||||
"再次感谢您对我们的支持和反馈。如果您需要任何帮助或有任何疑问,请随时联系我们的客户服务团队。\n",
|
||||
"\n",
|
||||
"祝您一切顺利!\n",
|
||||
"\n",
|
||||
"AI客户代理\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = f\"\"\"\n",
|
||||
"你是一位客户服务的AI助手。\n",
|
||||
"你的任务是给一位重要客户发送邮件回复。\n",
|
||||
"根据客户通过“```”分隔的评价,生成回复以感谢客户的评价。提醒模型使用评价中的具体细节\n",
|
||||
"用简明而专业的语气写信。\n",
|
||||
"作为“AI客户代理”签署电子邮件。\n",
|
||||
"客户评论:\n",
|
||||
"```{review}```\n",
|
||||
"评论情感:{sentiment}\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 三、引入温度系数\n",
|
||||
"\n",
|
||||
"接下来,我们将使用语言模型的一个称为“温度” (Temperature) 的参数,它将允许我们改变模型响应的多样性。您可以将温度视为模型探索或随机性的程度。\n",
|
||||
"\n",
|
||||
"例如,在一个特定的短语中,“我的最爱食品”最有可能的下一个词是“比萨”,其次最有可能的是“寿司”和“塔可”。因此,在温度为零时,模型将总是选择最有可能的下一个词,而在较高的温度下,它还将选择其中一个不太可能的词,在更高的温度下,它甚至可能选择塔可,而这种可能性仅为五分之一。您可以想象,随着模型继续生成更多单词的最终响应,“我的最爱食品是比萨”将会与第一个响应“我的最爱食品是塔可”产生差异。随着模型的继续,这两个响应也将变得越来越不同。\n",
|
||||
"\n",
|
||||
"一般来说,在构建需要可预测响应的应用程序时,我建议**设置温度为零**。在所有课程中,我们一直设置温度为零,如果您正在尝试构建一个可靠和可预测的系统,我认为您应该选择这个温度。如果您尝试以更具创意的方式使用模型,可能需要更广泛地输出不同的结果,那么您可能需要使用更高的温度。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"同一段来信,我们提醒模型使用用户来信中的详细信息,并设置温度:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Dear valued customer,\n",
|
||||
"\n",
|
||||
"Thank you for taking the time to share your review with us. We are sorry to hear that you were disappointed with the prices of our products and the quality of our blender. We apologize for any inconvenience this may have caused you.\n",
|
||||
"\n",
|
||||
"We value your feedback and would like to make things right for you. Please feel free to contact our customer service team so we can assist you with any concerns or issues you may have. We are committed to providing you with the best possible service and products.\n",
|
||||
"\n",
|
||||
"Thank you again for your review and for being a loyal customer. We hope to have the opportunity to serve you better in the future.\n",
|
||||
"\n",
|
||||
"Sincerely,\n",
|
||||
"AI customer agent\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = f\"\"\"\n",
|
||||
"You are a customer service AI assistant.\n",
|
||||
"Your task is to send an email reply to a valued customer.\n",
|
||||
"Given the customer email delimited by ```, \\\n",
|
||||
"Generate a reply to thank the customer for their review.\n",
|
||||
"If the sentiment is positive or neutral, thank them for \\\n",
|
||||
"their review.\n",
|
||||
"If the sentiment is negative, apologize and suggest that \\\n",
|
||||
"they can reach out to customer service. \n",
|
||||
"Make sure to use specific details from the review.\n",
|
||||
"Write in a concise and professional tone.\n",
|
||||
"Sign the email as `AI customer agent`.\n",
|
||||
"Customer review: ```{review}```\n",
|
||||
"Review sentiment: {sentiment}\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt, temperature=0.7)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"尊敬的客户,\n",
|
||||
"\n",
|
||||
"非常感谢您对我们产品的评价。我们由衷地为您在购买过程中遇到的问题表示抱歉。我们确实在12月份的第二周调整了价格,但这是由于市场因素所致,并非价格欺诈。我们深刻意识到您对产品质量的担忧,我们将尽一切努力改进产品,以提供更好的体验。\n",
|
||||
"\n",
|
||||
"我们非常感激您对我们产品的使用经验和制作技巧的分享。您的建议和反馈对我们非常重要,我们将以此为基础,进一步改进我们的产品。\n",
|
||||
"\n",
|
||||
"如果您有任何疑问或需要进一步帮助,请随时联系我们的客户服务部门。我们将尽快回复您并提供帮助。\n",
|
||||
"\n",
|
||||
"最后,请再次感谢您对我们产品的评价和选择。我们期待着未来与您的合作。\n",
|
||||
"\n",
|
||||
"此致\n",
|
||||
"\n",
|
||||
"敬礼\n",
|
||||
"\n",
|
||||
"AI客户代理\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = f\"\"\"\n",
|
||||
"你是一名客户服务的AI助手。\n",
|
||||
"你的任务是给一位重要的客户发送邮件回复。\n",
|
||||
"根据通过“```”分隔的客户电子邮件生成回复,以感谢客户的评价。\n",
|
||||
"如果情感是积极的或中性的,感谢他们的评价。\n",
|
||||
"如果情感是消极的,道歉并建议他们联系客户服务。\n",
|
||||
"请确保使用评论中的具体细节。\n",
|
||||
"以简明和专业的语气写信。\n",
|
||||
"以“AI客户代理”的名义签署电子邮件。\n",
|
||||
"客户评价:```{review}```\n",
|
||||
"评论情感:{sentiment}\n",
|
||||
"\"\"\"\n",
|
||||
"response = get_completion(prompt, temperature=0.7)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"在温度为零时,每次执行相同的 Prompt ,您获得的回复理应相同。而使用温度为 0.7 时,则每次都会获得不同的输出。\n",
|
||||
"\n",
|
||||
"所以,您可以看到它与我们之前收到的电子邮件不同。再次执行将再次获得不同的电子邮件。\n",
|
||||
"\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.8.13"
|
||||
},
|
||||
"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": false
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
957
content/Prompt Engineering for Developer/8. 聊天机器人 Chatbot.ipynb
Normal file
957
content/Prompt Engineering for Developer/8. 聊天机器人 Chatbot.ipynb
Normal file
File diff suppressed because one or more lines are too long
18
content/Prompt Engineering for Developer/9. 总结.md
Normal file
18
content/Prompt Engineering for Developer/9. 总结.md
Normal file
@ -0,0 +1,18 @@
|
||||
**恭喜您完成了这门短期课程。**
|
||||
|
||||
总的来说,在这门课程中,我们学习了关于 Prompt 的两个关键原则:
|
||||
|
||||
- 编写清晰具体的指令;
|
||||
- 如果适当的话,给模型一些思考时间。
|
||||
|
||||
您还学习了迭代式 Prompt 开发的方法,并了解了如何找到适合您应用程序的 Prompt 的过程是非常关键的。
|
||||
|
||||
我们还介绍了许多大型语言模型的功能,包括摘要、推断、转换和扩展。您还学会了如何构建自定义聊天机器人。在这门短期课程中,您学到了很多,希望您喜欢这些学习材料。
|
||||
|
||||
我们希望您能想出一些应用程序的想法,并尝试自己构建它们。请尝试一下并让我们知道您的想法。您可以从一个非常小的项目开始,也许它具有一定的实用价值,也可能完全没有实用价值,只是一些有趣好玩儿的东西。请利用您第一个项目的学习经验来构建更好的第二个项目,甚至更好的第三个项目等。或者,如果您已经有一个更大的项目想法,那就去做吧。
|
||||
|
||||
大型语言模型非常强大,作为提醒,我们希望大家**负责任地**使用它们,请仅构建对他人有**积极影响**的东西。在这个时代,构建人工智能系统的人可以对他人产生巨大的影响。因此必须负责任地使用这些工具。
|
||||
|
||||
现在,基于大型语言模型构建应用程序是一个非常令人兴奋和不断发展的领域。现在您已经完成了这门课程,我们认为您现在拥有了丰富的知识,可以帮助您构建其他人今天不知道如何构建的东西。因此,我希望您也能帮助我们传播并鼓励其他人也参加这门课程。
|
||||
|
||||
最后,希望您在完成这门课程时感到愉快,感谢您完成了这门课程。我们期待得知您构建的惊人之作。
|
||||
21
content/Prompt Engineering for Developer/readme.md
Normal file
21
content/Prompt Engineering for Developer/readme.md
Normal file
@ -0,0 +1,21 @@
|
||||
# Prompt Engineering for Developers
|
||||
|
||||
LLM 正在逐步改变人们的生活,而对于开发者,如何基于 LLM 提供的 API 快速、便捷地开发一些具备更强能力、集成LLM 的应用,来便捷地实现一些更新颖、更实用的能力,是一个急需学习的重要能力。由吴恩达老师与 OpenAI 合作推出的 《ChatGPT Prompt Engineering for Developers》教程面向入门 LLM 的开发者,深入浅出地介绍了对于开发者,如何构造 Prompt 并基于 OpenAI 提供的 API 实现包括总结、推断、转换等多种常用功能,是入门 LLM 开发的经典教程。在可预见的未来,该教程会成为 LLM 的重要入门教程,但是目前还只支持英文版且国内访问受限,打造中文版且国内流畅访问的教程具有重要意义。因此,我们将该课程翻译为中文,并复现其范例代码,支持国内中文学习者直接使用,以帮助中文学习者更好地学习 LLM 开发。
|
||||
|
||||
本教程为吴恩达《ChatGPT Prompt Engineering for Developers》课程中文版,主要内容为指导开发者如何构建 Prompt 并基于 OpenAI API 构建新的、基于 LLM 的应用,包括:
|
||||
> 书写 Prompt 的原则
|
||||
> 文本总结(如总结用户评论);
|
||||
> 文本推断(如情感分类、主题提取);
|
||||
> 文本转换(如翻译、自动纠错);
|
||||
> 扩展(如书写邮件)
|
||||
|
||||
**目录:**
|
||||
1. 简介 Introduction @邹雨衡
|
||||
2. Prompt 的构建原则 Guidelines @邹雨衡
|
||||
3. 如何迭代优化 Prompt Itrative @邹雨衡
|
||||
4. 文本总结 Summarizing @玉琳
|
||||
5. 文本推断 @长琴
|
||||
6. 文本转换 Transforming @玉琳
|
||||
7. 文本扩展 Expand @邹雨衡
|
||||
8. 聊天机器人 @长琴
|
||||
9. 总结 @长琴
|
||||
1249
content/Prompt Engineering for Developer/附1-使用ChatGLM进行学习.ipynb
Normal file
1249
content/Prompt Engineering for Developer/附1-使用ChatGLM进行学习.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user