Wzn的头像

总结

前言
前言
准备工作
准备工作
文本总结
4
文本总结
限定总结长度
限定总结长度
说明具体目的
说明具体目的
剔除无关信息
剔除无关信息
批量总结
批量总结
结语
结语
没时间阅读
没时间阅读
使用自动总结
使用自动总结
过渡如何实现
过渡如何实现
基础设置
基础设置
示例评论
示例评论
总结30词
总结30词
限制字符数
限制字符数
说明具体目的
说明具体目的
欲知物流情况
欲知物流情况
欲知价格情况
欲知价格情况
尝试练习
尝试练习
总结信息冗余
总结信息冗余
提取信息精简
提取信息精简
提取到达时间
提取到达时间
总结多个评论
总结多个评论
批量总结
批量总结
本集小结
本集小结
下集预告
下集预告

总结

2023-06-01
222 人已看
Wzn的头像
Wzn
粉丝:0
主题:8
描述:6
例子:5
其他:7
字数:9456
Wzn的头像
Wzn
粉丝:0

前言

问题 没时间阅读

当今世界有许多文本,几乎没有人有足够的时间阅读它们我们希望我们有时间做这些事情。

应对 使用自动总结

因此,我所看到的大型语言模型最令人兴奋的应用之一就是使用它总结文本。这是我看到多个团队在多个软件应用中建立的东西。

过渡 过渡如何实现

你可以在ChatGPT的Web界面中实现这一点。我经常这样做,以便阅读更多文章的内容,而不像以前只能读很少的文章。如果你想用编程的方式做这件事,你可以在这个课程中学习到如何做。让我们深入代码,看看你如何使用它来总结文本。

准备工作

基础设置

让我们从相同的起始代码开始,导入OpenAI,加载API密钥,

import openai
import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file

openai.api_key  = os.getenv('OPENAI_API_KEY')

这里是get_completion辅助函数。

def get_completion(prompt, model="gpt-3.5-turbo"): # Andrew mentioned that the prompt/ completion paradigm is preferable for this class
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, # this is the degree of randomness of the model's output
    )
    return response.choices[0].message["content"]
示例 示例评论

本课程将以概括这篇产品评论的任务作为运行示例。

我为女儿的生日买了这只熊猫玩具,她很喜欢,去哪都带着它,等等等等。

prod_review = """
Got this panda plush toy for my daughter's birthday, \
who loves it and takes it everywhere. It's soft and \ 
super cute, and its face has a friendly look. It's \ 
a bit small for what I paid though. I think there \ 
might be other options that are bigger for the \ 
same price. It arrived a day earlier than expected, \ 
so I got to play with it myself before I gave it \ 
to her.
"""

如果你正在建立一个电子商务网站,并且有大量的评论,那么使用工具来概括冗长的评论可以让你快速地查看更多的评论,以更好地了解你的所有客户在想什么。

文本总结

限定总结长度

总结30词

这里是一个生成总结的提示。你的任务是从电子商务网站生成产品的简要总结,总结下面的评论,等等,至多用30个字。

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 30 words. 

Review: ```{prod_review}```
"""

response = get_completion(prompt)
print(response)

这个软萌可爱的熊猫毛绒玩具受到了女儿的喜爱,但对于这个价格来说有点小。提前到货。不错。

Soft and cute panda plush toy loved by daughter, but a bit small for the price. Arrived early.

这是相当好的总结。

限制字符数

正如你在之前的视频中看到的,你还可以尝试控制字符数或句子数来改变总结的长度。

说明具体目的

说明具体目的

有时候,当你创建一个总结时,如果你有一个非常具体的目的例如,如果你想要向物流部门提供反馈,你可以修改提示,以便生成与特定目的相关的总结,满足特定群体的需求。

欲知物流情况 说明具体目的

例如,如果我要向物流部门提供反馈,我可以将提示修改为关注任何涉及产品物流和交付方面的内容。

prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
Shipping deparmtment. 

Summarize the review below, delimited by triple 
backticks, in at most 30 words, and focusing on any aspects \
that mention shipping and delivery of the product. 

Review: ```{prod_review}```
"""

response = get_completion(prompt)
print(response)

如果我运行这个代码,会得到一个总结,但它和上一个总结不同,现在它关注的事情是提前一天到货,还有其他的一些细节。

The panda plush toy arrived a day earlier than expected, but the customer felt it was a bit small for the price paid.
欲知价格情况 说明具体目的

或者作为另一个例子,如果我们不是向物流部门提供反馈,而是向定价部门提供反馈定价部门负责确定产品的价格我将告诉它关注任何与价格和感知价值相关的方面。

prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
pricing deparmtment, responsible for determining the \
price of the product.  

Summarize the review below, delimited by triple 
backticks, in at most 30 words, and focusing on any aspects \
that are relevant to the price and perceived value. 

Review: ```{prod_review}```
"""

response = get_completion(prompt)
print(response)

这将生成一个不同的总结,它说,也许对于这个尺寸来说,价格可能太高了。

The panda plush toy is soft, cute, and loved by the recipient, but the price may be too high for its size.
建议 尝试练习

在我为物流部门或定价部门生成的概括中,它更关注那些与特定部门相关的信息。

事实上,现在可以随时暂停视频,让它生成有关负责产品客户体验的部门或其他你感兴趣的内容。

剔除无关信息

总结信息冗余

但在这些总结中,尽管它生成了物流的信息,但它也包含其他的信息,你可以决定这些信息是否有用。

提取信息精简

因此,根据你想要总结的内容,你也可以要求它提取信息而不是概括信息。

提取到达时间 提取信息精简

所以这里有一个提示,表示你的任务是提取相关信息,以向物流部门提供反馈。

prompt = f"""
Your task is to extract relevant information from \ 
a product review from an ecommerce site to give \
feedback to the Shipping department. 

From the review below, delimited by triple quotes \
extract the information relevant to shipping and \ 
delivery. Limit to 30 words. 

Review: ```{prod_review}```
"""

response = get_completion(prompt)
print(response)

现在它只说了,产品提前了一天到货,没有其他信息,在宽泛的总结中是有益的,但对于只想知道物流发生了什么的物流部门来说,不够具体。

The product arrived a day earlier than expected.

批量总结

总结多个评论 批量总结

最后,让我与你分享一个具体的例子,说明如何在工作流程中使用这种方法,帮助总结多个评论,使它们更容易阅读。这里有一些评论。第二篇是关于一个立式台灯的评论,比较长,需要在卧室里使用的灯第三篇是关于一款电动牙刷的评论。我的牙医推荐了这款牙刷。关于电动牙刷的评论有点长。这是一篇关于搅拌机的评论,当时说是季节性促销的17件套装等等,等等。这实际上是非常多的文本。

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 blade locks into place doesn’t look as good \
as in previous editions from a few years ago, but I \
plan to be very gentle with it (example, I crush \
very hard items like beans, ice, rice, etc. in the \ 
blender first then pulverize them in the serving size \
I want in the blender then switch to the whipping \
blade for a finer flour, and use the cross cutting blade \
first when making smoothies, then use the flat blade \
if I need them finer/less pulpy). Special tip when making \
smoothies, finely cut and freeze the fruits and \
vegetables (if using spinach-lightly stew soften the \ 
spinach then freeze until ready for use-and if making \
sorbet, use a small to medium sized food processor) \ 
that you plan to use that way you can avoid adding so \
much ice if at all-when making your smoothie. \
After about a year, the motor was making a funny noise. \
I called customer service but the warranty expired \
already, so I had to buy another one. FYI: The overall \
quality has gone done in these types of products, so \
they are kind of counting on brand recognition and \
consumer loyalty to maintain sales. Got it in about \
two days.
"""

reviews = [review_1, review_2, review_3, review_4]

如果您想, 请随意停止视频并阅读所有这些文本.但是如果你想知道这些评论者写了什么,而不必停下来详细阅读所有内容呢?那么我将把第一篇评论设置为我们上面展示的产品评论,并将所有这些评论放入一个列表中。现在,如果我在这些评论上实施一个for循环。这是我的提示,我已经要求它在最多20个单词内进行概括。然后让它获取响应并打印出来。让我们运行一下。

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")

它打印出来的第一篇评论是熊猫玩具的评论、台灯的概述评论、电动牙刷的概述评论,以及搅拌机的评论。

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 \$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. 
批量总结

因此,如果你有一个有数百条评论的网站,你可以想象如何使用这个方法来构建一个仪表板,处理大量的评论,生成简短的摘要以便你或其他人能更快地浏览评论。然后如果他们愿意, 可以点击查看原始的长评论。这可以帮助您高效地了解顾客的想法和需求。

结语

小结 本集小结

好了,这就是总结的全部内容。我希望你能想象出如果你有包含大量文本的应用程序,如何使用这样的提示来对它们进行总结,以帮助人们快速了解文本中的内容,以及如果他们愿意,可以选择进一步深入了解。

预告 下集预告

在下一个视频中,我们将看一下大型语言模型的另一个功能,即使用文本进行推理。例如,如果你有产品评论,想要快速了解哪些评论具有积极或消极的情感,该怎么办?在下一个视频中,我们将了解如何做到这一点。

讨论
随记