Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟣 ML + AI  ·  Lesson 36

Prompt Engineering Basics

Prompt engineering क्या है?

Prompt वह निर्देश है जो आप AI model को देते हैं। Prompt engineering उस निर्देश को ऐसे लिखना है कि जवाब पहली बार में सही, सही format में और उपयोगी हो। Model सिर्फ़ वही जानता है जो आप बताते हैं और जो उसने training में सीखा – वह आपका मन, syllabus या company के नियम नहीं पढ़ सकता।

मज़बूत prompt के 6 हिस्से

हिस्साकिस सवाल का जवाबउदाहरण
RoleAI किसकी तरह काम करे?“आप CBSE Class 10 science teacher हैं।”
Taskठीक-ठीक क्या करना है?“Refraction पर 5 MCQs बनाइए।”
Contextकौन सी पृष्ठभूमि ज़रूरी?“Students ray diagrams में कमज़ोर हैं; exam 2 हफ़्ते में।”
Formatजवाब कैसा दिखे?“Table: सवाल, 4 options, उत्तर, एक line कारण।”
Examplesअच्छा कैसा दिखता है?जिस style में चाहिए उसका एक नमूना सवाल
Limitsकिससे बचना है?“Lenses से सवाल नहीं। सिर्फ़ NCERT शब्द।”

ख़राब बनाम अच्छे prompts

कमज़ोर promptमज़बूत prompt
“Loops समझाओ।”“Class 11 के ऐसे student को Python के for और while loops समझाइए जो variables जानता है पर loops नहीं। पहाड़ा छापने का एक उदाहरण, उसका output, फिर 3 आम ग़लतियाँ। 250 शब्दों से कम।”
“Parents को email लिखो।”“7-B के class teacher की तरफ़ से parents को विनम्र WhatsApp message (max 60 शब्द): PTM शनिवार 10–1 बजे, report cards दिए जाएँगे, students uniform में आएँ। Hindi और English दोनों।”
“मेरा code ठीक करो।”“यह Python function list का average लौटाना चाहिए पर ख़ाली list पर ZeroDivisionError से crash होता है। कारण 2 lines में बताइए और ठीक version दीजिए जो ख़ाली list पर 0 लौटाए। Code: …”

काम करने वाली techniques

  1. Few-shot examples: input → output के 1–3 उदाहरण दिखाएँ। Model विवरण मानने से कहीं बेहतर pattern copy करता है।
  2. Steps माँगें: गणित, logic या debugging में अंतिम जवाब से पहले step-by-step काम करने और अंतिम जवाब अलग line में साफ़ दिखाने को कहें।
  3. Source दें: chapter text, policy या data paste करके कहें “सिर्फ़ इस text से जवाब दो; न हो तो बता दो”।
  4. Format तय करें: table, bullet points, नाम वाली keys के साथ JSON, शब्द सीमा।
  5. बड़े काम बाँटें: पहले outline, फिर हर section; या पहले data निकालें, फिर analysis।
  6. सुधारते रहें: जवाब ग़लत हो तो दोबारा शुरू करने की जगह ठीक-ठीक बताएँ क्या बदलना है।

Few-shot उदाहरण: customer messages classify करना

Prompt
Classify each customer message as ORDER, COMPLAINT, ENQUIRY or OTHER.
Reply with only the label.

Message: "Please send 2 kg kaju katli to Raja Park tomorrow"
Label: ORDER

Message: "The ghewar I bought yesterday was stale"
Label: COMPLAINT

Message: "Are you open on Sunday?"
Label: ENQUIRY

Message: "Do you deliver to Mansarovar and what is the charge?"
Label:

तीन उदाहरण labels, style (“सिर्फ़ label”) और categories के बीच की सीमा सिखाते हैं। यहाँ जवाब ENQUIRY होना चाहिए।

Python में prompt templates

जब app के अंदर AI model इस्तेमाल करें, तो function से prompts बनाएँ ताकि हर request का structure एक जैसा रहे और आप उसे test कर सकें।

Python – prompt_template.py
def mcq_prompt(topic, grade, count=5, language="English", avoid=None):
    lines = [
        f"You are an experienced CBSE Class {grade} teacher.",
        f"Create {count} multiple-choice questions on: {topic}.",
        f"Language: {language}. Use NCERT terminology.",
        "Each question: 4 options (a-d), exactly one correct.",
        'Return JSON only: [{"q": str, "options": [str, str, str, str], "answer": "a|b|c|d", "why": str}]',
    ]
    if avoid:
        lines.append(f"Do not include questions about: {', '.join(avoid)}.")
    return "\n".join(lines)

print(mcq_prompt("Refraction of light", 10, count=3, avoid=["lenses", "power of lens"]))
You are an experienced CBSE Class 10 teacher. Create 3 multiple-choice questions on: Refraction of light. Language: English. Use NCERT terminology. Each question: 4 options (a-d), exactly one correct. Return JSON only: [{"q": str, "options": [str, str, str, str], "answer": "a|b|c|d", "why": str}] Do not include questions about: lenses, power of lens.

Output पर आँख मूँदकर भरोसा न करें: जाँचें

Models कभी extra text जोड़ देते हैं, कोई key छोड़ देते हैं या दो सही options दे देते हैं। अगर आपका program जवाब इस्तेमाल करता है, तो save करने या दिखाने से पहले code से जाँचें।

Python – validate_ai_output.py
import json

# a reply copied from an AI model – it added a sentence before the JSON
reply = """Sure! Here are your questions:
[{"q": "Light bends when it passes from air to water. This is called?",
  "options": ["Reflection", "Refraction", "Dispersion", "Scattering"], "answer": "b",
  "why": "Change of speed in a new medium bends light."},
 {"q": "The refractive index of water is about?",
  "options": ["1.00", "1.33", "1.50", "2.42"], "answer": "e", "why": "n = c / v"}]"""

def extract_questions(text):
    start, end = text.find("["), text.rfind("]") + 1      # keep only the JSON part
    data = json.loads(text[start:end])
    good, problems = [], []
    for i, item in enumerate(data, 1):
        if set(item) != {"q", "options", "answer", "why"}:
            problems.append(f"Q{i}: wrong keys {sorted(item)}")
        elif len(item["options"]) != 4:
            problems.append(f"Q{i}: needs 4 options")
        elif item["answer"] not in "abcd" or len(item["answer"]) != 1:
            problems.append(f"Q{i}: invalid answer {item['answer']!r}")
        else:
            good.append(item)
    return good, problems

good, problems = extract_questions(reply)
print("valid questions:", len(good))
print("problems:", problems)
valid questions: 1 problems: ["Q2: invalid answer 'e'"]

दूसरे सवाल का answer “e” है, जो असंभव है – code उसे पकड़ लेता है ताकि वह students तक न पहुँचे। Model से सिर्फ़ वही सवाल ठीक करवाएँ।

तैयार prompts

किसके लिएPrompt
Student – revision“Python topic dictionaries पर मुझसे एक-एक सवाल पूछो। मेरे जवाब का इंतज़ार करो, बताओ सही है या नहीं और क्यों, फिर अगला कठिन सवाल। 8 सवालों के बाद score दो।”
Teacher – lesson plan“Class 8 के लिए fractions पर 40 मिनट का lesson plan: 5 मिनट warm-up, भारतीय बाज़ार के उदाहरण से explanation, group activity, 5 exit-ticket सवाल। Table format।”
Developer – code review“इस function को bugs, edge cases और readability के लिए review करो। Issues गंभीरता के क्रम में, फिर comments के साथ बेहतर version। Valid input पर व्यवहार न बदले।”
Office – summary“इस meeting transcript का 5 bullet points में सार, फिर action items table में: काम, ज़िम्मेदार, deadline। अस्पष्ट items पर (?) लगाओ।”

सुरक्षा और ईमानदारी के नियम

  • Public AI tools में passwords, OTPs, Aadhaar/PAN numbers, medical या students का निजी data कभी paste न करें।
  • Facts, numbers, कानून और citations आधिकारिक स्रोतों से जाँचें – AI इन्हें गढ़ सकता है।
  • School के काम में teacher के नियम मानें और बताएँ कि AI इस्तेमाल किया।
  • “Prompt injection”: document के अंदर का text AI को नए निर्देश देने की कोशिश कर सकता है। Apps में अपने निर्देश अलग रखें और documents को emails भेजने जैसे काम शुरू न करने दें।

अभ्यास

  1. “photosynthesis पर notes बनाओ” को मज़बूत prompt के सभी 6 हिस्सों से दोबारा लिखें।
  2. Informal Hinglish messages को formal English में बदलने वाला few-shot prompt बनाएँ।
  3. mcq_prompt() में difficulty parameter जोड़कर छपे prompt को जाँचें।
  4. extract_questions() में duplicate options रोकने वाली जाँच जोड़ें।

सारांश

  • मज़बूत prompt = role + task + context + format + examples + limits।
  • Few-shot examples और source text सबसे भरोसेमंद जवाब देते हैं।
  • Apps में prompts functions से बनाएँ और हर output code से जाँचें।
  • निजी data बचाएँ और facts जाँचें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या prompt engineering असली job skill है?
हाँ, अलग पद से ज़्यादा कई नौकरियों के हिस्से के रूप में: developers AI features जोड़ते हैं, teachers material बनाते हैं, analysts data का सार निकालते हैं। AI को निर्देश देना और जाँचना आना इन सबको तेज़ बनाता है।
Prompts Hindi में लिखूँ या English में?
आधुनिक models में दोनों चलते हैं। English prompts अक्सर थोड़े ज़्यादा सटीक और कम tokens वाले होते हैं; audience को चाहिए तो जवाब Hindi में माँगें।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicAI Ethics और Responsible AI आपकी प्रगति: 36 / 66 (55%) · Lesson 37 / 66 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें