Few Shot Prompting with Examples
In the previous lesson, we learned that a shot
refers to the number of examples used by an AI model to learn or understand a specific task.
Zero-shot prompting was when the AI performed new tasks solely based on pre-trained data, without any additional examples.
Few-shot
prompting is a technique where a few (few-shot) related examples are presented to the AI, and it is then asked to perform a specific task based on these examples.
The AI uses the provided examples to infer the context and optimal output formatting in order to generate an appropriate response to the input.
For instance, when analyzing review data for an e-commerce site, a prompt with a few examples might look like this:
Below are examples of e-commerce reviews.
Fill in the emotion for [?].
---
'I really love this product!' : Positive
'It was terrible. I will never buy it again.' : Negative
'Great value for the price!' : Positive
'The delivery was too late.' : Negative
'It was not as good as I expected.' : Negative
'I would recommend this product' : [?]
Positive
In the example above, the AI learns from responses like I really love this product!
, It was terrible. I will never buy it again.
and provides an appropriate reply for the new statement I would recommend this product
.
The AI analyzes the tone and format of the provided questions and answers and applies the analyzed results to maintain consistent responses to new questions.
Thus, in few-shot prompting, providing diverse and specific examples is essential. Generally, only 2-3 examples are used to perform the task, hence the term Few-Shot.
How is a few-shot prompt structured?
A few-shot prompt mainly consists of:
-
Instruction: A sentence instructing the AI on the task it needs to perform.
-
Examples: Examples for the model to refer to when generating a response.
Typically, few-shot prompts use 2-5 examples.
### Instruction
Translate the following sentence into a business-oriented professional tone, using the examples below as references.
"""
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: "As per your request, we have estimated the time required for the task as follows."
Example 2:
- English: "Please send me the estimate by tomorrow."
- Translation: "Please send me the estimate by tomorrow."
Features of Few-shot Prompting
-
Efficient context learning: AI quickly learns and adapts to the prompt based on a few examples.
-
Cost optimization: It saves time and cost (input tokens) by using optimized inputs with just a few examples.
-
Consistent response generation: It provides consistent responses to new questions based on the provided examples.
Is Few-shot Prompting composed of multiple prompts?
Few-shot prompting includes multiple examples within one prompt rather than multiple prompts.
Rephrase the following sentence in a formal tone using the examples provided.
"""
Hey, it's been a while! How have you been?
"""
---
1.
- Informal: "Hey, are you free tonight?"
- Formal: "Hello, I wanted to inquire if you are available this evening."
2.
- Informal: "That's a really good idea."
- Formal: "I believe that is an excellent idea."
The above prompt uses the separator ---
to include one instruction with two examples in one prompt.
Utilizing the two examples, the AI understands the context of the prompt and generates a formal sentence for the provided informal sentence.
Few-shot prompting helps AI to understand the prompt more accurately and perform tasks effectively with minimal data.
However, since only a few examples are provided, if the quality of the examples is low, there's a higher chance of the AI producing incorrect results.
Therefore, when using a few-shot prompt, it is crucial to carefully review the consistency and quality of the examples.
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.