简介
梅子留酸软齿牙,芭蕉分绿与窗纱。日长睡起无情思,闲看儿童捉柳花。小伙伴们好,我是微信公众号《小窗幽记机器学习》的小编:卖雪糕的小女孩。紧接前文LLM 系列 | 04:ChatGPT Prompt编写指南 和 05:如何优化ChatGPT Prompt?,今天这篇小作文主要介绍如何通过构建ChatGPT Prompt以解决「文本摘要」任务。
「文本摘要」
以商品评论为例。对于电商平台来说,网站上往往存在着海量的商品评论,这些评论反映了所有客户的想法。如果有一个工具能够概括这些海量、冗长的评论,便能够快速洞悉客户的偏好,从而指导平台与商家提供更优质的服务。
示例评论如下:
prod_review_zh = """
这个熊猫公仔是我给女儿的生日礼物,她很喜欢,去哪都带着。
公仔很软,超级可爱,面部表情也很和善。但是相比于价钱来说,
它有点小,我感觉在别的地方用同样的价钱能买到更大的。
快递比预期提前了一天到货,所以在送给女儿之前,我自己玩了会。
"""
「限制长度」
尝试限制文本长度为最多30词,优化后的 prompt 如下:
prompt = f"""
你的任务是从电子商务网站上生成一个产品评论的简短摘要。
请对三个反引号之间的评论文本进行概括,最多30个词。
评论: ```{prod_review_zh}```
"""
response = get_completion(prompt)
print(response)
输出结果如下:
可爱软熊猫公仔,女儿喜欢,但价钱有点贵,大小不够。快递提前一天到货。
「关注不同角度」
有时,针对不同的业务,对文本的侧重会有所不同。例如对于同一个商品评论文本,物流侧的相关人员会更关心运输时效,而商家更加关心价格与商品质量,平台更关心整体服务体验。这时可以通过增加Prompt提示,来体现对于某个特定角度的侧重。
「侧重于运输」
prompt = f"""
你的任务是从电子商务网站上生成一个产品评论的简短摘要。
请对三个反引号之间的评论文本进行概括,最多30个词汇,并且聚焦在产品运输上。
评论: ```{prod_review_zh}```
"""
response = get_completion(prompt)
print(response)
输出结果如下:
快递提前一天到货,熊猫公仔很可爱但有点小,价钱稍高。
从上述结果可以看出,输出结果以“快递提前一天到货”开头,体现了对于快递效率的侧重。
「侧重于价格与质量」
prompt = f"""
你的任务是从电子商务网站上生成一个产品评论的简短摘要。
请对三个反引号之间的评论文本进行概括,最多30个词,并且聚焦在产品价格和质量上。
评论: ```{prod_review_zh}```
"""
response = get_completion(prompt)
print(response)
输出结果如下:
可爱的熊猫公仔,质量好,面部表情友好,但价钱有点高,尺寸有点小。快递提前到货。
「关键信息提取」
在上一个章节中,虽然通过添加关键角度侧重的Prompt,使得文本摘要更侧重于某一特定方面,但是可以发现,结果中也会保留一些其他信息,如价格与质量角度的概括中仍保留了“快递提前到货”的信息。有时这些信息是有帮助的,但如果我们只想要提取某一角度的信息,并过滤掉其他所有信息,则可以要求LLM进行“文本提取(Extract)”而非“文本概括(Summarize)”。
prompt = f"""
你的任务是从电子商务网站上的产品评论中提取相关信息。
请从以下三个反引号之间的评论文本中提取产品运输相关的信息,最多30个词汇。
评论: ```{prod_review_zh}```
"""
response = get_completion(prompt)
print(response)
输出结果如下:
快递比预期提前了一天到货。
「批量进行文本摘要」
在实际的工作流中,我们往往有许许多多的评论文本,以下展示了一个基于for循环调用“文本摘要”工具并依次打印的示例。当然,在实际生产中,对于上百万甚至上千万的评论文本,使用for循环也是不现实的,可能需要考虑整合评论、分布式等方法提升运算效率。
review_1 = prod_review
# review for a standing lamp
review_2 = """
Needed a nice lamp for my bedroom, and this one \
had additional storage and not too high of a price \
point. Got it fast - arrived in 2 days. The string \
to the lamp broke during the transit and the company \
happily sent over a new one. Came within a few days \
as well. It was easy to put together. Then I had a \
missing part, so I contacted their support and they \
very quickly got me the missing piece! Seems to me \
to be a great company that cares about their customers \
and products.
"""
# review for an electric toothbrush
review_3 = """
My dental hygienist recommended an electric toothbrush, \
which is why I got this. The battery life seems to be \
pretty impressive so far. After initial charging and \
leaving the charger plugged in for the first week to \
condition the battery, I've unplugged the charger and \
been using it for twice daily brushing for the last \
3 weeks all on the same charge. But the toothbrush head \
is too small. I’ve seen baby toothbrushes bigger than \
this one. I wish the head was bigger with different \
length bristles to get between teeth better because \
this one doesn’t. Overall if you can get this one \
around the $50 mark, it's a good deal. The manufactuer's \
replacements heads are pretty expensive, but you can \
get generic ones that're more reasonably priced. This \
toothbrush makes me feel like I've been to the dentist \
every day. My teeth feel sparkly clean!
"""
# review for a blender
review_4 = """
So, they still had the 17 piece system on seasonal \
sale for around $49 in the month of November, about \
half off, but for some reason (call it price gouging) \
around the second week of December the prices all went \
up to about anywhere from between $70-$89 for the same \
system. And the 11 piece system went up around $10 or \
so in price also from the earlier sale price of $29. \
So it looks okay, but if you look at the base, the part \
where the b
prompt 如下:
import time
for i in range(len(reviews)):
prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site.
Summarize the review below, delimited by triple \
backticks in at most 20 words.
Review: ```{reviews[i]}```
"""
response = get_completion(prompt)
print(i, response, "\n")
time.sleep(60) # 限流
输出结果如下:
0 Soft and cute panda plush toy loved by daughter, but a bit small for the price. Arrived early.
1 Affordable lamp with storage, fast shipping, and excellent customer service. Easy to assemble and missing parts were quickly replaced.
2 Good battery life, small toothbrush head, but effective cleaning. Good deal if bought around $50.
3 The product was on sale for$49in November, but the price increased to $70-$89in 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.