Today I Learned
Day 25 - Prompting Techniques - One/Few Shots
One-shot & few-shot
When creating prompts for AI models, it is helpful to provide examples. These examples can help the model understand what you are asking for. Examples are especially useful when you want to steer the model to a certain output structure or pattern.
- A one-shot prompt, provides a single example, hence the name one-shot. The idea is the model has an example it can imitate to best complete the task.
- A few-shot prompt provides multiple examples to the model. This approach shows the model a pattern that it needs to follow. The idea is similar to one-shot, but multiple examples of the desired pattern increases the chance the model follows the pattern.
The number of examples you need for few-shot prompting depends on a few factors, including:
- The complexity of the task
- The quality of the examples
- The capabilities of the generative AI (gen AI) model you are using.
As a general rule of thumb, you should use at least three to five examples for few-shot prompting. However, you may need to use more examples for more complex tasks, or you may need to use fewer due to the input length limitation of your model.
An example of few-shot prompt
- Goal: Parse pizza orders to JSON
- Model: gemini-pro
- Temperature: 0.1
- Token Limit: 250
- Top-K: N/A
- Top-P: 1
- Prompt:
Parse a customer's pizza order into valid JSON:
EXAMPLE:
I want a small pizza with cheese, tomato sauce, and pepperoni.
JSON Response:
{
"size": "small",
"type": "normal",
"ingredients": [["cheese", "tomato sauce", "peperoni"]]
}
EXAMPLE:
Can I get a large pizza with tomato sauce, basil and mozzarella
{
"size": "large",
"type": "normal",
"ingredients": [["tomato sauce", "bazel", "mozzarella"]]
}
Now, I would like a large pizza, with the first half cheese and
mozzarella. And the other tomato sauce, ham and pineapple.
JSON Response:- Output:
{
"size": "large",
"type": "half-half",
"ingredients": [["cheese", "mozzarella"], ["tomato sauce",
"ham", "pineapple"]]
}NOTES:
When you choose examples for your prompt, use examples that are relevant to the task you want to perform. The examples should be diverse, of high quality, and well written. One small mistake can confuse the model and will result in undesired output.
Tips: add edge case to the prompt for robust output:
If you are trying to generate output that is robust to a variety of inputs, then it is important to include edge cases in your examples. Edge cases are inputs that are unusual or unexpected, but that the model should still be able to handle.