Prompt Engineering 101 - Beginner's Guide

March 3, 2024 - 11 min read

Rashmi Shukla
Rashmi Shukla

Building AI Products

Introduction

With the recent progress made in the world of AI, prompt engineering has become an emerging field for knowledge workers. Before we dive into various prompt engineering techniques, let's briefly go through the basics:

  • Generative AI applications like ChatGPT, Perplexity, etc, are based on large language models (LLMs). We will use the term models to refer to these applications going forward.
  • Prompts are instructions given to Generative AI models to make them do a task.
  • While prompts are enough to get a helpful response from the model, it usually involves multiple iterations or back-and-forth to get it right.
  • Prompt engineering involves effectively framing the prompt to get the desired responses from the model without many iterations or back-and-forth.
  • A good prompt includes one or more components, such as instructions, context, examples, etc.

This beginner-friendly guide covers the following prompting tips and techniques:

These simple techniques will help elevate the quality of the model's response. Each of the above techniques is covered in detail with examples of an ineffective prompt and how to make it effective.

We have covered more advanced techniques in the Invoking Reasoning through Prompt Engineering Guide.

Prompt Engineering Tips

Be direct and specific in the instruction

Use language that is as direct as possible when stating what needs to be done.

These techniques can help you be direct and specific in your instructions.

  1. Avoid ambiguous words and phrases like "preferred output format", "write a short essay", etc. Clear and straightforward prompts help LLMs understand exactly what you're asking.

    Ineffective Prompt ❌

    • Prompt
      I'm thinking of getting in shape, maybe losing some weight, not sure how though, what do you suggest?
      
    • Response

    Effective Prompt ✅

    • Prompt
      Create a 12-week weight loss fitness plan for a beginner with exercises to be performed 4 days a week, focusing on cardio and strength training.
      
    • Response
  2. Specify the desired response format, the expected length, and the tone in the prompt.

    Ineffective Prompt ❌

    Tell me what I should eat to be healthier.
    

    Effective Prompt ✅

    Provide a 7-day vegetarian meal plan for weight loss, including three meals and two snacks per day. Each meal description should be brief, under 50 words, with calorie count.
    
  3. Be as objective as possible.

    Ineffective Prompt ❌

    I love food but need to eat better because I want to feel more energetic. Maybe less junk food?
    

    Effective Prompt ✅

    List 10 plant-based foods high in protein that can enhance energy levels, suitable for replacing junk food in daily snacks and meals.
    
  4. Do not mention redundant details in the prompt.

    Ineffective Prompt ❌

    Considering I usually wake up at 7 am, but sometimes 8 am, and I go to bed around 10 pm, and I sometimes work out in the evening but other times in the morning, what's a good workout plan?
    

    Effective Prompt ✅

    Design a flexible workout plan for someone with an irregular schedule, including morning and evening workout options, to be done 5 days a week, focusing on full-body exercises.
    

Provide as much context as possible

Setting clear context helps LLMs with all the necessary background information. It guides the conversation in the direction that you would want to.

You can follow the following steps to set the context:

  1. State the objective - What do you hope to achieve with the response?

    Effective Prompt ✅

    Create a 4-week fitness and diet plan for a 30-year-old vegetarian Indian female aiming to improve stamina and reduce body fat.
    
  2. Include background information to help LLMs understand the scenario.

    Effective Prompt ✅

    Create a 4-week fitness and diet plan for a 30-year-old vegetarian Indian female aiming to improve stamina and reduce body fat. She is a beginner in fitness activities, with a sedentary job. She can work out a maximum of 4 days a week. 
    
  3. Clearly outline any constraints for the response.

    Effective Prompt ✅

    The diet plan should exclude all forms of meat and eggs, and the fitness routine should require minimal equipment. Each session should not exceed 30 minutes.
    
  4. Clearly state the audience of the response.

    Effective Prompt ✅

    This plan is intended for individuals with minimal fitness experience and a basic understanding of Indian vegetarian cuisine.
    
  5. Highlight Key Terms - This is a technique very few people know about but it is very effective in getting the relevant results.

    Effective Prompt ✅

    While creating a 4-week diet plan, put emphasis on 'home-based exercises', 'plant-based proteins', and 'low-calorie meals' in your suggestions.
    

Break down the task into sub-tasks

Just like any complex problem becomes simpler when divided into smaller pieces, a complicated prompt becomes more manageable for an LLM when broken down into sub-tasks. It is a problem-solving strategy that maximizes the potential of LLMs.

Break down the main task into smaller and actionable sub-tasks. Each sub-task should be clearly defined with a specific outcome

Arrange the sub-tasks in a logical order. This task sequencing will be crucial for accurate response.

Ineffective Prompt ❌

  • Prompt

    Write an article on the impact of sugar.
    
  • Response

Effective Prompt ✅

  • Prompt

    Write an article on the impact of sugar.
    - Start with the state of diabetic patients in India
    - Explain how sugar is integral part of Indian diet
    - Explain what happens to body when it takes sugar
    - Explain long-term impact of Sugar
    - Explain sustainable ways of quitting sugar
    
  • Response

Specify the desired output format through examples

Provide clear examples of how you want the information to be structured, making it easier for the AI to align its responses with your expectations. This adds to clarity and leads to tailored responses

Ineffective Prompt ❌

Suggest a vegetarian diet plan for a 30-year-old female for losing weight

Response

Effective Prompt ✅

Suggest a vegetarian diet plan for a 30-year-old female for losing weight in tabular format with the following columns:
- Day
- Breakfast
- Lunch
- Dinner
- Snacks

Response

Do not ask leading questions

Leading questions suggest a specific answer or direction in the query itself, which can bias the response and limit the AI's ability to provide diverse, unbiased information.

This technique ensures objectivity and promotes unbiased responses. It ensures that there is a full exploration of the query in all aspects.

Ineffective Prompt ❌

  • Prompt

    How does cutting out carbohydrates lead to better health?
    
  • Response

Effective Prompt ✅

  • Prompt

    What are the impacts of reducing carbohydrate intake on health?
    
  • Response

Instead of saying what not to do, say what to do

To prevent the model from hallucinating and to ensure the generation of quality output, specify in the prompt what needs to be done.

Ineffective Prompt ❌

Create a diet plan for reducing weight and increasing muscle strength for a female of 78Kg? Do not suggest non vegetarian items

Effective Prompt ✅

Create a VEGETARIAN diet plan for reducing weight and increasing muscle strength for a female of 78Kg?

Prompt Engineering Techniques

Provide examples (Few-shot Prompting)

LLMs are trained on large data sets which may not be specific to your use case. Adding few examples that illustrate the task guides the LLMs to generate the desired output.

This technique is called Few-shot Prompting.

Few-shot Prompting involves providing a small set of examples (or "shots") to guide the LLM's understanding of a task it needs to perform.

Effective Prompt ✅

  • Prompt

    Task: Provide sentiment analysis of customer reviews.
    
    Examples:
    "Review: 'I loved the quick service and friendly staff.' Sentiment: Promoter"
    "Review: 'The food was bland and overpriced.' Sentiment: Detractor"
    "Review: 'Best coffee I've had in a long time!' Sentiment: Promoter"
    
    Based on the above examples what should be the sentiment for this review:
    "Waited 45 minutes for our order. Very disappointing"
    
  • Response

Effective Prompt ✅

  • Prompt

    Task: Generate a 4-week beginner workout plan focused on full-body strength for a female aged 30 years old with a sedentary lifestyle.
    
    Example 1 (Workout Plan for Cardio Fitness)
    Week 1:
    Monday: 20 minutes of brisk walking
    Wednesday: 15 minutes jogging
    Friday: 20 minutes cycling
    Goals: Increase heart rate
    
    Example 2 (Workout Plan for Flexibility)
    Week 1:
    Monday: 30 minutes of yoga focusing on lower body stretches
    Wednesday: 30 minutes Pilates focusing on core flexibility
    Friday: 30 minutes of yoga focusing on upper body stretches
    Goals: Improve flexibility, and reduce muscle tension
    
    Query: Based on the structure and goals of the above examples, create a 4-week beginner workout plan focused on full-body strength.
    
  • Response

Use Chain-of-Thought Prompting

This technique helps the model solve problems similar to humans. It does this by setting up questions in a way that makes the model think step-by-step before answering.

It can be used for a few important tasks:

  • Solving reasoning tasks (like hard math problems) by splitting them into easier, smaller parts.
  • Reading and explaining long pieces of writing by picking out the main ideas and how they link together.
  • Evaluating the pros and cons of different scenarios to help in decision-making.

Prompt

If Rashmi has twice as many apples as Gaurav, and together they have 18 apples, how many apples does each have?

Explain your reasoning step-by-step.

Response

This can be used with few shot-prompting on complex tasks.

Prompt

Query:  If Rashmi has twice as many apples as Gaurav, and together they have 18 apples, how many apples does each have?
Response: Gaurav has G apples and Rashmi has 2G apples. In total, they have 3G apples. This is equal to 18. This means that G=18/3=6. So Gaurav has 6 Apples and Rashmi has 2G i.e 12 apples. Using the above steps - what will be the answer to this query
'''
If Anika has three times as many oranges as Vikram, and together they have 24 oranges, how many oranges does each have?
'''

Response

Ask the model to explain before answering

Another powerful technique for improving the reliability of answers is to prompt the model to gradually reason out the answer rather than jumping immediately to the final answer. By 'thinking aloud' the model can be far more likely to arrive at the correct answer.

It promotes detailed responses, facilitates better learning, and ensures clarity for the model, leading to more accurate results.

Ineffective Prompt ❌

Suggest a vegetarian diet plan for losing weight sustainably for a 30 year old female

Effective Prompt ✅

Can you first explain the key nutritional considerations for sustainable weight loss in a 30-year-old female? Then, based on those considerations, suggest a vegetarian diet plan that would help in losing weight sustainably

Chain-of-Thought Prompting is covered in more detail in the Chain-of-Thought section of the intermediate prompt engineering guide.

Conclusion

Let's summarize what we learned in this article:

  1. Be direct and specific in the instruction.
  2. Provide as much context as possible.
  3. Break down the task into sub-tasks.
  4. Provide examples (Few-shot Prompting).
  5. Try to make the model think step-by-step before answering (Chain-of-Thought Prompting).

Effective prompting is more an art than science. It evolves by experimenting with different levels of detail and observing the response. To get good at prompt engineering, it is important to build intuition regarding which techniques can help get the best response depending on the scenario. Iterating with various variations and combinations of prompt engineering techniques for different use cases will help build that intuition.

In the next article, we will cover more advanced techniques in the focused on invoking reasoning through prompt engineering.

References