l4-summarizing
l4-summarizing
Summarizing
In this lesson, you will summarize text with a focus on specific topics.
Setup
In [1]: import openai
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
In [2]: def get_completion(prompt, model="gpt-3.5-turbo"): # Andrew mentioned that the prompt/ completion paradigm is prefera
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"]
Text to summarize
In [3]: 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 \
https://s172-29-15-222p8888.lab-aws-production.deeplearning.ai/nbconvert/html/l4_summarizing/l4-summarizing.ipynb?download=false 1/6
4/29/25, 2:02 PM l4-summarizing
to her.
"""
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
Soft, cute panda plush toy loved by daughter, but smaller than expected for the price. Arrived early, friendly face.
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
The customer was pleased with the early delivery of the panda plush toy, but felt it was slightly small for the price
paid.
https://s172-29-15-222p8888.lab-aws-production.deeplearning.ai/nbconvert/html/l4_summarizing/l4-summarizing.ipynb?download=false 2/6
4/29/25, 2:02 PM l4-summarizing
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
The panda plush toy is loved for its softness and cuteness, but some customers feel it's a bit small for the price.
Comment
Summaries include topics that are not related to the topic of focus.
Review: ```{prod_review}```
"""
https://s172-29-15-222p8888.lab-aws-production.deeplearning.ai/nbconvert/html/l4_summarizing/l4-summarizing.ipynb?download=false 3/6
4/29/25, 2:02 PM l4-summarizing
response = get_completion(prompt)
print(response)
Feedback: The product arrived a day earlier than expected, which was a pleasant surprise. Customers may prefer larger
options for the same price.
https://s172-29-15-222p8888.lab-aws-production.deeplearning.ai/nbconvert/html/l4_summarizing/l4-summarizing.ipynb?download=false 4/6
4/29/25, 2:02 PM l4-summarizing
https://s172-29-15-222p8888.lab-aws-production.deeplearning.ai/nbconvert/html/l4_summarizing/l4-summarizing.ipynb?download=false 5/6
4/29/25, 2:02 PM l4-summarizing
Review: ```{reviews[i]}```
"""
response = get_completion(prompt)
print(i, response, "\n")
0 Summary:
Adorable panda plush loved by daughter, but small for price. Arrived early, soft and cute.
1 Great lamp with storage, fast delivery, excellent customer service for missing parts. Company cares about customer
s.
2 Impressive battery life, small brush head, good deal for $50, generic replacement heads available, leaves teeth fee
ling clean.
3 17-piece system on sale for $49, quality decline, motor issue after a year, price increase, customer service, brand
loyalty.
https://s172-29-15-222p8888.lab-aws-production.deeplearning.ai/nbconvert/html/l4_summarizing/l4-summarizing.ipynb?download=false 6/6