Back to course
Module 2 · Day 9

Few-Shot Examples & Structured Output

Day 8-এ zero-shot baseline measure করেছ। আজ Day 9 — Module 2-এর সবচেয়ে ভারী দিন: ৩-৫টা few-shot example যোগ করবে, output-এর জন্য একটা JSON schema বানাবে, আর parse validation শিখে prompt-টা production-ready বানাবে — শেষে zero-shot বনাম few-shot pass rate compare করবে।

~240 min read6 learning objectives
What you'll learn
  • Few-shot prompting মানে কী এবং কেন এটা output-কে measurably improve করে।
  • ৩-৫টা well-chosen example যোগ করার ৩ criteria: Relevant, Diverse, Structured।
  • XML <examples><example> wrapper pattern apply করতে পারা।
  • JSON schema-কে prompt-এ embed করতে পারা — schema-in-prompt approach।
  • JSON parse failure handle করার strategy: try/catch + retry + schema validation।
  • Day 8-এর zero-shot baseline-এর সাথে comparison করে measure করতে পারা — few-shot কত percentage point জিতল।
Before this
  • Day 8 শেষ — নিজের zero-shot prompt আর ১০টা hand-built test case লেখা হয়ে গেছে।
  • Day 5-এ XML tag delimiter চিনেছ। Day 6-এর English vocabulary fresh আছে।
  • JSON-এর basic shape চেনা থাকলে ভালো (objects, arrays, strings)।
1

The Big Picture

Day 8-এ zero-shot দিয়ে শুরু করেছ। আজ examples যোগ করে prompt-টাকে আরেক ধাপ উপরে তুলবে।

আজ একটা ভারী দিন — তিনটা sub-topic এক sitting-এ শিখবে: (১) ৩-৫টা example যোগ করা (few-shot), (২) schema দিয়ে JSON output force করা, (৩) parsed JSON validate করা। Day 8-এর zero-shot baseline আজ upgrade হয়ে একটা structured prompt-এ পরিণত হবে — যেটা reliably parseable JSON রিটার্ন করে। এটাই সেই লেভেল যেখান থেকে team সত্যিকারের production prompt ship করে।

Few-shot promptingthe most reliable steering

Prompt-এ ২-৫টা input/output example pair যোগ করা। Model এই examples থেকে pattern, format, style, এবং edge-case handling pick up করে। Prompt engineering-এর established finding: output-এর format, tone, আর structure control করার সবচেয়ে reliable উপায় হলো examples দেখানো। Day 8-এর zero-shot baseline-এর তুলনায় few-shot সাধারণত ১৫-৩০ percentage points improvement দেয়, বিশেষ করে edge cases-এ।

Why it matters

Zero-shot → few-shot transition Module 2-এর সবচেয়ে dramatic improvement। Day 8-এর 60% baseline প্রায়ই Day 9-এ 85% হয়ে যায় — শুধু ৩টা ভালো example যোগ করে।

নিচে টগল করে দেখো — 0 → 1 → 3 → 5 example যোগ করলে prompt আর illustrative pass rate কীভাবে বদলায়।

Example selectionrelevant, diverse, structured

৩টা rule মনে রাখো: (১) Relevant — তোমার actual use case মিরর করুক; (২) Diverse — edge cases cover করুক, একই pattern repeat করবে না; (৩) Structured — XML <example> tags-এ wrap করো। ৩টা random example ≠ ৩টা carefully chosen example। Carefully chosen ৩টা zero-shot-কে ১৫-৩০% beat করে; random ৩টা barely improve করে।

Why it matters

অনেকেই ১০টা random example দেয় ভেবে “more is better”। Token waste + worse output। ৩টা ভালো > ১০টা mediocre।

নিচে তিনটা ভিন্ন selection strategy compare করো — একই count (৩টা), সম্পূর্ণ ভিন্ন ফল।

XML <example> tagsthe wrapper convention

একটা established convention: <examples><example><input>...</input><output>...</output></example></examples>। Plain text examples instructions-এ bleed করে, model confuse করে। XML tags clean boundary দেয় — model জানে এটা example, এটা actual instruction, এটা current input।

Why it matters

Day 5-এ তুমি delimiter concept শিখেছ। আজ specific application — modern model-গুলো XML-structured input বিশেষভাবে ভালো বোঝে, তাই examples এই wrapper-এ best perform করে।

xml
<examples>
  <example>
    <input>...</input>
    <output>...</output>
  </example>
  <example>
    ...
  </example>
</examples>

Examples বসালে। এবার output-টাকেই structured করার পালা — JSON schema দিয়ে।

2

Core Concepts

Prose output থেকে parseable JSON output — আর সেটাকে force করার schema trick।

Structured outputJSON — from prose to parseable

Output JSON (বা XML, CSV) shape-এ — downstream code parse করতে পারবে। ৩টা benefit: (১) Code-graded evals possible (পরের eval-harness lesson-এ), (২) Data সরাসরি downstream tools-এ flow করে, (৩) Consistent shape across calls। Production prompts প্রায় সবসময় structured output specify করে।

Why it matters

Prose output শুধু human-readable। JSON output machine-readable + human-readable দুটোই। পরের eval-harness lesson-এর assertions JSON output ধরে নিয়েই কাজ করবে।

Schema in promptdescribe the shape

JSON-এর exact shape prompt-এর ভেতরে XML দিয়ে describe করো। যেমন: <output_schema>{ "label": "positive | negative | neutral | mixed", "confidence": "number between 0 and 1", "reason": "one short sentence" }</output_schema>। <example> output-এর সাথে combine করলে model output reliably parse হয়।

Why it matters

Schema-in-prompt provider-agnostic — যেকোনো model family-ই follow করতে পারে। Provider-specific JSON mode-ও আছে — কিন্তু schema-in-prompt সহজ এবং portable।

নিচে schema builder-এ field add/edit/remove করে দেখো — schema block কীভাবে বদলায়।

উদাহরণ + schema একসাথে বসলে একটা সম্পূর্ণ production-grade prompt দেখতে এমন হয়:

text
You are a sentiment-classification assistant.

Classify the sentiment of the tweet below as exactly one of:
positive, negative, neutral, or mixed.
Sarcastic tweets count as their underlying sentiment.

Return JSON only matching this schema:
<output_schema>
{
  "label": "positive | negative | neutral | mixed",
  "confidence": "number from 0 to 1",
  "reason": "one short sentence"
}
</output_schema>

<examples>
  <example>
    <input>This movie was amazing!</input>
    <output>{"label": "positive", "confidence": 0.95, "reason": "Strong positive adjective"}</output>
  </example>
  <example>
    <input>Got my flight delayed 5 hours. #bestdayever</input>
    <output>{"label": "negative", "confidence": 0.88, "reason": "Sarcastic hashtag inverts surface positivity"}</output>
  </example>
  <example>
    <input>Loved the food, hated the service.</input>
    <output>{"label": "mixed", "confidence": 0.92, "reason": "Two opposite sentiments coexist"}</output>
  </example>
</examples>

Tweet: {{TWEET}}
Output JSON:

The prefill trickforce JSON shape

একটা provider-specific trick — সব model provider সাপোর্ট করে না। API call-এ assistant turn `{` দিয়ে শুরু করো — model সেখান থেকে continue করবে, output {-এর সাথেই শুরু হবে। কোনো “Sure! Here is...” preamble থাকবে না। Practical: assistant turn `{` দিয়ে prefill করা হয়, আর output হয় সেই `{` + model-এর continuation।

Why it matters

Day 6-এর “no preamble” trick + এই prefill trick = একসাথে production-এ near-100% JSON-shaped output। Parsing failures dramatically কমে যায়।

json
{
  "messages": [
    { "role": "user", "content": "Classify: ..." },
    { "role": "assistant", "content": "{" }
  ]
}

এতক্ষণ concept শিখলে। এবার নিজের হাতে চেষ্টা করো — validation আর comparison।

3

Try It Yourself

পড়া বন্ধ — এবার JSON validation চেক করো আর zero-shot বনাম few-shot compare করো।

Validate + retryproduction wisdom

Production code কখনো model JSON-কে trust করে না। try/catch দিয়ে JSON.parse wrap করো। Failure হলে retry করো (stricter prompt বা different temperature দিয়ে)। Parsed JSON-এর schema validation (Zod/Pydantic) — required fields check, type check, enum value check। 3 strikes pattern: parse fail → retry → এখনো fail → fallback default + log।

Why it matters

Model output ৯৫%+ valid JSON হতে পারে, কিন্তু ৫% failure production-এ user-visible bug হয়ে যায়। Validation + retry layer যোগ না করলে ship করার সাহস হবে না।

নিচে ৬টা real-world model output দেখো — কোনটা parse pass করে, কোনটা schema fail করে, কোনটায় দুটোই fail করে।

Zero vs few-shotthe before/after

Day 8-এর zero-shot baseline + Day 9-এর few-shot prompt — একই ১০টা test case। Pass rate জাম্প = examples-এর added value। Typical pattern: zero-shot ৬০%, ১-shot ৬৮%, ৩-shot ৮৪%, ৫-shot ৮৯%। সবচেয়ে বড় lift edge cases-এ (sarcasm, mixed, ambiguous) — কারণ edge case examples দেখলে model সেই pattern শিখে।

Why it matters

এই comparison Day 4-এর iteration loop-এর প্রথম real example। “v1 baseline 60% → v2 few-shot 84% → ship” — এটাই AI engineer-এর daily PR review currency।

নিচে দুটো prompt-এর ফলাফল পাশাপাশি দেখো — একই ১০টা case-এ।

Tip

আজ পাঁচটা জিনিস করো — (১) নিজের zero-shot prompt-এ ৩-৫টা relevant + diverse example যোগ করো XML <example> tags-এ; (২) output-এর জন্য একটা JSON schema লেখো, <output_schema> tag-এ wrap করো; (৩) prefill trick apply করো (assistant turn `{` দিয়ে শুরু); (৪) parse আর schema validation failure mode চিনে নাও, retry strategy ভাবো; (৫) নিজের ১০ test case-এ zero-shot বনাম few-shot pass rate compare করে percentage-point lift measure করো।

4

Check Your Understanding

ভুল হলে সমস্যা নেই — প্রতিটার ব্যাখ্যা আছে।

Misconception check

More examples মানেই সবসময় ভালো — 10 examples > 3 examples।

Misconception check

যেকোনো ৩টা example দিলেই few-shot হয়ে যায়

Misconception check

JSON mode থাকলে schema-in-prompt আর লাগে না

Misconception check

JSON parse হলেই output correct

Misconception check

Examples plain prose-এ লিখলেও চলে — XML tags overkill

Misconception check

Model সবসময় valid JSON produce করে

Quick check

Prompt engineering-এ recommended number of few-shot examples কত?

ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।

Quick check

Example selection-এর ৩টা criteria কী?

ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।

Quick check

নিচের কোন XML wrapper সবচেয়ে standard convention?

ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।

Quick check

JSON parse হয়ে গেলে output correct — এই বাক্য সঠিক?

ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।

Quick check

Day 8-এ তোমার zero-shot baseline ছিল 60%। Day 9-এ few-shot যোগ করলে। Typical pass rate কত expect করবে?

ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।

Quick check

Prompt engineering-এর “prefill trick” কী?

ভুল হলেও সমস্যা নেই — প্রতিটা option-এর সাথে ব্যাখ্যা আছে।

5

Prove It To Yourself

চারটা ধারণা মনে রাখো। নিজের ভাষায় লেখো। Day 9 শেষ।

Note

চারটা ধারণা মনে রাখো — ৩-৫টা example, well-chosen: more ≠ better, ৩-৫টা diverse (typical + edge + adversarial) ১০টা random-কে beat করে। XML <example> tags: plain prose bleed করে, <examples><example><input>...</input><output>...</output></example></examples> — এই canonical wrapper ব্যবহার করো। Schema in prompt + prefill: output-এর JSON shape XML-এ describe করো + assistant turn `{` দিয়ে prefill করো — production-ready JSON output পাবে। সবসময় validate + retry: parse প্রথম check, schema validation আলাদা, failure-এ retry + fallback + log — ৯৫% valid ≠ ১০০%, production-এ এই ৫%-ই user complaint।

আজকের শব্দগুলো — few-shot, example, relevant, diverse, structured, schema, JSON, prefill, parse, retry, validation — এখন থেকে English-ই থাকবে; team meeting, PR review, নিজের project-এর decision নেওয়ার সময় এভাবেই দেখবে। কাল কেউ “few-shot” বললে তুমি জানো সেটা কী। পুরো তালিকা নিচের glossary-তে।

Glossary

শব্দার্থ — পরে ফিরে দেখার জন্য

একনজরে সব key term। কোনো শব্দ ভুলে গেলে এখানে এসে খুঁজে নিও।

Confidence score
Model-এর output-এর সাথে যুক্ত একটা 0-1 number — কতটা sure। Schema-তে include করা downstream filtering-এর জন্য useful।
Diverse examples
Few-shot examples-এর coverage rule — সব positive না, mix থাকতে হবে।
Few-shot / Multishot
Prompt-এ ২-৫টা input/output example যোগ করা — সবচেয়ে reliable steering technique।
JSON schema
JSON output-এর expected shape — fields, types, required। Schema-in-prompt + validator-in-code = production-ready।
Multishot
Few-shot-এর synonym — কিছু vendor এই term ব্যবহার করে।
Parse
String → object — JSON.parse() / json.loads()। Production code try/catch করে।
Prefill
একটা provider-specific API trick — assistant turn `{` দিয়ে শুরু করা, model সেখান থেকে continue করে। Forces JSON shape।
Retry strategy
Parse fail → retry with stricter prompt → fallback default + log। 3 strikes pattern।
Schema-in-prompt
JSON schema-কে XML দিয়ে prompt-এর ভেতরে describe করা। Provider-agnostic, simple, effective।
Structured output
JSON/XML/CSV shape-এ output — parseable by downstream code। Production essential।
Validation
Parsed JSON-এর schema check — required fields, types, enum values। Zod (TypeScript), Pydantic (Python), ajv (Node)।
Zero-shot vs few-shot
0 example vs 3-5 example prompts। Same task। Pass rate jump = few-shot-এর added value।
References

Sources consulted to author this lesson. Citation style is informal — follow the links if you want to dig deeper.

Exit check

দুই বাক্যে নিজের ভাষায় লেখো

নিজের ভাষায় দুই বাক্যে লেখো — example selection-এর ৩টা criteria কী কী, আর JSON output validate করতে কেন শুধু parse যথেষ্ট না।

0 chars
Back to course