File size: 8,278 Bytes
5a44b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
---
title: SpringGeminiChat
emoji: 🏒
colorFrom: purple
colorTo: yellow
sdk: docker
app_port: 7860
pinned: false
---

# πŸ€– Spring Gemini Chat

A modern, reactive Spring Boot application that integrates with Google's Gemini AI to provide intelligent chat capabilities. Built with Spring WebFlux for high-performance, non-blocking operations and featuring a clean, responsive web interface.

## πŸ“Έ Screenshots

### Chat Interface
![Chat Interface](screenshots/chat-interface.png)
*Modern, responsive chat UI with real-time messaging*

### API Response Example
![API Response](screenshots/api-response.png)
*JSON response structure from the Gemini integration*

### Health Check Dashboard
![Health Check](screenshots/health-check.png)
*Backend status and monitoring*

---

## ✨ Features

- πŸš€ **Reactive Programming** - Built with Spring WebFlux for non-blocking operations
- πŸ€– **Gemini AI Integration** - Powered by Google's latest Gemini AI model
- πŸ’¬ **Real-time Chat** - Instant responses with typing indicators
- 🎨 **Modern UI** - Clean, responsive web interface
- πŸ“± **Mobile Friendly** - Works seamlessly on all devices
- πŸ”„ **Health Monitoring** - Built-in health checks and status indicators
- πŸ›‘οΈ **Error Handling** - Comprehensive error handling and validation
- ⚑ **High Performance** - Optimized for speed and scalability
- πŸ”§ **Easy Configuration** - Environment-based configuration
- πŸ“Š **Conversation Context** - Support for contextual conversations

## πŸ› οΈ Technology Stack

### Backend
- **Java 17** - Programming language
- **Spring Boot 3.1.5** - Application framework
- **Spring WebFlux** - Reactive programming
- **Spring Validation** - Input validation
- **Maven** - Dependency management
- **Google Gemini AI** - AI language model

### Frontend
- **HTML5** - Structure
- **CSS3** - Styling with modern animations
- **Vanilla JavaScript** - Interactive functionality
- **Fetch API** - HTTP requests

## πŸ“‹ Prerequisites

Before you begin, ensure you have the following installed:

- β˜‘οΈ **Java 17 or higher**
- β˜‘οΈ **Maven 3.6+**
- β˜‘οΈ **Git**
- β˜‘οΈ **Google Gemini API Key** ([Get it here](https://makersuite.google.com/app/apikey))

## πŸš€ Quick Start

### 1. Clone the Repository
```bash
git clone https://github.com/yourusername/spring-gemini-chat.git
cd spring-gemini-chat
```

### 2. Set Environment Variables
```bash
# Windows
set GEMINI_API_KEY=your_actual_gemini_api_key_here

# macOS/Linux
export GEMINI_API_KEY=your_actual_gemini_api_key_here
```

### 3. Build and Run
```bash
# Clean and install dependencies
mvn clean install

# Run the application
mvn spring-boot:run
```

### 4. Access the Application
- **Backend API**: http://localhost:8080
- **Health Check**: http://localhost:8080/api/chat/health
- **Frontend UI**: Open `chat-ui.html` in your browser

## πŸ“š API Documentation

### Base URL
```
http://localhost:8080/api/chat
```

### Endpoints

#### πŸ’¬ Send Message
```http
POST /message
Content-Type: application/json

{
  "message": "Hello, how are you?"
}
```

**Response:**
```json
{
  "response": "I'm doing well, thank you for asking! How can I help you today?",
  "success": true,
  "error": null,
  "timestamp": 1692994488297
}
```

#### πŸ—£οΈ Conversation with Context
```http
POST /conversation
Content-Type: application/json

{
  "messages": [
    {
      "role": "user",
      "content": "What is Spring Boot?"
    },
    {
      "role": "assistant", 
      "content": "Spring Boot is a Java framework..."
    },
    {
      "role": "user",
      "content": "Can you give me an example?"
    }
  ]
}
```

#### πŸ“‘ Streaming Response
```http
POST /stream
Content-Type: application/json

{
  "message": "Tell me a story"
}
```

#### πŸ” Health Check
```http
GET /health
```

**Response:**
```json
{
  "status": "UP",
  "service": "Chat Service", 
  "timestamp": 1692994488297
}
```

## βš™οΈ Configuration

### Application Properties
```properties
# Server Configuration
server.port=8080

# Gemini AI Configuration
gemini.api.key=${GEMINI_API_KEY:your-api-key-here}
gemini.api.base-url=https://generativelanguage.googleapis.com/v1beta/models
gemini.api.model=gemini-1.5-flash
gemini.api.timeout=30000

# CORS Configuration
cors.allowed-origins=http://localhost:3000,http://localhost:8080
```

### Environment Variables
| Variable | Description | Required | Default |
|----------|-------------|----------|---------|
| `GEMINI_API_KEY` | Your Google Gemini API key | βœ… Yes | - |
| `SERVER_PORT` | Server port | ❌ No | 8080 |
| `GEMINI_MODEL` | Gemini model to use | ❌ No | gemini-1.5-flash |

## πŸ§ͺ Testing

### Run Unit Tests
```bash
mvn test
```

### Run Integration Tests
```bash
mvn verify
```

### Manual Testing with cURL

**Windows Command Prompt:**
```cmd
curl -X POST http://localhost:8080/api/chat/message -H "Content-Type: application/json" -d "{\"message\": \"Hello!\"}"
```

**PowerShell/Linux/macOS:**
```bash
curl -X POST http://localhost:8080/api/chat/message \
  -H "Content-Type: application/json" \
  -d '{"message": "Hello!"}'
```

## πŸ—οΈ Project Structure

```
spring-gemini-chat/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main/
β”‚   β”‚   β”œβ”€β”€ java/com/example/p1/
β”‚   β”‚   β”‚   β”œβ”€β”€ P1Application.java           # Main application
β”‚   β”‚   β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”‚   β”‚   └── AiConfiguration.java     # Configuration
β”‚   β”‚   β”‚   β”œβ”€β”€ controller/
β”‚   β”‚   β”‚   β”‚   └── ChatController.java      # REST endpoints
β”‚   β”‚   β”‚   └── service/
β”‚   β”‚   β”‚       └── ChatService.java         # Business logic
β”‚   β”‚   └── resources/
β”‚   β”‚       └── application.properties       # Configuration
β”‚   └── test/
β”‚       └── java/com/example/p1/
β”‚               └── P1ApplicationTests.java 
β”œβ”€β”€ index.html                            # Frontend interface
β”œβ”€β”€ screenshots/                            # UI screenshots
β”œβ”€β”€ pom.xml                                 # Maven configuration
└── README.md                              # This file
```

## πŸš€ Deployment

### Using Docker
```dockerfile
FROM openjdk:17-jdk-slim
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
```

```bash
# Build the application
mvn clean package

# Build Docker image
docker build -t spring-gemini-chat .

# Run container
docker run -p 8080:8080 -e GEMINI_API_KEY=your_key spring-gemini-chat
```

### Using JAR
```bash
# Build JAR
mvn clean package

# Run JAR
java -jar target/p1-0.0.1-SNAPSHOT.jar
```

## 🀝 Contributing

We welcome contributions! Please follow these steps:

1. **Fork the repository**
2. **Create a feature branch** (`git checkout -b feature/amazing-feature`)
3. **Commit your changes** (`git commit -m 'Add some amazing feature'`)
4. **Push to the branch** (`git push origin feature/amazing-feature`)
5. **Open a Pull Request**

### Development Guidelines
- Follow Java coding conventions
- Write unit tests for new features
- Update documentation for API changes
- Use meaningful commit messages

## πŸ› Troubleshooting

### Common Issues

**❌ "Connection failed" in UI**
- Ensure backend is running on port 8080
- Check CORS configuration
- Verify network connectivity

**❌ "API key not found" error**
- Set GEMINI_API_KEY environment variable
- Restart the application after setting the key
- Verify the API key is valid

**❌ Maven build fails**
- Check Java version (requires Java 17+)
- Clear Maven cache: `mvn clean`
- Update dependencies: `mvn dependency:resolve -U`

**❌ CORS errors**
- Check allowed origins in `AiConfiguration.java`
- Verify frontend URL matches CORS settings

## πŸ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## πŸ™ Acknowledgments

- **Google Gemini AI** - For providing the AI capabilities
- **Spring Team** - For the excellent Spring Boot framework
- **Contributors** - Thanks to all who contribute to this project

## πŸ“ž Support

- πŸ“§ **Email**: [email protected]
- πŸ› **Issues**: [GitHub Issues](https://github.com/aniketqw/spring-gemini-chat/issues)
- πŸ’¬ **Discussions**: [GitHub Discussions](https://github.com/aniketqw/spring-gemini-chat/discussions)

---