Vibe Coding · درس

أمثلة Few-Shot

اعرض على AI النمط والبنية اللذين تريدهما

الدرس 2 من 413 خطوة

أمثلة Few-Shot درس مجاني في Vibe Coding على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Vibe Coding، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Vibe Coding 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Show, Don't Just Tell

Describing the code you want with words works — but showing an example works even better. This technique is called few-shot prompting: you give the AI one or more examples of input and the matching output, then ask it to follow the same pattern.

It's the difference between explaining a recipe and handing someone a finished dish to copy. Examples remove guesswork and lock in your exact style.

Zero-Shot vs Few-Shot

Zero-shot = you ask with no examples. Few-shot = you include a couple of examples first.

  • Zero-shot: "Write a function that turns a title into a URL slug."
  • Few-shot: same request, plus 2 examples of titles and the exact slugs you expect.

Few-shot wins whenever the format matters and is hard to describe in words. You teach by demonstration.

Your First Few-Shot Prompt

The pattern is simple: list a few input → output pairs, then leave the last one open for the AI to complete.

Here we teach the AI exactly how our slugs should look. It now knows: lowercase, spaces become dashes, punctuation dropped — without us writing a single rule.

Turn each title into a URL slug, following these examples:

Title: "Hello World!"  -> hello-world
Title: "My First App"  -> my-first-app
Title: "10 Tips & Tricks" -> 10-tips-tricks

Title: "Vibe Coding 101" ->

Examples Teach Style

Few-shot isn't only for data formats — it's how you transfer coding style. Show the AI one function written your way, and it will match your naming, comments and structure on the next one.

This is gold in a real project: paste an existing function as the example, then ask for a new one "in the same style." The AI matches your codebase instead of inventing its own conventions.

Here is how I write functions in this project:

// Returns the average of a list of numbers
function averageOf(numbers) {
  const total = numbers.reduce((sum, n) => sum + n, 0);
  return total / numbers.length;
}

Write a function maxOf(numbers) in exactly this style.

See the Matching Output

When you give a clean example like the one before, the AI returns code that fits right in. Here's the kind of result that prompt produces — same comment style, same naming, same shape.

Run it to confirm the pattern holds.

// Returns the largest number in a list
function maxOf(numbers) {
  return numbers.reduce((biggest, n) => (n > biggest ? n : biggest), numbers[0]);
}

console.log(maxOf([3, 9, 2, 7]));
console.log(maxOf([-5, -1, -10]));

How Many Examples?

You rarely need many. A few rules of thumb:

  • 1 example — when the pattern is mostly clear and you just want to pin the style.
  • 2–3 examples — when the format has edge cases (punctuation, numbers, special words).
  • Add an edge case on purpose so the AI learns how to handle the tricky input, not just the easy one.

More than 3 rarely helps and just makes the prompt long.

Format phone numbers like these examples (note the edge case with extra digits):

"1234567890" -> (123) 456-7890
"5551234567" -> (555) 123-4567
"12345"      -> invalid number

"9998887777" ->

Cover the Edge Cases

The trick that separates good few-shot prompts from great ones: include the weird input. If you only show happy-path examples, the AI assumes inputs are always clean — and your app crashes on the first odd one.

By showing an invalid or empty example and the response you want, you teach error handling by demonstration. No long explanation needed.

Convert each price string to a number. Handle bad input as shown:

"$19.99" -> 19.99
"$0"     -> 0
"free"   -> null
""       -> null

"$120.50" ->

Few-Shot for Tone and Text

Examples aren't only for code logic — they shape any text the AI writes. Building an app with messages, button labels, or error text? Show two examples in your voice and the AI matches it.

This keeps your whole product consistent: friendly, terse, professional — whatever you demonstrate.

Write friendly, short error messages in this style:

Problem: empty email field -> "Oops! We need your email to continue."
Problem: password too short  -> "Almost there — make your password a bit longer."

Problem: file too large ->

Examples Inside Real Tools

Few-shot works everywhere you prompt:

  • Cursor / Claude Code: paste an existing function as the example, ask for a sibling function "in the same style."
  • v0 / Lovable: describe a component, then say "like this card" and paste a sample to copy.
  • Copilot: write one example function above, and its autocomplete mimics your pattern for the next one.

The AI is a pattern-matcher — give it the pattern.

// @cursor: here is our standard API helper, follow this exact shape
async function getUser(id) {
  const res = await fetch(`/api/users/${id}`);
  if (!res.ok) return null;
  return res.json();
}

// Now write getOrder(id) in the same style.

When Examples Backfire

Few-shot is strong, so be careful:

  • A buggy example teaches the bug — the AI copies your mistake.
  • Inconsistent examples confuse it: if your two slugs follow different rules, output is random.
  • Examples that are too narrow make it overfit and fail on anything new.

Rule: your examples must be correct, consistent, and cover the range of inputs you expect.

Combine With Roles and Constraints

The most reliable prompts stack everything you've learned: a role, a few constraints, and one or two examples. Role sets the skill, constraints set the rules, examples set the exact shape.

That combination is how builders get code they can paste and ship — instead of rolling the dice on each request.

ROLE: Senior JS engineer.
CONSTRAINTS: vanilla JS, return only the function.
EXAMPLE:
  isEven(4) -> true
  isEven(7) -> false
TASK: Write isPositive(n) following the same simple style as isEven.

Quick Check

You're using few-shot prompting to format dates, but the AI keeps breaking on empty input. What's the best fix?

Recap: Teach by Example

Few-shot prompting is one of your sharpest tools:

  • Show input → output pairs and let the AI complete the pattern.
  • Examples transfer format, style, and tone better than words.
  • Include edge cases so the AI learns error handling.
  • Keep examples correct and consistent — bugs and contradictions get copied.
  • Stack with roles + constraints for the most reliable output.

Next: breaking big features into small, promptable steps.

البدء مجانًا

تعلم JavaScript مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
25
الدروس
100

الأسئلة الشائعة

هل درس «أمثلة Few-Shot» مجاني؟

نعم — نص درس «أمثلة Few-Shot» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Vibe Coding، انتقل إلى CoddyKit PRO. تتضمن دورة Vibe Coding 4 دروس في المجموع.

ماذا ستتعلم في «أمثلة Few-Shot»؟

اعرض على AI النمط والبنية اللذين تريدهما تتمرن على Vibe Coding مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Vibe Coding؟

لا تُشترط خبرة سابقة. Vibe Coding على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «أمثلة Few-Shot»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Vibe Coding هذا؟

نعم. كل درس في Vibe Coding يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الأدوار والقيود
  2. أمثلة Few-Shot
  3. تقسيم المهام الكبيرة إلى خطوات
  4. تحديد التقنية والأسلوب
← العودة إلى Vibe Coding