当前位置: 首页 > ai >正文

(done) 吴恩达版提示词工程 4. 摘要 (生成摘要,指定信息摘要,提取指定信息,多条评论摘要)

4. 摘要任务(Summarizing)

今天的世界有那么多的文字信息,几乎没有人有足够的时间来阅读这些内容。因此,大型语言模型最令人兴奋的应用之一,就是用它来对文本内容进行总结摘要。这是多个开发团队在不同软件应用中所构建的功能。

你可以在ChatGPT Web 界面中完成这个操作。我经常用这种方式来总结文章,这样我就可以比以前的文章内容。你将在本课程中,学习如何以编程的方式来实现文本摘要任务。让我们深入分析代码,看看如何使用它来总结文本。

让我们从之前的初始化代码开始,先导入OpenAI,再加载 API Key,然后是 get_completion 辅助函数。

import openai
import os
from openai import OpenAI# 1. 根据环境变量获取 openai key
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())openai.api_key = os.getenv('OPENAI_API_KEY') client = OpenAI()# 2. 定义 get_completion 方法
def get_completion(instructions, prompt, model="gpt-3.5-turbo"):response = client.responses.create(model=model,instructions=instructions,input=prompt,temperature=0, # this is the degree of randomness of the model's output)return response.output_text

4.1 生成评论的摘要

下面我将以“总结产品评论”任务作为示例。

“我买了一只熊猫毛绒玩具作为女儿的生日礼物,她非常喜欢它,无论去哪里都要带上它,等等。”

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个单词。

instructions=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. """prompt = f"""
Review: ```{prod_review}```
"""response = get_completion(instructions, prompt)
print(response) 

模型生成的预期评论摘要如下。

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

这个柔软可爱的熊猫毛绒玩具深受女儿的喜爱,但价格有点贵,提前到货。

不错,这是一个很好的总结。正如你在上一个视频中看到的,你还可以玩一些东西,比如要求字符数或句子数量,以控制这个摘要的长度。

4.2 指定信息的摘要

如果你对摘要有一个非常具体的目的,例如如果你想向运输部门提供反馈,你也可以修改提示来突出这一点,就可以使生成的摘要更适用于业务中某个特定群体的需求。

例如,如果我要向运输部门提供反馈,那么我的关注点就集中在商品的运输和交付方面,因此对提示进行修改如下。

instructions=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. """prompt = f"""
Review: ```{prod_review}```
"""response = get_completion(instructions, 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.

这次的摘要不是从“柔软可爱的熊猫毛绒玩具""开始,而是强调比预期提前了一天送达,还有其他细节。

再举一个例子,如果我们不想向运输部门,而是想向定价部门提供反馈。定价部门负责确定产品的价格,所以我要告诉它关注与价格和价值感知相关的内容。

instructions=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. """prompt = f"""
Review: ```{prod_review}```
"""response = get_completion(instructions, 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.

现在,在我为运输部门或定价部门生成的摘要中,它更多地关注与这些特定部门相关的信息。你现在可以暂停视频,可以修改提示来让它为负责产品客户体验的部门生成信息,或者为你认为与电子商务网站有关的其它方面提供信息。

4.3 提取指定的信息

在这些总结中,除了生成了与运输相关的信息,也有一些其它的信息,你可以决定这些信息是否有帮助。根据你想要总结的方式,你也可以要求它只是提取信息而不是进行总结。

这里有一个提示,它说你的任务是提取相关信息并给运输部门反馈。

instructions=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. """prompt = f"""
Review: ```{prod_review}```
"""response = get_completion(instructions, prompt)
print(response) 

现在它只是说产品比预期早了一天到达,没有其它信息。其它信息在一般的摘要中也是有帮助的,但如果只想知道运输方面的内容,其它信息就不那么具体了。

The product arrived a day earlier than expected.

4.4 多条评论的摘要

最后,我与你分享一个具体的例子,说明如何在工作流程中使用它来帮助总结多篇评论,使其更容易阅读。

这里有几条评论。这有点长。第二条评论是关于卧室落地灯的评论。第三条评论是关于电动牙刷的,”我的牙科保健师推荐的“,第四条是关于搅拌机的评论,当时它说这是季节性销售的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] 

如果你愿意的话,你可以暂停视频并阅读所有这些文本。但如果你想知道这些评论者写了什么,却不想停下来详细阅读所有这些细节内容呢?那么我要把 review_1 设为我们在上面展示的那个产品评论, 然后把所有这些评论放到列表中。

然后,我对这些评论使用一个 for 循环。这是我的提示,我要求它最多使用20个单词来总结,然后让它获得响应并打印出来。

instructions=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. 
"""for i in range(len(reviews)):prompt = f"""Review: ```{reviews[i]}```"""response = get_completion(instructions, prompt)print(i, response, "\n") 

让我们运行这个程序。

0

Adorable panda plush loved by daughter, soft and cute. Small for price, arrived early. Consider larger options.

1

Affordable lamp with storage, fast delivery, excellent customer service for replacements and missing parts. Easy assembly.

2

Impressive battery life, small head, good value at $50. Generic replacement heads recommended for cost savings. Leaves teeth feeling clean.

3 Summary: Fluctuating prices, decreased quality, and motor issues after a year. Tips for usage included. Brand relies on reputation.

它打印出第一条评论是熊猫玩具的评论摘要、然后是台灯的评论摘要、牙刷的评论摘要,然后是搅拌器的评论摘要。

因此,如果你有一个网站,有成百上千的评论,你可以使用它来建立一个控制面板,为大量的评论生成简短的摘要,这样你或其他人可以更快地浏览这些评论。然后如果他们愿意,也可以点击进去看原始的长篇评论。这可以帮助你更高效地了解所有客户的想法。

4.5 小结

关于摘要任务就讲到这里。如果你有任何大量文本的应用,你可以使用这样的提示来进行总结,以帮助人们快速地了解文本中的内容、多条文本,并在必要时选择性地深入提取更多的特定信息。

在下一个视频中,我们将介绍大型语言模型的另一个能力:使用文本进行推理。例如,如果你有一些产品评论数据,你想快速了解哪些评论带有正面或负面的情绪,该怎么办?

http://www.xdnf.cn/news/2234.html

相关文章:

  • 什么是智能导诊知识库?
  • Pinia 详细解析:Vue3 的状态管理利器
  • 【油猴脚本 2】bilibili 视频合集标题搜索
  • 软件维护类型四大类型(IEEE 14764 标准)
  • Java基础 4.26
  • Dijkstra‘s Algorithm Implementation
  • Compose笔记(十九)--NestedScroll
  • Pygame核心概念解析:Surface、Clock与事件循环
  • 教育领域的AIGC革命:构建多模态智能教学系统
  • Dify + Mermaid 实现自然语言转图表
  • Rule.issuer(通过父路径配置loader处理器)
  • Windows怎样改变鼠标指针方案
  • 使用FME生成Delaunay三角形
  • 《淘宝API数据治理实践:采集字段标准化与数据质量监控体系》
  • 戴维斯双击选股公式如何编写?
  • Makefile---自动化构建和管理项目的文件
  • Java基础 — 循环
  • BS架构与CS架构的对比分析:了解两种架构的不同特点与应用
  • C语言函数调用与声明
  • HTML基础
  • QNX/LINUX/Android系统动态配置动态库.so文件日志打印级别的方法
  • 悟空统计平台在教育行业的落地:课程转化路径优化实践
  • Python 实现从 MP4 视频文件中平均提取指定数量的帧
  • vue3学习之防抖和节流
  • module.noParse(跳过指定文件的依赖解析)
  • Spring Boot安装指南
  • Qt 5.15 编译路径吐槽点
  • QML Date:日期处理示例
  • dijkstra
  • 个人电子白板(svg标签电子画板功能包含正方形、文本、橡皮 (颜色、尺寸、不透明度)、 撤销、取消撤销 等等功能,)