Tutorials / Prompt Engineering with ChatGPT / Showing examples: few-shot prompting

Showing examples: few-shot prompting

14 min read text
Course contents (19 lessons)
  1. Understanding the tool
  2. What a language model is, in plain words
  3. How ChatGPT works from a user's point of view
  4. Building a good prompt
  5. The anatomy of a good prompt
  6. Being specific: how to remove guesswork
  7. Showing examples: few-shot prompting
  8. Asking for step-by-step reasoning
  9. Controlling and improving answers
  10. Controlling length, tone and format
  11. Iterating: how to improve an answer with follow-ups
  12. Custom instructions and memory
  13. Working with files and images
  14. ChatGPT for real tasks
  15. Using ChatGPT for writing: emails, reports and rewrites
  16. Using ChatGPT for studying and learning
  17. Using ChatGPT for coding and technical work
  18. Using ChatGPT for data, spreadsheets and business tasks
  19. Prompt templates
  20. A library of reusable prompt templates
  21. Reliable and safe use
  22. Avoiding hallucinations and checking facts
  23. Privacy and safe use
  24. Workflows and final project
  25. Building repeatable workflows and custom assistants
  26. Final project: build your own prompt toolkit

Show, do not just tell

Sometimes a description of what you want is hard to write but easy to show. Imagine telling a new hire how you like customer emails to sound. You could write a long list of rules, or you could hand over three emails you are proud of and say "like these". The second way is faster and clearer. The same works with ChatGPT.

Giving examples inside the prompt is called few-shot prompting. "Few" means a small number, usually two to five. A prompt with no examples is called zero-shot. A prompt with exactly one is one-shot. The model reads your examples, spots the pattern in them, and continues it.

A simple classification example

Suppose you want to sort customer messages into categories. First try zero-shot:

Classify this message as Billing, Technical, or Other:
"I was charged twice this month."

That will probably work. But what if your labels have your own meanings, or you want a strict output with no extra words? Add examples:

Classify each customer message as Billing, Technical or Other.
Reply with the label only.

Message: "My invoice shows the wrong company name."
Label: Billing

Message: "The app crashes when I open settings."
Label: Technical

Message: "Do you have an office in Karachi?"
Label: Other

Message: "I was charged twice this month."
Label:

The last line stops after "Label:". The model completes the pattern and returns one word. The examples taught it three things at once: the labels, the format, and the fact that there should be no explanation. You did not have to spell out any of those.

How to choose examples

  • Cover the variety. If you have three categories, show at least one example of each. Include one tricky or borderline case.
  • Keep them consistent. If your examples use different formats, the model will mix formats in the answer. Format every example the same way.
  • Make them realistic. Use real samples from your own work, with private details removed.
  • Do not overdo it. Three good examples often beat ten average ones. More examples cost length, and your model reads all of them every time.
  • Mind the order and balance. If every example has the same label, the model may lean toward that label. Mix them.

Example: matching a writing style

Style is a perfect use. It is much easier to show a voice than to describe it.

I write short product descriptions for a handmade soap shop. Here are
two I wrote:

1. "Oatmeal and honey bar. Gentle on dry hands, smells like warm toast.
   Made in small batches, wrapped in paper."
2. "Charcoal and mint bar. Clears the day off your skin. Cool, a bit
   gritty, very clean. Wrapped in paper."

Write one in the same voice for a lavender and goat milk bar.
Do not use the words "luxurious" or "pamper".

Look at what the examples carry: short sentences, a fragment style, sensory words, an honest tone and a repeated ending about packaging. A description such as "casual and sensory" could not carry all of that. The "do not use" line adds a guard against the typical marketing words the model likes.

Example: extracting data into a fixed shape

Pull the key details from each booking message into one line in this
exact format: Name | Date | Guests | Special request (or "none").

Message: "Hi, it's Maria Lopez, table for 4 on 12 June please, one is vegan."
Output: Maria Lopez | 12 June | 4 | one vegan guest

Message: "Booking for Tom Reed, 2 people, 15 June, nothing special."
Output: Tom Reed | 15 June | 2 | none

Message: "Aisha Khan here, 6 of us on the 20th of June, need a high chair."
Output:

Notice the date format in the examples. The model will copy whatever style you show. If your examples write "12 June" it will write "20 June" and not "June 20th". This kind of exact control is hard to get with instructions alone.

Bad examples teach bad habits

The model copies everything it sees, including your mistakes and accidents. If your sample answers all happen to be very short, the output will be very short. If one example has a typo, it may produce typos. Read your examples as if they were the whole rulebook, because for the model they are.

You can also show a bad example on purpose, clearly labelled:

Rewrite the sentence to be more direct.

Too wordy: "At this point in time we are in a position to offer you a refund."
Direct: "We can refund you now."

Too wordy: "It would be greatly appreciated if you could kindly send the form."
Direct:

Before-and-after pairs are among the most effective few-shot formats, because they show the change you want rather than the end result alone.

When examples are not needed

For common tasks, such as "summarise this paragraph", examples add little. Reach for them when the task has your own rules, your own labels, a strict output format, or a style that is hard to describe.

The common mistake

The most common slip is leaving the examples inconsistent, for example "Label:" in one and "Category -" in another. The second is using only one example, which the model copies almost word for word. If the result looks like a lightly edited version of your example, add a second or third example that is clearly different.

Try it yourself

  1. Write three examples of messages you send often and ask ChatGPT to write a fourth in the same style about a new situation.
  2. Build a classifier prompt for your own work, such as sorting emails by urgency. Use three examples and test it on five real messages.
  3. Take one weak answer you got in the past and rebuild the prompt with a before-and-after pair.
text
Classify each customer message as Billing, Technical or Other.
Reply with the label only.

Message: "My invoice shows the wrong company name."
Label: Billing

Message: "The app crashes when I open settings."
Label: Technical

Message: "Do you have an office in Karachi?"
Label: Other

Message: "I was charged twice this month."
Label:

This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.

Sign up free I already have an account