Prompt Engineering | Text summary prompt

Text summary is to extract key information from a large paragraph of text and make a summary. LLM has demonstrated a strong level of text summarization.

Article directory

  • 1. Example of a single text summary prompt
    • 1.1. Limit the output text length
    • 1.2. Focus on key angles
    • 1.3. Key information extraction
  • 2. Multiple text summary prompt examples

1. Example of a single text summary prompt

The following takes e-commerce product reviews as an example to explain. For e-commerce platforms, there are often massive commodity reviews on the website, and these reviews reflect the thoughts of all customers. If we have a tool to summarize these massive and lengthy reviews, we can quickly browse more reviews, gain insight into customer preferences, and guide platforms and merchants to provide better services.

1.1, limit the output text length

Summary product reviews and limit output text length to 30 words:

prod_review_zh = """
This panda doll is a birthday present for my daughter. She likes it very much and takes it everywhere.
The figure is soft and super cute with a kind facial expression. But compared to the price,
It's a bit small and I feel like you can get a bigger one for the same price elsewhere.
The courier arrived a day earlier than expected, so I played with it myself before sending it to my daughter.
"""

prompt = f"""
Your task is to generate a short summary of product reviews from an e-commerce website.
Please summarize the comment text between three backticks, up to 30 words.
Review: ```{<!-- -->prod_review_en}```
"""

output:
The cute soft panda doll, my daughter likes it, has a kind facial expression, but the price is a bit expensive, and the express delivery arrived one day earlier.

1.2, focus on key angles

For different businesses, our emphasis on text will be different. For example, for product review texts, logistics will pay more attention to the timeliness of transportation, merchants will pay more attention to price and product quality, and platforms will care more about the overall service experience.

Focus on shipping:

prompt = f"""
Your task is to generate a short summary of product reviews from an e-commerce website.
Please summarize the review text between three backticks, up to 30 words, and focus on product shipping.
Review: ```{<!-- -->prod_review_en}```
"""

output:
The express delivery arrived ahead of time, the panda doll is soft and cute, but a bit small, and the price is not very good.

Focus on price vs. quality:

prompt = f"""
Your task is to generate a short summary of product reviews from an e-commerce website.
Please summarize the review text between three backticks, up to 30 words, and focus on product price and quality.
Review: ```{<!-- -->prod_review_en}```
"""

output:
Adorable soft panda doll with friendly facial expressions, but the price is a bit high and the size is small. The courier arrived one day early.

1.3, key information extraction

Although we make the text summary more focused on a specific aspect by adding the Prompt that focuses on the key angle, it can be found that some other information will be retained in the result, such as the summary of price and quality. Early arrival" information. Sometimes this information is helpful, but if we only want to extract information from a certain angle, and filter out all other information, we can ask LLM to do "Extract" instead of "Summarize".

Key information extraction prompt:

prompt = f"""
Your task is to extract relevant information from product reviews on an e-commerce website.
Please extract product shipping-related information from the review text between the three backticks below, up to 30 words.
Review: ```{<!-- -->prod_review_en}```
"""

output:
The courier arrived a day earlier than expected.

2. Multiple text summary prompt examples

In the actual workflow, we often have many comment texts. The following shows an example of calling the "Text Summary" tool based on a for loop and printing it in sequence. Of course, in actual production, it is unrealistic to use a for loop for millions or even tens of millions of comment texts. You may need to consider methods such as [Integrated Comments] and [Distributed] to improve computing efficiency.

Multiple text examples:

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 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 manufacturer'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!
"""



reviews = [review_1, review_2, review_3, review_4]

prompt:

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 = chatgpt(prompt)
    print(i, response, "\
")


output:
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.

?
?
?
?
?
?
Reference link:
[1] OpenAI
[2] Teacher Wu Enda’s: DeepLearning.AI
[3] DataWhale
[4] https://learn.deeplearning.ai/