Few-Shot Prompting with Examples
In the previous lesson, we learned that a shot
refers to the number of examples used for the AI model to learn or understand a specific task.
Zero-shot prompting is when the AI performs new tasks using only the pre-trained data without additional examples.
Few-shot
prompting is a technique where a few related examples are provided to the AI, and it is asked to perform a specific task based on those examples.
The AI infers the context and the optimal output formatting from the provided examples to generate appropriate responses for given inputs.
For instance, when analyzing review data from an online store, you can craft a prompt with a few examples as follows:
Below are examples of online store reviews.
Identify the sentiment to fill in the [?].
---
'I really love this product!' : Positive
'It wasn’t good. I’ll never buy it again.' : Negative
'Excellent value for the price!' : Positive
'The shipping was far too slow.' : Negative
'It was not as great as I expected.' : Negative
'I recommend this product' : [?]
Positive
In the example above, the AI learns from questions like I really love this product!
, It wasn’t good. I’ll never buy it again.
, and provides a suitable answer for the new question I recommend this product
at [?]
.
The AI analyzes the tone and format of the given questions and answers and applies the analyzed results to new questions to provide consistent answers.
The key to few-shot prompting is to provide diverse and specific examples, but as the name suggests, it typically uses 2-3 examples to perform the task.
How is a Prompt Structured in Few-Shot Prompting?
Few-shot prompting is mainly structured into:
-
Instruction: A sentence directing the AI on the task to perform
-
Examples: References for the model to generate responses
Typically, few-shot prompting uses 2-5 examples.
### Instruction
Translate the following sentence into a formal business tone based on the examples below.
"""
Our company specializes in providing solutions that
enhance business efficiency through the use of cutting-edge technology.
"""
### Examples
Example 1:
- English: "As per your request, we have estimated the time required for the task as follows."
- Translation: "In response to your request, we have determined the estimated time for task completion as follows."
Example 2:
- English: "Please send me the estimate by tomorrow."
- Translation: "Kindly provide the quotation by tomorrow."
Features of Few-Shot Prompting
-
Efficient Context Learning: The AI quickly learns and adapts to the prompt using a minimal number of examples.
-
Cost Optimization: Optimized input with a few examples saves time and costs (number of input tokens).
-
Consistent Response Generation: Provides consistent responses to new questions based on given examples.
Does Few-Shot Prompting Consist of Multiple Prompts?
Few-shot prompting involves one prompt that includes multiple examples rather than multiple prompts.
Use the examples below to rephrase the following sentence in a formal tone.
"""
It's been a while! How have you been?
"""
---
1.
- Informal: "Hey, are you free tonight?"
- Formal: "Good evening, I hope you are available this evening?"
2.
- Informal: "That sounds like a great idea."
- Formal: "I believe that is an excellent idea."
The above prompt uses a delimiter ---
to separate one instruction and two examples within one prompt.
The generative AI understands the context of the prompt based on the two examples and creates a formal sentence for the informal sentence provided.
Few-shot prompting helps AI comprehend and perform prompts more accurately with minimal data.
However, since the AI is given only a few examples, poor quality of those examples may increase the likelihood of incorrect results.
Therefore, when utilizing few-shot prompting, it is crucial to meticulously review the consistency and quality of the examples provided.
Practice
Compare AI responses through various prompt examples.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.