A comprehensive Spring Boot template project for building AI-powered applications using Spring AI 1.0. This template provides a robust foundation for developing enterprise-grade applications that leverage artificial intelligence capabilities including chat interactions, vector storage, document processing, and persistent chat memory.
- Multi-modal Integrations - Chat completions and embeddings using OpenAI, Anthropic, and Amazon Bedrock with more to come later.
- Vector Storage - PostgreSQL with PGVector extension for semantic search and RAG applications
- Chat Memory - JDBC-based persistent chat memory for conversation continuity
- Document Processing - PDF document reader for knowledge extraction
- Vector Store Advisors - Enhanced AI responses with context-aware recommendations
- API Documentation - Interactive Swagger UI for API exploration
- Health Monitoring - Spring Boot Actuator endpoints for application monitoring
- Development Tools - Hot reload with Spring Boot DevTools
- Spring Boot 3.4.0 - Main application framework
- Spring AI 1.0.0 - AI integration and orchestration
- Java 17 - Programming language
- OpenAI GPT-4o-mini - Chat completions
- OpenAI text-embedding-3-small - Text embeddings
- Spring AI Chat Client - Simplified chat interactions
- Spring AI Vector Store - Vector similarity search
- PostgreSQL - Primary database
- PGVector Extension - Vector similarity search
- Spring Data JPA - Data access layer
- Hibernate - ORM framework
- SpringDoc OpenAPI 3 - API documentation
- Swagger UI - Interactive API explorer
- Spring Boot Actuator - Application monitoring
- Lombok - Boilerplate code reduction
- Spring Boot DevTools - Development productivity
- Maven - Build and dependency management
spring-ai-template/
βββ src/
β βββ main/
β β βββ java/com/sbansal/ai/template/
β β β βββ SpringAiTemplateApplication.java # Main application class
β β β βββ config/
β β β β βββ SpringAiTemplateConfig.java # Configuration beans
β β β βββ web/
β β β βββ ChatController.java # REST API endpoints
β β βββ resources/
β β βββ application.properties # Application configuration
β βββ test/
β βββ java/com/sbansal/ai/template/
β βββ SpringAiTemplateApplicationTests.java
βββ pom.xml # Maven dependencies
βββ README.md
- Java 17 or higher
- Maven 3.6+
- PostgreSQL 12+ with PGVector extension
- OpenAI API Key
git clone <repository-url>
cd spring-ai-template# Run PostgreSQL with PGVector extension
docker run --name postgres-ai \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=saranshbansal \
-p 5432:5432 \
-d pgvector/pgvector:pg16# Install PostgreSQL and PGVector (macOS with Homebrew)
brew install postgresql@15 pgvector
brew services start postgresql@15
# Create database and enable vector extension
createdb saranshbansal
psql saranshbansal -c "CREATE EXTENSION IF NOT EXISTS vector;"-- Connect to your database and run:
CREATE TABLE IF NOT EXISTS vector_store (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
content TEXT,
metadata JSONB,
embedding vector(1536)
);
-- Create index for better performance
CREATE INDEX IF NOT EXISTS vector_store_embedding_idx
ON vector_store USING hnsw (embedding vector_cosine_ops);# Set required environment variables
export OPEN_API_KEY=your_openai_api_key_here
export DB_USERNAME=postgres
export DB_PASSWORD=password# Using Maven
./mvnw spring-boot:run
# Or build and run JAR
./mvnw clean package
java -jar target/spring-ai-template-0.0.1-SNAPSHOT.jarKey configuration options in application.properties:
# OpenAI Configuration
spring.ai.openai.api-key=${OPEN_API_KEY}
spring.ai.openai.chat.options.model=gpt-4o-mini
spring.ai.openai.embedding.options.model=text-embedding-3-small
# Database Configuration
spring.datasource.url=jdbc:postgresql://localhost:5432/saranshbansal
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
# Vector Store Configuration
spring.ai.vectorstore.pgvector.index-type=HNSW
spring.ai.vectorstore.pgvector.distance-type=COSINE_DISTANCE
spring.ai.vectorstore.pgvector.dimensions=1536
# Chat Memory Configuration
spring.ai.chat.memory.enabled=true
spring.ai.chat.memory.repository.type=jdbc- GET
/chat/ask?message={your_message}- Send a message to the AI chat
# Basic chat interaction
curl "http://localhost:8080/chat/ask?message=Hello, how are you?"
# Default joke request (no message parameter)
curl "http://localhost:8080/chat/ask"- GET
/actuator/health- Application health status - GET
/actuator/info- Application information - GET
/actuator/metrics- Application metrics
- Swagger UI:
http://localhost:8080/swagger-ui.html - OpenAPI JSON:
http://localhost:8080/api-docs
- REST endpoint for AI chat interactions
- Uses Spring AI ChatClient for OpenAI integration
- Supports parameterized queries with default fallback
- Spring Boot
- PostgreSQL
- Spring AI
- Other technologies...
- Clone the repository
- Configure database properties in
application.properties - Set up environment variables:
- DB_USERNAME
- DB_PASSWORD
- OPENAI_API_KEY
- Run the application
API documentation is available at /swagger-ui.html when the application is running.