An Introduction to Prompt Engineering
Generative Artificial Intelligence systems (GenAI) are artificial intelligence systems that can generate content for users. In late 2022 and early 2023 there was an explosion of popularity in the usage of GenAI. A common interface for using GenAI systems is text inputs; many GenAI chatbots exist, such as OpenAI's ChatGPT, Microsoft's Copilot and Google's Gemini.
While users can give GenAI chatbots "prompts" - our text inputs and instructions - without much thought and get results, users can get better results if they use what is known as "prompt engineering". Below are my notes that I have gathered on prompt engineering.
What is Prompt Engineering?
Prompt engineering is the design and optimization of prompts to give better results. Users can provide feedback and respond to prompts, which leads to a cycle of prompting, seeing a response, responding to or critiquing a prompt and then receiving a new response.
Overall, we should provide as clear, precise and specific prompts as possible. We should make each task we want the GenAI to do as separate as possible. We should be cautious to not overspecify our prompts; if we do then the GenAI may ignore parts of a prompt or be too restricted to be creative.
Prompt Elements / Attributes
A good prompt will include several different elements or attributes. These are extra pieces of information that one tells the GenAI in order to tailor its output. The following attributes can be used in combination with each other to build a prompt:
- Objective: What do we want the Generative AI to do?
- Persona / Role: Tell the GenAI its role and from what persona it should tackle its objective. e.g. program manager, storyteller, a school teacher.
- Audience: Who will receive your prompt? What are their attributes, such as their age, interests or education?
- Context: In what context will the output be used in?
- Format: What format do we want the output in? A list, newspaper article, haiku, multiple-choice question, etc.?
- Domain: What area do we want the GenAI to focus on?
- Perspective: Do we want the GenAI to have a positive, neutral or negative perspective on the topic?
- Tone: What tone do we want the output in? e.g. authoritative, friendly etc.
Please be aware that the above is not an exhaustive list of attributes that one may pass a GenAI chatbot.
Using some of these we can build a better prompt. Let's say we want to generate a factsheet about apples for children aged 5. We could just use the following prompt:
Tell me about Apples
Here is one of the five facts the GenAI (ChatGPT) responded with, which isn't too useful for our goal:
Diverse Varieties: Apples (Malus domestica) come in thousands of varieties, each with its own flavor, texture, and appearance. Some popular varieties include Granny Smith, Fuji, Gala, and Red Delicious, each offering a unique taste profile.
We could be more precise and descriptive on what we want, using attributes:
You are a teacher designing a 5 point factsheet to be used in primary schools for children aged 5. Create a bullet-point list of fun facts about apples in a friendly tone. The word count should be no more than 200 words.
When I used the second prompt I received the following response:
Apple Variety Fun: Did you know there are over 7,500 different types of apples? Wow!
Nutritious Crunch: Apples are super healthy! They have vitamins to help us grow big and strong, and they're packed with fiber to keep our tummies happy.
An Apple a Day: Eating apples can keep the doctor away! They help us stay healthy and feel great.
Apple Adventures: Apples are perfect for all sorts of fun! We can munch them, crunch them, bake them into yummy pies, or even make tasty applesauce.
Apple Trees and Friends: Apples grow on trees! These special trees need sunshine, water, and love to grow big, juicy apples for us to enjoy. They're like nature's candy store!
As you can see, using the attributes really influenced the output and the response brings us much closer to our goal.
Generative Art Specific Attributes
If we are building prompts for generative art there are some more specific attributes we should consider:
- Art Style: Oil painting, cartoon, etc.
- Mood: Sad, inspiring, happy, etc.
- Genre: Fantasy, sci-fi, etc.
- Composition: Rule of thirds, golden ratio etc.
- Subject: focus, foreground, background etc.
- Colour schemes: Pastel colours, monochrome, etc.
Types of Prompts
Shot-Based Prompts
Shot-based prompts are where we give the GenAI examples of the output we want.
- Zero-Shot: Give a task with no examples. e.g. "Translate Hello from English to German"
- One-Shot: Give a task with a single example of the output. e.g. "Translating Hello from English to German would result in Hallo. Translate Goodbye."
- Many-Shot: Give a task with several examples. e.g. "Translating 'Hello' from English to German would result in Hallo. Translating 'How are you?' would result in 'Wie geht's?' Translate 'My name is Jamie'."
Prompt Chaining
Prompt chaining is where one takes a task and breaks it into subtasks. Once we have broken a task into subtasks, we use the first subtask as the initial prompt.
The response to the first prompt is used as the next input, which would correspond to the second subtask. We would repeat the same process with the second output for the third subtask, and so on until we have worked through all our subtasks.
One can imagine this as forming a chain of prompts, hence the name prompt chaining:
Subtask / Prompt 1 -- Output --> Subtask 2 -- Output --> ... --
Output --> Subtask N.
Chain of Thought Prompts
Chain of thought prompts allow the GenAI chatbot to explain it's reasoning. This is usually used with one-shot or many-shot prompting, where a user would provide an example of the working out of a simplified version of the problem they are trying to solve before setting the GenAI the more complex task.
It has also been found to be effective to begin your prompt with "Let's think about this step-by-step:"1 if you want the response to include a chain of thought response.
Sources
- Kojima, Takeshi; Shixiang Shane Gu; Reid, Machel; Matsuo, Yutaka; Iwasawa, Yusuke (2022). "Large Language Models are Zero-Shot Reasoners".