Few-shot examples for edge cases
Zero-shot is a great baseline, but it stumbles on the ambiguous cases: sarcasm, mixed sentiment, factual statements that read neutral. Few-shot prompting fixes this by showing the model worked examples inside the prompt itself. You are not retraining anything. You are just teaching the model what your labels mean through demonstration.
FEW_SHOT_EXAMPLES = """
Examples:
Text: "The service was fine but the food was cold."
Output: {"label": "negative", "score": -0.3, "rationale": "Mixed review with net-negative experience"}
Text: "Oh great, another software update that breaks everything."
Output: {"label": "negative", "score": -0.7, "rationale": "Sarcastic complaint about software updates"}
Text: "The train arrives at platform 3 at 9:45am."
Output: {"label": "neutral", "score": 0.0, "rationale": "Factual timetable information with no opinion"}
"""
async def classify_sentiment(text: str) -> dict:
prompt = f"""You are a sentiment classifier. Respond with ONLY a JSON object
of shape {{"label", "score", "rationale"}}.
{FEW_SHOT_EXAMPLES}
Text to analyze:
\"\"\"{text}\"\"\"
"""
raw = await _ask(prompt, max_tokens=200)
return _extract_json(raw)Three examples is usually the sweet spot. Enough to anchor the labels, few enough to keep token cost low. Pick examples that cover the failure modes you actually see in production.
More is not always better. Each example adds input tokens on every request, which raises cost and latency. Three to five well-chosen examples usually beat fifteen random ones. Pick examples that cover your actual failure modes: sarcasm, mixed sentiment, factual neutral text, very short inputs. If you have fifty failing examples, you want fine-tuning, not more few-shot.
Matching exercise: Match each edge case to why zero-shot fails on it
Loading practice…