नमस्ते दोस्तों!
स्वागत है The Easy Master पर!
पिछले post में AI ने जवाब तो दिया, लेकिन अपनी मर्ज़ी से – कभी लंबा, कभी छोटा, कभी अलग ढंग में। Chat के लिए यह चलता है, लेकिन app के लिए नहीं। आपकी app को चाहिए एक fixed shape का data, जिसे code पढ़ सके।
आज हम सीखेंगे कि AI से बिल्कुल वैसा जवाब कैसे लें जैसा चाहिए।
Is prompt engineering in Hindi post mein aap seekhenge:
✅ Achhe prompt ke 4 hisse
✅ System prompt – AI का role और नियम
✅ Few-shot – example दिखाकर समझाना
✅ Temperature – हर बार एक जैसा जवाब
✅ JSON schema – जवाब जिसे code सीधे पढ़ सके
✅ Project: customer review से JSON निकालने वाला function
1. Achhe Prompt Ke 4 Hisse
AI को एक नए intern की तरह समझिए – होशियार है, लेकिन आपके काम के बारे में कुछ नहीं जानता। अच्छा prompt चार सवालों का जवाब देता है (ChatGPT जैसे tools में prompts लिखने की basics के लिए How to Write AI Prompts देखिए – यहाँ हम वही बात code के लिए कर रहे हैं):
| Hissa | Sawal | Example |
|---|---|---|
| Role | Tum kaun ho? | "Tum customer reviews padhte ho" |
| Task | Kya karna hai? | "Review se JSON nikaalo" |
| Rules | Kya nahi karna? | "Sirf wahi problems likho jo review mein likhi hain" |
| Format | Jawab kis shape mein? | sentiment, product, problems |
Kharab prompt: "Is review ko analyse karo"
Achha prompt: "Tum customer reviews padhte ho. Review se sentiment, product aur problems ki list JSON mein nikaalo. Sirf wahi problems likho jo review mein likhi hain."
2. System Prompt – AI Ka Role Aur Niyam
system message पूरी बातचीत के लिए नियम तय करता है। User इसे नहीं देखता, लेकिन AI हर जवाब में इसे मानता है। Role, rules और tone – सब यहीं लिखे जाते हैं।
Tip: छोटे local models English के निर्देश ज़्यादा अच्छे से मानते हैं। जवाब Hinglish में चाहिए, तो system prompt English में लिखिए और आख़िर में जोड़िए "Reply in Hinglish"। Part 9 में हम इसका असर साफ़ देखेंगे।
3. Few-Shot – Example Dikhakar Samjhana
समझाने से अच्छा है करके दिखाना। हम एक user message और उसका सही assistant जवाब पहले से डाल देते हैं। AI उस pattern को पकड़ लेता है – जैसे intern को एक भरा हुआ form दिखा दिया हो।
इसे few-shot prompting कहते हैं। एक-दो अच्छे examples अक्सर लंबे निर्देशों से ज़्यादा काम करते हैं।
4. Temperature – Har Baar Ek Jaisa Jawab
temperature तय करता है कि AI कितना "creative" होगा:
- 0 – हर बार लगभग एक जैसा जवाब। Data निकालने, classify करने के लिए सही।
- 0.7–1 – हर बार थोड़ा अलग। Story, ideas, marketing text के लिए।
App के लिए data चाहिए तो temperature: 0 रखिए।
5. JSON Schema – Jawab Jise Code Padh Sake
"JSON में जवाब दो" लिखने पर भी AI कभी-कभी आगे "Here is your JSON:" लिख देता है, और आपका JSON.parse टूट जाता है। Ollama में format में JSON schema दे सकते हैं – तब जवाब उसी shape में आता है। (OpenAI, Claude जैसी hosted APIs में भी "structured output" का ऐसा ही feature है।)
6. Project – Review Se JSON
अब चारों चीज़ें एक साथ:
// prompts.js — review se JSON nikalna: system prompt + example + schema
const schema = {
type: 'object',
properties: {
sentiment: { type: 'string', enum: ['positive', 'negative', 'mixed'] },
product: { type: 'string' },
problems: { type: 'array', items: { type: 'string' } },
},
required: ['sentiment', 'product', 'problems'],
};
async function analyse(review) {
const res = await fetch('http://localhost:11434/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'llama3.2',
stream: false,
format: schema, // jawab isi shape ka JSON hoga
options: { temperature: 0 }, // har baar ek jaisa jawab
messages: [
{ role: 'system', content: 'Tum customer reviews padhte ho aur unse JSON nikaalte ho. Sirf wahi problems likho jo review mein likhi hain.' },
// ek example (few-shot): model ko dikhao ki kaisa jawab chahiye
{ role: 'user', content: 'Headphones ki sound mast hai, par 2 din mein wire toot gayi.' },
{ role: 'assistant', content: '{"sentiment":"mixed","product":"headphones","problems":["wire toot gayi"]}' },
{ role: 'user', content: review },
],
}),
});
const data = await res.json();
return JSON.parse(data.message.content);
}
const result = await analyse('Phone ki battery bahut jaldi khatam hoti hai aur camera bhi blurry hai. Paisa barbaad.');
console.log(result);
console.log('Problems ki ginti:', result.problems.length);Chalaiye: node prompts.js
{
sentiment: 'negative',
product: 'phone',
problems: [ 'battery bahut jaldi khatam hoti hai', 'camera blurry hai' ]
}
Problems ki ginti: 2अब result.problems.length सीधे काम करता है – यह text नहीं, असली data है। इसे database में save कीजिए, dashboard पर दिखाइए, या negative reviews पर alert भेजिए।
Har line kya kar rahi hai
- schema – जवाब की shape: तीन fields, और
sentimentसिर्फ़ तीन में से एक value - format: schema – Ollama इसी shape का JSON लौटाएगा
- temperature: 0 – दो बार चलाइए, जवाब वही आएगा
- system – role और rule ("sirf wahi problems")
- user + assistant जोड़ी – few-shot example
7. Python Version
# prompts.py — review se JSON nikalna
import json, requests
schema = {
"type": "object",
"properties": {
"sentiment": {"type": "string", "enum": ["positive", "negative", "mixed"]},
"product": {"type": "string"},
"problems": {"type": "array", "items": {"type": "string"}},
},
"required": ["sentiment", "product", "problems"],
}
def analyse(review):
res = requests.post("http://localhost:11434/api/chat", json={
"model": "llama3.2",
"stream": False,
"format": schema,
"options": {"temperature": 0},
"messages": [
{"role": "system", "content": "Tum customer reviews padhte ho aur unse JSON nikaalte ho. Sirf wahi problems likho jo review mein likhi hain."},
{"role": "user", "content": "Headphones ki sound mast hai, par 2 din mein wire toot gayi."},
{"role": "assistant", "content": '{"sentiment":"mixed","product":"headphones","problems":["wire toot gayi"]}'},
{"role": "user", "content": review},
],
})
return json.loads(res.json()["message"]["content"])
print(analyse("Phone ki battery bahut jaldi khatam hoti hai aur camera bhi blurry hai. Paisa barbaad."))8. Common Mistakes
- Bina schema ke
JSON.parse– AI एक बार भी बाहर कुछ लिख दे, तो app crash। Schema या कम से कमtry/catchरखिए। AI का JSON ग़लत लगे तो उसे हमारे free JSON Formatter और Validator में डालकर देखिए कि ग़लती कहाँ है। - Bahut lambe niyam – 20 rules वाला prompt छोटे model को उलझा देता है। ज़रूरी नियम रखिए, बाकी examples से सिखाइए।
- AI par bharosa ki data sahi hai – Schema shape पक्की करता है, सच नहीं। ज़रूरी चीज़ों को code में check कीजिए – Python में Pydantic इसके लिए बढ़िया है।
- Example galat – Few-shot का example ग़लत हो तो AI भी वही ग़लती दोहराएगा।
Conclusion
अच्छा prompt = role + task + rules + format। Few-shot से pattern सिखाइए, temperature: 0 से जवाब पक्का कीजिए, और JSON schema से उसे code के लायक बनाइए। अब AI सिर्फ़ chat नहीं, आपकी app का एक function बन गया है।
अगले post में देखेंगे कि हर AI call का खर्च कैसे गिना जाता है – tokens, limits, और bill कम रखने के तरीके। Part 4 पढ़िए → (19 Nov को आएगा) 🚀