A fully functional AI-powered chatbot backend built using Java, Spring Boot, and the Google Gemini API. This project processes user messages, sends them to Gemini, receives AI-generated responses, and stores the full conversation history in a database.
- AI chatbot using the Google Gemini API
- Clean REST API architecture
- Stores user message and bot reply in MySQL
- Follows proper Controller -> Service -> Repository pattern
- CORS enabled (frontend friendly)
- Basic HTML/CSS/JS frontend included in
chatbot-frontend/ - Lightweight, beginner-friendly codebase
- Java 17
- Spring Boot (Web, JPA)
- Spring Data JPA
- MySQL
- Maven
- RestTemplate (for Gemini API calls)
- Lombok
- org.json
- HTML, CSS, JavaScript (no framework, static files)
AI-chatBot/
├── chatbot-frontend/ # Static HTML/CSS/JS frontend
│ ├── index.html
│ ├── script.js
│ └── style.css
├── src/main/java/com/chatbot/chatbot/
│ ├── ChatbotApplication.java
│ ├── controller/ChatController.java
│ ├── model/ChatMessage.java
│ ├── repo/ChatMessageRepo.java
│ └── service/ChatService.java
├── src/main/resources/
│ └── application.properties # not committed, see setup below
├── pom.xml
└── README.md
Before you begin, install the following:
- Java 17 or newer (JDK)
- Maven (usually bundled with your IDE, or install separately)
- MySQL Server 8.0 or newer
- An IDE such as IntelliJ IDEA (Community or Ultimate)
- A Google Gemini API key (see below)
git clone https://github.com/Amankumar0152/AI-chatBot.git
Log in to MySQL:
mysql -u root -p
Then run:
CREATE DATABASE chatbot;Exit MySQL:
exit;- Go to https://aistudio.google.com/app/apikey
- Sign in with a Google account
- Click "Create API key"
- Copy the key
- If this is your first key on this project, link a billing account at https://console.cloud.google.com/billing if you hit quota errors, since Google requires account verification for consistent free-tier access on current models
This file is intentionally excluded from the repository (see .gitignore) so that credentials are never committed. Create it yourself at:
src/main/resources/application.properties
With the following content:
spring.datasource.url=jdbc:mysql://localhost:3306/chatbot
spring.datasource.username=root
spring.datasource.password=${DB_PASSWORD}
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
gemini.api.key=${GEMINI_API_KEY}
Rather than hardcoding secrets in the file above, provide them as environment variables.
In IntelliJ:
- Open Run menu, then Edit Configurations
- Select or create the Spring Boot run configuration for ChatbotApplication
- Open Modify Options, enable Environment Variables
- In the Environment Variables field, enter:
DB_PASSWORD=your_mysql_root_password;GEMINI_API_KEY=your_gemini_api_key
Replace both values with your own.
Click Run in IntelliJ, or from the command line:
mvnw.cmd spring-boot:run
The server starts on port 8080. A successful start shows a line like:
Started ChatbotApplication in X seconds
The chat endpoint accepts a raw plain-text request body (not JSON).
Using curl:
curl -X POST http://localhost:8080/api/chat -H "Content-Type: text/plain" -d "Hello, who are you?"
Or using Postman:
- Method: POST
- URL: http://localhost:8080/api/chat
- Body: raw, type Text
- Enter your message and send
Open chatbot-frontend/index.html directly in your browser while the backend is running on port 8080.
- Never commit
application.propertieswith real credentials. It is listed in.gitignorefor this reason. - Gemini model names change frequently as Google deprecates older versions. If you get a 404 model-not-found error, check https://ai.google.dev/gemini-api/docs/models for the current model list and update the model name in
ChatService.java. - If you see a 429 quota error mentioning
free_tier_requests, limit: 0, this usually means the model you are calling is no longer covered under free tier, not a problem with your key. Switch to a currently supported model.
See the LICENSE file in this repository.