Zero-shot and few-shot prompting: teaching by examples in the prompt

Updated

Zero-shot prompting asks a language model to perform a task without examples, relying on its pretrained knowledge. Few-shot prompting adds a few input-output examples to the prompt to steer the model toward the desired behavior. Both methods avoid fine-tuning and work by text instructions alone.

What zero-shot and few-shot prompting are

Zero-shot prompting is a prompt engineering method that asks a language model to infer an appropriate response without providing any examples of the desired output. The model relies on its pretrained knowledge to generate an answer from the instruction, context, and input data alone.

Few-shot prompting gives the model a few examples of a task within the prompt. This method is particularly useful when extensive labeled data is unavailable, and it helps the model adapt to a specific format or behavior. One-shot prompting sits in between, using a single example.

How zero-shot prompting works

A zero-shot prompt typically contains an instruction, context, input data, and an optional output indicator. The output indicator cues the model to respond with a specific type of output, such as a classification label. This structure is described in IBM's zero-shot prompting article.

Beyond simple classification, large language models can also reason in a zero-shot manner. Research shows that adding the phrase 'Let's think step by step' before each answer significantly improves performance on arithmetic and symbolic reasoning tasks without any hand-crafted examples, as demonstrated in the arXiv paper on zero-shot reasoners.

How few-shot prompting works

Few-shot prompting presents the model with several examples of the task in the prompt. In practice, the examples can be retrieved from a vector store using semantic search and then combined with the user query to form the prompt, a retrieval-augmented approach described in IBM's few-shot prompting article.

The GPT-3 paper on arXiv showed that scaling up language models greatly improves task-agnostic, few-shot performance. The model was applied without any gradient updates or fine-tuning, with tasks and demonstrations specified purely via text interaction.

The same finding is recorded in the NeurIPS version of the GPT-3 paper, which notes that GPT-3 is applied purely through text interaction. This text-only approach makes few-shot prompting a lightweight alternative to fine-tuning.

Practical considerations for writing prompts

Anthropic's prompting best practices advise being clear and direct, specifying the desired output format, and providing context or motivation behind instructions. The model behaves like a capable but new employee that needs precise guidance.

Zero-shot prompting is often the default strategy for a new problem because it requires no examples. However, tasks that need a strict output structure or a particular style benefit from a few examples. Few-shot prompting is especially helpful when labeled data is scarce and when the prompt must enforce a predefined format.

Accuracy after zero-shot chain-of-thoughtMultiArith: 78.7 percent; GSM8K: 40.7 percentAccuracy after zero-shot chain-of-thoughtMultiArith78.7 percentGSM8K40.7 percent
Figures from arXiv
Comparison of zero-shot, one-shot, and few-shot prompting
MethodExamples in promptTypical use caseHow the model responds
Zero-shotNoneNew tasks without examplesRelies on pretrained knowledge
One-shotOneWhen a single example clarifies the taskMimics that example's format
Few-shotSeveralWhen a few examples guide complex tasksGeneralizes patterns from examples

Frequently asked questions

What is the main difference between zero-shot and few-shot prompting?

Zero-shot prompting provides no examples, while few-shot prompting includes several examples in the prompt to guide the model's response.

When should a team use few-shot prompting?

Few-shot prompting is useful when labeled data is scarce and the task requires a specific output format that examples can illustrate.

Does few-shot prompting always require a vector store?

No, a vector store for semantic search is optional. It can help retrieve the most relevant examples, but simple static examples in the prompt also work.

What is zero-shot chain-of-thought?

It is a technique that adds a phrase like 'Let's think step by step' to a zero-shot prompt, eliciting multi-step reasoning without any examples.

Can zero-shot and few-shot prompting replace fine-tuning?

They avoid gradient updates and work through text alone, but fine-tuning may still achieve higher accuracy on specialized tasks.