Finish PE-7

This commit is contained in:
nowadays0421
2023-07-20 16:10:49 +08:00
parent 0ba99478e5
commit 39875fa42f

View File

@ -0,0 +1,447 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 第七章 文本扩展"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"扩展是将短文本(例如一组说明或主题列表)输入到大型语言模型中,让模型生成更长的文本(例如基于某个主题的电子邮件或论文)。这种应用是一把双刃剑,好处例如将大型语言模型用作头脑风暴的伙伴;但也存在问题,例如某人可能会使用它来生成大量垃圾邮件。因此,当你使用大型语言模型的这些功能时,请仅以**负责任** (responsible) 和**有益于人们** (helps people) 的方式使用它们。\n",
"\n",
"在本章中,你将学会如何基于 OpenAI API 生成*针对每位客户评价优化*的客服电子邮件。我们还将利用模型的另一个输入参数称为温度,这种参数允许您在模型响应中变化探索的程度和多样性。\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 一、定制客户邮件"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"我们将根据客户评价和情感,针对性写自动回复邮件。因此,我们将给定客户评价和情感,使用 LLM 针对性生成响应,即根据客户评价和评论情感生成定制电子邮件。\n",
"\n",
"我们首先给出一个示例,包括一个评论及对应的情感。"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# 我们可以在推理那章学习到如何对一个评论判断其情感倾向\n",
"sentiment = \"消极的\"\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": [
"我们已经使用推断课程中所学方法提取了情感,这是一个关于搅拌机的客户评价,现在我们将根据情感定制回复。\n",
"\n",
"以下述 Prompt 为例:假设你是一个客户服务 AI 助手,你的任务是为客户发送电子邮件回复,根据通过三个反引号分隔的客户电子邮件,生成一封回复以感谢客户的评价。"
]
},
{
"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": [
"from tool import get_completion\n",
"\n",
"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": [
"## 二、引入温度系数\n",
"\n",
"接下来,我们将使用语言模型的一个称为“温度” (Temperature) 的参数,它将允许我们改变模型响应的多样性。您可以将温度视为模型探索或随机性的程度。\n",
"\n",
"例如,在一个特定的短语中,“我的最爱食品”最有可能的下一个词是“比萨”,其次最有可能的是“寿司”和“塔可”。因此,在温度为零时,模型将总是选择最有可能的下一个词,而在较高的温度下,它还将选择其中一个不太可能的词,在更高的温度下,它甚至可能选择塔可,而这种可能性仅为五分之一。您可以想象,随着模型继续生成更多单词的最终响应,“我的最爱食品是比萨”将会与第一个响应“我的最爱食品是塔可”产生差异。随着模型的继续,这两个响应也将变得越来越不同。\n",
"\n",
"一般来说,在构建需要可预测响应的应用程序时,我建议**设置温度为零**。在所有课程中,我们一直设置温度为零,如果您正在尝试构建一个可靠和可预测的系统,我认为您应该选择这个温度。如果您尝试以更具创意的方式使用模型,可能需要更广泛地输出不同的结果,那么您可能需要使用更高的温度。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"同一段来信,我们提醒模型使用用户来信中的详细信息,并设置温度:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"尊敬的客户,\n",
"\n",
"感谢您对我们产品的评价。我们非常重视您的意见,并对您在使用过程中遇到的问题表示诚挚的道歉。\n",
"\n",
"我们对价格的变动深感抱歉。根据您的描述我们了解到在12月第二周套装的价格出现了不同程度的上涨。我们会进一步调查此事并确保我们的定价策略更加透明和一致。\n",
"\n",
"您提到了产品部分的质量下降,特别是锁定刀片的部分。我们对此感到非常遗憾,并将反馈给我们的研发团队,以便改进产品的设计和质量控制。我们始终致力于提供优质的产品,以满足客户的需求和期望。\n",
"\n",
"此外,我们将非常感谢您分享了您对产品的使用方式和相关提示。您的经验和建议对我们来说非常宝贵,我们将考虑将其纳入我们的产品改进计划中。\n",
"\n",
"如果您需要进一步帮助或有其他问题,请随时联系我们的客户服务团队。我们将竭诚为您提供支持和解决方案。\n",
"\n",
"再次感谢您的反馈和对我们的支持。我们将继续努力提供更好的产品和服务。\n",
"\n",
"祝您一切顺利!\n",
"\n",
"AI客户代理\n"
]
}
],
"source": [
"# 第一次运行\n",
"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": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"亲爱的客户,\n",
"\n",
"非常感谢您对我们产品的评价和反馈。我们非常重视您的意见,并感谢您对我们产品的支持。\n",
"\n",
"首先,我们对价格的变动感到非常抱歉给您带来了困扰。我们会认真考虑您提到的情况,并采取适当的措施来改进我们的价格策略,以避免类似情况再次发生。\n",
"\n",
"关于产品质量的问题,我们深感抱歉。我们一直致力于提供高质量的产品,并且我们会将您提到的问题反馈给我们的研发团队,以便改进产品的设计和制造过程。\n",
"\n",
"如果您需要更多关于产品保修的信息,或者对我们的其他产品有任何疑问或需求,请随时联系我们的客户服务团队。我们将竭诚为您提供帮助和支持。\n",
"\n",
"再次感谢您对我们产品的评价和支持。我们将继续努力提供优质的产品和出色的客户服务,以满足您的需求。\n",
"\n",
"祝您度过愉快的一天!\n",
"\n",
"AI客户代理\n"
]
}
],
"source": [
"# 第二次运行\n",
"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": [
"在温度为零时,每次执行相同的 Prompt ,您获得的回复理应相同。而使用温度为 0.7 时,则每次都会获得不同的输出。\n",
"\n",
"所以,您可以看到它与我们之前收到的电子邮件不同。再次执行将再次获得不同的电子邮件。\n",
"\n",
"因此,我建议您自己尝试温度,以查看输出如何变化。总之,在更高的温度下,模型的输出更加随机。您几乎可以将其视为在更高的温度下,助手**更易分心**,但也许**更有创造力**。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 三、英文版"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1.1 定制客户邮件**"
]
},
{
"cell_type": "code",
"execution_count": 6,
"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 doesnt 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": 7,
"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 appreciate your feedback and apologize for any inconvenience you may have experienced.\n",
"\n",
"We are sorry to hear about the price increase you noticed in December. We strive to provide competitive pricing for our products, and we understand your frustration. If you have any further concerns regarding pricing or any other issues, we encourage you to reach out to our customer service team. They will be more than happy to assist you.\n",
"\n",
"We also appreciate your feedback regarding the base of the system. We continuously work to improve the quality of our products, and your comments will be taken into consideration for future enhancements.\n",
"\n",
"We apologize for any inconvenience caused by the motor issue you encountered. Our customer service team is always available to assist with any warranty-related concerns. We understand that the warranty had expired, but we would still like to address this matter further. Please feel free to contact our customer service team, and they will do their best to assist you.\n",
"\n",
"Thank you once again for your review. We value your feedback and appreciate your loyalty to our brand. If you have any further questions or concerns, please do not hesitate to contact us.\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": "markdown",
"metadata": {},
"source": [
"**2.1 引入温度系数**"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dear Valued Customer,\n",
"\n",
"Thank you for taking the time to share your feedback with us. We sincerely apologize for any inconvenience you experienced with our pricing and the quality of our product.\n",
"\n",
"We understand your frustration regarding the price increase of our 17 piece system in December. We assure you that price gouging is not our intention, and we apologize for any confusion caused. We appreciate your loyalty and we value your feedback, as it helps us to improve our products and services.\n",
"\n",
"Regarding the issue with the blade lock and the decrease in overall quality, we apologize for any disappointment caused. We strive to provide our customers with the best possible products, and we regret that we did not meet your expectations. We will make sure to take your feedback into consideration for future improvements.\n",
"\n",
"If you require further assistance or if you have any other concerns, please do not hesitate to reach out to our customer service team. They will be more than happy to assist you in resolving any issues you may have.\n",
"\n",
"Once again, we apologize for any inconvenience caused and we appreciate your understanding. We value your business and 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, temperature=0.7)\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.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": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}