Mastering Prompt Engineering to Unleash ChatGPT’s Potential | by Idil Ismiguzel | Jun, 2023

1. Summarize text and extract information

Summarizing information can be a time-saving technique, especially when you need to extract key points or specific details. With the ChatGPT API, we can leverage its capabilities to generate summaries for large volumes of text.

Taking the example of the Disneyland reviews dataset, which consists of 42,000 reviews, we can ease the evaluation process by utilizing summaries. Although I will demonstrate with just one review, the approach can easily be scaled to handle larger quantities of text.

To use ChatGPT API, you will need to login into your OpenAI account and generate your API key by navigating to “View API Keys” section from the right top corner. Once you created your API key, you need to store it in a safe place and not display it.

# Install openai 
pip install openai

import os
import openai

# Safely store your API key
openai.api_key = OPENAI_API_KEY

We will now generate a helper function that will take our prompt and return a completion for that prompt.

# Helper function to return completion for a prompt

def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
temperature=0, # degree of randomness of the response
return response.choices[0].message["content"]

# Input one review

review = """
Have been to Disney World, Disneyland Anaheim and Tokyo Disneyland
but I feel that Disneyland Hong Kong is really too small to be
called a Disneyland. It has way too few rides and attractions.
Souvenirs, food and even entrance tickets are slightly more
expensive than other Disneyland as well. Basically, this park is
good only for small children and people who has never been to Disney.
The food choices were acceptable, mostly fast food, and not too expensive.
Bottled water, however, was VERY expensive but they do have water
fountains around for you to refill your water bottles. The parade was
pretty good. It was crowded not a problem but what was the problem was
the people were just so rude, the pushing and shoving cutting in lines
for the rides, gift shops, food stands was just to much to take. forget
trying to see one of the shows its a free for all for seats, i don't see
how Disney can let this happen, it was by far the worst managed Disney

# Write the prompt and generate the response using the helper function

prompt = f"""
Summarize the review below in 30 words.
Review: ```{review}```

response = get_completion(prompt)

Disneyland Hong Kong is too small with few rides and attractions. Food and souvenirs are expensive. Good for small children and first-time visitors. Crowded with rude people and poorly managed.

The summary is pretty great and around 30 words as we asked in the prompt. If we want we can also focus on a specific element in the summaries e.g. price and value.

prompt = f"""
Summarize the review below in 10 words and
focus price and value.
Review: ```{review}'''

response = get_completion(prompt)

Expensive, small, and crowded Disneyland with few attractions.

By generating a targeted summary, we have transformed our initial review into a more meaningful and impactful statement. 🍄

Now, let’s take it a step further and extract relevant information in the JSON format for easier processing and integration with other systems. We will define a template for keys in the prompt.

prompt = f"""
Identify the following items from the review:
- Sentiment (positive or negative)
- Which Disney park was reviewed
- Is the reviewer expressing disappointment? (true or false)
- Is the reviewer expressing happiness? (true or false)

Format your response as a JSON object with
"Sentiment", "Park", "Anger" and "Happiness" as the keys.
Format the Anger and Happiness value as a boolean.
Keep the answers short.

Review: ```{review}```
response = get_completion(prompt)

“Sentiment”: “negative”,
“Park”: “Hong Kong Disneyland”,
“Anger”: true,
“Happiness”: false

It’s pretty amazing!

2. Translate the text

As you can imagine, if we can create summaries, we can also translate the text into different languages. Building on the previous example, we will now request summaries to be generated in English, Italian, and German languages.

prompt = f"""
Your task is to summarise the following review.

Summarize the review below in at most 10 words.
Print the summary in English, Italian and German

Review: ```{review}```

response = get_completion(prompt)

English: Disneyland Hong Kong is small with few attractions and expensive.
Italian: Disneyland Hong Kong è piccolo con poche attrazioni e costoso.
German: Disneyland Hong Kong ist klein mit wenigen Attraktionen und teuer.

3. Expanding text

Expanding involves utilizing a smaller text piece, and leveraging the capabilities of a large language model to generate a more extensive piece of text. For example, if we input a customer review, we can produce a detailed reply email and address the concerns raised. We can provide guidelines for the format and content.

prompt = f"""
Your task is to write an email reply to the 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 the customer
for their feedback and refer to the specific details they mentioned.
- If the sentiment is negative, sincerely apologize for their poor
experience, take responsibility, and provide follow-up contact information.
- Express appreciation for their time and review. Refer to particular
points to build rapport.
- For negative experiences, apologize sincerely and accept responsibility.
Offer customer service contact information.
- Aim for a concise yet thoughtful tone.
- Close professionally as an "AI customer agent".
Customer review: ```{review}```

response = get_completion(prompt)

Dear valued customer,

Thank you for taking the time to share your experience at Disneyland Hong Kong. We appreciate your feedback and are sorry to hear that your visit did not meet your expectations.

We apologize for any inconvenience caused by the limited number of rides and attractions, as well as the higher prices for souvenirs, food, and tickets. We understand that these factors can impact your overall experience at the park.

We are glad to hear that you found the food choices acceptable and that the parade was enjoyable. We also appreciate your feedback regarding the crowds and rude behavior of some guests. We take these concerns seriously and will work to improve our management of the park to ensure a more enjoyable experience for all guests.

If you have any further concerns or would like to provide additional feedback, please do not hesitate to contact our customer service team at [insert contact information]. We value your feedback and hope to have the opportunity to provide a better experience for you in the future.

Thank you again for your review.

Best regards,

AI customer agent

Good job, AI customer agent! ✉️

In this article, we explored the best practices of prompt engineering using both the web UI and API. We discussed several effective prompt patterns, including persona patterns, question refinement, templates, zero-shot, and chain-of-thought prompting. Additionally, we delved into utilizing the ChatGPT API by generating our secret key and explored its capabilities in text summarization, translation, and expansion.

I hope this tutorial inspires you to use large language models to boost your creativity and productivity. I am sure, it can help you to generate ideas, gain new insights, solve complex problems — and enhance your daily work at many levels. The possibilities are vast! 🤖✨

🍓 If you enjoy reading articles like this and wish to support my writing, you may consider becoming a Medium member! Medium members get full access to articles from all writers and if you use my referral link, you will be directly supporting my writing.

🍓 If you are already a member and interested to read my articles, you can subscribe to be notified or follow me on Medium. Let me know if you have any questions or suggestions.

Source link

Leave a Comment