--- 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**: aniket21stcentury@gmail.com - ๐Ÿ› **Issues**: [GitHub Issues](https://github.com/aniketqw/spring-gemini-chat/issues) - ๐Ÿ’ฌ **Discussions**: [GitHub Discussions](https://github.com/aniketqw/spring-gemini-chat/discussions) ---