Spaces:
Sleeping
Sleeping
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

*Modern, responsive chat UI with real-time messaging*
### API Response Example

*JSON response structure from the Gemini integration*
### Health Check Dashboard

*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)
---
|