Ultimate Guide to Creating an AI Chatbot

Ultimate Guide to Creating an AI Chatbot

An AI-powered chat bot designed to handle customer requests and questionnaires using automated data sets.

Introduction

This comprehensive guide walks through building a production-ready AI chatbot that combines pre-programmed responses with OpenAI's GPT models. The chatbot features a modern web interface, efficient message handling, and a hybrid response system that balances speed with AI capabilities.

Click the link to explore : → Test Demonstration on Chatbot

Key Features

  • Hybrid response system combining predefined answers with AI-generated responses

  • Real-time chat interface with smooth user experience

  • Scalable architecture using Node.js and Express

  • Integration with OpenAI's GPT models

  • Persistent storage for predefined responses

  • Error handling and fallback mechanisms

  • Responsive design for all devices

Technical Architecture

The chatbot uses a three-tier architecture:

  1. Frontend: HTML/CSS/JavaScript for user interface

  2. Backend: Node.js/Express server for request handling

  3. Data Layer: JSON storage and OpenAI API integration

The system first attempts to match user queries against predefined responses in data.json. If no match is found, it forwards the query to OpenAI's API for an AI-generated response. This hybrid approach optimizes response time while maintaining conversation quality.

Prerequisites

  • Node.js and npm installed

  • Basic knowledge of HTML, CSS, JavaScript

  • OpenAI API key

Project Structure

Copychatbot/
├── server.js
├── index.html
├── styles.css
├── .env
└── data.json

Step 1: Setting Up the Project

First, initialize your Node.js project and install dependencies:

bashCopymkdir chatbot
cd chatbot
npm init -y
npm install express dotenv axios

Create a .env file for your OpenAI API key:

CopyOPENAI_API_KEY=your_api_key_here

Step 2: Creating the Server (server.js)

javascriptCopyrequire('dotenv').config();
const express = require('express');
const fs = require('fs').promises;
const path = require('path');
const axios = require('axios');
const app = express();
const port = 3000;

app.use(express.json());
app.use(express.static(path.join(__dirname)));

let data = {};

// Load predefined responses
const loadData = async () => {
    try {
        const jsonData = await fs.readFile('./data.json', 'utf8');
        data = JSON.parse(jsonData);
        console.log('Data loaded successfully.');
    } catch (err) {
        console.error('Error reading data file:', err);
    }
};

// Search endpoint
app.post('/search', async (req, res) => {
    const query = req.body.query?.toLowerCase() || '';
    const matches = [];

    // Check predefined responses
    for (const [keyword, response] of Object.entries(data)) {
        if (query.includes(keyword.toLowerCase())) {
            matches.push(response);
        }
    }

    if (matches.length > 0) {
        return res.send(matches[0]);
    }

    // Fallback to AI
    try {
        const aiResponse = await getAIResponse(query);
        res.send(aiResponse);
    } catch (error) {
        res.status(500).send('Error processing request.');
    }
});

// AI response function
const getAIResponse = async (query) => {
    try {
        const response = await axios.post(
            'https://api.openai.com/v1/chat/completions',
            {
                model: 'gpt-3.5-turbo',
                messages: [{ role: 'user', content: query }]
            },
            {
                headers: {
                    'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
                    'Content-Type': 'application/json'
                }
            }
        );
        return response.data.choices[0].message.content;
    } catch (error) {
        console.error("AI Error:", error);
        throw error;
    }
};

loadData().then(() => {
    app.listen(port, () => {
        console.log(`Server running at http://localhost:${port}`);
    });
});

Step 3: Building the Frontend (index.html)

htmlCopy<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Chatbot</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="chat-container">
        <div class="chat-header">
            <h1>AI Chatbot</h1>
        </div>
        <div class="chat-box" id="chat-box">
            <div class="bot-message">Hi! How can I help you today?</div>
        </div>
        <div class="input-container">
            <input type="text" id="query" placeholder="Type your message..." 
                   onkeypress="handleEnter(event)">
            <button onclick="submitQuery()">Send</button>
        </div>
    </div>

    <script>
        async function submitQuery() {
            const queryInput = document.getElementById('query');
            const query = queryInput.value.trim();
            if (!query) return;

            const chatBox = document.getElementById('chat-box');

            // Add user message
            const userMessage = document.createElement('div');
            userMessage.className = 'user-message';
            userMessage.innerText = query;
            chatBox.appendChild(userMessage);

            try {
                const response = await fetch('/search', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({ query })
                });

                const result = await response.text();

                // Add bot response
                const botMessage = document.createElement('div');
                botMessage.className = 'bot-message';
                botMessage.innerText = result;
                chatBox.appendChild(botMessage);
            } catch (error) {
                console.error('Error:', error);
            }

            queryInput.value = '';
            chatBox.scrollTop = chatBox.scrollHeight;
        }

        function handleEnter(event) {
            if (event.key === 'Enter') {
                submitQuery();
            }
        }
    </script>
</body>
</html>

Step 4: Styling the Chatbot (styles.css)

cssCopybody {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 20px;
    display: flex;
    justify-content: center;
    background-color: #f0f2f5;
}

.chat-container {
    width: 100%;
    max-width: 600px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.chat-header {
    padding: 20px;
    border-bottom: 1px solid #eee;
}

.chat-header h1 {
    margin: 0;
    font-size: 24px;
    color: #333;
}

.chat-box {
    height: 400px;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.bot-message, .user-message {
    max-width: 70%;
    padding: 10px 15px;
    border-radius: 15px;
    margin: 5px 0;
}

.bot-message {
    background-color: #f0f2f5;
    align-self: flex-start;
}

.user-message {
    background-color: #0084ff;
    color: white;
    align-self: flex-end;
}

.input-container {
    padding: 20px;
    border-top: 1px solid #eee;
    display: flex;
    gap: 10px;
}

input {
    flex: 1;
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 20px;
    outline: none;
}

button {
    padding: 10px 20px;
    background-color: #0084ff;
    color: white;
    border: none;
    border-radius: 20px;
    cursor: pointer;
}

button:hover {
    background-color: #0073e6;
}

Step 5: Creating the Knowledge Base (data.json)

jsonCopy{
    "hello": "Hi! How can I help you today?",
    "how are you": "I'm doing well, thank you! How can I assist you?",
    "goodbye": "Goodbye! Have a great day!",
    "thanks": "You're welcome! Let me know if you need anything else."
}

Running the Chatbot

  1. Save all files in your project directory

  2. Add your OpenAI API key to .env

  3. Run the server:

bashCopynode server.js
  1. Open http://localhost:3000 in your browser

Features

  • Hybrid response system (predefined + AI)

  • Real-time chat interface

  • Responsive design

Customization Options

  1. Styling: Modify styles.css to match your brand

  2. Responses: Add more predefined responses in data.json

  3. AI Model: Change the OpenAI model in getAIResponse()

  4. UI Elements: Add typing indicators, timestamps, or user avatars

Security Considerations

  1. Store API keys securely

  2. Implement rate limiting

  3. Validate user input

  4. Use HTTPS in production

  5. Add authentication if needed