Creating Prompts to Summarise Text with Large Language Models

In this article we look at how to use Large Language Models such as ChatGPT to summarize text with a focus on specific topics
natural-language-processing
deep-learning
openai
prompt-engineering
Author

Pranath Fernando

Published

May 3, 2023

1 Introduction

Large language models such as ChatGPT can generate text responses based on a given prompt or input. Writing prompts allow users to guide the language model’s output by providing a specific context or topic for the response. This feature has many practical applications, such as generating creative writing prompts, assisting in content creation, and even aiding in customer service chatbots.

For example, a writing prompt such as “Write a short story about a time traveler who goes back to the medieval period” could lead the language model to generate a variety of unique and creative responses. Additionally, prompts can be used to generate more specific and relevant responses for tasks such as language translation or summarization. In these cases, the prompt would provide information about the desired output, such as the language to be translated or the key points to be included in the summary. Overall, prompts provide a way to harness the power of large language models for a wide range of practical applications.

However, creating effective prompts for large language models remains a significant challenge, as even prompts that seem similar can produce vastly different outputs.

In my previous article, we looked at how to develop prompts for large language models iteratively.

In this article, we will use prompts to summarize text with a focus on specific topics.

2 Setup

2.1 Load the API key and relevant Python libaries.

First we need to load certain python libs and connect the OpenAi api.

The OpenAi api library needs to be configured with an account’s secret key, which is available on the website.

You can either set it as the OPENAI_API_KEY environment variable before using the library: !export OPENAI_API_KEY='sk-...'

Or, set openai.api_key to its value:

import openai
openai.api_key = "sk-..."
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')

2.2 Helper function

We will use OpenAI’s gpt-3.5-turbo model and the chat completions endpoint.

This helper function will make it easier to use prompts and look at the generated outputs:

We’ll simply define this helper function to make it easier to use prompts and examine outputs that are generated. GetCompletion is a function that just accepts a prompt and returns the completion for that prompt.

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"]

3 Text Summarisation

I’ll use the task of describing this product review as the ongoing example. If you’re developing an e-commerce website and there are a lot of reviews, having a tool to summarise the lengthy reviews may allow you to swiftly scan through more reviews to gain a better understanding of what all of your consumers are thinking.

So, here is a prompt for creating a summary. The assignment is to create a succinct description of a product review from an e-commerce website, summarising the review below and so forth in no more than 30 words.

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.
"""

4 Summarize with a word/sentence/character limit

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)
Output

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

It makes a decent summary. As you saw in the last post, you can also experiment to change the length of this summary by changing the number of characters or phrases. Now, occasionally when creating a summary, if you have a very specific purpose in mind for the summary, for example, if you want to provide feedback to the shipping department, you can also modify the prompt to reflect that so that it can generate a summary that is more applicable to one specific group in your business.

5 Summarize with a focus on shipping and delivery

Let’s say I update this to start focusing on any parts that state if I add to give input to the shipping department. delivery of the merchandise and shipment. And if I run this, you again receive a summary, but this time it starts with the fact that the Soft and Cute Panda Plush Toy arrived a day earlier than anticipated.

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)
Output

The panda plush toy arrived a day earlier than expected, but the customer felt it was a bit small for the price paid.

You receive a summary, but it now emphasises the fact that it arrived a day sooner than anticipated rather than the Soft and Cute Panda Plush Toy as the first item.

6 Summarize with a focus on price and value

But let’s say we want to give feedback to the pricing department. So the pricing department is responsible for determining the price of the product. And I’m going to tell it to focus on any aspects that are relevant to the price and perceived value.

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)
Output

The panda plush toy is soft, cute, and loved by the recipient, but the price may be too high for its size.

Then a new summary is produced, suggesting that perhaps the price is too high given the item’s size.

Although these summaries included information pertinent to shipping, they also contained additional information that you could determine may or may not be useful. So you may ask it to extract information rather than summarise it, depending on how you want to summarise it.

7 Try “extract” instead of “summarize”

So, this prompt asks you to gather pertinent data and provide the shipping department with feedback. And now, it just states, “Product arrived the day earlier than expected,” leaving out the rest of the details, which was less detailed for the shipping department if all it wants to know is what occurred with the shipping but was still encouraging in the broad overview.

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)
Output

The product arrived a day earlier than expected.

8 Summarize multiple product reviews

Let’s take a look at an example of how this could be used in a workflow to assist condense numerous reviews into one, easier-to-read document.

Following are some reviews. This is a little lengthy, but you know, this is the second review for a needle light that is a standing lamp for the bedroom. The third review of an electric toothbrush is presented here. My dental hygienist advised me to try it. Quite a lengthy evaluation of an electric toothbrush. This is a review of a blender that was mentioned in the sentences “so, so that 17 piece system is on seasonal sale,” etc.

But what if you don’t want to sit and read all of this in detail and just want to know what these reviews said? Therefore, I’m going to set review 1 to be the product review that was previously posted. And I’ll create a list with all of these reviews on it. Furthermore, if I use a for loop through the reviews. Here is my prompt, and I’ve asked you to sum it up in no more than 20 words. Let’s then have it retrieve the response and print it.


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")
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.

3 The product was on sale for $49 in November, but the price increased to $70-$89 in December. The base doesn’t look as good as previous editions, but the reviewer plans to be gentle with it. A special tip for making smoothies is to freeze the fruits and vegetables beforehand. The motor made a funny noise after a year, and the warranty had expired. Overall quality has gone down.

The Pantatoi review was the first review that was printed, followed by summaries for the lamp, toothbrush, and blender. You can therefore see how you might use this to develop a dashboard to take a large number of reviews and make brief summaries of them so that you or someone else can scan the reviews much more rapidly if you have a website with hundreds of reviews.

9 Acknowledgements

I’d like to express my thanks to the wonderful ChatGPT Prompt Engineering for Developers Course by DeepLearning.ai and OpenAI - which i completed, and acknowledge the use of some images and other materials from the course in this article.

Subscribe