Prompt Engineering Basics
Prompt engineering क्या है?
Prompt वह निर्देश है जो आप AI model को देते हैं। Prompt engineering उस निर्देश को ऐसे लिखना है कि जवाब पहली बार में सही, सही format में और उपयोगी हो। Model सिर्फ़ वही जानता है जो आप बताते हैं और जो उसने training में सीखा – वह आपका मन, syllabus या company के नियम नहीं पढ़ सकता।
मज़बूत prompt के 6 हिस्से
| हिस्सा | किस सवाल का जवाब | उदाहरण |
|---|---|---|
| Role | AI किसकी तरह काम करे? | “आप 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
- Few-shot examples: input → output के 1–3 उदाहरण दिखाएँ। Model विवरण मानने से कहीं बेहतर pattern copy करता है।
- Steps माँगें: गणित, logic या debugging में अंतिम जवाब से पहले step-by-step काम करने और अंतिम जवाब अलग line में साफ़ दिखाने को कहें।
- Source दें: chapter text, policy या data paste करके कहें “सिर्फ़ इस text से जवाब दो; न हो तो बता दो”।
- Format तय करें: table, bullet points, नाम वाली keys के साथ JSON, शब्द सीमा।
- बड़े काम बाँटें: पहले outline, फिर हर section; या पहले data निकालें, फिर analysis।
- सुधारते रहें: जवाब ग़लत हो तो दोबारा शुरू करने की जगह ठीक-ठीक बताएँ क्या बदलना है।
Few-shot उदाहरण: customer messages classify करना
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 कर सकें।
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"]))Output पर आँख मूँदकर भरोसा न करें: जाँचें
Models कभी extra text जोड़ देते हैं, कोई key छोड़ देते हैं या दो सही options दे देते हैं। अगर आपका program जवाब इस्तेमाल करता है, तो save करने या दिखाने से पहले code से जाँचें।
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)दूसरे सवाल का 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 भेजने जैसे काम शुरू न करने दें।
अभ्यास
- “photosynthesis पर notes बनाओ” को मज़बूत prompt के सभी 6 हिस्सों से दोबारा लिखें।
- Informal Hinglish messages को formal English में बदलने वाला few-shot prompt बनाएँ।
mcq_prompt()मेंdifficultyparameter जोड़कर छपे prompt को जाँचें।extract_questions()में duplicate options रोकने वाली जाँच जोड़ें।
सारांश
- मज़बूत prompt = role + task + context + format + examples + limits।
- Few-shot examples और source text सबसे भरोसेमंद जवाब देते हैं।
- Apps में prompts functions से बनाएँ और हर output code से जाँचें।
- निजी data बचाएँ और facts जाँचें।
