ChatGPT, accessed via the OpenAI
API, is a powerful tool for automating and enhancing text analysis tasks:
To use GPT via the API, you need to import the os
and openai
Python packages.
The code pattern to call the OpenAI API and get a chat response is as follows:
Note: do not run. This is just for demonstration purposes:
One of the typical uses of ChatGBT is summarizing texts.
This can be particularly useful in the context of long customer reviews.
This how we count the number of words:
profile_name | rating | rating_date | title | review_text | word_count | |
---|---|---|---|---|---|---|
0 | J.F. Carroll | 5.0 out of 5 stars | Reviewed in the United States on May 6, 2024 | 5.0 out of 5 stars\nIt's a PS5, everyone wants one, was there any doubt?\n | \nNobody could get their hands on this until just recently. It's a PS5, the craftmanship is awesome.Operates well, system works well with store, everything is easy, this console is a masterpiece and I keep it close. Haven't got into too many games yet. It looks sexy as heck, it looks dumb in ads but in real life it's fantastic looking. And you can buy sexy plates for it as well.Controller feels great. Considering the upgrade controller, but I haven't decided. The game choices are phenomenal. It's official: XBOX lost, buy a Playstation.Playstation all the way.\n | 95 |
1 | Brandon | 5.0 out of 5 stars | Reviewed in the United States on April 17, 2024 | 5.0 out of 5 stars\nGreat Product, Great Price, Great Games!\n | \nI was looking for a great deal. Luckily, I found this product on sale for $450! I love this new PlayStation 5! I've been waiting years to get my own and now I have it! I use it vertically instead of horizontally, so I had to buy a vertical stand for an extra $20. Overall, I would recommend you buy this!\n | 61 |
2 | Brock | 5.0 out of 5 stars | Reviewed in the United States on May 5, 2024 | 5.0 out of 5 stars\nAmazing Console\n | \nAfter using the playstation for over 4 months; I can say, this is one of the best consoles I’ve played on. I had an Xbox Series S and after playing on the PS5 there is a world of difference in graphics, FPS, and loading times. Also the spiderman game that comes with this model is spectacular.\n | 56 |
We might be interested in summarizing really long customer reviews,
ChatGPT can ve particularly useful for this task.
Let us say that in a first instance we want to summarize the review with the largest number of words
Review with the largest number of words
Greetings, fellow gamers! Gather ’round as I recount my leap into the realm of PlayStation with the illustrious PS5 Bundle. As someone who’s long been immersed in the world of Xbox, I couldn’t resist the allure of Sony’s latest offering, and boy, am I glad I made the jump.Let’s kick things off with a nod to the PS5’s design. This thing is a marvel to behold, sleek and futuristic, commanding attention in my entertainment setup like a true centerpiece. And with a whopping 1TB of SSD storage, I’ve got all the room I need for my ever-expanding library of games, without having to endure those dreaded load times.Now, onto the real meat of the matter – the performance. The PS5 doesn’t pull any punches, delivering jaw-dropping 4K visuals and buttery-smooth frame rates that breathe new life into every game I play. And let’s talk about that SSD – it’s like greased lightning, ensuring I spend less time waiting and more time doing what I love: gaming.But hold onto your controllers, because the real star of the show is the DualSense. This thing is a game-changer in every sense of the word, with its immersive haptic feedback and adaptive triggers that elevate every gaming experience to new heights. And with backward compatibility, I can revisit old favorites with a fresh perspective, like catching up with old friends over a round of drinks.Of course, no review would be complete without addressing a few bumps along the road. The limited storage space is a bit of a downer, especially in today’s world of massive game downloads. And let’s not forget the price tag – while the PS5 is worth every penny in my book, it’s definitely an investment.And then there’s the hunt for the elusive console itself. Scouring the internet for availability felt like navigating a labyrinth, but let me tell you, the payoff was worth it. Sure, the PS5’s size might be a tad imposing for some setups, but once you fire up that first game, you’ll forget all about it.In conclusion, the PS5 Bundle has exceeded my wildest expectations, converting this die-hard Xbox fan into a true believer. So if you’re on the fence, take the plunge – you won’t regret it. Happy gaming, folks! .
We first load the library and define our key:
This is how we define our function:
Python
# Import the necessary OpenAI client library
# Make sure you have installed the OpenAI Python package: `pip install openai`
def get_completion(prompt):
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=[{"role": "user", "content": prompt}],
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message.content
Here is an example:
"Why couldn't the bicycle stand up by itself?\n\nBecause it was two tired!"
Let us now imagine that we want to summarize the long review
Python
# Assuming longest_review is defined and contains text
prompt_template = """
Task: Summarize the e-commerce product review below.
Instructions:
1. Create a shortened version of the review in 50 words or fewer.
2. Use first person and immitate the reviewer's tone directly.
3. Do not add any opinions, explanations, conversational tone, or new information.
4. Simply condense the main points as written by the reviewer.
Review:
```{review_text_here}```
"""
This is how we create a function o help us
Here is our function in action:
Python
I switched from Xbox to the PS5 Bundle and it's been amazing. The design is sleek, the performance is top-notch with 4K visuals and fast load times, and the DualSense controller is a game-changer. Limited storage and price are downsides, but overall, it's worth the investment. Happy gaming!
Review with the largest number of words
Greetings, fellow gamers! Gather ’round as I recount my leap into the realm of PlayStation with the illustrious PS5 Bundle. As someone who’s long been immersed in the world of Xbox, I couldn’t resist the allure of Sony’s latest offering, and boy, am I glad I made the jump.Let’s kick things off with a nod to the PS5’s design. This thing is a marvel to behold, sleek and futuristic, commanding attention in my entertainment setup like a true centerpiece. And with a whopping 1TB of SSD storage, I’ve got all the room I need for my ever-expanding library of games, without having to endure those dreaded load times.Now, onto the real meat of the matter – the performance. The PS5 doesn’t pull any punches, delivering jaw-dropping 4K visuals and buttery-smooth frame rates that breathe new life into every game I play. And let’s talk about that SSD – it’s like greased lightning, ensuring I spend less time waiting and more time doing what I love: gaming.But hold onto your controllers, because the real star of the show is the DualSense. This thing is a game-changer in every sense of the word, with its immersive haptic feedback and adaptive triggers that elevate every gaming experience to new heights. And with backward compatibility, I can revisit old favorites with a fresh perspective, like catching up with old friends over a round of drinks.Of course, no review would be complete without addressing a few bumps along the road. The limited storage space is a bit of a downer, especially in today’s world of massive game downloads. And let’s not forget the price tag – while the PS5 is worth every penny in my book, it’s definitely an investment.And then there’s the hunt for the elusive console itself. Scouring the internet for availability felt like navigating a labyrinth, but let me tell you, the payoff was worth it. Sure, the PS5’s size might be a tad imposing for some setups, but once you fire up that first game, you’ll forget all about it.In conclusion, the PS5 Bundle has exceeded my wildest expectations, converting this die-hard Xbox fan into a true believer. So if you’re on the fence, take the plunge – you won’t regret it. Happy gaming, folks! .
Let’s say that we want to summarize all the reviews that have more than 100 words:
This is how we summarize those:
Python
# Apply the function only where word_count > 200
def process_row(row):
if row["word_count"] > 200:
summary = summarize_review(prompt=prompt_template, review_text=row["review_text"])
print(f"Processed row {row.name}: Summary created")
print(summary)
return summary
else:
print(f"Processed row {row.name}: No summary needed (word_count <= 200)")
return None
# Apply the function to process each row
docs["summary"] = docs.apply(process_row, axis=1)
These are the first 3 entries:
profile_name | rating | rating_date | title | review_text | word_count | summary | |
---|---|---|---|---|---|---|---|
0 | J.F. Carroll | 5.0 out of 5 stars | Reviewed in the United States on May 6, 2024 | 5.0 out of 5 stars\nIt's a PS5, everyone wants one, was there any doubt?\n | \nNobody could get their hands on this until just recently. It's a PS5, the craftmanship is awesome.Operates well, system works well with store, everything is easy, this console is a masterpiece and I keep it close. Haven't got into too many games yet. It looks sexy as heck, it looks dumb in ads but in real life it's fantastic looking. And you can buy sexy plates for it as well.Controller feels great. Considering the upgrade controller, but I haven't decided. The game choices are phenomenal. It's official: XBOX lost, buy a Playstation.Playstation all the way.\n | 95 | None |
1 | Brandon | 5.0 out of 5 stars | Reviewed in the United States on April 17, 2024 | 5.0 out of 5 stars\nGreat Product, Great Price, Great Games!\n | \nI was looking for a great deal. Luckily, I found this product on sale for $450! I love this new PlayStation 5! I've been waiting years to get my own and now I have it! I use it vertically instead of horizontally, so I had to buy a vertical stand for an extra $20. Overall, I would recommend you buy this!\n | 61 | None |
2 | Brock | 5.0 out of 5 stars | Reviewed in the United States on May 5, 2024 | 5.0 out of 5 stars\nAmazing Console\n | \nAfter using the playstation for over 4 months; I can say, this is one of the best consoles I’ve played on. I had an Xbox Series S and after playing on the PS5 there is a world of difference in graphics, FPS, and loading times. Also the spiderman game that comes with this model is spectacular.\n | 56 | None |
3 | Whispering Whiskers | 5.0 out of 5 stars | Reviewed in the United States on March 18, 2024 | 5.0 out of 5 stars\nEmbarking on a Next-Gen Adventure: An Honest Review of the PS5 Bundle\n | \nGreetings, fellow gamers! Gather 'round as I recount my leap into the realm of PlayStation with the illustrious PS5 Bundle. As someone who's long been immersed in the world of Xbox, I couldn't resist the allure of Sony's latest offering, and boy, am I glad I made the jump.Let's kick things off with a nod to the PS5's design. This thing is a marvel to behold, sleek and futuristic, commanding attention in my entertainment setup like a true centerpiece. And with a whopping 1TB of SSD storage, I've got all the room I need for my ever-expanding library of games, without having to endure those dreaded load times.Now, onto the real meat of the matter – the performance. The PS5 doesn't pull any punches, delivering jaw-dropping 4K visuals and buttery-smooth frame rates that breathe new life into every game I play. And let's talk about that SSD – it's like greased lightning, ensuring I spend less time waiting and more time doing what I love: gaming.But hold onto your controllers, because the real star of the show is the DualSense. This thing is a game-changer in every sense of the word, with its immersive haptic feedback and adaptive triggers that elevate every gaming experience to new heights. And with backward compatibility, I can revisit old favorites with a fresh perspective, like catching up with old friends over a round of drinks.Of course, no review would be complete without addressing a few bumps along the road. The limited storage space is a bit of a downer, especially in today's world of massive game downloads. And let's not forget the price tag – while the PS5 is worth every penny in my book, it's definitely an investment.And then there's the hunt for the elusive console itself. Scouring the internet for availability felt like navigating a labyrinth, but let me tell you, the payoff was worth it. Sure, the PS5's size might be a tad imposing for some setups, but once you fire up that first game, you'll forget all about it.In conclusion, the PS5 Bundle has exceeded my wildest expectations, converting this die-hard Xbox fan into a true believer. So if you're on the fence, take the plunge – you won't regret it. Happy gaming, folks!\n | 374 | I recently switched from Xbox to the PS5 Bundle and I'm thrilled. The design is sleek, storage is ample, and performance is top-notch with 4K visuals and fast load times. The DualSense controller is a game-changer. Limited storage and high price are drawbacks, but overall, it's worth the investment. Happy gaming! |
4 | Carson Morby | 5.0 out of 5 stars | Reviewed in the United States on May 8, 2024 | 5.0 out of 5 stars\nI finally go one!\n | \nFinally I got a PS5! I've been waiting forever, but I made the purchase because I heard NCAA Football 24 isn't coming out on PS4. Yep. I literally upgraded for that reason only :) So far it has been super easy to use. Navigation is pretty similar to the PS4 so there hasn't been much of an adjustment. The controller battery lasts wayyyyy longer than the ps4 controllers. The image is crystal clear, especially where I have it paired with a Sony TV. Can't ask for better quality than that.It runs quiet, and I literally have no complaints about the PS5. I'm excited to playing around and experiences more!\n | 109 | None |
5 | Chris | 5.0 out of 5 stars | Reviewed in the United States on April 17, 2024 | 5.0 out of 5 stars\nAwesome - Bad Packaging\n | \nThe unit arrived in good condition and works as intended, but the Amazon packaging was an oversized box and some paper. I would’ve like to see the air packaging surrounding this for shipping. Amazon should know if something is so expensive they even require a one time password for delivery that maybe the expensive product should have extra care taken to make sure the box is properly insulated for rough shipping and handling. I worked for FedEx, and packages are tossed like garbage throughout the process because of the pace required to keep up with the load. They really should ensure that sensitive or expensive items are packaged securely for perfect condition arrival to the customer.5 stars for the product, but 0 stars for Amazon on the packaging. It was delivered two days ahead of schedule, so that was a nice bonus. It seems like these are stocked locally in an Amazon near you.\n | 154 | None |
Let’s say that we want more targetted summaries to inform specific departments within companies.
Python
# Assuming longest_review is defined and contains text
prompt_template2 = """
Task: Summarize the e-commerce product review below, delimited by triple backticks,\
to provide feedback to the engineering department responsible for the product's design.
Instructions:
1. Create a concise summary in at most 30 words.
2. Focus specifically on aspects relevant to the product's design or engineering.
3. If the review does not mention design or engineering, respond with exactly the\
word "None" (without quotes or additional text).
Review:
```{review_text_here}```
"""
Let’s do this for the first 10 entries:
And these is how we define the function to process the reviews:
And this is how we apply it to those entries:
And this is what the processed entried look like:
profile_name | rating | rating_date | title | review_text | word_count | summary | targeted_summary | |
---|---|---|---|---|---|---|---|---|
0 | J.F. Carroll | 5.0 out of 5 stars | Reviewed in the United States on May 6, 2024 | 5.0 out of 5 stars\nIt's a PS5, everyone wants one, was there any doubt?\n | \nNobody could get their hands on this until just recently. It's a PS5, the craftmanship is awesome.Operates well, system works well with store, everything is easy, this console is a masterpiece and I keep it close. Haven't got into too many games yet. It looks sexy as heck, it looks dumb in ads but in real life it's fantastic looking. And you can buy sexy plates for it as well.Controller feels great. Considering the upgrade controller, but I haven't decided. The game choices are phenomenal. It's official: XBOX lost, buy a Playstation.Playstation all the way.\n | 95 | None | The review praises the PS5's craftsmanship, ease of use, and game choices. Positive feedback on controller and aesthetics. Suggests considering an upgraded controller. No negative comments on design or engineering. |
1 | Brandon | 5.0 out of 5 stars | Reviewed in the United States on April 17, 2024 | 5.0 out of 5 stars\nGreat Product, Great Price, Great Games!\n | \nI was looking for a great deal. Luckily, I found this product on sale for $450! I love this new PlayStation 5! I've been waiting years to get my own and now I have it! I use it vertically instead of horizontally, so I had to buy a vertical stand for an extra $20. Overall, I would recommend you buy this!\n | 61 | None | The customer is satisfied with the PlayStation 5 purchase but had to buy an additional vertical stand. Consider including a built-in stand in future designs. |
ChatGPT OpenAI can also be used for some inferrence task:
Examples:
Here is how we can infer sentiment.
And this is the reponse
We can make the prompt more concise
We might also be interested in identifying emotions in text.
Python
text = """
Needed a nice lamp for my bedroom, and this one had \
additional storage and not too high of a price point. \
Got it fast. The string to our 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. I had a missing part, so I contacted their \
support and they very quickly got me the missing piece! \
Lumina seems to me to be a great company that cares \
about their customers and products!!
"""
This is what our prompt could look like:
An this is the response:
We can also obtain more information related to emotions from text.
Python
text = """
Needed a nice lamp for my bedroom, and this one had \
additional storage and not too high of a price point. \
Got it fast. The string to our 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. I had a missing part, so I contacted their \
support and they very quickly got me the missing piece! \
Lumina seems to me to be a great company that cares \
about their customers and products!!
"""
This is what our prompt could look like:
Python
prompt = f"""
Identify the following items from the review text:
- Item purchased by reviewer
- Company that made the item
The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Item" and "Brand" as the keys.
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
Review text: '''{text}'''
"""
This is what our response looks like:
We can thus perform multiple tasks within the same prompt:
Python
prompt = f"""
Identify the following items from the review text:
- Sentiment (positive or negative)
- Is the reviewer expressing anger? (true or false)
- Item purchased by reviewer
- Company that made the item
The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Sentiment", "Anger", "Item" and "Brand" as the keys.
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
Format the Anger value as a boolean.
Review text: '''{text}'''
"""
And this is the response:
Similar to LDA and word-embeddings, we can easily identify topics within a text.
Speech
Python
speech = """I am encouraged by the honourable Gentleman’s anger. His intervention bears no relationship\
to the reality in Britain or to reality for our European Union partners. His comment was important.\
I hope that it will be well circulated to his constituents. It is clear that this whole process is \
in a state of disarray. I welcomed the new Secretary of State for Transport. His main challenge is \
to persuade a number of his colleagues in the Cabinet that the whole process should be dumped. I want\
to finish on two points that sum up this fiasco. I am sure that the honourable Member for Ross,\
Cromarty and Skye will deal with the issue of sleeper services to Scotland. The Government and\
the British Railways Board were dragged into the courts in Scotland to be exposed for what they\
had shown to Scots - utter contempt for their rail network. They thought that, through stealth\
and the incompetence of the director of franchising, Mr. Roger Salmon, they could close services\
under the guise of consultation on a draft passenger service requirement. They were found out.\
Is it not a disgrace that railway policy is now going to be partly dictated in the courts of\
England and Scotland? That is a fiasco and the Government know it. The second issue is trans-European\
networks. It is an important issue for Britain because it involves integrating the major routes in Britain\
with those in Europe. It is absolutely right that that should be done. What did Ministers do\
when they went to Brussels? They sabotaged the proposals. We find that the European Union is\
giving no priority to schemes. It has thrown out environmental impact assessments and of course\
no cash is being provided by the European Union for such developments..
"""
This is what our prompt looks like:
This is what our response looks like:
We might also be interested in identifying whether certain topics are present within texts.
Python
prompt = f"""
Determine whether each item in the following list of \
topics is a topic in the text below, which
is delimited with triple backticks.
Format your response as a JSON object with \
the topics as the keys.
Give your answer as follows:
item from the list: 0 or 1
List of topics: {", ".join(topic_list)}
Text sample: '''{speech}'''
"""
And this is the response:
We can also use ChatGPT to translate.
And this is what the response could look like:
We can also use ChatGPT to translate to multiple languages.
This is what the prompt looks like:
Python
{
"Italian": {
"text": "Amo ciò che posso fare con ChatGPT OpenAI."
},
"Spanish": {
"text": "Amo lo que puedo hacer con ChatGPT OpenAI."
}
}
We can also use ChatGPT to proofread and fix grammar mistakes.
Python
This is a mock short paragraph to see how ChatGPT performs when it comes to grammar and spelling. There is a wrong sentence. Spelling is off.
This is how we can see better the fixes:
Python
This is a mock short paragraf paragraph to see how ChatGPT performs when it comes to\ ¶ grammer to grammar and spelling. These There is a wrong sentence. Speling Spelling is of.off.
Final Thoughts:
Popescu (JCU): Lecture 15