Chapter 3: Intro to Prompt Engineering
-
What’s a Prompt?
You’re already familiar with ChatGPT, but let’s go deeper into the mechanics of your interactions: the prompts. Every message you send to ChatGPT is actually a prompt. Instead of thinking of a prompt as a standalone question or command, imagine it as your contribution to an ongoing dialogue with ChatGPT. This isn’t just a Q&A session; it’s more like a flowing conversation where each prompt serves a specific role.
Here are the core types of prompts you’ll use:
-
Questions: These are straightforward inquiries that seek a direct answer. For instance, “ChatGPT, how does photosynthesis work?” or “What is the square root of 64?”
-
Requests: This type is for when you need ChatGPT to perform a specific task. For example, “ChatGPT, summarize this chapter for me,” or “Create a list of renewable energy sources.”
-
Information: These prompts add extra context or clarification to the conversation. Maybe ChatGPT misunderstood a previous prompt, or perhaps you want to add more detail to get a better answer. For example, “I meant to summarize the chapter about World War I, not World War II,” or “By renewable energy, I’m referring to sources that are not fossil fuels.”
In a real-world chat with ChatGPT, you’ll often use a mix of these types of prompts. You could start with a question, then make a request based on ChatGPT’s answer, and provide additional information to clarify or steer the conversation.
Understanding these different types of prompts allows you to have a more dynamic and meaningful conversation with ChatGPT. With this knowledge, you’re better equipped to make ChatGPT a valuable tool for learning, creating, and exploring.
Building a Great Prompt: The CORE Framework
If you are just having a casual chat with ChatGPT, you can pretty much speak your mind freely in your prompts. However, when your goal is to ask ChatGPT to carry out a task and deliver satisfactory results, the prompt often needs to be more complex and structured.
Although there are many ways to write a prompt, some are more effective than others in getting a high-quality response. You may often hear others giving very different opinions on what ChatGPT can do: some people may tell you ChatGPT is awesome and can do great things, while other people may say ChatGPT is very confused and can’t get anything useful out of it. Most of the time, this difference is caused by the difference in the prompts they have used. ChatGPT is like a gold mine, and a well-written prompt is like a well-calibrated metal detector that allows you to find gold in it.
To help you get started, let me introduce a simple but powerful framework called CORE. It stands for Context, Objective, Requirement, and Example. Using these four elements, you will be able to create prompts that bring out the best in ChatGPT, giving you answers that are as awesome as you hoped they would be.
Context: Setting the Stage
Context is like the setting in a story. It tells ChatGPT the background details: who you are, what you are trying to do, and why you’re asking ChatGPT for help. Good context is like giving ChatGPT a backstage pass to your situation. Without it, ChatGPT might give you an answer that’s sort of like a one-size-fits-all t-shirt — kind of okay but not a great fit. For example, if you ask ChatGPT “How can I earn some coins?”, its answer will be very different depending on whether you are talking about “Super Mario” or real life.Usually, an easy way to describe the context is to start with “I”, such as “I’m a 6th-grade student, and I need to …”, “I am trying to …”, or “I need your help with …”. Alternatively, some people prefer to start with “You” (i.e. ChatGPT), such as “You are helping me with …”, or “You are talking to a 6-th grade student”.
Sometimes, you may need to provide additional information or reference to ChatGPT in the context. For example, if you need ChatGPT to help summarize an article, then you need to include that article in your prompt. Your prompt may look like this:
I am reading an article about the French Revolution: [insert the entire article] It is a bit too long. can you summarize it for me?
Examples of Using Context
- Math Homework Help:
- Less Effective Prompt: “Help me with fractions.”
- Effective Prompt: “I’m a middle school student struggling with my math homework on fractions. Can you help me?”
- Explanation: The effective prompt specifies that it’s a middle school student needing help with homework. This helps ChatGPT tailor the explanation to a middle school level, making it more accessible for the student.
- Science Fair Project on Renewable Energy:
- Less Effective Prompt: “How do solar panels work?”
- Effective Prompt: “I’m working on a science fair project about renewable energy, specifically focusing on solar panels. How do they work?”
- Explanation: This prompt sets the stage for a more educational and project-focused response, informing ChatGPT that the user isn’t just curious but has a specific academic goal.
- Writing a Poem for English Class:
- Less Effective Prompt: “How do I write a poem?”
- Effective Prompt: “I have an English class assignment to write a poem about nature. How do I get started?”
- Explanation: The effective prompt makes it clear that the task is for an English class assignment about nature, allowing ChatGPT to offer more targeted tips and suggestions.
- Learning to Play a New Instrument:
- Less Effective Prompt: “Tips for learning the guitar.”
- Effective Prompt: “I just joined my school’s music club and chose to learn the guitar. Any tips?”
- Explanation: The context specifies that the user is a beginner who has joined a music club. This helps ChatGPT provide tips that are suitable for someone who is new to playing the guitar and perhaps working in a group setting.
Each of these examples shows how a little bit of context goes a long way in getting a response that really fits what you need.
Objective: What’s the Mission?
Once you’ve set the stage with your context, it’s time to get down to business with the Objective. This is where you tell ChatGPT exactly what you want it to do for you. You’re essentially giving it a mission to complete. Without a clear objective, ChatGPT would have to guess what it needs to do, and sometimes may take the wrong action.For example, imagine you tell ChatGPT “I failed my math test today”. If you don’t say anything else, most likely ChatGPT will offer some advice on how to improve your math skills. However, if you say “I failed my math test today. How should I tell my parents about it?” you will get very different answers.
Usually, the objective should contain a verb that describes the action that ChatGPT needs to take, such as “explain”, “describe”, “list”, “design”, “summarize”, etc.
Examples of Great Objective Descriptions
- Math Homework Help:
- Less Effective Prompt: “I’m a middle school student struggling with my math homework on fractions. Help me.”
- Effective Prompt: “I’m a middle school student struggling with my math homework on fractions. Can you walk me through how to solve ( \frac{1}{2} + \frac{3}{4} )?”
- Explanation: The highlighted objective clearly asks for a walkthrough on solving a specific fraction problem. This directs ChatGPT to provide a step-by-step solution tailored for a middle school student.
- Science Fair Project on Renewable Energy:
- Less Effective Prompt: “I’m working on a science fair project about renewable energy, specifically focusing on solar panels. How do they work?”
- Effective Prompt: “I’m working on a science fair project about renewable energy, specifically focusing on solar panels. Please explain how they convert sunlight into electricity.”
- Explanation: The highlighted objective narrows down the query to ask how solar panels convert sunlight into electricity. This specificity guides ChatGPT to deliver a more focused explanation, ideal for a science fair project.
- Writing a Poem for English Class:
- Less Effective Prompt: “I have an English class assignment to write a poem about nature. How do I get started?”
- Effective Prompt: “I have an English class assignment to write a poem about nature. Please give me a template or outline to start my poem?”
- Explanation: By asking for a template or outline, the highlighted objective specifies the type of assistance needed. This helps ChatGPT provide a useful starting point for writing the poem.
- Learning to Play a New Instrument:
- Less Effective Prompt: “I just joined my school’s music club and chose to learn the guitar. Any tips?”
- Effective Prompt: “I just joined my school’s music club and chose to learn the guitar. Please list the basic chords I should start with.”
- Explanation: The highlighted objective asks for specific information on basic chords, directing ChatGPT to provide a beginner’s guide that’s useful for someone in a school music club.
By stating your objective clearly, you’re setting up ChatGPT for success, making it way more likely that you’ll get the answer you’re really after. Up next, we’ll add even more finesse to our prompts by introducing requirements and examples.
Requirement: The Nitty-Gritty Details
After you have laid out the Context and Objective in your prompt, ChatGPT would know what it needs to do. However, there are many different ways to carry out the same task, so we need to provide some requirements that specify how to do it.
There are many things you can include in your requirements. For example, you might specify the tone, such as “formal” or “casual,” or the length, like “detailed” or “brief.” You can even specify a limit on the number of words, though ChatGPT may not be able to strictly control its output word count. You could also ask for the answer to be formatted in a particular way, like a list of bullets or a paragraph. You may also tell ChatGPT what you don’t want in your requirements.
In many ways, the requirement is very similar to the rubrics your teacher gives you for some assignments. So imagine you are a teacher assigning a task to ChatGPT, then design your rubrics for ChatGPT to follow.
One commonly used technique in writing the requirement is to assign a “role” to ChatGPT. For example, you can say “Write the answer as an expert in …”, and ChatGPT will use the tone and level of details that are typically used by experts. On the other hand, if you say “Pretend you are 5 years old”, then ChatGPT’s response will look like written by a very young kid with very simple words and sentences.
Examples of Giving Requirements
- Math Homework Help:
- Less Effective Prompt: “I’m a middle school student struggling with my math homework on fractions. Can you walk me through how to solve ( \frac{1}{2} + \frac{3}{4} )?”
- Effective Prompt: “I’m a middle school student struggling with my math homework on fractions. Can you walk me through how to solve ( \frac{1}{2} + \frac{3}{4} )? Please use simple language and show all steps.”
- Explanation: The highlighted requirement specifies the language level and detail needed, ensuring that the explanation is both understandable and comprehensive for a middle school student.
- Science Fair Project on Renewable Energy:
- Less Effective Prompt: “I’m working on a science fair project about renewable energy, specifically focusing on solar panels. Please explain how they convert sunlight into electricity.”
- Effective Prompt: “I’m working on a science fair project about renewable energy, specifically focusing on solar panels. Please explain how they convert sunlight into electricity. Keep it concise but include the basic science behind it.”
- Explanation: The requirement narrows down the focus to a concise explanation while still including the basic science. This helps prevent ChatGPT from writing lengthy explanations.
- Writing a Poem for English Class:
- Less Effective Prompt: “I have an English class assignment to write a poem about nature. Please give me a template or outline to start my poem?”
- Effective Prompt: “I have an English class assignment to write a poem about nature. Please give me a template or outline to start my poem? Make sure to include at least three different poetic devices.”
- Explanation: The requirement specifies the inclusion of different poetic devices, guiding ChatGPT to offer a more educational and enriching starting point for the poem.
- Learning to Play a New Instrument:
- Less Effective Prompt: “I just joined my school’s music club and chose to learn the guitar. Please list the basic chords I should start with.”
- Effective Prompt: “I just joined my school’s music club and chose to learn the guitar. Please list the basic chords I should start with. Pretend you are an experienced music teacher.”
- Explanation: By asking ChatGPT to take on the role of an experienced music teacher, the highlighted requirement ensures that the guide will be intuitive and well-designed.
By being meticulous with your requirements, you’re fine-tuning the ChatGPT’s responses to fit like a glove. You get to be the director of how you want your information served.
Examples: Show and Tell
So, you’ve given ChatGPT the context, objective, and even the fine details of your requirements. What’s left? Examples! While this step is optional, it can be incredibly effective in getting the response you’re after. If you find ChatGPT’s answers are not what you expect, but you don’t know how to change the objective or requirements, then it is probably a good time to provide some examples directly.
Examples can illustrate the style, tone, or format you prefer. If you’ve ever heard the saying, “A picture is worth a thousand words,” well, in this case, an example can be worth a thousand guidelines. It’s like giving ChatGPT a mini “preview” of your ideal answer.
Examples of Providing Examples
- Math Homework Help:
- Less Effective Prompt: “I’m a middle school student struggling with my math homework on fractions. Can you walk me through how to solve ( \frac{1}{2} + \frac{3}{4} )? Please use simple language and show all steps.”
- Effective Prompt: “I’m a middle school student struggling with my math homework on fractions. Can you walk me through how to solve ( \frac{1}{2} + \frac{3}{4} )? Please use simple language and show all steps. For example: ( \frac{1}{3} + \frac{1}{4} = \frac{1 \times 4 + 3 \times 1}{3 \times 4} = \frac{4 + 3}{12} = \frac{7}{12} ).”
- Explanation: The example provides a sample walkthrough for adding fractions, setting the expectation for the style and format of the answer.
- Science Fair Project on Renewable Energy:
- Prompt: “I’m working on a science fair project about renewable energy, specifically focusing on solar panels. Please explain how they convert sunlight into electricity. Keep it concise but include the basic science behind it.”
- Explanation: For this prompt, it is not easy to provide an example since you would have to include an entire paragraph that explains another concept.
- Writing a Poem for English Class:
- Less Effective Prompt: “I have an English class assignment to write a poem about nature. Please give me a template or outline to start my poem? Make sure to include at least three different poetic devices.”
- Effective Prompt: “I have an English class assignment to write a poem about nature. Please give me a template or outline to start my poem? Make sure to include at least three different poetic devices. For example, start with a simile, include some alliteration, and end with a metaphor.”
- Explanation: The example outlines the structure and poetic devices desired, providing ChatGPT with a blueprint for crafting the template or outline.
- Learning to Play a New Instrument:
- Prompt: “I just joined my school’s music club and chose to learn the guitar. Please list the basic chords I should start with. Pretend you are an experienced music teacher.”
- Explanation: For this prompt, it is also not easy to provide an example, and it might not be necessary.
In short, providing an example is like giving ChatGPT a cheat sheet for acing your query. It’s your way to make sure you get the answer that fits your needs to a T.
With this, you’ve completed the C.O.R.E framework, and you’re all set to craft prompts that will make your interactions with ChatGPT as fruitful as possible.
Absolutely, a hands-on activity is a fantastic way to cement the understanding of the CORE framework. Here’s how you could set up this educational exercise:
Hands-On Activity: Fix that Prompt!
You’ve learned about the C.O.R.E framework — Context, Objective, Requirement, and Example. Now it’s time to put your knowledge to the test! Below are some less effective prompts that lack one or more elements of the CORE framework. Your mission is to figure out what’s missing or poorly written, and then rewrite the prompt to make it better.
For each case, you can try both the original and the improved versions on ChatGPT to see how they compare. Does your prompt yield a better answer? You can keep improving your prompt until you see a clear improvement.
You can use this chat project to run the tests:
https://play.creaticode.com/projects/6531b7e60fdce080a4481c1d
Prompts for Analysis and Improvement
- Prompt: “Tell me about the solar system.”
- Hints: Is the context clear? What’s the objective here? Any requirements or examples?
- Prompt: “I need to write a story for my class.”
- Hints: Who are you? What exactly do you need for your story? What should it look like?
- Prompt: “I am preparing for my Spanish exam. How do I improve?”
- Hints: Which area are you looking for improvement?
- Prompt: “I’m hosting a party. Give me a recipe for salads.”
- Hints: Any dietary restrictions? How detailed should it be?
Follow-Up Prompts: It’s a Conversation, Not a Monologue
You’ve got your CORE framework down, but let’s be real — nobody’s perfect. It’s rare to get exactly what you need from ChatGPT with just one prompt. And guess what? That’s totally okay! Working with ChatGPT is more like a conversation than a monologue. Sometimes, you need to clarify, adjust, or even backtrack to get to the answer you’re looking for.
The key to effective follow-up prompts is to identify what went wrong (or what could be better) with the initial response. Is ChatGPT missing some background information? Were your original requirements unclear? Did your example mislead the model? Once you spot the issue, your follow-up prompt can address it, refining ChatGPT’s understanding and output.
Don’t worry if you have to go through several iterations. The goal is to gradually narrow down to the most accurate and helpful response, and that usually takes a bit of back-and-forth.
Common Issues and How to Address Them Through Follow-Up Prompts
-
Missing Background Information
Explanation: Sometimes ChatGPT doesn’t have enough context to give a tailored response. This usually happens when the initial prompt is too vague or general. In such cases, you’ll likely get a broad or generic answer that might not meet your specific needs. The key is to pinpoint what additional information would make your question clearer and include that in your follow-up prompt.
- Example 1:
- Initial Prompt: “Tell me about photosynthesis.”
- Initial Response: “Photosynthesis is the process by which green plants and some other organisms use sunlight to synthesize foods with the help of chlorophyll pigments.”
- Follow-Up Prompt: “I’m interested in understanding how chlorophyll plays a role in photosynthesis. Could you elaborate?”
- Improved Response: “Absolutely! Chlorophyll is essential for photosynthesis in plants. It’s the molecule in plant cells that absorbs sunlight and uses its energy to convert CO2 and water into glucose and oxygen.”
- Example 2:
- Initial Prompt: “Explain the concept of renewable energy.”
- Initial Response: “Renewable energy is energy that is collected from resources that are naturally replenished on a human timescale, such as sunlight, wind, and rain.”
- Follow-Up Prompt: “Can you focus on how solar energy is harnessed and converted into electricity?”
- Improved Response: “Certainly! Solar energy is harnessed using photovoltaic cells commonly found in solar panels. These cells convert sunlight directly into electricity by the photovoltaic effect.”
- Example 1:
-
Misleading Descriptions
Explanation: At times, the words you use in your prompt might inadvertently guide ChatGPT down the wrong path. For example, asking for a “quick recipe” could still yield a complex dish if ChatGPT’s definition of “quick” differs from yours. It’s important to clarify any terms or phrases that could be interpreted in multiple ways.
- Example 1:
- Initial Prompt: “Give me a quick recipe.”
- Initial Response: “How about Beef Bourguignon? It’s a delightful French dish, although it takes about 2 hours to make.”
- Follow-Up Prompt: “I’m looking for something quick and easy to prepare. Do you have a recipe that takes less than 30 minutes?”
- Improved Response: “Of course! A quick and easy option would be a classic Spaghetti Aglio e Olio. You’ll need spaghetti, garlic, red pepper flakes, and olive oil, and it takes just about 20 minutes to prepare.”
- Example 2:
- Initial Prompt: “How do I become popular?”
- Initial Response: “Gaining popularity can involve a myriad of factors such as your social media presence, networking skills, and even your charisma.”
- Follow-Up Prompt: “I was referring to becoming well-liked among my local community members. Do you have any tips for that?”
- Improved Response: “Ah, I understand. In a local community setting, becoming well-liked often involves being actively involved in community events, being kind and approachable, and showing genuine interest in the well-being of others.”
- Example 1:
-
Unclear Requirements
Explanation: If you have specific requirements for how you’d like the information to be presented, it’s crucial to state them clearly. Whether you want a detailed explanation, a summarized list, or any other specific format, not stating this clearly can lead to getting a response that doesn’t meet your expectations. Always specify your requirements in your prompt or follow-up to get the answer you’re looking for.
- Example 1:
- Initial Prompt: “Write me a poem about the ocean.”
- Initial Response: “The ocean’s waves dance to and fro, a ballet of nature, don’t you know.”
- Follow-Up Prompt: “Could you write the poem in a sonnet form?”
- Improved Response: “The ocean’s waves in moonlight gleam so bright, a dance of liquid sapphire through the night. Each crest and trough an artist’s stroke, it seems, a living, breathing, ever-changing dream.”
- Example 2:
- Initial Prompt: “Tell me how to take care of a dog.”
- Initial Response: “Taking care of a dog involves feeding, exercise, grooming, and regular veterinary check-ups.”
- Follow-Up Prompt: “Could you provide the information in a detailed list format?”
- Improved Response: "Certainly! Here’s a detailed list:
- Feeding: Choose nutritionally balanced food and provide fresh water.
- Exercise: Daily walks and playtime are essential.
- Grooming: Brush regularly and keep nails trimmed.
- Vet Visits: Annual check-ups and vaccinations are a must."
- Example 1:
-
Misleading Examples
Explanation: Sometimes, you might use an example to indicate the style or format you’re interested in. But if your example isn’t spot-on, ChatGPT might take it too literally or misunderstand your intention. Be mindful of the examples you use and make sure they align closely with what you’re actually looking for.
- Example 1:
- Initial Prompt: “Write me a story like Harry Potter.”
- Initial Response: “In a dystopian future, young Sarah discovers she’s a tech wizard capable of controlling machines with her mind.”
- Follow-Up Prompt: “I meant a fantasy story set in a magical world.”
- Improved Response: “Ah, got it. In the magical kingdom of Eldoria, a young orphan named Tim discovers an ancient wand that bestows upon him incredible magical powers.”
- Example 2:
- Initial Prompt: “Suggest a hearty dinner recipe.”
- Initial Response: “How about a seafood paella?”
- Follow-Up Prompt: “I was thinking of something more along the lines of a meat and potatoes kind of meal.”
- Improved Response: “Ah, I see. How about a classic meatloaf with a side of mashed potatoes? It’s a hearty, comforting meal that’s sure to satisfy.”
- Example 1:
-
Needs Additional Information
Explanation: Even when you think you’ve provided enough details, sometimes ChatGPT might still need more information to give you the best possible answer. This could be additional context, specific preferences, or other factors that weren’t included in your initial prompt. If the initial response is lacking, think about what additional details could make it more precise.
- Example 1:
- Initial Prompt: “What laptop should I buy?”
- Initial Response: “The ideal laptop depends on various factors, including what you’ll be using it for, your budget, and personal preferences.”
- Follow-Up Prompt: “I’ll mainly be using it for video editing.”
- Improved Response: “For video editing, you’ll want a laptop with a strong processor, at least 16GB of RAM, and a high-resolution display. MacBook Pro and Dell XPS 15 are good options.”
- Example 2:
- Initial Prompt: “What pet should I get?”
- Initial Response: “Choosing a pet depends on your lifestyle, living situation, and personal preferences. Dogs and cats are popular choices, but there are also birds, fish, and small mammals like hamsters.”
- Follow-Up Prompt: “I should add that I live in an apartment.”
- Improved Response: “If you live in an apartment, you might consider smaller pets that require less space. Small dog breeds, fish, or even a rabbit could be a good fit.”
- Example 1:
Understanding these common pitfalls and knowing how to navigate them can greatly improve your interactions with ChatGPT. A good prompt often requires some iteration and fine-tuning, so don’t hesitate to refine your questions in follow-up prompts.
Hands-On Activity: Mastering the Art of Follow-Up Prompts
Objective
In this activity, you will practice writing follow-up prompts to fine-tune ChatGPT’s responses. You’ll start with some initial prompts that are intentionally less effective. After getting the initial responses, you’ll identify issues based on the categories we’ve discussed and write follow-up prompts to address them. Finally, you’ll evaluate the improved responses to see if they meet your expectations.
Guidelines for a Great Response
- Relevance: Does the response answer your question directly and completely?
- Clarity: Is the response easy to understand, with no ambiguous or confusing parts?
- Depth: Does the response provide a sufficient level of detail?
- Tone: Does the tone of the response match what you were looking for (e.g., formal, casual, educational, etc.)?
- Formatting: Is the response formatted in a way that makes it easy to read and understand?
Initial Prompts
- “Tell me how solar energy works.”
- “What’s a good meal I can make quickly?”
- “How can I make my bedroom more comfortable?”
- “How do I get better at playing basketball?”
- “Explain how a car engine works.”
Steps
- Step 1: Copy one of the initial prompts and put it into the same chat project used in the previous activity.
- Step 2: Examine the response you get. Identify any issues based on the categories we’ve discussed (Missing Background Information, Misleading Descriptions, Unclear Requirements, Misleading Examples, Needs Additional Information).
- Step 3: Write a follow-up prompt to address the issue(s) you’ve identified. Put your follow-up prompt into ChatGPT.
- Step 4: Again, examine the new response. Is it improved? Does it meet the guidelines for a great response? If not, go back to Step 3 and write another follow-up prompt.
- Step 5: Once you’re satisfied with the response, move on to the next initial prompt and repeat the process.
Example of the Process
- Initial Prompt: “Tell me how solar energy works.”
- Initial Response: “Solar energy works by capturing the sun’s energy and turning it into electricity for your home or business.”
- Issues Identified: Missing Background Information, Unclear Requirements
- Follow-Up Prompt: “I’m doing a project on renewable energy sources. Can you explain how solar panels capture the sun’s energy and convert it into electricity, in a detailed manner?”
- Improved Response: “Certainly! Solar panels are made of photovoltaic cells, which contain semiconductors. When sunlight hits these cells, it energizes the electrons, creating an electric current. This electricity is then converted from DC to AC and can be used to power your home. Solar inverters and battery storage systems can also be used for a more constant energy supply.”
- Evaluation: The improved response is relevant, clear, detailed, educational in tone, and well-formatted, meeting all the guidelines for a great response.
Feel free to go through all the initial prompts to get the most out of this hands-on activity. Remember, the key to a meaningful interaction with ChatGPT often lies in the art of the follow-up prompt!
Chapter Summary
In this chapter, we explored the CORE framework to craft effective prompts for ChatGPT. The CORE framework consists of Context, Objective, Requirement, and Example, each adding clarity and precision to your questions. We illustrated this with detailed examples and explained how to fix common issues through follow-up prompts. Hands-on activities were included to offer practical experience in writing both initial and follow-up queries. By mastering these principles, you’re set to get the most relevant and accurate responses from ChatGPT.
Project - An Improved Chat App
It’s time to do some coding now. Please follow this tutorial to build an improved chatbot.
-
-