Few-shot classification
Classify incoming text into labels by showing labeled examples.
AI & ML · Const
Few-shot learning steers a language model with a handful of input/output examples in the prompt. The model infers the pattern and applies it to a new task input.
Few-shot learning guides a language model with a small set of input/output demonstrations included in the prompt. The model infers the demonstrated pattern and applies it to a new request, commonly with the examples placed in a developer message.
developer message containing input/output demonstrationsThe model's response to a new input, generated according to the demonstrated pattern.
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
input=[
{"role":"developer","content":"Classify sentiment.\n\nExamples:\nInput: I love it.\nOutput: positive\n\nInput: It broke.\nOutput: negative"},
{"role":"user","content":"The service was excellent."}
]
)
print(response.output_text)The developer message supplies demonstrations, and the user message contains a new input for the model to classify.
Classify incoming text into labels by showing labeled examples.
Convert inputs into a target format by demonstrating representative transformations.
Teach a response style or task procedure without fine-tuning.
Few-shot learning steers a model by placing several input/output demonstrations in the prompt instead of fine-tuning it. The model infers the pattern represented by those demonstrations and uses it for a new input.
Follow-up: How should the demonstrations be selected? Include a diverse range of possible inputs paired with the desired outputs.
The examples are usually supplied as part of a developer message in the API request. The new task input is then presented separately for the model to process using the demonstrated pattern.
Follow-up: Why use a developer message? It is the documented location OpenAI recommends for these examples in an API request.
Few-shot learning uses a handful of examples in the prompt, while fine-tuning trains the model with many more examples than can fit in the prompt. Fine-tuning can therefore improve performance across a wider range of tasks.
Follow-up: What is the main practical trade-off? Prompt demonstrations avoid training but consume prompt space; fine-tuning requires a training workflow but supports more examples.
promptingdeveloper messagefine-tuninginput/output examples