Sub topic 3
Anatomy of a Good Example
This chapter assumes you know what a large language model is, can read short Python, and know the system, user, and assistant roles in chat prompts. Here you will take one few-shot example apart and see what each piece does.
What an example contains
A few-shot example is a worked case that shows the model one complete input and the output you want for it. It has three parts:
- Input: the text the model will receive in real use, such as a review.
- Output: the exact answer you want back, in the form you want it.
- Delimiters: the labels and blank lines that mark where the input ends and the output begins.
Look at the prompt from the earlier chapter:
Classify the sentiment of each review as positive, negative, or neutral.
Review: The battery lasts all day and charging is fast.
Sentiment: positive
Review: It stopped working after a week.
Sentiment: negative
Review: The package arrived on Tuesday.
Sentiment: neutral
Review: The screen is bright but the speakers are weak.
Sentiment:The first line is the instruction. Each block after it is one example. Review: and Sentiment: are the delimiters. The blank line between blocks tells the model where one example ends. The last block has an input but no output, so the model completes it.
How the pieces work
The model reads the whole prompt and continues the pattern it sees. Each part of an example has a job.
| Part | Job | What goes wrong if it is weak |
|---|---|---|
| Input | Shows what real data looks like | The model fails on inputs unlike your samples |
| Output | Shows the answer and its form | The model invents its own format |
| Delimiters | Mark the boundaries | The model runs inputs and outputs together |
The unfinished last entry matters as much as the examples. It ends right after Sentiment:, so the most natural continuation is a single label. The model fills in that slot and usually stops at the next blank line.
Choosing the output form
Decide what the output looks like before you write any example. If you want one word, every example output must be one word. The model copies the length, casing, and punctuation of your outputs. An example that answers Positive. teaches the model to produce a capital letter and a period, and your code that checks for positive will then fail.
Keep the output only as long as the task needs. If your program parses the answer, show only the answer. Do not add a sentence such as "The sentiment is positive."
Choosing delimiters
Pick labels that name the role of each part, and use the same ones everywhere. Review: and Sentiment: are clear. Generic labels such as Input: and Output: also work. Avoid delimiters that can appear inside the data. If reviews can contain a colon or a blank line, add a stronger marker such as ### between examples, or put each input in quotes.
Writing examples in code
Writing prompts by hand gets tedious. A small function keeps the delimiters identical for every example:
def format_example(text, label=None):
block = f"Review: {text}\nSentiment:"
if label is not None:
block += f" {label}"
return block
def build_prompt(instruction, examples, query):
parts = [instruction]
for text, label in examples:
parts.append(format_example(text, label))
parts.append(format_example(query))
return "\n\n".join(parts)
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(
"Classify the sentiment of each review as positive, negative, or neutral.",
examples,
"The screen is bright but the speakers are weak.",
))Because one function builds both the examples and the query, the final entry always matches the examples exactly. Formatting drift between them is a common cause of odd output.
Common mistakes
- Giving the query a different label than the examples use.
- Leaving a trailing space after
Sentiment:in the query, which can change how the model tokenizes the next word. - Mixing output styles, such as
positivein one example andPositive!in another. - Writing examples that are too easy, so the model never sees a hard case.
Exercise
A teammate's prompt uses Input: and Output: in the first two examples, but the final query ends with Answer:. The model replies with a long paragraph. What is the likely cause, and how do you fix it?
Show answer
The final entry uses a different delimiter from the examples, so the pattern breaks and the model no longer knows to give a short label. Change the query to end with Output:, and ideally build every entry with one formatting function so the labels cannot differ.
Exercise
Your examples show outputs such as Positive. with a capital letter and a period, but your code compares the reply to positive. What should you change?
Show answer
Change the example outputs to the exact form your code expects, here lowercase positive with no period. The model copies the form of the example outputs.