Prompt Engineering for Summarization
Nowadays one of the hot topics that has gained everyone’s attention towards it is ChatGPT which has been launched by OpenAI. The web service is suspected to reduce many jobs as well as increase the efficiency of the people manifold as it can give tailored answers to your questions.
Now if you have tried to explore a little bit about ChatGPT regarding how effectively you can use it for your application you must have come across the term that is known as Prompt Engineering. Articles like:
- Top 30 prompts you need to use ChatGPT effectively.
- Prompts that can change your life. ?
These articles look very fascinating but the reality is somewhat different based on the fact that one prompt cannot serve the purpose for two different individuals as their requirements may differ. Hence Prompt Engineering has become a field of study to effectively utilize these web-based AI services based on the personal use case and the need to customize it as per the need.
In this article, we will look at some cool examples of prompts and how they are used to generate desired results.
Import the Openai package and assign the Openai API key
Python3
import openai import os openai.api_key = "<OpenAI API Key>" |
Chat Completions API
Python3
openai.ChatCompletion.create( model = "gpt-3.5-turbo" , messages = [ { "role" : "user" , "content" : "Who won the ICC world cup in 2011?" }, ] ) |
Output:
<OpenAIObject chat.completion id=chatcmpl-7T4PnNcMT2tjElv4v47xzYvYc88lG at 0x7f2359b8f0e0> JSON: {
"id": "chatcmpl-7T4PnNcMT2tjElv4v47xzYvYc88lG",
"object": "chat.completion",
"created": 1687162839,
"model": "gpt-3.5-turbo-0301",
"usage": {
"prompt_tokens": 19,
"completion_tokens": 2,
"total_tokens": 21
},
"choices": [
{
"message": {
"role": "assistant",
"content": "India."
},
"finish_reason": "stop",
"index": 0
}
]
}
Create a function based on the above Chat Completions API and output
Python3
def get_completion(prompt, model = "gpt-3.5-turbo" , temperature = 0 ): messages = [{ "role" : "user" , "content" :prompt}] response = openai.ChatCompletion.create( model = model, messages = messages, temperature = temperature ) return response.choices[ 0 ].message[ "content" ] |
Prompt Engineering for Summarization Task
You must have played the game Candy Crush Saga. Games or any application keep updating for a better customer experience and user interface. But sometimes it works out and sometimes it doesn’t people do provide their review regarding the product itself or the new feature itself. But it can be tedious to fo through each review which is way too long. Here we can use the ChatGPT to summarize them for us in the desired number of words.
Python3
prod_review = """ I love this game very much. I love the colourful candies.\ Recently, the developer of the application changed the\ music and added new candies. Although, the new music\ is also good but the previous one was actually suitable \ for the game and it was perfect for this game. We wish \ if we get the options to switch the music to the previous\ one. We miss that music alot and hope one day we get it\ back again. What I don't like about this game is the\ presence of horse and cartoons with dark complexion. """ |
Now let’s write a prompt that can summarize the above review as per our needs.
Python3
prompt = f """ Your task is to generate a short summary of a product \ review of an application that is available on playstore. Summarize the review below, delimited by triple backticks, in at most 30 words. Review: ```{prod_review}``` """ response = get_completion(prompt) print (response) |
Output:
A user loves the colorful candies and gameplay of the app, but wishes for the option to switch back to the previous music. They also express discomfort with the presence of certain characters.
But this is not it we can modify our prompt as per our requirements. Let’s say we would like to improve our application and hence we would like to get that part of the review only in which the user shares his/her dissatisfaction with the product.
Python3
prompt = f """ Your task is to generate a short summary of a product \ review of an application that is available on playstore. Summarize the review below, delimited by triple backticks, in at most 30 words and focus on that\ part of the review where user has expressed his \ dissatisfaction with the product. Review: ```{prod_review}``` """ response = get_completion(prompt) print (response) |
Output:
User loves the game but dislikes the presence of horse and cartoons with dark complexion. Also, wishes to have the option to switch back to the previous music.
Now one can easily observe the difference between the summaries which has been provided by the model is different for the two cases as per the requirements we have passed to the model.
The summary that will be generated by the model will differ greatly depending upon what keywords we have used in the instruction to the model. For e.g, using the extracted keyword instead of the summarize the information extracted will differ and the one obtained with the extract keyword will be to the point.
Python3
prompt = f """ Your task is to extract the relevant information from a \ product review of an application that is available on\ playstore. From the review below, delimited by triple backticks, extract the information that is strongly\ related to the customer satisfaction\ in at most 30 words Review: ```{prod_review}``` """ response = get_completion(prompt) print (response) |
Output:
Customer loves the game and colorful candies. Developer changed music and added new candies, but customer prefers old music and wishes for option to switch back. Dislikes presence of horse and cartoons with dark complexion.
Similarly, we can include an icon that provides us with the summary using this prompt using the ChatGPT model and the prompt developed to reduce the time required to go through the customer reviews.
Similarly, let’s say we have received some reviews from the users of our product and some of them are very negative to have a better user experience and they feel like their feedback is heard we would like to develop a system by using which we can send replies to the customer and the steps that will be taken to resolve the issues.
Python3
# given the sentiment from the lesson on "inferring", # and the original customer message, customize the email sentiment = "negative" # review for the app update review = f """ They have stopped ads for lives and extra moves if a level is not passed. Its making the game boring when we're stuck on same level for one week. Also, I'm not receiving any lives from friends since I last updated the app. If you want to force us in buying lives and goodies than its your mistake. I'll just uninstall it if you don't fix the problem and so will many others like me. Thanks in advance """ |
If we have the above review then it is easily noticeable that the sentiment is negative in this feedback or review so, we would like to apologize to the user for this and provide him assurance for the changes to resolve the issues.
Python3
prompt = f """ You are a customer service AI assistant. Your task is to send an email reply to a valued customer. Given the customer email delimited by ```, \ Generate a reply to thank the customer for their review. If the sentiment is positive or neutral, thank them for \ their review. If the sentiment is negative, apologize and suggest that \ they can reach out to customer service. Make sure to use specific details from the review. Write in a concise and professional tone. Sign the email as `AI customer agent`. Customer review: ```{review}``` Review sentiment: {sentiment} """ response = get_completion(prompt) print (response) |
Output:
Dear valued customer,
Thank you for taking the time to share your feedback with us. We apologize for any inconvenience caused by the recent changes in our game. We understand that the removal of ads for lives and extra moves if a level is not passed has made the game less enjoyable for you. We also apologize for the issue with not receiving lives from friends since the last app update.
We assure you that our intention is not to force our customers into buying lives and goodies. We appreciate your loyalty and want to make sure that you have the best possible experience while playing our game. We would like to suggest that you reach out to our customer service team for assistance with these issues. They will be happy to help you and provide you with a solution.
Thank you again for your feedback. We hope to have the opportunity to improve your experience with our game.
Best regards,
AI customer agent
The same can be done for positive feedback as well.
Python3
sentiment = "positive" # review for the app update review = f """ Love it, it takes luck and skill to get through the stages, excellent variation. I have had to start over twice due to the game getting stuck, hope someone will look into the matter. Some of the moves reminds me of playing chess. This is, in my view one of the best games to test me as well as a time killer when waiting for meetings to start. Young and old can play, as the stages are moved up the level difficulty increases but lots of ways to get help. """ |
Now let’s create the email response to this positive review of the user to thank him/her.
Python3
prompt = f """ You are a customer service AI assistant. Your task is to send an email reply to a valued customer. Given the customer email delimited by ```, \ Generate a reply to thank the customer for their review. If the sentiment is positive or neutral, thank them for \ their review. If the sentiment is negative, apologize and suggest that \ they can reach out to customer service. Make sure to use specific details from the review. Write in a concise and professional tone. Sign the email as `AI customer agent`. Customer review: ```{review}``` Review sentiment: {sentiment} """ response = get_completion(prompt) print (response) |
Output:
Dear Valued Customer,
Thank you for taking the time to leave a review of our game. We are thrilled to hear that you are enjoying the game and find it challenging yet entertaining. We appreciate your feedback on the game getting stuck and we will definitely look into the matter.
We are glad to hear that you find the game reminiscent of playing chess and that it is a great way to pass the time. We designed the game to be accessible to players of all ages and we are happy to hear that you find the difficulty level to be just right.
Thank you again for your review and for choosing our game. We hope you continue to enjoy playing it.
Best regards,
AI customer agent
Reference:
- https://learn.deeplearning.ai/chatgpt-prompt-eng/lesson/4/summarizing
- https://learn.deeplearning.ai/chatgpt-prompt-eng/lesson/7/expanding
Contact Us