Building Your First AI Chatbot with OpenAI
Introduction#
Creating an AI agent has never been easier with OpenAI's powerful API. In this tutorial, we'll build a complete agent from scratch.
Prerequisites#
- Node.js installed
- OpenAI API key
- Basic JavaScript knowledge
Step 1: Setup OpenAI API#
First, get your API key from OpenAI:
npm install openaiimport OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});Step 2: Create Chat Function#
async function chatWithAI(message) {
const completion = await openai.chat.completions.create({
messages: [{ role: "user", content: message }],
model: "gpt-3.5-turbo",
});
return completion.choices[0].message.content;
}Step 3: Build the Interface#
Create a simple HTML interface:
Production Deployment Notes (South Asia & Nepal)#
When deploying OpenAI-based bots for businesses in Kathmandu or regional South Asia:
- Cost Management in NPR: API calls billed in USD translate to approximately NPR 0.15 - 0.40 per turn on GPT-4o-mini. Set strict monthly usage budgets to avoid unexpected currency exchange charges.
- Payment Verification: For e-commerce chatbots, bridge checkout intents directly with eSewa or Fonepay QR payment webhooks rather than relying solely on international cards.
- Network Optimization: Ensure fallback mechanisms for fluctuating 4G mobile connections across NTC and Ncell networks.
Conclusion#
You now have a working AI chatbot! This is just the beginning - you can add features like conversation memory, file uploads, and more advanced AI capabilities.