ChatGPT AI - Product Review Summary (Difficulty 4)
-
Introduction
ChatGPT can read, write and understand natural languages, so it can be used to build very useful text-processing tools.
One example application is text summarization. Nowadays we are overwhelmed by data, such as websites, books, and blog posts. It will save people a lot of time if ChatGPt can generate a summary for people to read.
In this tutorial, you will use ChatGPT to summarize product reviews. For example, a popular product on Amazon may have thousands of reviews, and often in different languages. No one has the time to go through all the reviews. Our solution is to ask ChatGPT to read all the reviews and generate a summary for us.
When this tool runs, ChatGPT will keep updating its summary as it reads through more user reviews:
Step 1 - Create a Starting Project
We will use this project as the starting point, so please remix it first:
https://play.creaticode.com/projects/ca4bbe666a1934bab795661b
This project contains a table named “reviews”, which is 100 reviews for a toy product, including star rating, review subject and review details.
There are many ways to collect such review data, but they are out of the scope of this tutorial, since we want to focus on how to use ChatGPT to process these reviews.
Step 2 - The Core Idea
Before we start coding, let’s discuss what’s the core problem we need to solve. We already know ChatGPT can summarize any text we feed it. But the problem is there are too many reviews to feed all of them to ChatGPT at once. Imagine a product with 1000 reviews, and on average 20 words in each review, then that is a total of 20000 words. But the ChatGPT API we are using can only handle up to 4000 tokens, which is about 3000 words.
So the key question is how to make ChatGPT read all of the reviews, and summarize them into one summary.
Imagine we need to add 1000 numbers to get the sum, but each time we can only add 2 numbers. In this case, we would add the first 2 numbers, then keep adding the next number one by one.
We can apply the same idea to summarizing the reviews.
- We first get a summary of the first few reviews;
- We give ChatGPT some new reviews, and also give it the summary from #1, then we ask ChatGPT to update this summary based on the new information.
- We continue this process to repeatedly update the summary based on the new information.
We can call this solution “incremental summarization”. Now let’s implement it.
Step 3 - Print the First Review
To start simply, let’s try to read the first review in the “reviews” table, and print it out. We will ignore the “rating stars”, and focus on the “subject” and “details” columns. Here are the blocks we can use to read out the subject and details into 2 variables, and then join them into the “reviews” variable, and print it out in the console panel.
Step 4 - Print the First 20 Reviews
Next, we can use a for-loop to repeatedly read 20 rows of reviews one by one. We need to set the “reviews” variable to empty initially, then repeatedly append each review to it:
Note that we are adding a “\n” at the end of each review, so that the next review will start in a new line. Here is what we get in the console panel:
As you can see, with only 20 reviews, it already starts to feel difficult to read through all of them one by one ourselves.
Step 5 - Get Reviews for Any Rows
Since we will need to repeatedly read out more reviews from the table, we should wrap our code into a new block “get reviews”. It should take the first and last row numbers, and set the variable “reviews” with the content of those rows.
Step 6 - Get the Initial Summary from ChatGPT
Now we have the reviews, we can ask ChatGPT to give us the first summary of them:
Note that we are just using “waiting” mode, for now, to keep the code simple. Here is an example summary we get:
Step 7 - Improve the Summary Format
Before we process more reviews, it is a good time to improve the format of the ChatGPT output. The long paragraph is still hard to read. It is much better if ChatGPT can break it down into pros and cons, and give a few bullet points for each. We can easily achieve that by slightly modifying our prompt:
Now you should get something like this:
Step 8 - Avoid Duplicates
We can immediately see another problem: some of the bullet points are duplicates. For example, “Fragile and not made of sturdy plastic” is similar to “Not made as sturdy as old transformers”.
We can try to ask ChatGPT to improve the answer like this:
Summarize these product reviews as bullet points with pros and cons. Skip similar points.
Unfortunately, the output is still not very concise:
It appears that ChatGPT is not well-trained in removing similar points. What can we do?
Another method is to simply limit the number of bullet points, which will force ChatGPT to select the most common points:
Summarize these product reviews as bullet points with the top 3 pros and cons.
This time, we get a good summary with not many duplicates:
Step 9 - Get the Next 20 Reviews
Now let’s pull out the reviews from rows 21 to 40, and print them out to take a look:
They look like this in the console panel:
Step 10 - Update the Summary
Now we have both the “summary” from previous reviews and the “reviews” variable for the new reviews, we just need to ask ChatGPT to combine them into an updated summary.
The basic structure of our prompt would be like this:
- Here is a summary: the summary
- Here are some new reviews: the reviews
- combine them
Therefore, we will need to compose this long prompt using a few variables:
Part 1:
Here is a summary of a product from some customer reviews:
Part 2:
Here are some new customer reviews:
Part 3:
Now please update the summary above based on the new customer reviews above. Use the same format of bullet points of the top 3 pros and cons.
Here is the code to combine all the variables into one prompt, and print out the updated summary:Here is the updated summary we get:
Step 11 - Repeat the Process for All Rows
Now we just need to repeatedly grab more reviews and update the summary using them. We can use a for-loop that changes a variable “start row” to take the value of 21, 41, 61 and 81. For each “start row”, we get the next 20 rows of reviews.
Step 12 - Display the Summary in a Textbox
For our last step, instead of printing out the summary, let’s display them on the stage using a textbox. To do that, we first need to add the textbox add the beginning of the program, then set its value to the variable “summary” repeatedly:
Here is what we will get when we run the program:
Creative Ideas
Now it’s time for you to try to extend this project to practice what you have learned. Here are some ideas for your inspiration:
-
Limit the Review Type: Different users may be interested in reviews about different aspects of the product. Try to add a dropdown to allow the user to choose a category, such as “suitability”, “ease of use”, “product quality”, etc. The user will first choose a category, then click a button to get the summary of reviews in that category.
-
Other Contents: The technique used in this project can be applied to summarize other content, such as answers from school-wide surveys on AI usage. For another example, this method can be used to summarize a long article or even a book.
-