Skip to content

shihabhasan0161/AI-Chatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

39 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Chatbot Logo

AI Chatbot

A full-stack, open-source chatbot application that combines the power of Spring AI (backend) with React (frontend) to deliver intelligent chat completions and AI-powered image generation in a seamless, user-friendly experience.

chatbot_1 chatbot_2

🎯 Project Purpose

This project demonstrates a modern full-stack architecture for building AI-powered applications. It serves as:

  • A learning resource for developers interested in integrating OpenAI APIs with Spring Boot and React
  • A production-ready foundation for building custom chatbot applications
  • A best-practices example for handling user API credentials securely on the client side (localstorage)
  • A template for extending with additional AI features and integrations

✨ Key Features

πŸ’¬ Intelligent Chat Interface

  • Real-time conversational UI with smooth message streaming
  • Multi-session history that persists across browser sessions using localStorage
  • Responsive sidebar with conversation switching and management
  • Built with React and optimized for all screen sizes

🎨 Image Generation

  • DALLΒ·E 3 integration for high-quality AI image generation
  • Dedicated image tab with preview and download capabilities
  • History tracking of generated images within your session

πŸ” Secure API Key Handling

  • Client-side API key storage in localStorage (never sent to backend unless needed)
  • Modal-based configuration for easy setup and key management
  • User control over credentials without server-side storage

πŸš€ Backend Architecture

  • Spring AI integration for OpenAI chat completions and image generation
  • REST API endpoints with proper error handling and validation
  • CORS configuration for secure frontend-backend communication
  • Docker support for containerized deployment

πŸ“‹ System Requirements

Before getting started, ensure you have the following installed:

  • Node.js β‰₯ 20.x
  • Maven β‰₯ 3.9
  • JDK 21 or later
  • OpenAI API key with access to:
    • GPT-4o model (for chat completions)
    • DALLΒ·E-3 model (for image generation)

πŸš€ Quick Start

1. Clone the Repository

git clone https://github.com/shihabhasan0161/react-spring-chatbot.git
cd react-spring-chatbot

2. Configure Environment Variables

Backend Configuration - Edit backend/src/main/resources/application.properties:

# OpenAI Model Configuration
spring.ai.openai.chat.options.model=gpt-4o
spring.ai.openai.api-key=

# Note: Leave the API key empty here - it will be passed at runtime from the frontend

Frontend Configuration - Create frontend/.env:

VITE_BACKEND_URL=http://localhost:8080/

3. Start the Backend

cd backend
mvn spring-boot:run

The Spring Boot server will start on http://localhost:8080.

4. Start the Frontend

In a new terminal window:

cd frontend
npm install
npm run dev

The React development server will start on http://localhost:5173.

5. Access the Application

  1. Open your browser to http://localhost:5173
  2. Click the "Set API Key" button in the top-right corner
  3. Enter your OpenAI API key
  4. Start chatting or generating images!

πŸ—οΈ Architecture Overview

Three-Layer Architecture

Layer Technology Responsibility
Frontend React + Vite User interface, conversation management, local persistence, API key handling
Gateway Axios HTTP Client Routing requests from frontend to backend API endpoints
Backend Spring Boot + Spring AI OpenAI integration, chat completion orchestration, image generation

Data Flow

User Input (Frontend)
    ↓
React Chat Component
    ↓
Axios HTTP Request β†’ Backend REST API
    ↓
Spring AI Service Layer
    ↓
OpenAI API (with user's API key)
    ↓
Response β†’ React Component
    ↓
Browser LocalStorage (for persistence)

πŸ“‘ API Reference

Chat Completion Endpoint

POST /api/chat

Request payload:

{
  "prompt": "What is machine learning?",
  "apiKey": "sk-..."
}

Response:

{
  "response": "Machine learning is...",
  "timestamp": "2025-11-17T10:30:00Z"
}

Image Generation Endpoint

POST /api/generate-image

Request payload:

{
  "prompt": "A futuristic city at sunset",
  "apiKey": "sk-..."
}

Response:

{
  "imageUrl": "https://...",
  "timestamp": "2025-11-17T10:30:00Z"
}

πŸ“ Project Structure

react-spring-chatbot/
β”œβ”€β”€ backend/                           # Spring Boot application
β”‚   β”œβ”€β”€ src/main/java/com/chatbot/
β”‚   β”‚   β”œβ”€β”€ controller/               # REST API controllers
β”‚   β”‚   β”œβ”€β”€ service/                  # Business logic layer
β”‚   β”‚   β”œβ”€β”€ model/                    # Data models
β”‚   β”‚   └── config/                   # Configuration classes
β”‚   β”œβ”€β”€ pom.xml                       # Maven dependencies
β”‚   └── Dockerfile                    # Container configuration
β”‚
β”œβ”€β”€ frontend/                          # React application
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/               # React components
β”‚   β”‚   β”œβ”€β”€ utils/                    # Helper functions
β”‚   β”‚   β”œβ”€β”€ App.jsx                   # Main app component
β”‚   β”‚   └── main.jsx                  # Entry point
β”‚   β”œβ”€β”€ package.json                  # NPM dependencies
β”‚   └── vite.config.js               # Vite configuration
β”‚
β”œβ”€β”€ README.md                          # This file
β”œβ”€β”€ LEARN.md                          # Deep dive into architecture
β”œβ”€β”€ CONTRIBUTING.md                   # Contribution guidelines
└── LICENSE.md                        # MIT License

πŸ”— Additional Resources

  • LEARN.md β€” Detailed architectural documentation, design patterns, and extension ideas
  • CONTRIBUTING.md β€” Guidelines for contributing, development workflow, and code standards

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Feel free to use this project for learning, development, and as a foundation for your own applications.


πŸ’‘ Getting Help

Happy coding! πŸŽ‰

About

A full-stack, open-source chatbot application that combines the power of Spring AI (backend) with React (frontend) to deliver intelligent chat completions and AI-powered image generation in a seamless, user-friendly experience.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors