Mastering Prompt Engineering: The Ultimate Guide for Absolute Beginners

19 June 2023 - 7 minute read
Mastering Prompt Engineering: The Ultimate Guide for Absolute Beginners
Photo by Maxime Lecomte

Introduction

Welcome to Prompt Engineering 101, where we will explore the art of crafting compelling results with language models. Whether you're a writer or an engineer, this beginner's guide will provide you with strategies, guidelines, and safety recommendations to help you strengthen your prompt engineering skills. By understanding the importance of prompt engineering and mastering the art of crafting engaging prompts, you can unlock the full potential of large language models for a wide range of tasks. In this guide, we will delve into the fascinating discipline of prompt engineering and share practical tips on creating prompts that generate accurate and compelling results. So, if you're struggling to create effective prompts or looking to enhance your writing skills, this guide is for you!

What is a Prompt?

Before we dive into prompt engineering, let's start by understanding what a prompt is. A prompt is the text provided to a language model before it begins generating output. It is a message or statement that you give to the model to begin a conversation or request information. The model uses the prompt to generate a relevant response based on its training and understanding of language patterns. The prompt helps guide the model in understanding the context and gives it a direction to follow in generating a reply. The quality and clarity of the prompt can greatly influence the usefulness and accuracy of the model's response.

Imagine you want to ask a language model to complete the sentence “The sky is…” and anticipate a meaningful response. Here's what a simple prompt might look like:

Prompt: The sky is

Output: blue

While this response aligns with our expectations, the model's output can sometimes be unexpected or fail to address the desired task. To improve the prompt and obtain more accurate results, we can add more context and instructions. For instance:

Prompt: Complete the sentence: The sky is

Output: so beautiful today.

By providing clear instructions, we guide the model to perform the desired task successfully.

What is Prompt Engineering?

Prompt engineering is the process of designing and creating effective prompts for language models, or chatbots. It involves carefully crafting the instructions or input provided to prompt the desired output from the model. Prompt engineering can be used to guide the model's responses, generate specific types of content, or improve the model's performance in certain domains or tasks. It requires understanding the capabilities and limitations of the model as well as experimenting with different prompts to achieve the desired results. Prompt engineering is crucial for achieving reliable and desirable results with language models. Without proper prompting engineering, language models can generate output that is inconsistent, incoherent, or irrelevant to the desired task.

Prompt Anatomy

To generate a better text, it is important to consider the specific use case and the desired response. The format and elements used in the prompt can vary depending on the task at hand. You have the flexibility to use one or combine multiple elements to suit your needs. Remember that the response you receive will depend on the information provided in the prompt.

1. Input Data

This is the actual input or question that we want the model to respond to. It forms the basis of the prompt and provides the necessary information for the model to generate a response.

Prompt: What is a healthy diet?

2. Instruction

This is like a clear task or instruction given to a model. It tells the model what it should do or perform.

Prompt: Write a post article about the effects of healthy diet.

3. Context

The context of a prompt is important because it provides additional information for the model. Similar to how having more information can help us make better decisions, giving context to the model helps it generate more accurate and relevant responses.

For example, if you want to ask a question to the model, providing some background information or context about the topic of the question can help the model understand what you are asking and provide a more accurate answer.

Think of context as the overall setting or background that gives the model a more in-depth understanding of what you are asking or talking about. It can help guide the model's responses and make them more useful and relevant to your needs.

Prompt:

You are a certified nutritionist and dietitian. Write a post article about the effects of healthy diet.

Post Title: "The Remarkable Effects of a Healthy Diet: Enhancing Your Mind, Body, and Soul"

4. Output Indicator

Similar to specifying the type or format of the output we want from a machine, the output indicator in a prompt tells the model the desired structure or type of response it should produce.

It's important to note that not all four elements are necessary for every prompt. The inclusion of elements depends on the specific task or goal we have at hand. In upcoming guides, we will explore more concrete examples to better understand the use of these elements in prompts.

Prompt:

You are a certified nutritionist and dietitian. Write a post article about the effects of healthy diet.

The post must have an introduction, content and final thoughts. At the end, show 3 healthy meals for breakfast, lunch and dinner. Must be 3 for every day of the week.

Post Title: "The Remarkable Effects of a Healthy Diet: Enhancing Your Mind, Body, and Soul"

Prompt Engineering Guidelines.

As you embark on your prompt engineering journey, keep these general tips in mind to make the most out of your prompt design:

1. Start Simple

Prompt engineering is an iterative process that requires experimentation and refinement. Begin with simple prompts and gradually introduce more complexity to optimize the results. OpenAI and Cohere provide useful playgrounds for experimentation.

2. Give clear and precise guidance

To get the desired output from a text-generation model, it's essential to provide clear instructions. Remember, these models can't read your mind! To avoid ambiguity, be explicit about what you want the model to do. Here are some tactics to consider:

1. Include details in your query. The more specific you are in your instructions, the more relevant the answers will be. For example, instead of asking, “How do I add numbers in Excel?”, ask, “How do I add up a row of dollar amounts in Excel? I want to do this automatically for a whole sheet of rows, with all the totals ending up on the right in a column called 'Total'.”

2. Ask the model to adopt a persona. If you want the model's responses to have a specific tone or style, you can instruct it to adopt a persona. For example, you can specify that the model's replies should contain a playful comment in every paragraph.

3. Use delimiters to indicate distinct parts of the input. Delimiters like triple quotation marks or XML tags can help demarcate sections of text that should be treated differently by the model. For example, you can instruct the model to summarize the text delimited by triple quotes with a haiku.

4. Specify the steps required to complete a task. For complex tasks, breaking them down into a sequence of steps can help the model follow the instructions more accurately. For example, if you want the model to summarize meeting notes, you can specify that it should first summarize the main points in a paragraph, then list the speakers and their key points in a Markdown list, and finally provide any suggested next steps or action items.

5. Provide examples: In some cases, it can be more efficient to provide examples rather than explicit instructions for every permutation of a task. This is especially useful for tasks that require a specific style or format. For example, if you want the model to answer user queries consistently, you can provide a few query-response pairs that follow the desired style.

6. Specify the desired length of the output: If you have specific requirements for the length of the generated text, you can instruct the model to produce outputs of a certain target length. This can be specified in terms of words, sentences, paragraphs, bullet points, and more. Keep in mind that specifying the number of words may not be precise, but specifying the number of paragraphs or bullet points can give more reliable results.

3. Provide Reference Text

To enhance the quality of the generated responses, you can provide reference text to the model. Think of this as giving the model a trusted source of information to draw upon. Here are some tactics to consider:

1. Instruct the model to answer using a reference text: If you have a specific document or text that contains relevant information, you can instruct the model to use that reference text to compose its answer. This can help the model provide more accurate and informed responses.

2. Instruct the model to answer with citations from a reference text. If you want the model to provide evidence or support for its answers, you can instruct it to include citations from the reference text. This can help validate the information provided by the model and make the responses more reliable.

4. Split Complex Tasks into Simpler Subtasks

Complex tasks can lead to higher error rates in text-generation models. To improve performance and reliability, consider breaking down complex tasks into simpler subtasks. This approach is similar to the modular design principles used in software engineering. Here are some tactics to consider:

1. Use intent classification to identify relevant instructions. For tasks that involve multiple sets of instructions or different cases, consider using intent classification to identify the most relevant instructions for a particular user query. This can help tailor the instructions and improve the model's understanding of the task.

2. Summarize or filter previous dialogue in dialogue-based applications: In dialogue-based applications with long conversations, it's important to summarize or filter previous dialogue to keep within the context window of the model. This can improve the efficiency and effectiveness of the model's responses.

3. Summarize long documents piecewise: When dealing with very long documents, it may be necessary to summarize them piecewise, focusing on specific sections or topics. These section summaries can be concatenated to create a full summary of the document recursively. This approach helps the model handle large volumes of text and ensures that significant information is not overlooked.

5. Give Time to "Think"

Text generation models like GPT can benefit from taking time to work out complex problems or calculations. Just like humans, these models can reason their way to accurate answers given enough time. Here are some tactics to consider:

1. Instruct the model to work out its solution: Instead of expecting the model to provide an immediate answer, you can instruct it to reason through the issue or calculation on its own before arriving at a conclusion. This can help reduce reasoning errors and improve the reliability of the model's responses.

2. Use an inner monologue or a sequence of queries to hide the model's reasoning process. In some cases, it may be necessary to hide the model's reasoning process from the user. An inner monologue or a sequence of queries can be used to achieve this. By instructing the model to output its thought process or queries without revealing the answer, you can maintain an interactive and engaging experience while keeping the reasoning hidden.

3. Ask the model if it missed anything on previous passes. When a model needs to search for relevant information or excerpts, it's common for it to stop too early and miss some relevant items. To address this, you can prompt the model with follow-up queries to find any information it may have missed on previous passes. This helps improve the comprehensiveness of the results.

6. Test Changes Systematically

When making changes to your prompt or system design, it's crucial to evaluate their impact systematically. Testing and evaluating changes can help you determine whether they improve or hinder performance. Here are some tactics to consider:

1. Evaluate model outputs against gold-standard answers: To assess the quality of model outputs, you can compare them to gold-standard answers or known correct responses. This can be done by counting how many required facts or points are present in the model's answers. By comparing the model's outputs to the expected answers, you can measure the performance and improve the accuracy of the text generation.

Prompts Examples by Use Cases

Now that we have covered the fundamentals, let's explore some examples to solidify our understanding of prompt engineering. We will showcase different tasks and provide corresponding prompts to demonstrate effective prompt design.

1. Text Summarization

Prompt:

Resume in a sentence the concept of Antibiotics

Concept:

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

Output:

Antibiotics are medications used to treat bacterial infections by either killing the bacteria or stopping them from reproducing, but they are not effective against viruses and overuse can lead to antibiotic resistance.

2. Information Extraction:

Prompt:

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

Mention where is the antibiotic does not work in the text above:

Output: The text above mentions that antibiotics are not effective against viral infections.

3. Question Answering

Prompt:

Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.

Question: What was OKT3 originally sourced from?

Output: Mice

4. Text Classification

Prompt:

Classify the text into neutral, negative, or positive.

Text: I think the food was okay.

Output: Sentiment: Neutral

5. Idea Generation

Prompt:

Generate a 3 blog post title for the concept below

Concept:

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

Output:

  1. "Understanding Antibiotics: How They Work and When to Use Them"
  2. "The Risks of Antibiotic Misuse: A Closer Look at Antibiotic Resistance"
  3. "Demystifying Antibiotics: What Every Patient Should Know"

5. Content Writing

Prompt:

Generate a 3 twits for the concept below

Concept:

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

Output:

  • Antibiotics are incredible medications that save lives by fighting off harmful bacteria. Remember, they're not a cure for viral infections, so use them wisely! #Antibiotics #InfectionFighters
  • Did you know that antibiotics work by killing bacteria or stopping them from multiplying? They give our immune system a chance to kick in and defeat those pesky infections! 🌡️💊 #Antibiotics #ImmuneSystem
  • It's important to take antibiotics as prescribed and complete the full course. Misusing or overusing them can result in antibiotic resistance, making them less effective against future infections. Let's protect the power of antibiotics! 💪🔬 #AntibioticResistance #PublicHealth

6. Code Generation

Prompt: Create a MySQL query for all students in the Computer Science Department.

Output: SELECT StudentId, StudentName FROM students WHERE DepartmentId IN (SELECT DepartmentId FROM departments WHERE DepartmentName = 'Computer Science');

7. Reasoning

Prompt: I have a business that produces 100 shoes a week for 1.99 usd each pair and a selling price of 3.95 usd. How much money do I have to invest to achieve a production of 2000 shoes monthly and what would be my winnings.

Output:

To calculate the amount of money you need to invest to achieve a production of 2000 shoes monthly, we can use the following formula:

Investment = Cost per unit * Desired production quantity

Cost per unit = $1.99

Desired production quantity = 2000 shoes per month

Investment = $1.99 * 2000 = $3,980

So, you would need to invest $3,980 to achieve a monthly production of 2000 shoes.

To calculate your winnings, we can use the following formula:

Winnings = (Selling price - Cost per unit) * Desired production quantity

Selling price = $3.95

Cost per unit = $1.99

Desired production quantity = 2000 shoes per month

Winnings = ($3.95 - $1.99) * 2000 = $3,920

Your winnings would be $3,920 per month.

Final Thoughts

Prompt engineering is a vital skill for crafting compelling and informative text outputs. By understanding the strategies outlined in this guide, you can enhance the quality, reliability, and effectiveness of your prompts. Remember to provide clear instructions, leverage reference text and external tools, decompose complex tasks, give models time to think, and systematically test changes. With practice and experience, you can become skilled at prompt engineering and generate engaging and accurate responses. Happy prompt engineering!

Theory is great, but practice makes the master. Are you ready to become an absolute legend in your field?

Related articles