Few-Shot Prompting: Asking with Examples
In the previous lesson, we learned that a shot refers to the number of examples used to help an AI model learn or understand a specific task.
Zero-shot prompting was a method in which AI performs a new task using only its pre-trained data, without any separate examples provided.
Few-shot prompting is a prompting technique in which you give the AI a few (few-shot) relevant examples and ask it to perform a specific task based on those examples.
The AI infers the context and optimal output format for solving the problem from the provided examples, and generates an appropriate response to new inputs.
For example, when analyzing product review data from an online store, you can write a prompt like this with a few examples included:
Below are examples of online store reviews.
Please identify the sentiment that belongs in [?].
---
'I really love this product!' : Positive
'It was disappointing. I won't buy it again.' : Negative
'Excellent value for the price!' : Positive
'The delivery was way too slow.' : Negative
'It was not as good as I expected.' : Negative
'I recommend this product.' : [?]
Positive
In the example above, the AI learns from the provided question-answer pairs, such as 'I really love this product!' and 'It was disappointing. I won't buy it again.', and then provides an appropriate answer for [?] in response to the new input 'I recommend this product.'
The AI analyzes the tone and format of each provided question-answer pair, applies the analysis to the new question, and delivers a consistent response.
As shown, the key to few-shot prompting is providing diverse and specific examples, though as the name implies, it typically works with just a few.
How Is a Few-Shot Prompt Structured?
A few-shot prompt consists mainly of:
-
Instruction: A sentence telling the AI what task to perform
-
Examples: Examples the model will reference when generating its response
In general, few-shot prompting uses 2–5 examples.
### Instruction
Using the examples below, please rewrite the following sentence in a business-specialized, professional tone.
"""
Our company specializes in providing solutions that
enhance business efficiency through the use of cutting-edge technology.
"""
### Examples
Example 1:
- Casual: "Can you let me know how long this will take?"
- Professional: "Could you please let us know the estimated time required to complete this task?"
Example 2:
- Casual: "Send me the estimate by tomorrow."
- Professional: "Kindly send us the estimate by tomorrow."
Characteristics of Few-Shot Prompting
-
Efficient in-context learning: AI quickly learns from the provided examples and adapts to the prompt.
-
Cost optimization: Using a small number of examples reduces time and cost (input token count).
-
Consistent response generation: Responds consistently to new questions based on the provided examples.
Does Few-Shot Prompting Use Multiple Prompts?
Few-shot prompting does not use multiple separate prompts. It includes multiple examples within a single prompt.
Using the examples below, please rephrase the following sentence in a formal tone.
"""
Hey, long time no see! How have you been?
"""
---
1.
- Informal: "Hey, are you free this evening?"
- Formal: "Hello, I was wondering if you might be available this evening."
2.
- Informal: "That sounds like a really good idea."
- Formal: "I believe that is an excellent idea."
The prompt above is a single prompt that uses the delimiter --- to separate one instruction and two examples.
The generative AI understands the context of the prompt based on the two examples, and produces a formal version of the given informal sentence.
Few-shot prompting helps AI understand prompts more accurately and perform tasks more effectively using only a small amount of data.
However, because only a limited number of examples are provided, if the quality of those examples is poor, there is a higher chance the AI will produce incorrect results.
Therefore, when using few-shot prompts, it is important to carefully review the consistency and quality of the examples you include.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.