Skip to content
KumeshaRashmiPublic

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

🤖 KumiaiBot – AI Chatbot Web Application

KumiaiBot is a full-stack AI chatbot web application developed using the MERN stack (MongoDB, Express.js, React.js, and Node.js) and integrated with the OpenAI API to generate intelligent, natural-language responses.

The application provides a ChatGPT-like conversational experience with secure user authentication, persistent user data, responsive UI, and real-time communication between the frontend, backend, database, and OpenAI services.


📌 Project Overview

KumiaiBot was developed as a full-stack AI-powered chatbot application that allows users to register, securely log in, and interact with an AI assistant through a modern web interface.

The application sends user messages from the React frontend to the Node.js/Express backend. The backend securely communicates with the OpenAI API, receives the generated response, and sends it back to the frontend.

User authentication and application data are managed using MongoDB.

Project Duration

May 2024 – June 2024

Project Type

Full-Stack AI Web Application


✨ Features

  • 🤖 AI-powered chatbot using the OpenAI API
  • 💬 ChatGPT-like conversational interface
  • 👤 User registration
  • 🔐 Secure user login
  • 🔑 JWT-based authentication
  • 🍪 HTTP-only cookies for authentication
  • 🗄️ MongoDB database integration
  • ⚡ REST API communication
  • 🔄 Axios-based frontend/backend communication
  • 🎨 React and Material UI interface
  • 📱 Responsive web design
  • 💾 Persistent user information
  • 🧠 Automatically generated AI responses
  • 🔒 Backend-based OpenAI API integration
  • 🚪 Secure logout functionality

🛠️ Technologies Used

Frontend

  • React.js
  • Material UI
  • Axios
  • JavaScript
  • HTML5
  • CSS3

Backend

  • Node.js
  • Express.js
  • REST API
  • JWT
  • HTTP-only Cookies

Database

  • MongoDB
  • MongoDB Atlas / MongoDB Server

Artificial Intelligence

  • OpenAI API
  • OpenAI SDK

Development Tools

  • Git
  • GitHub
  • npm
  • Visual Studio Code

🏗️ System Architecture

                    ┌─────────────────────┐
                    │       User          │
                    │    Web Browser      │
                    └──────────┬──────────┘
                               │
                               │ User Message
                               ▼
                    ┌─────────────────────┐
                    │    React Frontend   │
                    │   Material UI       │
                    └──────────┬──────────┘
                               │
                               │ Axios / REST API
                               ▼
                    ┌─────────────────────┐
                    │ Node.js + Express   │
                    │      Backend        │
                    └───────┬─────┬───────┘
                            │     │
                ┌───────────┘     └──────────────┐
                │                                │
                ▼                                ▼
      ┌──────────────────┐             ┌──────────────────┐
      │     MongoDB      │             │    OpenAI API    │
      │                  │             │                  │
      │ Users / Data     │             │ AI Generation    │
      └──────────────────┘             └────────┬─────────┘
                                                │
                                                │ AI Response
                                                ▼
                                      ┌─────────────────────┐
                                      │   Express Backend   │
                                      └──────────┬──────────┘
                                                 │
                                                 ▼
                                      ┌─────────────────────┐
                                      │   React Frontend    │
                                      │  Display Response   │
                                      └─────────────────────┘

📂 Project Structure

The exact structure may vary slightly depending on the version of the repository, but the application follows a typical MERN architecture:

chatbot/
│
├── backend/
│   ├── controllers/
│   ├── models/
│   ├── routes/
│   ├── middleware/
│   ├── config/
│   ├── server.js
│   ├── package.json
│   └── .env
│
├── frontend/
│   ├── src/
│   │   ├── components/
│   │   ├── pages/
│   │   ├── services/
│   │   ├── App.js
│   │   └── index.js
│   ├── public/
│   ├── package.json
│   └── .env
│
├── .gitignore
└── README.md

🚀 Getting Started

Follow the steps below to run KumiaiBot locally.

1. Prerequisites

Before running the application, install the following software:

  • Node.js
  • npm
  • Git
  • MongoDB or MongoDB Atlas account
  • OpenAI API key

You can verify Node.js and npm installation using:

node --version
npm --version

2. Clone the Repository

Clone the repository from GitHub:

git clone https://github.com/KumeshaRashmi/chatbot.git

Move into the project directory:

cd chatbot

3. Install Backend Dependencies

Navigate to the backend directory:

cd backend

Install the required npm packages:

npm install

The backend dependencies include packages such as:

express
mongoose
jsonwebtoken
cookie-parser
cors
dotenv
openai
bcrypt / bcryptjs

The exact dependencies installed by npm install are defined in the project's package.json.


4. Install Frontend Dependencies

Open another terminal or navigate back to the project root:

cd ..

Then move into the frontend directory:

cd frontend

Install the frontend dependencies:

npm install

The frontend uses technologies including:

react
axios
@mui/material
@emotion/react
@emotion/styled

🔐 5. Configure Environment Variables

Environment variables are required for sensitive configuration such as the MongoDB connection and OpenAI API key.

OpenAI recommends keeping API keys on the server and storing them through environment variables rather than exposing them in browser-side code.

Backend .env

Inside the backend directory, create:

.env

Add the required configuration:

PORT=5000

MONGODB_URI=your_mongodb_connection_string

JWT_SECRET=your_secure_jwt_secret

OPENAI_API_KEY=your_openai_api_key

Replace:

your_mongodb_connection_string

with your MongoDB connection string.

Replace:

your_secure_jwt_secret

with a strong secret used for JWT authentication.

Replace:

your_openai_api_key

with your OpenAI API key.

The official OpenAI JavaScript SDK can be installed with:

npm install openai

and is designed for server-side JavaScript environments such as Node.js.


⚠️ Important: Protect Your API Key

Never commit your .env file to GitHub.

Add the following to .gitignore:

node_modules/
.env
.env.local

Your API key should remain private.

Do not write:

const apiKey = "sk-your-secret-key";

inside frontend React components.

Instead, the frontend should communicate with your backend, and the backend should communicate with OpenAI.

React
  │
  │ User message
  ▼
Express Backend
  │
  │ OpenAI API request
  ▼
OpenAI
  │
  │ Generated response
  ▼
Express Backend
  │
  ▼
React

This architecture prevents the OpenAI secret key from being exposed to users.


🗄️ 6. Configure MongoDB

You can use either:

  • Local MongoDB
  • MongoDB Atlas

Example MongoDB connection:

MONGODB_URI=mongodb://localhost:27017/kumiaiBot

Or a MongoDB Atlas connection:

MONGODB_URI=mongodb+srv://<username>:<password>@cluster.mongodb.net/kumiaiBot

Make sure your MongoDB server is running before starting the backend.


▶️ 7. Start the Backend Server

Navigate to the backend directory:

cd backend

Start the server:

npm start

Or, if the project uses a development script:

npm run dev

The backend should start on something similar to:

http://localhost:5000

▶️ 8. Start the Frontend

Open a new terminal.

Navigate to the frontend:

cd frontend

Start the React development server:

npm start

The frontend will normally be available at:

http://localhost:3000

Open the URL in your browser.


💬 9. Using the Chatbot

Once the application is running:

Step 1 – Register

Create a new user account using the registration page.

Step 2 – Login

Log in using your registered credentials.

Step 3 – Open Chat

After successful authentication, access the chatbot interface.

Step 4 – Send a Message

Enter a question or prompt into the chat input.

For example:

Explain what machine learning is.

Step 5 – Backend Processing

The frontend sends the message to the backend using Axios.

React → Axios → Express API

Step 6 – OpenAI Processing

The backend sends the request to the OpenAI API.

Express Backend → OpenAI API

Step 7 – Generate Response

OpenAI generates an AI response.

Step 8 – Display Response

The backend returns the generated response to React.

OpenAI
   ↓
Express
   ↓
Axios
   ↓
React
   ↓
Chat Interface

The response is then automatically displayed in the conversation.


🔑 Authentication Flow

KumiaiBot uses JWT-based authentication.

The authentication process works as follows:

User
 │
 │ Register / Login
 ▼
React Frontend
 │
 │ Axios Request
 ▼
Express Backend
 │
 ├── Validate User
 │
 ├── Check MongoDB
 │
 └── Generate JWT
          │
          ▼
     HTTP-only Cookie

After authentication, protected API requests can be authenticated using the JWT stored in the HTTP-only cookie.

Benefits

  • Secure authentication
  • Password-based user accounts
  • JWT-based authorization
  • HTTP-only cookies
  • Protected backend routes
  • Persistent user sessions

🤖 OpenAI Integration

The chatbot integrates the OpenAI API through the backend.

The basic request flow is:

User Input
     │
     ▼
React Chat Interface
     │
     ▼
Axios POST Request
     │
     ▼
Express API
     │
     ▼
OpenAI SDK
     │
     ▼
OpenAI Model
     │
     ▼
Generated AI Response
     │
     ▼
Express API
     │
     ▼
React
     │
     ▼
Chat Message

The OpenAI API provides the AI-generated natural-language response. The official OpenAI JavaScript SDK supports server-side Node.js applications and can be installed through npm.


🔄 Example API Flow

A simplified chatbot request can look like:

POST /api/chat

Request:

{
  "message": "What is artificial intelligence?"
}

Backend:

Receive message
      ↓
Validate request
      ↓
Send prompt to OpenAI
      ↓
Receive generated response
      ↓
Return response to frontend

Response:

{
  "response": "Artificial intelligence is a field of computer science..."
}

📡 Frontend–Backend Communication

Axios is used to communicate between the React frontend and Express backend.

Example:

axios.post("/api/chat", {
    message: userMessage
});

The backend processes the request and returns the AI-generated response.

This separation keeps the frontend responsible for presentation while the backend handles authentication, database operations, and OpenAI API communication.


🧪 Testing the Application

After starting both servers, test the following functionality:

Authentication

  • User registration
  • User login
  • Invalid login handling
  • JWT authentication
  • HTTP-only cookie
  • Logout

Chatbot

  • Send message
  • Receive AI response
  • Multiple messages
  • Empty message validation
  • API error handling

Database

  • MongoDB connection
  • User creation
  • User authentication
  • Data persistence

UI

  • Responsive layout
  • Chat interface
  • Login page
  • Registration page
  • Navigation
  • Error messages

🐛 Troubleshooting

OpenAI API Key Error

If you receive an error related to the API key, check that:

OPENAI_API_KEY=your_api_key

exists in the backend .env file.

Restart the backend after changing environment variables.

OpenAI's documentation recommends configuring the SDK through the OPENAI_API_KEY environment variable.


MongoDB Connection Error

Check:

MONGODB_URI=your_connection_string

Make sure MongoDB is running and the connection string is correct.

For MongoDB Atlas, ensure that your IP address is permitted in the Atlas network access configuration.


CORS Error

If the frontend cannot communicate with the backend, verify:

  • Backend server is running
  • Frontend URL is configured correctly
  • CORS middleware is enabled
  • API endpoint URL is correct

Port Already in Use

If port 5000 or 3000 is already being used, stop the process using that port or configure a different port.

Example:

PORT=5001

🔒 Security Considerations

The application uses several security mechanisms:

  • JWT authentication
  • HTTP-only cookies
  • Environment variables
  • Backend-only OpenAI API communication
  • Password hashing
  • Protected API routes
  • MongoDB authentication

Never expose secrets

Do not commit:

.env
API keys
JWT secrets
Database passwords

to GitHub.

OpenAI specifically recommends not committing API keys to repositories and recommends environment variables for API-key management.


📈 Future Improvements

Possible improvements for future versions include:

  • 💾 Persistent chat history
  • 🗂️ Multiple conversations
  • 📝 Conversation titles
  • 🗑️ Delete conversations
  • 🔍 Search chat history
  • 🌙 Dark/light theme
  • 🎙️ Voice input
  • 🔊 Text-to-speech
  • 📎 File uploads
  • 🧠 RAG-based document question answering
  • 📊 Usage analytics
  • ⚡ Streaming AI responses
  • 👥 Multi-user conversation management
  • 🛡️ Rate limiting
  • 🚀 Cloud deployment
  • 🐳 Docker containerization

🎯 Learning Outcomes

Through this project, the following technical concepts were practiced:

  • Full-stack web application development
  • MERN stack development
  • REST API development
  • React component development
  • Material UI
  • MongoDB database integration
  • User authentication
  • JWT authentication
  • HTTP-only cookies
  • API integration
  • OpenAI API integration
  • Asynchronous JavaScript
  • Axios communication
  • Environment variable management
  • Backend security
  • Git and GitHub

📸 Application Screenshots

Add screenshots of the application here.

Example:

## Login Page

![Login Page](screenshots/login.png)

## Chat Interface

![Chat Interface](screenshots/chat.png)

## Registration Page

![Registration Page](screenshots/register.png)

Recommended screenshots:

  1. Login page
  2. Registration page
  3. Chat interface
  4. AI-generated response
  5. Responsive/mobile interface

👩‍💻 Author

Kumesha Rashmi

Computer Engineering Undergraduate University of Ruhuna, Sri Lanka

GitHub:

https://github.com/KumeshaRashmi


📄 License

This project was developed as an academic/personal software development project.

You may modify and extend the project for learning and development purposes.


⭐ Acknowledgements

  • React.js
  • Node.js
  • Express.js
  • MongoDB
  • Material UI
  • Axios
  • OpenAI API

🚀 Quick Start

For experienced developers, the complete setup can be summarized as:

# Clone repository
git clone https://github.com/KumeshaRashmi/chatbot.git

cd chatbot

# Install backend
cd backend
npm install

# Configure .env
# Add MONGODB_URI
# Add JWT_SECRET
# Add OPENAI_API_KEY

# Start backend
npm start

In another terminal:

cd chatbot/frontend

npm install

npm start

Then open:

http://localhost:3000

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages