Few-Shot Prompting: Including Examples in a Prompt

Few-shot prompting includes specific examples of what a model should generate given an input; the documentation says this improves accuracy and performance.

Verified

On this page

Definition

The documentation says few-shot prompting improves accuracy and performance by including specific examples of what a model should generate given an input. The explicit examples give the model a better understanding of the task and the output format the reader is looking for. Crafting a good prompt alone is also known as zero-shot prompting, and the documentation says it may not be enough to get the results you want.

Prompt engineering, or prompting, uses natural language to improve large language model (LLM) performance on a variety of tasks. A prompt can steer the model towards generating a desired output. The documentation advises starting with a short and simple prompt and iterating on it to get better results.

At a glance

The documentation describes two ways to structure a few-shot prompt: a single text string and a chat template. These are alternative formats.

  • Single string: embed the examples within one text string.
  • Chat template: structure the prompt as a turn-based conversation between user and assistant, then tokenize and format it with the model’s chat template.

The source does not state the order of these points, so they are listed without arrows.

How it works

The documentation says base models are excellent at completing text given an initial prompt, but they are not as good at following instructions. Instruction-tuned models are specifically trained versions of the base models on instructional or conversational data. To improve few-shot prompting for modern instruction-tuned LLMs, the documentation says to use a model's specific chat template.

These models are trained on datasets with turn-based conversations between a user and an assistant. The prompt is structured as a turn-based conversation, then tokenized and formatted by a method. The basic few-shot prompting approach embedded examples within a single text string, while the chat template format offers benefits.

With a chat template, the model may better understand the prompt by recognizing the pattern and the roles of user input and assistant output. The model may also more consistently output the desired output format because the prompt is structured like its input during training. The documentation advises clearly separating instructions from the text of interest.

Example

The documentation's few-shot example provides the model with 1 example (1-shot) of the output format it should return, which is a date in MM/DD/YYYY format. The documentation mentions experimenting with different numbers of examples, such as 2, 4 and 8, to see how it affects performance. A downside of few-shot prompting is that it requires creating lengthier prompts, which increases computation and latency.

Prompt lengths also have a limit. A model can also learn unintended patterns from the examples, and it may not work well on complex reasoning tasks. Chain-of-thought (CoT) is described as effective at generating more coherent and well-reasoned outputs by providing a series of prompts that help a model think more thoroughly about a topic.

Like few-shot prompting, CoT has the downside that it requires more effort to design a series of prompts that help the model reason through a complex task, and its prompt length increases latency. The documentation also says there are scenarios where a fine-tuned model, or even fine-tuning a model, works better. The documentation advises testing prompts with different models to assess their robustness.

From the official source
python
from transformers import pipeline
import torch

pipeline = pipeline(model="mistralai/Mistral-7B-Instruct-v0.1", dtype=torch.bfloat16, device_map="auto")
prompt = """Text: The first human went into space and orbited the Earth on April 12, 1961.
Date: 04/12/1961
Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon.
Date:"""

outputs = pipeline(prompt, max_new_tokens=12, do_sample=True, top_k=10)
for output in outputs:
    print(f"Result: {output['generated_text']}")
# Result: Text: The first human went into space and orbited the Earth on April 12, 1961.
# Date: 04/12/1961
# Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon.
# Date: 09/28/1960

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.