Prompt Engineering 101

Shanmukh
16 min readApr 30, 2023

I came across a LinkedIn post yesterday announcing a free course on prompt engineering by Andrew Ng, the founder of Coursera and deeplearning.ai. Excited to learn more, I enrolled in the course and started watching the videos.

Ref: https://www.youtube.com/watch?v=H4YK_7MAckk

P.S: We will be building a chat bot in the end so make sure you stick to the end

I have started integrating multiple OpenAI products into my work, including Codex (Copilot), Whisper, Dall-E, Chat Completion, and Translation. Hence, I was eager to enhance my prompt engineering skills by this course.

The course is a short crash course of only 3.5–4 hours, but it covers the basics of LLM models and a few other prompt principles.

In this article, I’ll provide a brief summary of the course and what it contains. However, I won’t provide many examples, as I highly recommend that you explore the topics yourself to gain a deeper understanding.

I am sure all of you have explored a lot of ChatGPT and most of the below topics may sound familiar and basic so let me remind you that this is a Level 0 article of Prompt Engineering.

Why is prompt engineering important for developers?

Prompt engineering is becoming increasingly important for developers due to the rising demand for AI applications. As the use of large language models (LLMs) becomes more common, developers must know how to construct effective prompts to interact with these models and extract more accurate and useful results.

Moreover, prompt engineering is an essential skill for developers because it enables them to customize the behavior of LLMs to better fit their specific use cases. By mastering prompt engineering techniques, developers can stay ahead of the curve in this rapidly changing field of Software

What is LLM ?

LLM stands for Large Language Models. These are machine learning models that are trained on massive amounts of data to understand and generate human-like language. These models are used in a lot of natural language processing tasks like summarisation, text completion and translation which we are gonna see later in this article

Setting up the base code (Make sure you update the api key)

Here is the link to the Juypter Notebook for you to play around:

https://github.com/bugsurfer/medium/blob/6d2d5f5a752e2891a5612cbb3663a55cd7b17223/prompt_engineering_101.ipynb

Below is the code interact with OpenAI using the python library openai the method uses a `gpt-3.5-turbo` model

import openai
openai.api_key = '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'

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

Prompting Principles:

1. Write clear and specific instructions

Clear and specific instructions are essential for effective prompts, as they help ensure that large language models provide accurate and relevant results. Remember Clear != Short - in fact, longer prompts can often lead to more accurate results.

Tactic 1: Use delimiters to clearly indicate distinct parts of the input

text = f"""
50 First Dates" is a romantic comedy that offers a unique and heartwarming storyline.
The chemistry between the two lead actors, Adam Sandler and Drew Barrymore, is undeniable and their performances are outstanding.
The film does a great job of balancing humor and drama, and the scenes are beautifully shot in the stunning location of Hawaii.
While the premise may seem a bit unbelievable, the movie captures the essence of true love and the lengths that one is willing to go to keep it.
Overall, "50 First Dates" is a delightful movie that is sure to leave you with a smile on your face and a warm heart.
"""
prompt = f"""
Summarize the text which is an review given by a user
into a single sentence.
```{text}```
"""
response = get_completion(prompt)
print(response)

Response:
"50 First Dates" is a heartwarming romantic comedy with great chemistry between the lead actors,
a balance of humor and drama, and beautiful scenes shot in Hawaii, capturing the essence of true love.

Tactic 2: Ask for a structured output

text = f"""
50 First Dates" is a romantic comedy that offers a unique and heartwarming storyline.
The chemistry between the two lead actors, Adam Sandler and Drew Barrymore, is undeniable and their performances are outstanding.
The film does a great job of balancing humor and drama, and the scenes are beautifully shot in the stunning location of Hawaii.
While the premise may seem a bit unbelievable, the movie captures the essence of true love and the lengths that one is willing to go to keep it.
Overall, "50 First Dates" is a delightful movie that is sure to leave you with a smile on your face and a warm heart.
"""
prompt = f"""
Summarize the text which is an review given by a user
into a single sentence.
```{text}```

Return the output in a Json Format with summary, actors, movie_name, locations
"""
response = get_completion(prompt)
print(response)

Response:
{
"movie_name": "50 First Dates",
"summary": "A romantic comedy about a man who falls in love with a woman who suffers from short-term memory loss and has to win her heart every day.",
"actors": [
"Adam Sandler",
"Drew Barrymore"
],
"locations": [
"Hawaii"
],
"genre": [
"Romantic Comedy",
"Drama"
]
}

Tactic 3: Ask the model to check whether conditions are satisfied

prompt = f"""
```{text}```
Calculate the sentiment of the review
If the sentiment of the review is positive
return better list of the movies the actors are listed in
If the sentiment of the review is negative
Return the output in a Json Format with summary, actors, movie_name, locations, Genre
"""

Response:
Sentiment: Positive

Better list of movies the actors are listed in:
- The Wedding Singer (Adam Sandler and Drew Barrymore)
- Blended (Adam Sandler and Drew Barrymore)
- Never Been Kissed (Drew Barrymore)
- 50 First Dates (Adam Sandler and Drew Barrymore)
- The Waterboy (Adam Sandler)

Tactic 4: “Zero-shot” prompting

Allows you to obtain results without any training or fine-tuning.

text = f"""Happy Birthday!"""
prompt = f"""
```{text}```
Return a response to the above text acting as a friend
"""
response = get_completion(prompt)
print(response)

Response:
"Thank you so much! You're such a sweet friend."

Tactic 5: “Few-shot” prompting

Allows you to achieve accurate results with just a few examples.

text = f"""Happy Birthday!"""
prompt = f"""
Repond to the given text based on the below style of chandler
<friend>: Happy Work Anniversary!
<response>: Could I BE any happier? Thanks for the well wishes!
<friend>: Happy christmas!
<response>: Well, well, well, if it isn't the annual gift-giving ceremony... I mean, Merry Christmas!

```{text}```
"""
response = get_completion(prompt)
print(response)

Response:
"Could I BE any older? Thanks for the birthday wishes!"

2. Give the model time to think

Refers to the importance of allowing the large language model enough time to process and generate responses to complex prompts. By giving the model time to think, we can improve the accuracy and quality of the generated outputs.

This is similar to humans. More time More accurate Results

Note: The model may give you response within smaller duration but the results may not be that satisfactory so always make sure you give enough time to the model to process for better results

Tactics:

  1. Split the task into multiple tasks and provide steps
  2. Use a larger max response length
  3. Use a more specific prompt
  4. Use a longer pause between inputs

Iterative Prompt Development:

As the name suggest, Iterative prompt development is a process of refining a prompt through multiple iterations to improve its accuracy and relevance. It involves testing the initial prompt, analyzing the results, and modifying the prompt based on the feedback received. This process can be repeated several times until the desired results are achieved.

Best example would be developing a prompt for creating an image but for now take an example of creating a summary of the above movie

prompt = f"""
Write me a review of the movie 50 first dates
"""
response = get_completion(prompt)
print(response)

prompt = f"""
Write me a review of the movie 50 first dates
The review should include the actors performance
"""
response = get_completion(prompt)
print(response)

prompt = f"""
Write me a review of the movie 50 first dates
The review should include the actors performance
But make sure it is restricted to 2 paragraphs and 100 words only
"""
response = get_completion(prompt)
print(response)

Summarization and Extraction:

Summarization and extraction prompts would be very helpful in understanding reviews, feedbacks, articles, research papers and more

Using these prompts effectively can make you create a assistant of your own to help you as a study or work companion

Lets look at some examples:

review = f"""
50 First Dates is a charming romantic comedy that manages to balance humor with heart in a unique and memorable way.
The chemistry between Adam Sandler and Drew Barrymore is undeniable, and their performances are both hilarious and heartfelt.
The film's unique premise of a woman with short-term memory loss makes for a compelling story that keeps the audience engaged throughout.

While some of the humor may be crude at times, it never detracts from the overall enjoyment of the film.
Additionally, the supporting cast, including Rob Schneider and Sean Astin, add to the film's comedic moments and bring even more heart to the story.

Overall, 50 First Dates is a feel-good movie that is perfect for a date night or just a night in with friends.
Its mix of humor and heart make it a great choice for anyone looking for a fun and romantic film that is sure to leave a smile on their face.
"""
prompt = f"""
Summarize the review given by a user for a movie
Review: ```{review}```
"""
response = get_completion(prompt)
print(response)

Response:
"The user gave a positive review for the movie 50 First Dates, describing it as a charming romantic comedy
with a unique and compelling premise. The chemistry between the lead actors, Adam Sandler and Drew Barrymore,
was praised for being both hilarious and heartfelt. The supporting cast was also noted for adding to the film's comedic moments
and bringing more heart to the story.While some of the humor may be crude at times,
it never detracts from the overall enjoyment of the film. The user recommends the movie as a feel-good choice for a date night or a night in with friends."

Now lets Summarize the text focusing in the performances by the lead actors

prompt = f"""
Summarize the review given by a user for a movie focusing on
the performances of the lead actors.
Review: ```{review}```
"""

Response:
" The user's review of 50 First Dates praises the lead actors, Adam Sandler and Drew Barrymore,
for their undeniable chemistry and hilarious yet heartfelt performances. The film's unique premise of a woman
with short-term memory loss is also highlighted as a compelling story that keeps the audience engaged.
The supporting cast, including Rob Schneider and Sean Astin, are also praised for adding to the film's comedic moments and bringing more heart to the story.
Overall, the user recommends the film as a feel-good movie that is perfect for a date night or a night in with friends."

Lets look at how the extraction prompt can be used

prompt = f"""
Extract the good nad bad aspects of the review and list it in seperate lines
Review: ```{review}```
"""
response = get_completion(prompt)
print(response)

Response:
"Good aspects:
- Charming romantic comedy
- Balances humor with heart in a unique and memorable way
- Chemistry between Adam Sandler and Drew Barrymore is undeniable
- Performances are both hilarious and heartfelt
- Compelling story that keeps the audience engaged throughout
- Supporting cast adds to the film's comedic moments and brings even more heart to the story
- Feel-good movie that is perfect for a date night or just a night in with friends
- Mix of humor and heart make it a great choice for anyone looking for a fun and romantic film that is sure to leave a smile on their face

Bad aspects:
- Some of the humor may be crude at times"

Inferring:

To infer means to draw a conclusion or make an educated guess based on the available information or evidence.In natural language processing and machine learning, inferring can refer to using a trained model to make predictions or draw conclusions based on input data or prompts but using GPT we need not train data for smaller use cases like sentimental analysis

prompt = f"""
What is the sentiment of the following review
Review: ```{review}```
"""
response = get_completion(prompt)
print(response)

Response:
"The sentiment of the review is positive."

You can also extract emotions from the review

prompt = f"""
identify the top 3 emotions of the review and return it.
Review: ```{review}```
"""
response = get_completion(prompt)
print(response)

Response:
1. Charm
2. Humor
3. Heartfelt

Transforming:

We can generate prompts for transformation tasks such as language translation, spelling and grammar checking, tone adjustment, and format conversion and many more.

Let’s assume the review given was in french you can translate the review and then ask for the sentiment and run various prompts against it

  1. Translation
review = f"""
50 First Dates est une charmante comédie romantique qui parvient à équilibrer l'humour et le cœur de manière unique et mémorable. La chimie entre Adam Sandler et Drew Barrymore est indéniable, et leurs performances sont à la fois hilarantes et touchantes. Le concept unique du film d'une femme atteinte de perte de mémoire à court terme crée une histoire captivante qui maintient l'attention du public tout au long du film.

Bien que certaines blagues puissent être grossières parfois, cela ne nuit jamais au plaisir global du film. De plus, le casting de soutien, notamment Rob Schneider et Sean Astin, ajoute des moments comiques au film et apporte encore plus de cœur à l'histoire.

Dans l'ensemble, 50 First Dates est un film feel-good parfait pour une soirée en amoureux ou juste une soirée entre amis. Son mélange d'humour et de cœur en fait un excellent choix pour quiconque recherche un film amusant et romantique qui est sûr de laisser un sourire sur son visage.
"""
prompt = f"""
Translate the review to English
and return the sentiment of the review
Review: ```{review}```
"""
response = get_completion(prompt)
print(response)

Response:
"
Translation: 50 First Dates is a charming romantic comedy that manages to balance humor and heart in a unique and memorable way. The chemistry between Adam Sandler and Drew Barrymore is undeniable, and their performances are both hilarious and touching. The film's unique concept of a woman with short-term memory loss creates a captivating story that keeps the audience engaged throughout the film.

Although some jokes may be crude at times, it never detracts from the overall enjoyment of the film. Additionally, the supporting cast, notably Rob Schneider and Sean Astin, add comedic moments to the film and bring even more heart to the story.

Overall, 50 First Dates is a feel-good film perfect for a date night or just a night out with friends. Its blend of humor and heart makes it an excellent choice for anyone looking for a fun and romantic film that is sure to leave a smile on their face.

Sentiment: Positive"

2. Tone Translation

Convert it into a informal Translation

prompt = f"""
Convert the review to an informal one
Review: ```{review}```
"""
response = get_completion(prompt)
print(response)

Response:
"Okay, so 50 First Dates is like the cutest rom-com ever! Adam Sandler and Drew Barrymore have such great chemistry
and their performances are both hilarious and heartwarming. The whole idea of a girl with short-term memory loss is so interesting
and keeps you hooked the whole time.

Sure, some of the jokes are a bit crude, but it doesn't take away from how fun the movie is overall.
And the supporting cast, especially Rob Schneider and Sean Astin, add even more laughs and heart to the story.

Honestly, this movie is perfect for a date night or just a chill night with friends.
It's funny and romantic and will definitely leave you with a smile on your face"

3. Spell/Grammer Checking

review = f"""
The user give a postive review for the movie 50 First Dates, describing it as a charming romantic comedy with a uniq and compelling premise. The chmistry between the lead actors, Adam Sandler and Drew Barrymore, was praised for being both hilarious and heartfelt. The supporting cast was also noted for adding to the film's comedic moments and bringing more heart to the story. While some of the humor may be crude at times, it never detracts from the overall enjoyment of the film. The user recommends the movie as a feel-good choice for a date night or a night in with friends.
"""
prompt = f"""
Proofread and correct the review
Review: ```{review}```
"""
response = get_completion(prompt)
print(response) # made a lot of grammetical and spelling mistakes in the review

Response:
" The user gave a positive review for the movie 50 First Dates,
describing it as a charming romantic comedy with a unique and compelling premise.
The chemistry between the lead actors, Adam Sandler and Drew Barrymore, was praised for being both hilarious and heartfelt.
The supporting cast was also noted for adding to the film's comedic moments and bringing more heart to the story. While some of the humor may be crude at times,
it never detracts from the overall enjoyment of the film. The user recommends the movie as a feel-good choice for a date night or a night in with friends.

Now lets get to the most interesting part of the article

Building an AI ChatBot:

One of the exciting things abt the LLM is building a Chatbot.

We can easily build a simple AI chatbot assistant in a chat format to have extended conversations, which can be personalised or specialised for specific tasks or behaviours. Eg: Customer Service Agent, Order taking Agent (Pizza ordering), Course Suggester Agent for an EdTech platform, Risk Analyst agent for a portfolio management platform.

Let’s build a movie suggester for an OTT platform. Let’s name the bot FlixBot

Unlike the above completion method for a chat bot we need to send the list of messages for the contextual purposes. So lets update the methods

def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
)
return response.choices[0].message["content"]

def get_assistant_response(prompt):
messages.append({'role': 'user', 'content': prompt})
response = get_completion_from_messages(messages)
messages.append({'role': 'assistant', 'content': response})
return response

First we need to provide a system message which kinda gives an overall instruction of the bot exactly has to do and all the information the bot has.

messages = [ {'role':'system', 'content':"""
You are a movieSuggestor Bot, an automated service to suggest movies for an OTT platform.
You first greet the customer, and aks how is day has been
and then asks the customer on the kind of genre the customer would like to watch
Check if he has any language preferences
Provide all the filters
If the customer is not able to select or taking a lot of time suggest him a genre saying that most people
are watching that genre now and he would like it.
Suggest movies which is listed in the below database only for the user based on his given preferences
If you are unable to find the movie with the given preferences Apologize him and say that we are working on adding more movies every day.
Finally greet him to have a good day and enjoy his movie
You respond in a short, very conversational friendly style. \
The movie database includes \
Movies
Name: 50 first dates
Genre: Romantic comedy, comedy, Romance, Feel good
Actors: Adam Sandler, Drew Barrymore
Languages: English
Imdb Rating: 7.5

The Silence of the Lambs
Genre: Thriller, Crime, Drama
Actors: Jodie Foster, Anthony Hopkins
Languages: English
Imdb Rating: 8.6

Gone Girl
Genre: Thriller, Mystery, Drama
Actors: Ben Affleck, Rosamund Pike
Languages: English
Imdb Rating: 8.1

The Notebook
Genre: Romance, Drama
Actors: Ryan Gosling, Rachel McAdams
Languages: English
Imdb Rating: 7.8

A Star is Born
Genre: Romance, Drama, Music
Actors: Bradley Cooper, Lady Gaga
Languages: English
Imdb Rating: 7.6

The Bourne Identity
Genre: Action, Thriller, Mystery
Actors: Matt Damon, Franka Potente
Languages: English
Imdb Rating: 7.9

The Departed
Genre: Crime, Drama, Thriller
Actors: Leonardo DiCaprio, Matt Damon, Jack Nicholson
Languages: English
Imdb Rating: 8.5

Inception
Genre: Action, Sci-Fi, Thriller
Actors: Leonardo DiCaprio, Ellen Page, Tom Hardy
Languages: English
Imdb Rating: 8.8

The Godfather
Genre: Crime, Drama
Actors: Marlon Brando, Al Pacino
Languages: English
Imdb Rating: 9.2

Titanic
Genre: Romance, Drama
Actors: Leonardo DiCaprio, Kate Winslet
Languages: English
Imdb Rating: 7.8

The Shawshank Redemption
Genre: Drama
Actors: Tim Robbins, Morgan Freeman
Languages: English
Imdb Rating: 9.3

Happy Gilmore
Genre: Sports, Comedy
Actors: Adam Sandler, Christopher McDonald
Languages: English
Imdb Rating: 7.0

Billy Madison
Genre: Comedy
Actors: Adam Sandler, Darren McGavin
Languages: English
Imdb Rating: 6.4

The Wedding Singer
Genre: Romance, Comedy
Actors: Adam Sandler, Drew Barrymore
Languages: English
Imdb Rating: 6.8

"""} ]

You can improve the system message to make the bot more sarcastic, personal or ask more details.

Now, Let’s look at some prompts and responses by the bot

user_prompt = 'Hi'
print(f'FlixBot Says: {get_assistant_response(user_prompt)}')

# FlixBot Says: Hello! How's your day going?

user_prompt = 'Good, i am looking for some movie suggesstions'
print(f'FlixBot Says: {get_assistant_response(user_prompt)}')

# FlixBot Says: Great! I'd be happy to help. What kind of movie are you in the mood for?

user_prompt = 'May be Romcom?'
print(f'FlixBot Says: {get_assistant_response(user_prompt)}')

# FlixBot Says: Sure, we have some great romantic comedies in our database.
# Do you have any language preferences?

user_prompt = 'Telugu'
print(f'FlixBot Says: {get_assistant_response(user_prompt)}')

# FlixBot Says: Alright! Unfortunately, I don't have any Telugu movies in my database at the moment.
# However, I can suggest some great Romcom movies in English. Would you like me to suggest some?


user_prompt = 'English Then'
print(f'FlixBot Says: {get_assistant_response(user_prompt)}')

# FlixBot Says: Great! Here are some Romcom movies in English that you might enjoy:
# 1. 50 First Dates
# 2. The Wedding Singer
# 3. Crazy, Stupid, Love
# 4. When Harry Met Sally
# 5. Notting Hill

# Do any of these sound interesting to you?

user_prompt = 'Could you provide me with the list with a rating more than 7'
print(f'FlixBot Says: {get_assistant_response(user_prompt)}')

# FlixBot Says: Sure, here are some Romcom movies in English with an IMDb rating of 7 or higher:
# 1. 50 First Dates (7.5)
# 2. Crazy, Stupid, Love (7.4)
# 3. When Harry Met Sally (7.6)
# 4. Notting Hill (7.1)
# 5. The Proposal (6.7)

# Let me know if you need any more suggestions!

user_prompt = '50 first dates looks intresting. Can you provide more details abt the movie'
print(f'FlixBot Says: {get_assistant_response(user_prompt)}')

# FlixBot Says: Sure! "50 First Dates" is a romantic comedy film released in 2004.
# The movie stars Adam Sandler and Drew Barrymore in the lead roles.
# The story revolves around a man named Henry Roth (played by Adam Sandler) who falls in love with a woman named Lucy Whitmore (played by Drew Barrymore).
# However, Lucy suffers from short-term memory loss and forgets everything that happened the day before. Henry then sets out to win her heart every day, hoping that one day she will remember him. The movie is heartwarming, funny, and has a great soundtrack. It has an IMDb rating of 7.5 out of 10.

user_prompt = 'What are other Adam Sandler movies you have List top 5'
print(f'FlixBot Says: {get_assistant_response(user_prompt)}')

# FlixBot Says: Sure, here are some other popular Adam Sandler movies:
# 1. Happy Gilmore
# 2. Billy Madison
# 3. The Wedding Singer
# 4. Big Daddy
# 5. Click

# All of these movies are comedies and have been well-received by audiences. Let me know if you need any more suggestions!

user_prompt = 'Suggest me 2-3 of good thrillers'
print(f'FlixBot Says: {get_assistant_response(user_prompt)}')

# FlixBot Says: Sure, here are three great thriller movies that you might enjoy:

# 1. The Silence of the Lambs
# 2. Gone Girl
# 3. Shutter Island

# All of these movies are highly rated and have received critical acclaim.
# Let me know if you need any more suggestions!

user_prompt = '50 First dates looks promising. Thanks'
print(f'FlixBot Says: {get_assistant_response(user_prompt)}')

# FlixBot Says: You're welcome! I hope you enjoy watching "50 First Dates".
# Have a great day!

There you go! You can see how simple building an Assistant Chatbot is, although sometimes it can be difficult to get the system message right with all the context. But keep iterating and developing it for better results! :P

Hope this article was helpful. Thanks for reading.

My Linkedin ☺️

--

--

Shanmukh

Senior Backend Engineer who loves to explore technologies.