Sub topic 4
Keeping Format and Wording Consistent
This chapter assumes you know what a few-shot prompt is, can read short Python, and understand the system, user, and assistant roles in chat prompts. Here you will see why every example in a prompt should follow one pattern, and how to enforce that pattern in code.
Why consistency matters
A model infers the task from the pattern shared by your examples. If the examples differ in layout, label spelling, or tone, the model has to guess which differences matter. It may copy any of them in its answer.
Compare two versions of the same labeled review.
Review: It stopped working after a week.
Sentiment: negative
review - The package arrived on Tuesday
=> Neutral
Text: The battery lasts all day.
Label: POSITIVEThis prompt uses three field names, three delimiters, and three spellings of the label. A model that completes the next entry could return positive, POSITIVE, or Positive. with a period. Any code that parses the reply must handle all of them.
What to hold constant
Keep these parts identical across all examples:
- Field names. Use
Review:andSentiment:every time. - Delimiters. Use the same blank line between examples and the same separator between input and output.
- Label vocabulary. Pick one spelling and case for each label. Do not mix
positive,Positive, andpos. - Output shape. If one answer is a single word, all answers are single words.
- Whitespace and punctuation. Trailing periods, extra spaces, and line breaks all count as pattern.
Vary the content of the inputs, not their packaging. Different lengths and topics are good. Different layouts are not.
The unfinished final entry
The last entry in the prompt must match the earlier ones up to the point where the model should answer. Stop right after the output field name, as in Sentiment:. The model then continues the pattern. If you add extra words after it, or leave out the field name, you break the pattern at the most important spot.
Generating examples from one template
Hand-typing each example invites drift. Build every example from one template instead.
TEMPLATE = "Review: {text}\nSentiment: {label}"
INSTRUCTION = "Classify the sentiment of each review as positive, negative, or neutral."
LABELS = {"positive", "negative", "neutral"}
def build_prompt(examples, query):
for _, label in examples:
if label not in LABELS:
raise ValueError(f"Unknown label: {label!r}")
blocks = [TEMPLATE.format(text=t, label=l) for t, l in examples]
blocks.append(TEMPLATE.format(text=query, label="").rstrip())
return INSTRUCTION + "\n\n" + "\n\n".join(blocks)
examples = [
("The battery lasts all day and charging is fast.", "positive"),
("It stopped working after a week.", "negative"),
("The package arrived on Tuesday.", "neutral"),
]
print(build_prompt(examples, "The screen is bright but the speakers are weak."))The printed prompt ends with Sentiment:. The template guarantees identical structure, and the label check rejects stray spellings before they reach the model. The rstrip() removes the trailing space that an empty label would otherwise leave after Sentiment:.
Wording of the instruction
The instruction should use the same words as the examples. If the instruction says "positive, negative, or neutral" but the examples use good and bad, the two parts disagree. State the allowed labels once, in the spelling the examples use.
How a mismatch spreads
Common errors
- Mixed case labels. An exact-match check against
positivefails onPositive. - Explanations in some examples only. If one example adds a reason, the model may add reasons unpredictably.
- Different delimiters for the query. The unfinished entry must use the same labels and separators as the examples.
- Hidden characters from copy-paste. Curly quotes or tab characters can differ from your other examples. Generating examples from a template avoids this.
Exercise
A few-shot prompt uses Review: and Sentiment: in two examples, but the third example uses Text: and Label:. The final query ends with Sentiment:. Name two likely problems and one fix.
Show answer
The model may mix the two field styles or vary its label format, and the third example may teach a different output pattern than the other two. The final Sentiment: also matches only two of the three examples. Fix it by building all examples, and the final query, from a single template string with the same field names, delimiters, and label set.
Exercise
In the build_prompt code, what does the check against LABELS protect you from, and why does it run before the prompt is built?
Show answer
It stops an example with a misspelled or differently cased label, such as Positive, from entering the prompt. It runs first so the error is raised while you are building the prompt, instead of appearing later as inconsistent model output.