提示工程(3):总结(summarizing)能力

总结(Summarizing)能力

总结能力是我最喜欢的模型能力。很多的文章是基本上没有时间去阅读的,利用模型的总结能力,可以快速的了解文章的大概。
接下来,我们用代码的方式来试验模型总结的能力。

import openai
openai.api_key="your openai key"
def get_completion (prompt, model="gpt-3.5-turbo"): 
    messages = [{"role": "user", "content": prompt}] 
    response = openai.ChatCompletion.create( 
        model=model, 
        messages=messages, 
        temperature=0
    ) 
    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词的总结。如下:

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 a child, but considered small for the price. Arrived early.

上面关于产品评论的30 word左右的总结。

如果想把这个评论总结给到发货部分(shipping department),我们可以改一个提示(short summary of a product to the shipping department, 并且,focusing on any aspectes that mention shipping and delivery)。

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

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

Review: '''{prod_review}'''

"""
response = get_completion(prompt)
print(response)
The customer received the panda plush toy a day earlier than expected, allowing them to play with it before giving it as a gift.

我们看上述的总结,“发货时间早于期望时间”,十分简明的

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

Summarize the review below, delimited by triple backticks, in at most 30 words, and focusing on any aspects that metion pricing and preceived value.

Review: '''{prod_review}'''

"""
response = get_completion(prompt)
print(response)

“too small fo the price paid”.

上面是我们利用总结(summarising)的能力。

现在我们来讲讲提出/摘取(extract)的能力。
我们只是要提出或摘取客户评价中我们关心的语句,应该怎么做呢?请看下面的例子(extract the info. relevant to shipping and delivery…)。

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

Summarize the review below, delimited by triple backticks, extract the information relevant to shipping and delivery of product. Limit to 30 words.

Review: '''{prod_review}'''

"""
response = get_completion(prompt)
print(response)
The product arrived a day earlier than expected.

假如我们有很多评论,希望做一个快速的总结和浏览,以了解其大概。我们可以见下面的示例。

下面是三个不同产品的评论。

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 manufacturers’ \
replacements heads are pretty expensive, but you can \
get generic ones those are 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 around49 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 of29. \
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. PYI: 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]

我们快速生成每个评估的总结,每段总结在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 tripe backticks in at most 20 words.

    Review: '''{reviews[i]}'''

    """

    response = get_completion(prompt)
    print (i, response, "\n")
0 "Panda plush toy loved by daughter, soft and cute, but small for the price." 

1 ```Nice lamp with storage, fast delivery, excellent customer service. Great company that cares about customers and products.``` 

2 The reviewer recommends the electric toothbrush for its impressive battery life, but criticizes the small toothbrush head. 

3 The reviewer mentions a price increase, a slight flaw in the base, and a motor issue after a year. 

上面讲解的就是关于模型总结(summarizing)的能力。下次我们讲推断。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注