Sub topic 12
Testing a Few-Shot Prompt on Held-Out Data
This chapter assumes you can read short Python, know what a chat prompt looks like, and have already built a few-shot prompt with labeled examples. Here you learn how to check whether those examples help.
Why you need a held-out set
A prompt that looks good on three hand-picked reviews tells you little. To measure it, you need labeled data the prompt has never seen. This is a held-out set: inputs with known correct answers that are not used as in-prompt examples.
If an example appears in the prompt and also in your test data, the model can copy the answer. The score then reflects memorization of the prompt, not skill at the task. This is called leakage, and it makes a weak prompt look strong.
The working split has two parts:
- Example pool: labeled items you may place in prompts.
- Held-out set: labeled items used only for scoring.
Keep the two disjoint. Fix the split once, before you start tuning, so you do not drift toward examples that happen to help the test items.
Scoring a prompt
The accuracy function below takes any function that maps an input text to a model answer. It compares each answer to the true label and returns the fraction correct.
def accuracy(predict, labeled):
correct = 0
for text, label in labeled:
if predict(text).strip().lower() == label:
correct += 1
return correct / len(labeled)The strip() and lower() calls forgive stray whitespace and capitalization. They do not forgive extra words. If the model answers "Positive, because the battery is good," the comparison fails. That is often a prompt problem, so look at the failures before blaming the model.
Comparing variants
Run the same held-out set through each variant and compare the scores. Change one thing at a time, such as the number of examples, their order, or the instruction wording. Include a zero-shot baseline, so you can see whether the examples help at all.
The sketch below assumes a call_model(messages) function that you supply. It returns the model's reply as a string.
def make_predict(examples):
def predict(text):
return call_model(build_messages(examples, text))
return predict
variants = {
"zero-shot": [],
"3 examples": examples[:3],
"6 examples": examples[:6],
}
for name, exs in variants.items():
print(name, accuracy(make_predict(exs), held_out))Here build_messages is the chat builder from the earlier chapter on chat messages, and examples and held_out are lists of (text, label) pairs.
Reading the results
Accuracy alone can mislead. Check these points:
| Check | What to look for |
|---|---|
| Sample size | With 20 test items, one miss changes the score by 5 points. Small gaps between variants are noise. |
| Class balance | If 80% of items are "neutral," always answering "neutral" scores 0.80. Compare against that. |
| Failure types | Read the wrong answers. Are they format errors, one confused label, or truly hard cases? |
| Repeat runs | If sampling is random, run each variant several times, or set temperature to 0. |
Common errors
- Reusing an in-prompt example as a test item.
- Judging from a handful of items and treating a 5-point gain as real.
- Changing the examples and the instruction at once, so you cannot tell which helped.
- Counting format mismatches as reasoning errors, or the reverse.
Exercise
Your 3-example prompt scores 0.90 and your zero-shot prompt scores 0.85 on a held-out set of 20 items. Then you find that one of the three prompt examples is also in the held-out set. What do you do, and what can you conclude from the current numbers?
Show answer
Remove that item from the held-out set (or replace the example) and rerun both variants. The current numbers are unreliable because the model may have copied the answer for the leaked item. A 5-point gap on 20 items is also only one item, so it is within noise. Use a larger held-out set before concluding that the examples help.