Prompt Engineering 101
Now that you have a basic understanding of Artificial Intelligence, it’s time to put that knowledge to use! This document will cover basic terms, tips & tricks, and best practices to optimize your interactions with generative AI tools.
What is a prompt?
A prompt, as it relates to generative artificial intelligence (AI) tools, refers to a question or request given to a computer program to generate a desired response. For example, you may ask a generative AI tool, such as ChatGPT, to sort a data set in alphabetical order or translate an essay into a different language. The possibilities are endless, but understanding the basics of prompt engineering will help you get the best results.
Here are a few key terms to know when working with generative AI programs:
Input: The prompt you give to a generative AI tool
Output: The response generated by a generative AI tool
What is prompt engineering?
Prompt engineering refers to the process of crafting a prompt to generate a specific response from a generative AI tool.
Here’s an example of input/output in action:
Input: Write a statement about cats.
Output: Cats are known for their independent and enigmatic nature, making them fascinating and beloved companions.
Now that we know what a basic prompt looks like, let’s dive into some specifics so we can learn how to better engineer prompts for our needs.
The elements of a prompt
A prompt contains any (but not necessarily all) of the following elements:
Instruction: A clear and specific task you want the model to do, like answering a question or providing information.
Context: Additional information that can help guide the model to better responses, making sure it fully understands the situation.
Input Data: The information or question you provide.
Output Indicator: How you want the output to be formatted, such as a paragraph or list.
How do I write an effective prompt?
Here are some things to keep in mind as you write a prompt:
Keep it simple: Start your prompts as basic and concise as possible. You’ll be able to iterate on each response and add more context to get better results.
Set a format: Let the model know if you want to receive a response in a preferred style or format, such as a bulleted list or in the style of a poem.
Be specific: Be as clear and direct as possible with your prompt so that the model can understand the full context of what you’re asking.
Using the example from above, let’s say we didn’t get the exact result that we wanted. So, let’s keep it simple and specific and see how the results change.
Input: In the style of Lewis Carroll, write 2-3 sentences about why cats are awesome.
Output: In the whimsical world of feline enchantment, cats reign supreme with their tales of wonder. With graceful poise and tails of mystery, these enchanting creatures beguile us with their purrs and whiskers' history. Like Cheshire's grin, they appear and disappear, leaving behind a trail of fascination and cheer.
The Power of Iteration
If you’ve done all of the above and you’re not satisfied with the results, you can iterate on the model’s output. Iteration is a way of experimenting with different prompts to improve upon the desired output. This helps to alleviate the pressure of crafting the perfect input right off the bat. There are a few ways to iterate:
Use the built-in options to have the model modify its output using the same prompt. These usually look like “refine response,” “modify response,” etc.
Refine your prompt or ask follow-up questions. As the chatbot offers its output, you can continue refining its response by guiding it to your desired output. Here is an example of iteration using ChatGPT.
Use any of the best practices listed below (or a combination of them) to achieve your desired output.
Prompt Engineering Best Practices:
Break down your prompt
When inputting a complex prompt, break it down into step-by-step instructions or subtasks and provide a list of items you’d like to see in the response.
Original complex prompt: "Explain the process of photosynthesis, including the role of chlorophyll, sunlight, and carbon dioxide in the conversion of light energy into chemical energy by plants."
Prompt broken down into step-by-step instructions:
"Explain what photosynthesis is and its overall purpose."
"Describe the role of chlorophyll in the process."
"Explain how sunlight is involved in photosynthesis."
"Describe the role of carbon dioxide in this process."
"Summarize how plants convert light energy into chemical energy during photosynthesis."
Use delimiters or labels to separate sections of your prompt
Delimiters are symbols or characters used to define boundaries or separate different parts of text. Using delimiters helps the AI tool understand the structure of the user’s input. For example, perhaps your input contains a passage that you would like the chatbot to summarize. You can use delimiters to clarify exactly which parts of the input the chatbot should summarize.
Original prompt: Summarize this passage: In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to eat: it was a hobbit-hole, and that means comfort.
Prompt with delimiters:
Summarize this passage:
“””
In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to eat: it was a hobbit-hole, and that means comfort.
“””Prompt with labels:
[instructions] Summarize the following passage:
[passage] In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to eat: it was a hobbit-hole, and that means comfort.
[summary]
Ask to “think step-by-step”
When you don't know the subtasks well yourself (ex. you're asking a generative AI tool to solve a problem), you can significantly improve the reasoning and accuracy of the response by including “think step-by-step” in your prompt.
Original prompt: “Provide a solution to address the issue of declining bee populations and their impact on agriculture.”
Prompt asking to “think step-by-step:” "Think step-by-step and propose a solution to tackle the problem of declining bee populations and its impact on agriculture. Start by identifying the key factors contributing to the decline in bee populations, then outline a plan to address these factors. Break down the solution into clear steps, considering aspects such as habitat preservation, pesticide use, and community involvement. Explain how each step contributes to mitigating the issue and supporting bee populations for the benefit of agriculture."
Limit response length
You can ask for a specific response length in your prompt. For instance, you can request that your output is limited to a single sentence or 50 words.
Original prompt: “Describe the plot of the 1995 movie Toy Story.”
Prompt with limited response length: “In 50 words, describe the plot of the 1995 movie Toy Story.”
Use commands
Start your prompt with a command such as "Write," "Rephrase," "Summarize," etc.
Original prompt: What are some interesting facts about birds?
Prompt using commands: “List interesting facts about birds. Include details on their diverse species, unique adaptations, and significance in ecosystems.”
Request “No talk; just go”
Include the phrase “No talk; just go” in your prompt to keep the response concise and simple.
"Describe the life cycle of a butterfly. No talk; just go. Provide a succinct overview, including the stages of metamorphosis, key adaptations, and the importance of butterflies in pollination. Keep the information clear and straightforward without additional explanations or details."
Assign a persona
Ask the generative AI tool to assume the identity or persona of a particular profession, person, or character (farmer, Bob Ross, Cookie Monster).
"You are a wise and ancient wizard named Merlin. Imagine guiding a young adventurer seeking your counsel. What wisdom would Merlin impart, and what magical tales would he share?"
"You are a chef specializing in Italian cuisine. Share your expertise by suggesting a three-course Italian meal for a special occasion. Consider appetizers, main courses, and desserts, providing details on ingredients, cooking techniques, and presentation to create a memorable dining experience."
Avoiding hallucinations
What is a hallucination?
In the context of AI, hallucinations refer to generated outputs that contain inaccuracies, fabrications, or distortion.
In general, a generative AI tool will try, very literally, to do exactly what you ask it to do. So, if you ask it a question and it doesn't know the answer, it assumes that providing any answer—even if it is false—is more helpful than not providing an answer at all. In other words, generative AI tools don't have the implicit social context that humans have, that lying is worse than saying "I don't know."
Here’s an example of a hallucination:
Input: In which episode of Gilmore Girls does Rory adopt a cat?
Output: Rory adopts a cat in Season 2, Episode 7 of Gilmore Girls, titled "Like Mother, Like Daughter." In this episode, Rory adopts a stray cat that she finds outside her dormitory at Yale. She names the cat "Paul Anka," after the singer, and takes care of him throughout the series.
This is an example of a hallucination because the model fabricated the following:
Rory Gilmore never adopts a cat.
In Season 2, Episode 7, Rory is still in high school. She does not move into a college dormitory until Season 4.
There is a pet named Paul Anka, but he is a dog that is adopted by Rory’s mother Lorelai later in the series.
How can I avoid hallucinations?
Use “I don’t know”: Explicitly tell the generative AI tool to respond with "I don't know" if it doesn't know the answer to your question.
Request sources: If you require information supported by research, explicitly ask for the response to include sources or references. Sometimes a generative AI tool will provide a fake URL or dead link, so always double-check the sources in its response.