Skip to content
AI Engineering

2. Pehli AI API Call – 10 Lines Mein Chatbot Banaiye (Node.js + Python)

November 17, 2026 4 min read

नमस्ते दोस्तों!

स्वागत है The Easy Master पर!

पिछले post में हमने समझा कि AI Engineering क्या है। आज बातें नहीं, code – हम अपनी पहली AI API call करेंगे और एक chatbot बनाएंगे जो पिछली बातें याद रखता है।

सबसे अच्छी बात? कोई credit card नहीं, कोई API key नहीं। AI model आपके अपने laptop पर चलेगा, बिल्कुल free।

Is AI API Call in Hindi post mein aap seekhenge:

✅ Ollama – अपने laptop पर free AI model चलाना
✅ Pehli API call – एक सवाल, एक जवाब
✅ messages array – AI से बात करने का format
✅ Chatbot jo yaad rakhe – history कैसे काम करती है
✅ Node.js aur Python – दोनों में पूरा code

1. Ollama Install Kijiye – Free AI Apne Laptop Par

Ollama एक free tool है जो AI models को आपके computer पर चलाता है और उन्हें एक API की तरह देता है – बिल्कुल वैसे ही जैसे आपका Express server localhost:3000 पर चलता है, Ollama localhost:11434 पर चलता है।

  • ollama.com से download करके install कीजिए (Windows, Mac, Linux – सबके लिए है)
  • Terminal खोलिए और एक छोटा model download कीजिए:
Code
ollama pull llama3.2

llama3.2 करीब 2 GB का है और 8 GB RAM वाले laptop पर आराम से चलता है। Check कीजिए कि सब ठीक है:

Code
ollama run llama3.2 "Hello, ek line mein apna parichay do"

जवाब आ गया? बधाई हो – आपके laptop पर AI चल रहा है! 🎉

Laptop ki RAM 16 GB hai? तो ollama pull qwen2.5:7b भी कर लीजिए। यह बड़ा (4.7 GB) और ज़्यादा समझदार model है – RAG और tools वाले posts में हम इसे use करेंगे।

2. Pehli API Call – Ek Sawal, Ek Jawab

अब code से बात करते हैं। Node.js 18 या उससे नया हो तो fetch पहले से मौजूद है – कोई package install नहीं करना। (Node.js बिल्कुल नया है? पहले Node.js क्या है देख लीजिए।)

एक folder बनाइए, उसमें npm init -y चलाइए, और package.json में "type": "module" जोड़ दीजिए (ताकि await सीधे लिख सकें)। ESM और CommonJS का फ़र्क NPM Packages और Modules वाले post में समझाया है, और async/await नया लगे तो Async Await vs Promises पढ़ लीजिए। फिर यह file बनाइए:

Code
// first-call.js — ek sawal, ek jawab
const res = await fetch('http://localhost:11434/api/chat', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    model: 'llama3.2',
    messages: [{ role: 'user', content: 'JavaScript mein closure kya hota hai? 2 line mein batao.' }],
    stream: false,
  }),
});
const data = await res.json();
console.log(data.message.content);

Chalaiye: node first-call.js

Code
JavaScript mein closure ek function kiye jiski reference function kiye kisi variable ya function kiye nahi hoti, aur use kisi alag function mein use kiya jaa sakta hai.

जवाब आया – लेकिन ध्यान दीजिए, Hinglish थोड़ी अटपटी है (closure की सही और पूरी समझ के लिए हमारा JavaScript Deep Dive पढ़िए)। यह छोटे model की कमी है, आपके code की नहीं। बड़ा model (qwen2.5:7b) बेहतर जवाब देता है। AI Engineering का पहला सबक: model चुनना भी एक फ़ैसला है।

Is code mein kya ho raha hai?

  • URL – localhost:11434/api/chat Ollama का chat endpoint है
  • model – कौन सा AI model जवाब देगा
  • messages – बातचीत, एक array के रूप में
  • stream: false – पूरा जवाब एक बार में चाहिए (streaming हम Part 5 में सीखेंगे)
  • data.message.content – AI का जवाब यहाँ मिलता है

3. messages Array – AI Se Baat Karne Ka Format

हर AI API (Ollama, OpenAI, Claude, Gemini) लगभग इसी format में बात करती है। हर message में एक role होता है:

roleKaun bol raha haiExample
systemआप, AI को निर्देश देते हुए"Tum ek friendly coding teacher ho"
userआपकी app का user"Closure kya hai?"
assistantAI ने पहले क्या कहा"Closure ek function hai jo…"

Sabse zaroori baat: AI को कुछ याद नहीं रहता। हर API call बिल्कुल नई होती है। अगर आप चाहते हैं कि AI पिछली बात याद रखे, तो पूरी बातचीत हर बार भेजनी पड़ती है। यही काम chatbot करता है।

4. Chatbot Jo Pichli Baatein Yaad Rakhe

Code
// chat.js — terminal chatbot jo pichli baatein yaad rakhta hai
import readline from 'node:readline/promises';

const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const messages = [
  { role: 'system', content: 'Tum ek friendly coding teacher ho. Hinglish mein, chhote jawab do.' },
];

while (true) {
  const question = await rl.question('nAap: ');
  if (question.trim() === 'exit') break;

  messages.push({ role: 'user', content: question });

  const res = await fetch('http://localhost:11434/api/chat', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ model: 'llama3.2', messages, stream: false }),
  });
  const data = await res.json();

  console.log('nAI:', data.message.content);
  messages.push(data.message); // AI ka jawab bhi history mein
}
rl.close();

Chalaiye: node chat.js (बंद करने के लिए exit लिखिए)

Code
Aap: Mera naam Rahul hai.

AI: Namaskar Rahul, aapka swagat hai! Mujhe aapki pasand hai, aap kitne naye logon ko milte hain?

Aap: Mera naam kya hai?

AI: Rahul bhai, aapka naam jaanne mein mujhe khushi hoti hai! Aapko kis cheez mein madad chahiye?

दूसरे सवाल में AI को नाम याद था – क्योंकि हमने पहली बातचीत भी साथ भेजी थी। Chatbot की "memory" बस इतनी है: एक array जिसमें हर सवाल और जवाब जुड़ता जाता है।

Teen lines jo sab kuch karti hain

  • messages.push({ role: 'user', ... }) – user का सवाल history में
  • body: JSON.stringify({ model, messages }) – पूरी history AI को
  • messages.push(data.message) – AI का जवाब भी history में

5. Python Version

Python वाले दोस्तों के लिए वही chatbot। पहले pip install requests कीजिए:

Code
# chat.py — terminal chatbot jo pichli baatein yaad rakhta hai
import requests

messages = [
    {"role": "system", "content": "Tum ek friendly coding teacher ho. Hinglish mein, chhote jawab do."}
]

while True:
    question = input("nAap: ")
    if question.strip() == "exit":
        break

    messages.append({"role": "user", "content": question})

    res = requests.post("http://localhost:11434/api/chat", json={
        "model": "llama3.2",
        "messages": messages,
        "stream": False,
    })
    answer = res.json()["message"]

    print("nAI:", answer["content"])
    messages.append(answer)  # AI ka jawab bhi history mein

Chalaiye: python chat.py

6. Common Mistakes

  • fetch failed / Connection refused – Ollama चल नहीं रहा। Terminal में ollama serve चलाइए, या Ollama app खोलिए।
  • model not found – Model download नहीं हुआ। ollama pull llama3.2 चलाइए।
  • await is only valid in async functions – package.json में "type": "module" नहीं जोड़ा।
  • Chatbot naam bhool jata hai – messages.push(data.message) लिखना भूल गए। AI का जवाब history में नहीं गया।
  • Jawab bahut slow – पहली call में model memory में load होता है, इसलिए धीमी होती है। अगली calls तेज़ होंगी।

7. Kal Ko Hosted API Use Karni Ho Toh?

OpenAI, Claude या Gemini जैसी hosted APIs भी यही pattern use करती हैं – URL बदलता है, एक API key header में जाती है, और model का नाम बदलता है। messages और roles का idea वही रहता है। इसलिए जो आप आज Ollama पर सीख रहे हैं, वो सीधे production में काम आएगा। Hosted API का एक पूरा example – API key के साथ – हमारे FastAPI OpenAI Integration post में है। API key हमेशा code में नहीं, .env file में रखिए।

FAQ

Kya Ollama ke liye GPU zaroori hai?

नहीं। llama3.2 normal laptop के CPU पर भी चलता है – बस थोड़ा धीमा होगा। GPU या Apple M-series chip हो तो तेज़ चलेगा।

Kya mera data internet par jaata hai?

नहीं। Ollama सब कुछ आपके computer पर चलाता है। यह private notes या company data के साथ experiment करने का अच्छा तरीका है।

Conclusion

आज आपने अपने laptop पर AI चलाया, उससे code से बात की, और एक chatbot बनाया जो बातचीत याद रखता है। सबसे ज़रूरी सबक: AI खुद कुछ याद नहीं रखता – memory आपका code है।

अगले post में हम सीखेंगे कि AI से बिल्कुल वैसा जवाब कैसे लें जैसा चाहिए – system prompt, examples और JSON output के साथ। Part 3 पढ़िए → (18 Nov को आएगा) 🚀

Author at The Easy Master.

Related posts

Fetch API & REST APIs Cheat Sheet – Complete Guide in Hindi

The tutorial teaches it; the cheat sheet is for when you just need reminding.

Leave a Reply

Your email address will not be published. Required fields are marked *