Few-Shot Examples

Show the model a couple of input/output examples to lock in the pattern.

Few-shot prompting means including a few worked examples in your prompt. The model imitates the pattern you demonstrate — a fast way to control format and style without a long explanation.

Zero-shot vs. few-shot

  • Zero-shot — just the instruction, no examples. Fine for simple tasks.
  • Few-shot — instruction plus 1 to 5 examples. Better for specific formats, tricky edge cases, or a particular tone.

Example

Example · json
{
  "messages": [
    {"role": "system", "content": "Classify each message as POSITIVE or NEGATIVE."},
    {"role": "user", "content": "Great service!"},
    {"role": "assistant", "content": "POSITIVE"},
    {"role": "user", "content": "I waited an hour and no one helped."},
    {"role": "assistant", "content": "NEGATIVE"},
    {"role": "user", "content": "The food was cold and late."}
  ]
}

When to use it

  • A customer-support team adds three example ticket-to-category mappings to the prompt so the model learns their specific category labels without fine-tuning.
  • A data team adds two formatted input/output examples to teach the model to extract structured fields from unformatted order emails.
  • A content team shows the model three on-brand social-media posts so it generates new ones in the exact same voice and hashtag style.

More examples

Two-shot sentiment classification

Shows two labelled examples inline so the model learns the exact output format to follow.

Example · python
from openai import OpenAI

client = OpenAI()
r = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{
        'role': 'user',
        'content': (
            'Classify sentiment as POSITIVE or NEGATIVE.\n\n'
            'Text: I love this product!\nSentiment: POSITIVE\n\n'
            'Text: The delivery was terrible.\nSentiment: NEGATIVE\n\n'
            'Text: This is the best purchase I have made this year.\nSentiment:'
        )
    }]
)
print(r.choices[0].message.content)

Few-shot structured data extraction

Teaches the model an extraction format via two examples, then applies it to a new order line.

Example · python
from openai import OpenAI

client = OpenAI()
prompt = """Extract name and price from order text. Output: Name: X, Price: Y

Order: I would like to buy the Blue Hoodie for $29.99
Name: Blue Hoodie, Price: $29.99

Order: Can I get the Red Sneakers priced at $89.00?
Name: Red Sneakers, Price: $89.00

Order: Add the Green Backpack at $45.50 to my cart."""

r = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'user', 'content': prompt}]
)
print(r.choices[0].message.content)

Few-shot tone and style matching

Supplies two brand-voice examples so the model generates a new post in the same style and hashtag pattern.

Example · python
from openai import OpenAI

client = OpenAI()
examples = [
    'Post: Just dropped our summer collection. Hot takes only. #fashion #drop',
    'Post: New arrivals land Friday. You will not be ready. #newdrop #streetwear',
]
prompt = '\n'.join(examples) + '\nPost:'

r = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'user', 'content': prompt}]
)
print('Post:', r.choices[0].message.content)

Discussion

  • Be the first to comment on this lesson.