Prompt Writing
Welcome to our introductory course on prompt engineering!
Prompt engineering (PE) is the process of communicating effectively with an AI to achieve desired results. As AI technology continues to rapidly advance, the ability to master prompt engineering has become a particularly valuable skill. Prompt engineering techniques can be applied to a wide variety of tasks, making it a useful tool for anyone seeking to improve their efficiency in both everyday and innovative activities.
This course is tailored to beginners, making it the perfect starting point if you're new to AI and PE. However, even if you're not a beginner, you'll still find valuable insights within this course. This course is the most comprehensive prompt engineering course available, and the content ranges from an introduction to AI to advanced PE techniques.
Chapters
Below is a brief overview of each chapter:
Basics: Introduction to prompt engineering and fundamental techniques
Basic Applications: Simple, practical applications of prompt engineering
Intermediate: Research-based PE techniques with moderate complexity
Applied Prompting: Comprehensive PE process walkthroughs contributed by community members
Advanced Applications: Powerful, and more complex applications of prompt engineering
Reliability: Enhancing the reliability of large language models (LLMs)
Images: Prompt engineering for text-to-image models, such as DALLE and Stable Diffusion
Prompt Injection: Hacking, but for prompt engineering
Tooling: A review of various prompt engineering tools and IDEs
Prompt Tuning: Refining prompts using gradient-based techniques
Miscellaneous: A collection of additional topics and techniques related to prompt engineering
This chapter introduces simple prompting techniques as well as terminology. In order to understand prompting/prompt engineering, you first need to understand some very basic AI concepts.
What is AI?
For our purposes, artificial intelligence (AI) is a field in which people have created "smart" algorithms that "think" like humans. These algorithms can write essays, solve math problems, and create art. Recent advances in the field have created AIs so advanced that they can write convincing sales emails, news articles, and even win art competitions.
AI is currently revolutionizing many industries, including journalism, healthcare, and education. You will likely see it deployed in your workplace/classroom soon if you have not already. Although AI is extremely useful, it requires humans to direct it on what to do. Often AIs are like very smart five-year-old. They can do a lot of things, but they need careful instruction to do them well.
Being able to properly direct AIs is a very powerful skill. So, how do you communicate with them? The AIs this course discusses understand English (and other languages), so we can usually just describe tasks that we want done. For example, we can tell an AI to Write an essay about the benefits of AI and it would say.
In recent years, Artificial Intelligence (AI) has become a hot topic among the tech world. With its potential to revolutionize many aspects of our lives, AI is becoming increasingly relevant as technology continues to develop. From its applications in healthcare and transportation to its use in education and manufacturing, AI has already shown us its immense potential to benefit society in multiple ways.
The most obvious benefit of AI is its ability to automate mundane tasks. AI systems can be used to automate large-scale operations, such as data analysis, customer service, and even the production of goods. This frees up human labor to focus on complex, creative tasks that require more human intelligence and ingenuity. AI can also be used to analyze large amounts of data and make decisions in a fraction of the time it would take a human. This can be beneficial in industries such as finance, where AI can detect patterns in the stock market and offer insights that would otherwise go unnoticed.
AI can also be used to make predictions and generate insights that can be used to improve efficiency and accuracy. For instance, AI can be used to predict customer trends and preferences, helping businesses make better decisions and increase profits. AI can also be used to identify potential risks and opportunities in various industries, such as finance, healthcare, and manufacturing. This can help companies make informed decisions and reduce the risk of costly mistakes.
Finally, AI can be used to improve our understanding of the world. AI can be used to analyze large datasets and generate insights that can be used to better understand the world around us. This can be used to help us better understand the impacts of climate change, develop new treatments for diseases, and even predict events such as earthquakes and floods.
Overall, AI has the potential to revolutionize many aspects of our lives. From automating mundane tasks to generating insights and predictions, AI can be used to improve efficiency, accuracy, and our understanding of the world. As technology continues to develop, AI will become increasingly relevant and its potential to benefit society will only grow.
Why should I care?
AI can be used to automate tasks that you spend countless hours doing right now. We mentioned several examples above, but you can also consider any slightly repetitive task you do. It could be writing emails, writing reports, or even writing code. If you can describe the task to an AI, it can likely either do it for you or at least give you a starting point.
How do I get started?
Read the rest of this chapter, then check out other chapters that interest you. In particular, the applied prompting section may be of interest if you would like to see how professionals use AI to automate their work. You can experiment with AIs using resources like Playground, other IDEs.
Before reading the next article, it is important to note that you don't need any technical background to do prompt engineering. Most of it is trial and error, and you can learn as you go.
Lets go through one more example of prompt.
Prompt:
Generate a comma separated list of 10 ice cream flavors:
Output:
Chocolate, Vanilla, Strawberry, Mint Chip, Rocky Road, Cookie Dough, Butter Pecan, Neapolitan, Coffee, Coconut
In the previous chapter, we discussed AI and how humans can instruct AIs to perform tasks. The process of instructing an AI to do a task is called prompting. We tell the AI a set of instructions (the prompt) and it performs the task. Prompts can be as simple as a instruction/question, or as complex as huge chunks of text.
Here are two examples of prompts:
1) Summarizing an Article
Say you are reading about Filmcity opening in Noida. You want to quickly get the main ideas of the article, so you show the AI what you are reading, and ask for a summary
The opening of Filmcity in Noida, India is a major development for the film industry. The state-of-the-art facility offers multiple sound stages, production offices, post-production facilities, and outdoor shooting areas. With its world-class facilities and proximity to major film markets, the facility is expected to attract major productions from around the world. The development of Filmcity in Noida is also expected to create jobs in the film industry and support local businesses. The supportive and collaborative environment for filmmakers is essential for creating successful productions. In conclusion, the opening of Filmcity in Noida is a significant development for the Indian film industry and the local economy.
Summarize this paragraph in a single sentence:
Filmcity opening in Noida offers state-of-the-art facilities, including multiple sound stages, production offices, post-production facilities, and outdoor shooting areas, expected to attract major productions from around the world, create jobs, support local businesses, and provide a supportive and collaborative environment for filmmakers.
2) Math Word Problem Solving
If you have a dataset of mathematical equations that you would like a language model to solve, you can create a prompt by posing the question "What is EQUATION". For a given question, your full prompt could look like this:
What is 965*590?
For this prompt, GPT-3 (text-davinci-003) (an AI) sometimes answers 569,050 (incorrect). This is where prompt engineering comes in.
Prompt Engineering
If, instead of asking What is 965*590?, we ask Make sure your answer is exactly correct. What is 965*590? Make sure your answer is exactly correct:, GPT-3 will answer 569350 (correct). Why is this the case? Why is telling the AI twice to give a correct answer helpful? How can we create prompts that yield optimal results on our task? This last question, in particular, is the focus of the field of Prompt Engineering, as well as this course.
One of the simplest prompting methods is just giving instructions (sometimes called instruction prompting). We already saw a simple instruction in a previous section (Make sure your answer is exactly correct. What is 965*590? Make sure your answer is exactly correct:). However, modern AIs can follow much more complex instructions.
Below are the examples:
Prompt:
A user has input their first and last name into a form. We don't know in which order their first/last name is, but we need it to be in the format 'Last, First'. Convert the following:
Rahul Singh, Vijay Sharma
Output:
Singh, Rahul
Sharma, Vijay
The AI is easily able to solve this problem. We can also ask it to solve many other simple and more complex problems.
Example 2:
Here is a more complicated example:
Read the following sales email. Remove any personally identifiable information (PII), and replace it with the appropriate placeholder. For example, replace the name "John Doe" with "[NAME]".
Hi John,
I'm writing to you because I noticed you recently purchased a new car. I'm a salesperson at a local dealership (Cheap Dealz), and I wanted to let you know that we have a great deal on a new car. If you're interested, please let me know.
Thanks,
Jimmy Smith
Phone: 410-805-2345
Email: jimmysmith@cheapdealz.com
Here is the AI's response:
Hi [NAME],
I'm writing to you because I noticed you recently purchased a new car. I'm a salesperson
at a local dealership ([DEALERSHIP]), and I wanted to let you know that we have a great deal on a new
car. If you're interested, please let me know.
Thanks,
[SALESPERSON]
Phone: [PHONE NUMBER]
Email: [EMAIL ADDRESS]
It is very exciting to see how the model can extrapolate from the instructions. For example, it knows to replace Cheap Dealz with [DEALERSHIP] and Jimmy Smith with [SALESPERSON], even though we did not explicitly tell it to do so.
Another prompting technique is to assign a role to the AI. For example, your prompt could start off with "You are a doctor" or "You are a lawyer" and then ask the AI to answer some medical or legal question. Here is an example:
You are a brilliant mathematician who can solve any problem in the world.
Attempt to solve the following problem:
What is 100*100/400*56?
The answer is 1400.
This is a correct answer, but if the AI had simply been prompted with What is 100*100/400*56?, it would have answered 280 (incorrect). Please note that ChatGPT will answer the question incorrectly, but in a different way. When assigning a role to the AI, we are giving it some context. This context helps the AI understand the question better. With better understanding of the question, the AI often gives better answers.
Examples
You can find some more interesting prompts in the Awesome ChatGPT Prompts repository on GitHub. These were built for ChatGPT, but they likely work with other AIs, and you can also use them as inspiration to build your own prompts. Let's see two examples:
Act as an Etymologist
I want you to act as an etymologist. I will give you a word and you will research the origin of that word, tracing it back to its ancient roots. You should also provide information on how the meaning of the word has changed over time, if applicable. My first request is "I want to trace the origins of the word 'pizza'"
Act as an Absurdist
I want you to act as an absurdist. The absurdist's sentences are meaningless. The words used by an absurdist are completely ridiculous. The absurdist does not make commonplace sentences in any way. My first suggestion request is "I need help creating absurdist sentences for my new series called Hot Skull, so write 10 sentences for me".
Yet another prompting strategy is few shot prompting, which is basically just showing the model a few examples (called shots) of what you want it to do.
Example:
Prompt:
Great product, 10/10: positive, Didn't work very well: negative, Super helpful, worth it: positive It doesnt work!:
Output:
negative
Consider the above example, in which we are attempting to classify customer feedback as positive or negative. We show the model 3 examples of positive/negative feedback, then we show it a new piece of feedback that has not been classified yet (It doesnt work!:). The model sees that the first 3 examples were classified as either positive or negative, and uses this information to classify the new example as negative.
The way that we structure the examples is very important. Since we have structured these 3 examples as input: classification, the model outputs a single word after the final line, instead of saying a full sentence like this review is positive.
More on structure
A key use case for few shot prompting is when you need the output to be structured in a specific way that is difficult to describe to the model. To understand this, let's consider a relevant example: say you need to compile names and occupations of well known citizens in towns nearby by analyzing local newspaper articles. You would like the model to read each article and output a list of names and occupations in Last, First [OCCUPATION] format. In order to get the model to do this, you can show it a few examples:
Prompt:
In the bustling town of Emerald Hills, a diverse group of individuals made their mark. Sarah Martinez, a dedicated nurse, was known for her compassionate care at the local hospital. David Thompson, an innovative software engineer, worked tirelessly on groundbreaking projects that would revolutionize the tech industry. Meanwhile, Emily Nakamura, a talented artist and muralist, painted vibrant and thought-provoking pieces that adorned the walls of buildings and galleries alike. Lastly, Michael O'Connell, an ambitious entrepreneur, opened a unique, eco-friendly cafe that quickly became the town's favorite meeting spot. Each of these individuals contributed to the rich tapestry of the Emerald Hills community.
1. Sarah Martinez [NURSE]
2. David Thompson [SOFTWARE ENGINEER]
3. Emily Nakamura [ARTIST]
4. Michael O'Connell [ENTREPRENEUR]
At the heart of the town, Chef Oliver Hamilton has transformed the culinary scene with his farm-to-table restaurant, Green Plate. Oliver's dedication to sourcing local, organic ingredients has earned the establishment rave reviews from food critics and locals alike.
Just down the street, you'll find the Riverside Grove Library, where head librarian Elizabeth Chen has worked diligently to create a welcoming and inclusive space for all. Her efforts to expand the library's offerings and establish reading programs for children have had a significant impact on the town's literacy rates.
As you stroll through the charming town square, you'll be captivated by the beautiful murals adorning the walls. These masterpieces are the work of renowned artist, Isabella Torres, whose talent for capturing the essence of Riverside Grove has brought the town to life.
Riverside Grove's athletic achievements are also worth noting, thanks to former Olympic swimmer-turned-coach, Marcus Jenkins. Marcus has used his experience and passion to train the town's youth, leading the Riverside Grove Swim Team to several regional championships.
1. Oliver Hamilton [CHEF]
2. Elizabeth Chen [LIBRARIAN]
3. Isabella Torres [ARTIST]
4. Marcus Jenkins [COACH]
Oak Valley, a charming small town, is home to a remarkable trio of individuals whose skills and dedication have left a lasting impact on the community.
At the town's bustling farmer's market, you'll find Laura Simmons, a passionate organic farmer known for her delicious and sustainably grown produce. Her dedication to promoting healthy eating has inspired the town to embrace a more eco-conscious lifestyle.
In Oak Valley's community center, Kevin Alvarez, a skilled dance instructor, has brought the joy of movement to people of all ages. His inclusive dance classes have fostered a sense of unity and self-expression among residents, enriching the local arts scene.
Lastly, Rachel O'Connor, a tireless volunteer, dedicates her time to various charitable initiatives. Her commitment to improving the lives of others has been instrumental in creating a strong sense of community within Oak Valley.
Through their unique talents and unwavering dedication, Laura, Kevin, and Rachel have woven themselves into the fabric of Oak Valley, helping to create a vibrant and thriving small town.
Output:
1. Laura Simmons [FARMER]
2. Kevin Alvarez [DANCE INSTRUCTOR]
3. Rachel O'Connor [VOLUNTEER
Variants of shot prompting
The word "shot" is synonymous with "example". Aside from few-shot prompting, there are two other types of shot prompting that exist. Do not overthink this! The only difference between these variants is how many examples you show the model.
Variants:
-
0 shot prompting: no examples are shown to the model
-
1 shot prompting: 1 example is shown to the model
-
few shot prompting: 2+ examples are shown to the model
0-shot prompting
0-shot prompting is the most basic form of prompting. It is simply showing the model a prompt without examples and asking it to generate a response. As such, all of instruction and role prompts that you have seen so far are 0-shot prompts. An additional example of a 0-shot prompt is:
Add 2+2:
It is 0-shot since we have not shown the model any complete examples.
1-shot prompting
1-shot prompting is when you show the model a single example. For example, the 1-shot analogue of the 0-shot prompt Add 2+2: is:
Add 3+3: 6 Add 2+2:
We have shown the model only 1 complete example (Add 3+3: 6), so this is a 1-shot prompt.
Few-shot prompting
Few-shot prompting is when you show the model 2 or more examples. All prompts above this variants section have been few-shot prompts. The few-shot analogue of the above two prompts is:
Add 3+3: 6 Add 5+5: 10 Add 2+2:
This is the case since we have shown the model at least 2 complete examples (Add 3+3: 6 and Add 5+5: 10). Usually, the more examples you show the model, the better the output will be, so few-shot prompting is preferred over 0-shot and 1-shot prompting in most cases.
Conclusion
Few-shot prompting is a powerful technique for getting the model to produce accurate and properly formatted output!
Here we combine different technique, which we have used previously. Example
Role + Instructions + Example 1 + Example 2 ..
As we have seen in the previous pages, prompts can have varying formats and complexity. They can include context, instructions, and multiple input-output examples. However, thus far, we have only examined separate classes of prompts. Combining these different prompting techniques can lead to more powerful prompts.
Here is an example of a prompt that includes context, instructions, and multiple examples:
Twitter is a social media platform where users can post short messages called "tweets". Tweets can be positive or negative, and we would like to be able to classify tweets as positive or negative. Here are some examples of positive and negative tweets. Make sure to classify the last tweet correctly.
Q: Tweet: "What a beautiful day!" Is this tweet positive or negative?
A: positive
Q: Tweet: "I hate this class" Is this tweet positive or negative?
A: negative
Q: Tweet: "I love pockets on jeans"
A:
By adding additional context/examples, we can often improve the performance of AIs on different tasks.
We have now covered multiple types of prompts, as well as ways to combine them. This page will provide you with terms to explain different types of prompts. Although there have been approaches to formalize discourse around prompt engineering, the field is ever changing, so we will provide you with just enough information to get started.
Parts of a Prompt
There are a few different parts of a prompt that you will see over and over again. They are roughly:
-
A role
-
An instruction/task
-
A question
-
Context
-
Examples (few shot)
We have covered roles, instructions, and examples in previous pages. A question is simply a question! (E.g. what is the capital of France?). Context is any relevant information that you want the model to use when answering the question/performing the instruction.
Not all of these occur in every prompt, and when some do occur, there is no standard order for them. For example, the following two prompts, which each contain a role, an instruction, and context, will do roughly the same thing:
You are a doctor. Read this medical history and predict risks for the patient: January 1, 2000: Fractured right arm playing basketball. Treated with a cast. February 15, 2010: Diagnosed with hypertension. Prescribed lisinopril. September 10, 2015: Developed pneumonia. Treated with antibiotics and recovered fully. March 1, 2022: Sustained a concussion in a car accident. Admitted to the hospital and monitored for 24 hours.
January 1, 2000: Fractured right arm playing basketball. Treated with a cast. February 15, 2010: Diagnosed with hypertension. Prescribed lisinopril. September 10, 2015: Developed pneumonia. Treated with antibiotics and recovered fully. March 1, 2022: Sustained a concussion in a car accident. Admitted to the hospital and monitored for 24 hours. You are a doctor. Read this medical history and predict risks for the patient:
However, the second prompt is likely preferable since the instruction is the last part of the prompt. This is good since the LLM is less likely to simply write more context instead of following the instruction. For example, if given the first prompt, the LLM might add a new line: March 15, 2022: Follow-up appointment scheduled with neurologist to assess concussion recovery progress.
A "Standard" Prompt
We have heard of a few different formats of prompts thus far. Now, we will quickly jump back to the beginning and define a "standard" prompt. Following Kojima et al.2, we will refer to prompts that consist solely of a question as "standard" prompts. We also consider prompts that consist solely of a question that are in the QA format to be "standard" prompts.
Why should I care?
Many articles/papers that we reference use this term. We are defining it so we can discuss new types of prompts in contrast to standard prompts.
Two examples of standard prompts:
Standard Prompt
What is the capital of France?
Standard Prompt in QA format
Q: What is the capital of France? A:
Few Shot Standard Prompts
Few shot standard prompts are just standard prompts that have exemplars in them. Exemplars are examples of the task that the prompt is trying to solve, which are included in the prompt itself. In research, few shot standard prompts are sometimes referred to simply as standard prompts (though we attempt not to do so in this guide).
Two examples of few shot standard prompts:
Few Shot Standard Prompt
What is the capital of Spain?
Madrid
What is the capital of Italy?
Rome
What is the capital of France?
Few Shot Standard Prompt in QA format
Q: What is the capital of Spain?
A: Madrid
Q: What is the capital of Italy?
A: Rome
Q: What is the capital of France?
A:
Few shot prompts facilitate "few shot" AKA "in context" learning, which is the ability to learn without parameter updates.