avatarAnjolaoluwa Ajayi

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4642

Abstract

b>Tell the LLM to rephrase the question into a better prompt before generating the final answer.</p><p id="e2bf"><b>When to use: </b>For higher accuracy on complex tasks or when you need to evaluate the LLM’s understanding of a task.</p><div id="e6a0"><pre>PROMPT: Rephrase and expand the following question, and then answer it: What is the difference between correlation and causation?</pre></div><h2 id="a0b7">9. Re-reading (RE2)</h2><p id="73df"><b>How to use:</b> Begin with an instruction or question, add the phrase: “Read that again:” immediately after, and then repeat the original instruction/question.</p><p id="ba43"><b>When to use:</b> For complex tasks that involve reasoning.</p><div id="e6ca"><pre>PROMPT: A farmer has a rectangular field that is 3 times as long as it is wide. The perimeter of the field is 400 meters. What are the dimensions of the field?

Read the question again: "A farmer has a rectangular field that is 3 times as long as it is wide. The perimeter of the field is 400 meters. What are the dimensions of the field?"</pre></div><h2 id="b3f0">10. System Prompting</h2><p id="7251"><b>How to use:</b> Give the LLM high-level instructions or context that will be considered throughout the interaction with the LLM.</p><p id="70b1">In ChatGPT, you can do this using the ‘customize GPT’ feature.</p><p id="b460">When building LLM apps, you can do this by providing the context/ instructions as the ‘system prompt’.</p><p id="d6eb"><b>When to use: </b>When you need to set the overall behavior and tone of the LLM in a conversational setting.</p><div id="8675"><pre>SYSTEM PROMPT: You are a helpful assistant that will provide factual responses in a concise tone</pre></div><h2 id="1eef">11. Self-Ask</h2><p id="d2db"><b>How to use: </b>Ask the LLM a question, tell it to break the question into smaller sub-questions all of which it would answer to arrive at the final answer.</p><p id="4733"><b>When to use: </b>When the task is complex, requires reasoning, or involves multiple steps.</p><div id="4502"><pre>PROMPT: Should I pursue a master's degree in data science? Break this question into smaller sub-questions, answer them, and provide a final recommendation based on your reasoning.</pre></div><h2 id="c26d">12. Chain-of-Thought (CoT)</h2><p id="9756"><b>How to use:</b> Ask the model a question and add the phrase ‘Let’s think step by step’</p><p id="1046"><b>When to use: </b>For tasks that require reasoning, such as math or logic problems.</p><div id="421c"><pre>PROMPT: What is the total cost of a meal with a 10% discount and a 7% tax? Let's think step by step.</pre></div><h2 id="1fb0">13. Step-back Prompting</h2><p id="5cff"><b>How to use:</b> Ask a broad question, then prompt the model to answer a specific question based on its response to the broader question.</p><p id="d445"><b>When to use: </b>When the task involves analysis or decision-making based on multiple (broader/ foundational) factors.</p><div id="9d33"><pre>PROMPT: Explain the key factors that influence a company's decision to expand into a new market. Based on this, should a company in the tech industry expand to Europe?

</pre></div><h2 id="d847">14. Self-Consistency</h2><p id="c591"><b>How to use: </b>Ask a question or give an instruction then tell the LLM to generate multiple outputs and return only the most occurring answer.</p><p id="daaf"><b>When to use:</b> When the task has many possible answers and when consistency and accuracy are required.</p><div id="cf10"><pre>PROMPT: What is the most popular programming language for machine learning? Generate 5 possible answers and return only the one that appears most often.</pre></div><h2 id="1f9f">15. Thread-of-Thought (ThoT)</h2><p id="e7b1"><b>How to use: </b>Just like CoT, but instead of adding ‘let’s think step by step’, you’ll say ‘walk me through this in manageable parts step by step.”</p><p id="9c39"><b>When to use: </b>When the task involves question-answering and large complex contexts, such as in RAG systems.</p><div id="87f7"><pre>CONTEXT: I have a problem involving a group of people attending a party. There are 10 guests: Alice, Bob, Carol, Dave, Eve, Frank, Grace, Henry, Irene, and Jack. Each person has a preference for the type of music they want to listen to, and there are 3 types of music: Jazz, Rock, and Classical. Each person will only attend if they get to hear their preferred type of music. However, due to limited speakers, only 3 music genres can be played, and only one genre can be played at a time. PROMPT: Walk me through this in manageable parts step by step to figure out the maximum number of guests who can attend

Options

the party ( the maximum number of people who can be satisfied with their music preference).</pre></div><h2 id="243c">16. Tree-of-Thought (ToT)</h2><p id="d82e"><b>How to use: </b>Ask the model to break down a complex problem into smaller steps. At each step, the model should generate multiple possible solutions, evaluate their quality, and continue with the best option until it reaches a final solution.</p><p id="89aa"><b>When to use:</b> When the task requires deep reasoning, multi-step planning, and high accuracy.</p><div id="8571"><pre>PROMPT: I am designing a new type of coffee cup that keeps drinks hot longer.

Break down this problem into smaller steps. At each step, generate multiple possible solutions, evaluate their quality (consider factors like feasibility, cost-effectiveness, and potential impact), and continue with the best option until you reach a final solution.

Start by brainstorming initial design concepts.</pre></div><h2 id="5bc5">17. ReAct (Reason and Act)</h2><p id="f876"><b>How to use: </b>Instruct the LLM to generate a thought, take action based on that thought, observe the outcome of the action, and then use the observation to refine subsequent thoughts and actions.</p><p id="2aec"><b>When to use: </b>When the task requires iterative decision-making and interaction with external systems or data.</p><div id="5ee9"><pre>PROMPT: I need to find the latest market trends for electric vehicles. First, generate a thought about the most relevant keywords to search for. Then, perform a search using those keywords by calling the search API. Observe the results of the search, refine the keywords based on the data retrieved, and conduct another search if necessary. Repeat this process, adjusting your strategy based on the latest findings, until you find the most relevant and recent market trend data</pre></div><h1 id="e118">Closing Thoughts</h1><p id="d126">There’s no magic formula or one-size-fits-all when it comes to prompt engineering.</p><p id="7adf">Every model is different, and the best way to get the results you want is to experiment.</p><p id="44c9">You may need to tweak your instructions, add more context, or even combine multiple techniques as seen in some examples above.</p><p id="26c5">Now, over to you, what other prompt engineering techniques have you found useful?👉</p><h2 id="2ae6">Do you know you can clap up to 50 times?</h2><p id="3df8">If you gained some value from this, don’t forget to clap as many times and you can appreciate my work further by buying me a<a href="https://buymeacoffee.com/dataprincess"> coffee</a>.</p><figure id="ff62"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*UsVXRrmufFnE7E7_O86dQw.png"><figcaption></figcaption></figure><p id="b9e6">Thank you for reading :)</p><h2 id="afca">You may like my other article:</h2><div id="89e4" class="link-block"> <a href="https://readmedium.com/build-an-ai-agent-to-analyse-your-data-using-plain-english-ed7b038c7df8"> <div> <div> <h2>Build an AI Agent to Analyse Your Data Using Plain English</h2> <div><h3>In 5 Easy Steps and With Free Tools Only</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*QQ_a5IE3L2B7MgzMTnrCmQ.png)"></div> </div> </div> </a> </div><h1 id="bfda">Subscribe to my Newsletter!</h1><p id="3c48">I recently started a Newsletter ‘<a href="https://dataprincesstoyou.substack.com/subscribe"><i>Dataprincess To You</i></a>’ where I write short (1–2 mins read) letters about almost anything data — ideas, concepts, stories, trivia, and other things I’ve not thought about yet.</p><p id="8f49">Subscribe <a href="https://dataprincesstoyou.substack.com/subscribe">here</a> to receive my future letters :)</p><div id="d541" class="link-block"> <a href="https://dataprincesstoyou.substack.com/subscribe"> <div> <div> <h2>Subscribe to DATAPRINCESS TO YOU</h2> <div><h3>Data science, data analysis, ML, and AI ideas and tips from Dataprincess to you. Click to read DATAPRINCESS TO YOU, by…</h3></div> <div><p>dataprincesstoyou.substack.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*3WX8Ce2cR0R34AEy)"></div> </div> </div> </a> </div><p id="e857">Bye for now :D</p></article></body>

17 Prompt Engineering Techniques and When to Use Them

From Easy to Advanced with Examples

17 Prompt engineering techniques| Image by Author

Prompting is how you directly interact with Large Language Models (LLMs) i.e.

  • Through AI tools like ChatGPT.
  • When building LLM apps.

If you want to gain more control over the outputs of LLMs, then you need to master prompt engineering which is the art of crafting well-designed prompts to achieve just that.

With the right prompt engineering technique, you can improve these LLMs' accuracy, consistency, creativity, or functionality to fit your specific use cases.

In this blog, we will look at 17 proven prompt engineering techniques and learn when to apply them to get better responses from LLMs and create better LLM apps.

Let’s get started GIF | source

1. Zero-Shot Prompting

How to use: give clear instructions without examples.

When to use: when the task is simple and straightforward like translations or factual queries.

PROMPT: Translate the English phrase 'Flowers on the road' to Spanish.

2. One-Shot Prompting

How to use: give clear instructions and add a single example to demonstrate the task/ desired output.

When to use: when one example is needed (and enough) to clarify the task or output format for the model.

PROMPT: In uppercase, return the Spanish trasnlation of the English word 'basket' only. 

EXAMPLE:
English word (input): River
Spanish translation (output): RÍO

3. Few-Shot Prompting:

How to use: Give clear instructions with a few examples in the prompt.

When to use: When you need to adapt the model to a specific task/ domain without fine-tuning and when you need more consistent and accurate outputs.

PROMPT: return the sentiment of this statement 'The lecture was quite boring' only. Either positive, negative, or neutral.
EXAMPLES:
'This movie was great!': Positive, 
'I hated the service.': Negative,
'I don't know how I feel about it.': Neutral 

4. Role Prompting

How to use: Give clear instructions and assign a specific persona to the model .

When to use: When the task is open-ended and the output needs to be in line with a peculiar perspective, personality, or tone.

PROMPT: Write a short blog (500 words) with 4 points about college hacks.
ROLE: Act as a sweet college girl who uses a lot of Gen z slangs.

5. Style Prompting

How to use: Specify the desired style, tone, or genre in the prompt.

When to use: When the output needs to match a particular style or tone.

PROMPT: Write a brief formal email requesting a raise.

6. Emotion Prompting

How to use: Add an emotionally charged sentence or phrase to the prompt.

When to use: When the task involves creative text generation such as storytelling and poetry.

PROMPT: Write a poem about my lost imaginary friend who never gave up. I still miss my friend.

7. Contextual Prompting

How to use: provide background or custom information before giving clear instructions in the prompt.

When to use: When background or domain-specific details are needed to make the response more accurate or relevant i.e. in RAG chatbots.

CONTEXT: My name is Jennifer Luke and I'm a marketing manager in JL firm.
PROMPT: Write an email to the team about the upcoming campaign.

8. Rephrase and Respond (RaR)

How to use: Tell the LLM to rephrase the question into a better prompt before generating the final answer.

When to use: For higher accuracy on complex tasks or when you need to evaluate the LLM’s understanding of a task.

PROMPT: Rephrase and expand the following question, and then answer it: What is the difference between correlation and causation?

9. Re-reading (RE2)

How to use: Begin with an instruction or question, add the phrase: “Read that again:” immediately after, and then repeat the original instruction/question.

When to use: For complex tasks that involve reasoning.

PROMPT: 
A farmer has a rectangular field that is 3 times as long as it is wide. The perimeter of the field is 400 meters. What are the dimensions of the field?

Read the question again: "A farmer has a rectangular field that is 3 times as long as it is wide. The perimeter of the field is 400 meters. What are the dimensions of the field?"

10. System Prompting

How to use: Give the LLM high-level instructions or context that will be considered throughout the interaction with the LLM.

In ChatGPT, you can do this using the ‘customize GPT’ feature.

When building LLM apps, you can do this by providing the context/ instructions as the ‘system prompt’.

When to use: When you need to set the overall behavior and tone of the LLM in a conversational setting.

SYSTEM PROMPT: You are a helpful assistant that will provide factual responses in a concise tone

11. Self-Ask

How to use: Ask the LLM a question, tell it to break the question into smaller sub-questions all of which it would answer to arrive at the final answer.

When to use: When the task is complex, requires reasoning, or involves multiple steps.

PROMPT:
Should I pursue a master's degree in data science? 
Break this question into smaller sub-questions, answer them, and provide a final recommendation based on your reasoning.

12. Chain-of-Thought (CoT)

How to use: Ask the model a question and add the phrase ‘Let’s think step by step’

When to use: For tasks that require reasoning, such as math or logic problems.

PROMPT: What is the total cost of a meal with a 10% discount and a 7% tax? 
Let's think step by step.

13. Step-back Prompting

How to use: Ask a broad question, then prompt the model to answer a specific question based on its response to the broader question.

When to use: When the task involves analysis or decision-making based on multiple (broader/ foundational) factors.

PROMPT: Explain the key factors that influence a company's decision to expand into a new market. 
Based on this, should a company in the tech industry expand to Europe?

14. Self-Consistency

How to use: Ask a question or give an instruction then tell the LLM to generate multiple outputs and return only the most occurring answer.

When to use: When the task has many possible answers and when consistency and accuracy are required.

PROMPT: What is the most popular programming language for machine learning? 
Generate 5 possible answers and return only the one that appears most often.

15. Thread-of-Thought (ThoT)

How to use: Just like CoT, but instead of adding ‘let’s think step by step’, you’ll say ‘walk me through this in manageable parts step by step.”

When to use: When the task involves question-answering and large complex contexts, such as in RAG systems.

CONTEXT: I have a problem involving a group of people attending a party. There are 10 guests: Alice, Bob, Carol, Dave, Eve, Frank, Grace, Henry, Irene, and Jack. Each person has a preference for the type of music they want to listen to, and there are 3 types of music: Jazz, Rock, and Classical. Each person will only attend if they get to hear their preferred type of music. However, due to limited speakers, only 3 music genres can be played, and only one genre can be played at a time. 
PROMPT: Walk me through this in manageable parts step by step to figure out the maximum number of guests who can attend the party ( the maximum number of people who can be satisfied with their music preference).

16. Tree-of-Thought (ToT)

How to use: Ask the model to break down a complex problem into smaller steps. At each step, the model should generate multiple possible solutions, evaluate their quality, and continue with the best option until it reaches a final solution.

When to use: When the task requires deep reasoning, multi-step planning, and high accuracy.

PROMPT: I am designing a new type of coffee cup that keeps drinks hot longer.

Break down this problem into smaller steps. At each step, generate multiple possible solutions, evaluate their quality (consider factors like feasibility, cost-effectiveness, and potential impact), and continue with the best option until you reach a final solution.

Start by brainstorming initial design concepts.

17. ReAct (Reason and Act)

How to use: Instruct the LLM to generate a thought, take action based on that thought, observe the outcome of the action, and then use the observation to refine subsequent thoughts and actions.

When to use: When the task requires iterative decision-making and interaction with external systems or data.

PROMPT: I need to find the latest market trends for electric vehicles. 
First, generate a thought about the most relevant keywords to search for. 
Then, perform a search using those keywords by calling the search API. 
Observe the results of the search, refine the keywords based on the data retrieved, and conduct another search if necessary. 
Repeat this process, adjusting your strategy based on the latest findings, until you find the most relevant and recent market trend data

Closing Thoughts

There’s no magic formula or one-size-fits-all when it comes to prompt engineering.

Every model is different, and the best way to get the results you want is to experiment.

You may need to tweak your instructions, add more context, or even combine multiple techniques as seen in some examples above.

Now, over to you, what other prompt engineering techniques have you found useful?👉

Do you know you can clap up to 50 times?

If you gained some value from this, don’t forget to clap as many times and you can appreciate my work further by buying me a coffee.

Thank you for reading :)

You may like my other article:

Subscribe to my Newsletter!

I recently started a Newsletter ‘Dataprincess To You’ where I write short (1–2 mins read) letters about almost anything data — ideas, concepts, stories, trivia, and other things I’ve not thought about yet.

Subscribe here to receive my future letters :)

Bye for now :D

Artificial Intelligence
Prompt Engineering
AI
Llm
Recommended from ReadMedium