OpenAI Prompt Engineering-4. 文本总结

OpenAI的Prompt Engineering 课程

4. 文本总结

当今世界,有太多的文字信息,以至于我们压根没有精力去了解这些文字的内容。因此吴恩达老师认为,大模型最令人兴奋的应用之一就是对文本进行总结。

在本节课中,我们学习如何以编程的方式来实现这个任务。

基础代码还是直接几节课的内容,这里就不再赘述了,可以直接翻阅第一节课的内容查看是如何调用DeepSeek接口并传入提示的。

课程中以“总结此产品评价”的任务作为运行示例,假设有一个电子商务网站,上面有大量的用户评论,可以使用大模型对大量的评论进行精简总结,从而让你可以了解更多用户真实的想法,改进产品。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.
"""

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}```
"""

DeepSeek输出:

1
**Summary:** The panda plush is soft, cute, and loved by the daughter, but a bit small for the price. Arrived early, with friendly design. Might consider bigger options. (30 words)

可以看到大模型按照提示总结的很不错,在 30 个字左右,当然也可以按照上节课程演示的那样,通过限制字符数和句子数来控制输出的长度。也可以让大模型针对某个部门总结信息,比如主要是想给运输部门提供反馈,就可以在提示词中突出这一点:

1
2
3
4
5
6
7
8
9
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}```
"""

这样我们就得到了一个新的输出:

1
"Panda plush arrived a day early, which was great. Daughter loves it, but size is smaller than expected for the price. No shipping issues noted." (30 words)

我们也可以修改提示,突出给定价部门或者用户体验部门一些反馈,这里我们就不一一演示了。

有的时候评论里可能有一些你并不需要关注的信息,也可以修改提示词根据你想要总结的方式,要求它提取信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
review_1 = """
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.
"""

# review for a standing 1amp
review_2 ="""
Needed a nice lamp for my bedroom, and this one\
half additional storage and not too high of a price \
porat. 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 recomended 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 segvice but the warranty expired \
already, so I had tof buy another one.FYI:rhe overal1\
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.
"""

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}```
"""

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(open_api_key, prompt, model)
print("OpenAi Response:\n" + str(i) + response)

这个案例中选取了 4 段评论,我们构建一个 for 循环,依次对评论进行精简总结输出:

1
2
3
4
5
6
7
0, Soft, cute panda plush loved by daughter; small for price but arrived early. (20 words)

1, Affordable lamp with storage, fast delivery. Excellent customer service resolved broken part and missing piece promptly. Highly recommended. (20 words)

2, The electric toothbrush has excellent battery life but a small brush head. It provides a thorough clean and is a good deal around $50, though replacement heads are pricey.

3, The 17-piece system was half off in November but prices rose in December. Quality has declined, and the motor failed after a year, requiring a replacement. (20 words)

这只是一个示例,你完全可以按照你当前的使用场景去改造,比如点击总结跳转到原评论查看详情等等。