Chapter 2 of 8
Few-shot prompting: teaching by example
Zero-shot means you ask with only an instruction. Few-shot means you show the model a few worked examples first, and this is one of the strongest levers you have. Examples teach two things at once: what a correct answer looks like, and what format it should take. The model reads your demonstrations and copies the pattern. In this lab you classify the same review twice, once zero-shot and once with two examples that write the label in uppercase, and you watch the answer flip its format to match the examples while keeping the same meaning. That is the examples doing real, measurable work.
The lab: read it, then run it
#!/usr/bin/env python3
"""
LAB PE2: Few-shot prompting. Teaching by example.
Zero-shot asks the model to do a task with only an instruction. Few-shot shows
it a handful of worked examples first, and those examples measurably steer both
WHAT it answers and the FORMAT it answers in. In this lab you classify the same
review zero-shot and few-shot, and prove the examples changed the output: the
demonstrated UPPERCASE label format carries straight into the answer.
Run: python3 modules/academy-content/labs/prompt-engineering/pe2-few-shot.py
"""
import sys, os
_cands = [os.path.join(os.path.dirname(__file__), "..") if "__file__" in globals() else None,
os.path.join(os.getcwd(), "..", "labs"), os.path.join(os.getcwd(), "labs")]
for _c in _cands:
if _c and os.path.exists(os.path.join(_c, "academy_llm.py")):
sys.path.insert(0, os.path.abspath(_c)); break
from academy_llm import complete, chat, embed, cosine, tool_route
review = "I love this"
# 1. ZERO-SHOT: instruction only. The model classifies, but in its own default
# lowercase format.
zero = complete("Classify the sentiment of this review: " + review)
print("STEP 1: zero-shot (instruction only)")
print(f" output : {zero!r}")
# 2. FEW-SHOT: prepend worked examples. The demonstrated outputs are UPPERCASE,
# so the model copies that format. Same task, different output.
few = complete(
"great -> POS\n"
"awful -> NEG\n"
"Classify the sentiment of this review: " + review
)
print("")
print("STEP 2: few-shot (examples then the task)")
print(" examples: great -> POS , awful -> NEG")
print(f" output : {few!r}")
# 3. Prove the examples DID something: the answer is still correct (positive),
# but the format flipped to match the demonstrations.
same_meaning = (zero == "positive" and few == "POSITIVE")
changed = (zero != few)
print("")
print(f"STEP 3: meaning preserved (both positive) : {same_meaning}")
print(f" format changed by the examples : {changed}")
ok = same_meaning and changed
print("")
print(f"FEW-SHOT STEERS OUTPUT: {'YES' if ok else 'NO'}")
if not ok:
sys.exit(1)
print("Examples teach format and behavior. Next: chain-of-thought reasoning.")
Runnable lab
pe2-few-shot.pyClassify a review zero-shot and few-shot, and prove the examples changed the output format.
Proves: FEW-SHOT STEERS OUTPUT: YES
Runs in your browser via Pyodide. First run loads the runtime once; no install, no server.
The meaning stayed the same, the review was still positive, but the format changed to match your examples. That is the core of few-shot: the model imitates what you show it. Two cautions for real work. Use three to five diverse examples, because too few is noisy and examples that all look alike teach a narrow, biased pattern. And remember examples cost tokens, so reach for few-shot when format or edge cases matter, not for every call. Next you learn to make a model reason through hard problems instead of guessing.
Check your understanding
1. What does few-shot prompting add over zero-shot?
2. In the lab, what changed when the uppercase examples were added?
3. Why use three to five diverse examples rather than one?