在这堂课中,你将会以特定主题为重点来总结文本。
在今天的世界里,文字太多了,我们几乎没有足够的时间去读所有我们想读的东西。所以,我看到的最令人兴奋的大型语言模型的应用之一是用它来总结文本,这是我看到多个团队在多个软件应用中构建的功能。你可以在chatGPT网页界面中做这个。我总是这样做,以便总结文章,这样我就可以阅读比以前更多的文章的内容,如果你想更系统地做这个,你将在这节课中看到如何做。
所以,让我们深入代码,看看你如何利用它来总结文本。让我们以你之前看到的相同的启动代码开始,导入OpenAI,加载API密钥,这是获取完成的辅助函数。我将使用的运行示例是总结这个产品评价的任务。为女儿的生日买了这个熊猫毛绒玩具,她很喜欢,带着它到处走,等等等等。如果你正在建一个电子商务网站,有大量的评论,有一个工具来总结冗长的评论可以让你更快地查看更多的评论,以更好地了解所有客户的想法。所以,这是一个生成摘要的提示。你的任务是从电子商务网站生成一个产品评价的简短摘要,总结下面的评论,等等,最多30个字。所以,这个很软,很可爱,女儿很喜欢的熊猫毛绒玩具,对于价格来说有点小,提前到达。不错,这是一个很好的总结。正如你在前一个视频中看到的,你也可以玩一玩控制字符的数量或句子的数量来影响这个总结的长度。
有时候,当创建一个总结的时候,如果你有一个非常具体的目的,比如,如果你想给运输部门提供反馈,你也可以修改这个提示,以反映这一点,这样他们就可以生成一个更适合你的商业中的一个特定群体的总结。所以,比如,如果我加上给运输部门提供反馈,我就说我改变这个开始关注任何提到产品运输和交付的方面。如果我运行这个,然后,你得到一个总结,但是它不是以软和可爱的熊猫毛绒玩具开始,而是关注于它比预期提前一天到达。然后它仍然有,你知道,其他的细节
。或者作为另一个例子,如果我们不是试图给运输部门提供反馈,而是我们想给定价部门提供反馈。所以定价部门负责确定产品的价格,我将告诉它关注任何与价格和感知价值相关的方面。然后,这就生成了一个不同的总结,它说,也许价格对于大小来说可能太高了。现在我为运输部门或定价部门生成的总结,它更多地关注了与那些特定部门相关的信息。实际上,随时可以暂停视频,也许让它生成一些你认为对电子商务网站可能有趣的产品部门负责产品的客户体验,或者其他什么的信息。
但是在这些总结中,即使它生成了与运输相关的信息,它还有一些其他的信息,你可以决定这可能有或可能没帮助。所以,根据你想要如何总结它,你也可以要求它提取信息,而不是总结它。所以这里有一个提示说你的任务是提取相关的信息给运输部门提供反馈。现在它只说,产品比预期提前一天到达,而没有所有其他的信息,这在一般的总结中也有帮助,但对于运输部门来说,如果它只想知道运输发生了什么,那么就不太具体。
最后,让我和你分享一个具体的例子,如何在工作流中使用这个来帮助总结多个评论,使它们更容易阅读。所以,这里有一些评论。这有点长,但你知道,这是一个站立灯的第二个评论,卧室需要一个灯。这是一个电动牙刷的第三个评论。我的牙科卫生师推荐的一种电动牙刷的长评。这是一个搅拌机的评论,他们说,所以说17p系统在季节销售,等等等等。这实际上是很多的文字。
如果你想,随时可以暂停视频,读完所有这些文字。但是如果你想知道这些评论者写了什么,而不用停下来详细阅读所有这些呢?所以,我将把第一条评论设置为我们之前的产品评论。然后我将把所有这些评论放入一个列表。现在,如果我对评论进行循环,这是我的提示。这里我要求它在最多20个字内总结。然后让它得到回应并打印出来。让我们运行那个。它打印出的第一条评论是那个熊猫玩具的评论,灯的评论总结,电动牙刷的评论总结,然后是搅拌机。所以,如果你有一个网站,有几百条评论,你可以想象如何使用这个来建立一个仪表板,取得大量的评论,生成简短的总结,这样你或者其他人可以更快地浏览评论。然后,如果他们愿意,也许点击查看原来更长的评论。这可以帮助你有效地了解所有客户的想法。对吧?所以,对于总结就是这样。我希望你能想象,如果你有任何含有大量文本的应用,你可以如何使用这些提示来总结它们,帮助人们快速了解文本中的许多片段的内容,也许可以选择性地更深入地研究,如果他们愿意的话。
在下一个视频中,我们将看一下大型语言模型的另一个能力,那就是使用文本进行推断。例如,如果你有产品评论,你想很快地了解哪些产品评论是积极的或消极的情绪呢?让我们在下一个视频中看看如何做。
Setup准备
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')
In [ ]:
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"]
In [ ]:
Text to summarize 文本总结
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.
In [ ]:
Summarize 长度限制
In [ ]:
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)
Summarize 关注运输和交付
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)
In [ ]:
Summarize 关注价格
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)
Try "extract" instead of "summarize"(尝试提取而非总结)
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)
Summarize multiple product reviews(对多个产品评论总结)
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 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")