Prompt Engineering: Introduction, Principles and Techniques

Prompt Engineering: Introduction, Principles and Techniques

Introduction

In this post, I go over all the lessons I have picked up about Prompt Engineering from taking multiple courses, reading academic papers and testing out different models to solve a variety of problems.

All the example prompts below can be run on the Chat GPT-3.5 model.

What Is Prompt Engineering?

Prompts serve as inputs that users can provide to elicit specific responses from a Large Language Model (LLM). To make prompts to do what you want you need to employ techniques that'll help accomplish your goal and that, in a nutshell, is what Prompt Engineering is about.

To put it more formally:

Prompt Engineering refers to methods and techniques to efficiently communicate with Large Language Models (LLMs) to steer their behavior for desired outcomes without updating the model itself.  

To breakdown the definition further and elucidate on some aspects of it:

  1. The Goal of Prompt Engineering is to get the LLM to solve your task efficiently.
  2. To achieve your task, you'll need to frame your prompt in a way that best conveys enough information to the LLM to solve your problem.
  3. The prompt doesn't change the state of the model, it just an input that the LLM can understand to produce an output.

Components of A Prompt

  • Instruction: A specific task or instruction you want the model to perform.
  • Context: External information or additional context that can steer the model to better responses.
  • Input Data: The input or question that we are interested to find a response for.
  • Output Format: The type or format of the output.

Example

Prompt:

Recommend 4 books that highlight the philosophy of Fredrick Nietzsche.

Context
=======
I am philosophy student interested in learning about Nihilism, the Will to Power.

Input Data
==========
I have read "Thus Spoke Zarathustra".

Output Format
=============
Only output books in this format with no other information and sentences:

- Book 1 (Year: yyyy)
- Book 2 (Year: yyyy)
- Book 3 (Year: yyyy)

Output from Chat GPT-3.5:

"Beyond Good and Evil" (Year: 1886)
"On the Genealogy of Morality" (Year: 1887)
"The Gay Science" (Year: 1882)
"Twilight of the Idols" (Year: 1889)

Prompt Engineering Principles

There are 2 main prompt Engineering principles that lead to the efficient results:

  1. Write Clear Instructions That are Specific and Complete.
  2. Give the Model Time To Think.
  3. Iterate Until You Solve Your Problem.

Principle 1: Write Clear Instructions That Are Specific and Complete

  1. Effectively communicating with the LLM requires that you are specific and complete or in other words, provide all the necessary details required to efficiently solve your problem.
  2. Clear instructions aren't necessarily short instructions. The more specificity added to the prompt, the better your results.

Examples:

Task: You are a climatic researcher who has to write a report about details about the causes of global warming.

Not Clear and Specific Enough:

Write me a summary about the causes of global warming.

More Clear and Specific:

I am a climate researcher tasked with writing a report on the causes of global warming. Please write a report for me that includes citations from reputable sources, metrics, and figures regarding significant factors and the implications if we do not change our current trajectory. Additionally, kindly suggest three solutions to address these causes.

Some tactics that are helpful to be more concise and specific are:

Tactic Description Example Prompt
Use delimiters to clearly indicate distinct parts of the input: This means put your user input in delimiters that your prompt knows how to interpret. Delimiters can be anything like: ```, """, < >, ====. This tactic is helpful because it prevents exploitative prompts or prompt injections. Additionally, we make it extremely clear what the user input is and augment the input based on what we want to achieve. Summarize within the ====: ===={prompt}====
Ask for a structured output: JSON, HTML etc., could be all examples out structured output. Requesting the output in a specific format forces the LLM to respond in a better specified format than otherwise; the idea here is that the intentions of the prompt are made clear to the model based on the output. Generate a list of three made-up book titles along with their authors and genres. Provide them in JSON format with the following keys: book_id, title, author, genre.
Ask the model to check whether conditions are satisfied: This tactic validates the user input by making sure what's requested is correct. Your prompt should include the validation steps and reporting those. The following input delimited by ===== is that of a list of instructions. Your task is to summarize them appropriately after checking if the input is a list. If the input isn't a list, respond with "The input isn't a list - please provide a list to summarize". ===== {input} =====.

Principle 2: Give the Model Time To Think

If the model is making reasoning errors by rushing to an incorrect conclusion, you should try to reframe the prompt to request a chain or series of relevant reasoning steps before the model provides its final answer. What these chains of reason do is elicit the model to reason more about the problem to get to a more well-thought out final answer.

Tactic and Summary Description Example Prompt
Specify the steps required to complete a task: Explicitly state the steps involved before you expect the final answer. By asking the model to state the steps, you are pushing it in the right direction to come up with reasoning steps that lead to a more well thought out answer. Perform the following actions: 1 - Summarize the following text delimited by triple backticks with 1 sentence. 2 - Translate the summary into French. 3 - List each name in the French summary. 4 - Output a json object that contains the following keys: french_summary, num_names. Separate your answers with line breaks.
Instruct the model to work out its own solution before rushing to a conclusion Along the similar lines as the previous tactic, forcing the model to reason by creating steps and then checking if those steps make sense will drive the model to better outcomes because them model has been given time to think through issues. Your task is to determine if the student's solution is correct or not. To solve the problem do the following: - First, work out your own solution to the problem. - Then compare your solution to the student's solution and evaluate if the student's solution is correct or not. Don't decide if the student's solution is correct until you have done the problem yourself. Use the following format: Question: question here Student's solution: student's solution here Actual solution: steps to work out the solution and your solution here Is the student's solution the same as actual solution just calculated: yes or no Student grade: correct or incorrect

Principle 3: Iterate Until You Solve Your Problem

The iterative steps involved until you reach a prompt that solves your problem are:

  1. Try a prompt by starting off small after having an idea as to what you want the model to output.
  2. Analyze if the prompt gives you what you want.
  3. Clarify instructions.
  4. Refine prompts with a batch of examples.

Basic Prompt Applications

The following table highlights examples of good prompts to accomplish different types of tasks:

Usage Example Prompt
Summarization "Your task is to generate a short summary of a product review from an ecommerce site. Summarize the review below, delimited by triple backticks, in at most 30 words. Review: {prod_review}".
Inference "Identify the following items from the review text: 1. Sentiment (positive or negative) 2. Is the reviewer expressing anger? (true or false) 3. Item purchased by reviewer 4. Company that made the item The review is delimited with triple backticks. Format your response as a JSON object with 'Sentiment', 'Anger', 'Item' and 'Brand' as the keys. If the information isn't present, use 'unknown' as the value. Make your response as short as possible. Format the Anger value as a boolean. Review text: '''{lamp_review}'''"
Transformation "Proofread and correct the following text and rewrite the corrected version. If you don't find any errors, just say 'No errors found'. Don't use any punctuation around the text:{t}"
Expansion / Augmentation "You are a customer service AI assistant. Your task is to send an email reply to a valued customer. Given the customer email delimited by triple quotes, Generate a reply to thank the customer for their review. If the sentiment is positive or neutral, thank them for their review. If the sentiment is negative, apologize and suggest that they can reach out to customer service. Make sure to use specific details from the review. Write in a concise and professional tone. Sign the email as 'AI customer agent'. Customer review: {review} Review sentiment: {sentiment}"
Code Generation "Write Code for me that implements a priority queue in C#."

Advanced Prompt Engineering Techniques

Building off of the 3 basic principles mentioned above, these more advanced prompt engineering techniques help solve more complex tasks.

1. Few Shot Prompting

Providing the prompt with examples so that the LLM knows the train of thought to follow. The more the demonstrations, the better the model learns about the context of the task.

Example

Task: Find out if the odd numbers in a list of numbers add up to an even or odd number.

Prompt:

Find out if the odd numbers in a list of numbers add up to an even or odd number.

The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: The answer is False.

The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: The answer is False.

The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 4.
A:

2. Chain of Thought (CoT) Prompting

Asking and optmodel steps to solve the task so that we allow for the context and time of reasoning.

  1. Very useful for tasks requiring reasoning.
  2. Combine this technique with few-shot prompting to get better results.
  3. Use Zero-Shot CoT where exemplars are not available i.e., prod the model to think step by step.

Example

Task: Find out if the odd numbers in a list of numbers add up to an even or odd number.

Prompt:

The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: Adding all the odd numbers (9, 15, 1) gives 25. Since 25 is an odd number, the answer is False.

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 1.
A: Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. Since 41 is an odd number, the answer is False.

The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 4.
A:

3. Self-Consistency

The idea is to sample multiple, diverse reasoning paths through few-shot CoT, and use the generations to select the most consistent answer. In other words, it is an approach that simply asks a model the same prompt multiple times and takes the majority result as the final answer.

  1. Self-Consistency aims to improve on the naïve greedy decoding used in chain-of-thought prompting.
  2. The idea is to sample multiple, diverse reasoning paths through few-shot CoT, and use the generations to select the most consistent answer.
  3. This helps to boost the performance of CoT prompting on tasks involving arithmetic and commonsense reasoning.
  4. By generating many chains of thought and taking the most commonly occurring answer, we can get a more consistently correct answer.

Example

Task: Classify emails as Important or Not Important.

Prompt:

Classify the following email as Important or Not Important as it related to a software engineering company.

Write out all the steps involved.

Email
=====
Hello,

I have discovered a major security vulnerability in your system. Although it is not
easy to use, it is possible to gain access to all of your users' data. I have attached
a proof of concept. Please fix this issue as soon as possible.

Cheers,

Donny

Responses:

Response 1:

The sender has discovered a major security vulnerability in the system. 
This is important because it could lead to a data breach. 
I would classify this email as IMPORTANT

Response 2:

The sender has discovered a major security vulnerability in the system, but they have not provided any proof of concept and it is not easy to use. 
I would classify this email as NOT IMPORTANT

Response 3:

The sender is likely a hacker and may have access to the system. 
This is important because it could lead to a data breach. 
I would classify this email as IMPORTANT

The most consistent result is IMPORTANT and therefore, the answer is that the email should be marked as IMPORTANT.

4. Generated Knowledging Prompting

Using additional knowledge provided as part of the context to improve results on complex tasks such as commonsense reasoning.

  1. The knowledge used in the context is generated by a model and used in the prompt to make a prediction.
  2. Highest confidence prediction is used.
  3. The first step is to generate knowledge by asking the model what it knows.
  4. The next step is to take the generated knowledge and then generate knowledge augmented questions to get answer proposals.

Example

Task: Write a blogpost about the Kermode bear.

Prompt:

Generate 4 facts about the Kermode bear, then use these facts to write a short blog post using the information.

Generated facts:

The Kermode bear, also known as the spirit bear, is a rare subspecies of the American black bear that is found in the coastal regions of British Columbia, Canada. 

1. The Kermode bear is a unique subspecies of the American black bear, with approximately 10% of the population having white fur. 

2. The Kermode bear is a symbol of the First Nations people of British Columbia, and is seen as a spirit animal. 

3. The Kermode bear is an important part of the ecosystem, helping to disperse seeds and maintain the balance of the forest. 

4. The Kermode bear is a protected species, and its population is monitored closely by the provincial government.

Blogpost

The Kermode bear, also known as the Spirit Bear, is a rare subspecies of the American black bear found in British Columbia, Canada. This unique bear has a white or cream-colored coat, caused by a recessive gene, and is a symbol of hope and renewal for the First Nations people of British Columbia. The Kermode bear is a protected species and is listed as a species of special concern by the Committee on the Status of Endangered Wildlife in Canada, making it an important part of the region's biodiversity.

5. Program Aided Language Model (PAL)

PAL uses an LLM to read problems and generate problems as the intermediate reasoning steps

  1. CoT is not enough as it depends only on the generated text from the model. PAL can help with programmatically generating intermediate steps to guide the model in a certain way.
  2. It offloads the solution step to a runtime such as the Python interpreter.

Example

The following example uses Python to solve the following task.

Task: Today is 2nd July 2023. I was born exactly 25 years ago. What is the date I was born in MM/DD/YYYY?

Prompt

# Q: 2015 is coming in 36 hours. What is the date one week from today in MM/DD/YYYY?
# If 2015 is coming in 36 hours, then today is 36 hours before.
today = datetime(2015, 1, 1) - relativedelta(hours=36)
# One week from today,
one_week_from_today = today + relativedelta(weeks=1)
# The answer formatted with %m/%d/%Y is
one_week_from_today.strftime('%m/%d/%Y')

# Q: The first day of 2019 is a Tuesday, and today is the first Monday of 2019. What is the date today in MM/DD/YYYY?
# If the first day of 2019 is a Tuesday, and today is the first Monday of 2019, then today is 6 days later.
today = datetime(2019, 1, 1) + relativedelta(days=6)
# The answer formatted with %m/%d/%Y is
today.strftime('%m/%d/%Y')

Response

To determine the date you were born in MM/DD/YYYY format, we need to subtract 25 years from the given date.
Given: Today is 2nd July 2023.
To find the date you were born, we subtract 25 years:
Year: 2023 - 25 = 1998
Therefore, the date you were born in MM/DD/YYYY format is 07/02/1998.
Note: The additional questions remain the same and the previous answers provided for those questions are still valid.

6. ReAct

LLMs are used to generate both reasoning traces and task-specific actions in an interleaved manner.

  1. Generating reasoning traces allow the model to induce, track and update action plans, and even handle exceptions.
  2. The action step allows to interface with and gather information from external sources such as knowledge bases or environments.
  3. ReAct allows LLMs to interact with external tools to retrieve additional information that leads to more reliable and factual responses.
  4. ReAct is inspired by the synergies between "acting" and "reasoning" which allow humans to learn new tasks and make decisions or reasoning.
  5. The limitations of CoT are that its lack of access to the external world or inability to update its knowledge; this leads to hallucinations and error propagation.

Example

Task: To find what is the elevation range for the area that the eastern sector of the
Colorado orogeny extends into?

Prompt:

Thought 1: I need to search Colorado orogeny, find the area that the eastern sector
of the Colorado orogeny extends into, then find the elevation range of the
area.
Action 1: WebSearch("Colorado orogeny")
Observation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas.
Thought 2: It does not mention the eastern sector. So I need to look up eastern
sector.
Action 2: WebSearch("eastern sector of Colorado orogeny")
Observation 2: (Result 1 / 1) The eastern sector extends into the High Plains and is called the Central Plains orogeny.
Thought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I need to search High Plains and find its elevation range.
Action 3: WebSearch("high plains + eastern sector of Colorado orogeny")
Observation 3: High Plains refers to one of two distinct land regions.
Thought 4: I need to instead search High Plains (United States).
Action 4:  WebSearch("high plains in the US + eastern sector of Colorado orogeny")
Observation 4: The High Plains are a subregion of the Great Plains. From east to west, the High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130
m).
Thought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.
Answer: 1,800 to 7,000 ft.
``

Conclusion

The following is the summary of this post:

  1. Prompt Engineering helps you create better prompts to successfully execute your task.
  2. There are 3 Main Principles of Prompt Engineering:
  • Write Clear Instructions That Are Specific and Complete.
  • Give the Model Time to Think.
  • Iterate Until You Solve Your Problem.

3. More Advanced Prompt Engineering Techniques:

  • Few Shot Prompting: Give examples as a part of the prompt.
  • Chain of Thought (CoT) Prompting: Ask the model to think in steps. If examples are available, make sure that the examples also highlight the steps to solve the task.
  • Self-Consistency: Provide the model with the same prompt multiple times and then sample the results for the answer that's most common or consistent.
  • Generated Knowledge Prompting: Provide additional generated knowledge within the prompt.
  • Program Aided Language Model (PAL): Write code to solve complex tasks.
  • ReAct: Generating reasoning steps and then executing them.