How do I use few-shot prompting?
Setting up few-shot examples
In few-shot prompting, you provide 2-5 input-output examples that demonstrate the task. For instance, if you want to classify sentiment, you might show: 'I love this! -> positive', 'This is terrible. -> negative', then ask for a new sentence. The model picks up on the pattern and applies it.
The examples should be clear, correct, and representative of the task. They can be formatted as a list or a dialogue. The key is to show the model exactly what you expect, including any specific labels or formatting.
- Use 2-5 examples for best results
- Keep examples consistent in format
- Cover edge cases if relevant
- Place examples before the actual query
- Label examples clearly if needed
When few-shot helps
Few-shot prompting is great for tasks where the desired output format is unusual or where zero-shot (no examples) fails. It's commonly used for translation, text summarization, data extraction, and style imitation. It can also reduce bias and improve accuracy on niche topics.
However, few-shot uses more tokens and may not be necessary for simple tasks. For very complex tasks, you might need more examples or combine with chain-of-thought. Always test with and without examples to see what works best for your model.
- Custom output formats (e.g., JSON, tables)
- Tasks with specific jargon or style
- When zero-shot gives inconsistent results
- Classification with multiple categories
- Generating text in a particular voice
Common mistakes
- Using too many examples, which can confuse the model or waste tokens.
- Providing inconsistent or incorrect examples that mislead the model.
- Forgetting to include the actual query after the examples.
