Skip to content

vijay-backend-lab/spring-ai-chat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Spring AI Chat API (Gemini 2.5 Flash Lite)

A lightweight REST-based Chat API built with Spring Boot and Spring AI, integrating with Google Gemini 2.5 Flash Lite to generate intelligent responses for user queries.


🔥 Highlights

  • ✅ Simple REST API for chat interaction
  • ✅ Integrated with Google GenAI (Gemini) via Spring AI
  • ✅ Clean layered architecture (Controller → Service → Config)
  • ✅ Environment-based API key configuration
  • ✅ Production-ready structure for extensibility

🛠 Tech Stack

  • Java 25
  • Spring Boot 4.0.6
  • Spring AI (2.0.0-M4)
  • Google Gemini 2.5 Flash Lite
  • Maven

📌 API Endpoint

POST /api/chat

🔹 Request

curl --location 'http://localhost:8080/api/chat' \
--header 'Content-Type: application/json' \
--data 'what is java. explain in 1 line'

🔹 Response

{
  "explanation": "Java is a high-level, class-based, object-oriented programming language designed to have minimal implementation dependencies."
}

⚙️ Configuration

application.yaml

spring:
  application:
    name: spring-ai-chat
  ai:
    google:
      genai:
        api-key: ${GOOGLE_GENAI_API_KEY}
        chat:
          options:
            model: gemini-2.5-flash-lite

🔐 Environment Variable

Set your API key:

export GOOGLE_GENAI_API_KEY=your_api_key_here

🏗 Project Structure

com.vijay.spring.boot.ai.spring_ai_chat
│
├── api/        → REST Controllers
├── service/    → Business Logic (Gemini integration)
├── config/     → AI / App Configuration
└── (main)      → Application bootstrap

🔄 Request Flow

  1. Client sends POST request with message
  2. Controller receives request
  3. Service layer calls Spring AI
  4. Spring AI invokes Gemini model
  5. Response is returned to client

🔧 Build & Run

1️⃣ Clone Repository

git clone https://github.com/vijay-backend-lab/spring-ai-chat.git
cd spring-ai-chat

2️⃣ Build Project

mvn clean install

3️⃣ Run Application

mvn spring-boot:run

📦 Maven Dependencies (Key)

  • spring-boot-starter-webmvc
  • spring-ai-starter-model-google-genai
  • Spring AI BOM for dependency management

🧠 Design Considerations

  • Uses Spring AI abstraction for model integration
  • Model configurable via application.yaml
  • Clean separation of concerns (Controller, Service, Config)
  • Ready for extension (multi-model, streaming, etc.)

📈 Future Enhancements

  • 🔄 Streaming responses (real-time chat)
  • 🧵 Conversation memory support
  • ⚡ Rate limiting & caching
  • 🔐 Authentication & API security
  • 📊 Observability (metrics, logging)

⚠️ Notes

  • Ensure GOOGLE_GENAI_API_KEY is set before running
  • Input is currently treated as raw text (can be enhanced to JSON payload)
  • Uses Gemini Flash Lite for low latency responses

📄 License

Licensed under Apache License 2.0


🙌 Contribution

Contributions are welcome! Feel free to fork, raise issues, or submit PRs.


👨‍💻 Author

Vijay Kumar Backend Engineer | AI | Java | System Design


About

Spring AI Chat API

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages