Mastering Prompt Engineering: A Beginner’s Guide to AI Interaction
In today’s world of artificial intelligence (AI), prompt engineering has become a key skill. It changes how we talk to AI models and make them work better. Whether you’re experienced or just starting, knowing how to create good prompts can make a big difference in how well your AI applications perform.
Prompt engineering is about designing the questions or instructions we give to AI models. This helps them give accurate and useful answers. It’s not just a technical skill; it’s also about using creativity and careful thinking to get the best results from AI.
In this blog, we’ll explore what prompt engineering is, how it works, and why it matters. You’ll learn simple techniques and see real-life examples of how it can improve things like chatbots and content creation. Join us as we uncover the basics of prompt engineering and how it can help you make smarter, more effective AI solutions.
1. Zero Shot Prompting
Zero-shot prompting is a technique used in the field of artificial intelligence where a model generates responses or performs tasks without any prior specific training on those tasks. This approach relies on the general knowledge and capabilities that the model has acquired during its initial comprehensive training phase. In zero-shot prompting, the model is given a task description or a prompt and is expected to understand and execute the task based solely on this input, without any examples or additional guidance. This method is particularly valuable for testing the flexibility and adaptability of AI models, as it showcases their ability to handle a wide variety of tasks and questions they have not explicitly been prepared for.
Example
Prompt:
Classify the text into neutral, negative or positive.
Text: I think the vacation is okay.
Sentiment:
Output:
Neutral
Limitation
Firstly, these models often aren’t as accurate as models trained on specific tasks because they’re trying to generalize without direct examples. This can lead to errors or lower confidence in the results. Also, because they handle such a broad range of tasks, zero-shot models might struggle with very detailed or niche requests, where specialized knowledge is needed.
Prompt:
An example of a sentence that uses the word farduddle is:
Output:
The word "farduddle" doesn't appear to be a standard or recognized English
word. Could it be a typo or a specific term from a particular context or
language that you're exploring? If you have more details or a different
spelling, please share, and I'll be happy to help with a sentence!
2. Few Shot Prompting
Few-shot prompting is a technique used in the field of artificial intelligence, particularly with models like GPT (Generative Pre-trained Transformer), to improve the model’s ability to understand and generate context-specific responses with minimal input data. Unlike zero-shot or one-shot methods, few-shot prompting involves providing the AI with a few examples to guide its responses, thereby helping the model infer the desired task from these examples without explicit programming.
Example
Prompt:
A "whatpu" is a small, furry animal native to Tanzania.
An example of a sentence that uses the word whatpu is:
We were traveling in Africa and we saw these very cute whatpus.
To do a "farduddle" means to jump up and down really fast.
An example of a sentence that uses the word farduddle is:
Output:
When we won the game, we all started to farduddle in celebration.
Limitation
Standard few-shot prompting is good for many tasks, but it’s not perfect, especially for complex thinking tasks. Let’s show why that is.
Prompt:
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: 17, 10, 19, 4, 8, 12, 24.
A: The answer is True.
The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24.
A: The answer is True.
The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2.
A: The answer is False.
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:
Output:
The answer is True.
3. Chain-of-Thought (CoT) Prompting
Chain-of-thought (CoT) prompting helps machines think through problems step-by-step to understand them better. You can use it with few-shot prompting, where the machine sees a few examples first, to improve how it handles more difficult tasks that need thinking before answering.
Example
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. The answer is False.
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:
Output:
Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False.
3.a. Zero-shot CoT Prompting
Let’s try a simple problem and see how the model performs:
Prompt:
I went to the market and bought 10 apples.
I gave 2 apples to the neighbor and 2 to the repairman.
I then went and bought 5 more apples and ate 1.
How many apples did I remain with?
Output:
11 apples
The answer is incorrect! Now Let’s try with the special prompt “Let’s think step by step.”.
Example
Prompt:
I went to the market and bought 10 apples.
I gave 2 apples to the neighbor and 2 to the repairman.
I then went and bought 5 more apples and ate 1.
How many apples did I remain with?
Let's think step by step.
Output:
First, you started with 10 apples.
You gave away 2 apples to the neighbor and 2 to the repairman,
so you had 6 apples left.
Then you bought 5 more apples, so now you had 11 apples.
Finally, you ate 1 apple, so you would remain with 10 apples.
It’s remarkable that such a simple prompt works well for this task, especially when you don’t have many examples to use.
3.b. Automatic Chain-of-Thought (Auto-CoT)
It consists of two main stages:
- Question Clustering: partition questions of a given dataset into a few clusters
- Demonstration Sampling: select a representative question from each cluster and generate its reasoning chain using Zero-Shot-CoT with simple heuristics
Example
4. Self-Consistency Decoding
Self-consistency aims “to replace the naive greedy decoding used in chain-of-thought prompting”. The idea is to sample multiple, diverse reasoning paths through few-shot CoT, and use the generations to select the most consistent answer. This helps to boost the performance of CoT prompting on tasks involving arithmetic and commonsense reasoning. The self-consistency method contains three steps:
- Prompt a language model using chain-of-thought (CoT) prompting.
- Replace the “greedy decode” in CoT prompting by sampling from the language model’s decoder to generate a diverse set of reasoning paths.
- Marginalize out the reasoning paths and aggregate by choosing the most consistent answer in the final answer set.
Example
Computing for the final answer involves a few steps (check out the paper for the details) but for the sake of simplicity, we can see that there is already a majority answer emerging so that would essentially become the final answer.
5. Generated Knowledge Prompting
LLMs continue to be improved and one popular technique includes the ability to incorporate knowledge or information to help the model make more accurate predictions. Using a similar idea, can the model also be used to generate knowledge before making a prediction.
Let’s try a simple prompt:
Prompt:
Part of golf is trying to get a higher point total than others. Yes or No?
Output:
Yes.
This type of mistake reveals the limitations of LLMs to perform tasks that require more knowledge about the world.
It involves
1. Knowledge Generation: using few-shot demonstrations to generate question related knowledge statements from a language model
Prompt:
Input: Greece is larger than mexico.
Knowledge: Greece is approximately 131,957 sq km, while Mexico is approximately
1,964,375 sq km, making Mexico 1,389% larger than Greece.
Input: Glasses always fog up.
Knowledge: Condensation occurs on eyeglass lenses when water vapor from your
sweat, breath, and ambient humidity lands on a cold surface, cools, and then
changes into tiny drops of liquid, forming a film that you see as fog. Your
lenses will be relatively cool compared to your breath, especially when the
outside air is cold.
Input: A fish is capable of thinking.
Knowledge: Fish are more intelligent than they appear. In many areas, such as
memory, their cognitive powers match or exceed those of ’higher’ vertebrates
including non-human primates. Fish’s long-term memories help them keep track
of complex social relationships.Input: A common effect of smoking lots of
cigarettes in one’s lifetime is a higher than normal chance of getting lung
cancer.Knowledge: Those who consistently averaged less than one cigarette per
day over their lifetime had nine times the risk of dying from lung cancer than
never smokers. Among people who smoked between one and 10 cigarettes per day,
the risk of dying from lung cancer was nearly 12 times higher than that of
never smokers.
Input: A rock is the same size as a pebble.
Knowledge: A pebble is a clast of rock with a particle size of 4 to 64
millimetres based on the Udden-Wentworth scale of sedimentology. Pebbles are
generally considered larger than granules (2 to 4 millimetres diameter) and
smaller than cobbles (64 to 256 millimetres diameter).
Input: Part of golf is trying to get a higher point total than others.
Knowledge:
Knowledge 1:
The objective of golf is to play a set of holes in the least number of strokes.
A round of golf typically consists of 18 holes. Each hole is played once in
the round on a standard golf course. Each stroke is counted as one point, and
the total number of strokes is used to determine the winner of the game.
Knowledge 2:
Golf is a precision club-and-ball sport in which competing players (or golfers)
use many types of clubs to hit balls into a series of holes on a course using
the fewest number of strokes. The goal is to complete the course with the
lowest score, which is calculated by adding up the total number of strokes
taken on each hole. The player with the lowest score wins the game.
2. Knowledge Integration: using a second language model to make predictions with each knowledge statement, then selecting the highest-confidence prediction.
Prompt 1:
Question: Part of golf is trying to get a higher point total than others.
Yes or No?
Knowledge: The objective of golf is to play a set of holes in the least number
of strokes. A round of golf typically consists of 18 holes. Each hole is
played once in the round on a standard golf course. Each stroke is counted
as one point, and the total number of strokes is used to determine the winner
of the game.
Explain and Answer:
Answer 1 (confidence very high):
No, the objective of golf is not to get a higher point total than others.
Rather, the objective is to play a set of holes in the least number of strokes.
The total number of strokes is used to determine the winner of the game, not
the total number of points.
Prompt 2:
Question: Part of golf is trying to get a higher point total than others.
Yes or No?
Knowledge: Golf is a precision club-and-ball sport in which competing players
(or golfers) use many types of clubs to hit balls into a series of holes on a
course using the fewest number of strokes. The goal is to complete the course
with the lowest score, which is calculated by adding up the total number of
strokes taken on each hole. The player with the lowest score wins the game.
Explain and Answer:
Answer 2 (confidence is a lot lower):
Yes, part of golf is trying to get a higher point total than others. Each
player tries to complete the course with the lowest score, which is calculated
by adding up the total number of strokes taken on each hole. The player with
the lowest score wins the game.
6. Tree-of-Thought Prompting
For complex tasks that require exploration or strategic lookahead, traditional or simple prompting techniques fall short. Tree of Thoughts (ToT), isa framework that generalizes over chain-of-thought prompting and encourages exploration over thoughts that serve as intermediate steps for general problem solving with language models.
Lets ask the following question to GPT-3.5
The answer is wrong. Lets try CoT
You can see how ChatGPT realised the ball fell out at step 5 but still didn’t get the answer correct.
ToT maintains a tree of thoughts, where thoughts represent coherent language sequences that serve as intermediate steps toward solving a problem. This approach enables an LM to self-evaluate the progress through intermediate thoughts made towards solving a problem through a deliberate reasoning process. The LM’s ability to generate and evaluate thoughts is then combined with search algorithms (e.g., breadth-first search and depth-first search) to enable systematic exploration of thoughts with lookahead and backtracking.
Example:
When using ToT, different tasks requires defining the number of candidates and the number of thoughts/steps. For instance, Game of 24 is used as a mathematical reasoning task which requires decomposing the thoughts into 3 steps, each involving an intermediate equation. At each step, the best b=5 candidates are kept.
To perform BFS in ToT for the Game of 24 task, the LM is prompted to evaluate each thought candidate as “sure/maybe/impossible” with regard to reaching 24. As stated by the authors, “the aim is to promote correct partial solutions that can be verdicted within few lookahead trials, and eliminate impossible partial solutions based on “too big/small” commonsense, and keep the rest “maybe””. Values are sampled 3 times for each thought. The process is illustrated below: