"The future of AI is not just about making machines smarter, but about making them more accessible and integrated into our everyday applications." β Unknown
Spring AI Guide is a comprehensive educational project that demystifies the integration of Large Language Models (LLMs) into Spring Boot applications. This repository serves as a complete guide to understanding and implementing AI-powered features using Spring AI framework, covering everything from basic chat implementations to advanced Retrieval-Augmented Generation (RAG) systems, Model Context Protocol (MCP), and intelligent tool calling.
Whether you're a beginner looking to understand how LLMs work internally or an experienced developer wanting to leverage Spring AI's powerful abstractions, this guide provides hands-on examples with real-world implementations using OpenAI, Ollama, and vector databases.
π€ ChatClient Implementation - Seamless integration with LLM providers (OpenAI, Ollama) for conversational AI
β‘ High-Performance Responses - Optimized implementations for fast model responses
π Dynamic Prompt Templates - Variable injection and template-based prompt engineering
ποΈ Vector Store Integration - Embed and store data in PgVector for semantic search
π Similarity Search - Intelligent retrieval based on semantic similarity
π§ RAG Implementation - Complete Retrieval-Augmented Generation system for context-aware responses
πΎ Long-Term Memory - VectorStoreChatMemoryAdvisor for persistent conversation context
π― Short-Term Memory - MessageChatMemoryAdvisor for session-based chat history
π¬ Hybrid Memory Strategy - Intelligent fallback between memory layers
π Database-Backed Chat History - Persistent storage and lookup of conversation data
β QuestionAnswer Advisor - Specialized advisor optimized for RAG-based Q&A systems
π‘οΈ SafeGuard Advisor - Policy enforcement and governance to prevent irrelevant conversations
π Custom Token Usage Tracking - Monitor and optimize LLM token consumption
π§ Tool Calling - External API integration (OpenWeatherMap) with function calling
π³ Dockerized Infrastructure - Easy setup with Docker for PgVector and dependencies
| Technology | Version | Purpose |
|---|---|---|
| Spring Boot | 3.x | Application framework and dependency injection |
| Spring AI | Latest | AI integration framework for LLMs and vector stores |
| Java | 17+ | Primary programming language |
| PgVector | Latest | PostgreSQL extension for vector similarity search |
| Docker | Latest | Containerization for database services |
| OpenAI API | GPT-4o-mini | Cloud-based LLM provider |
| Ollama | Latest | Local LLM runtime |
| PostgreSQL | Latest | Relational database with vector support |
| DBeaver | Latest | Database management and visualization |
| Postman | Latest | API testing and development |
| Lombok | Latest | Java boilerplate code reduction |
2. Fast Response with OpenAI - Performance demonstration with OpenAI models showing rapid response times
3. PromptTemplate Variables -Dynamic variable injection into prompts for flexible prompt engineering
10. Hybrid Memory Response - Intelligent fallback between memory layers (MessageChatMemory β VectorStore)
- Java 17 or higher
- Docker and Docker Compose
- Maven 3.8+
- OpenAI API Key (or Ollama for local models)
- Git
-
Clone the repository
git clone https://github.com/ARONAGENT/Spring_AI.git cd Spring_AI -
Set up PgVector with Docker
docker-compose up -d
-
Configure environment variables
Create an
application.propertiesorapplication.ymlfile:spring.ai.openai.api-key=${OPENAI_API_KEY} spring.datasource.url=jdbc:postgresql://localhost:5432/vectordb spring.datasource.username=postgres spring.datasource.password=postgres
-
Build the project
mvn clean install
-
Run the application
mvn spring-boot:run
-
Access the application
The application will be available at
http://localhost:8080
@Autowired
private ChatClient chatClient;
public String chat(String userMessage) {
return chatClient.prompt()
.user(userMessage)
.call()
.content();
}PromptTemplate promptTemplate = new PromptTemplate(
"Tell me about {topic} in {style} style"
);
Map<String, Object> model = Map.of(
"topic", "Spring AI",
"style", "professional"
);
Prompt prompt = promptTemplate.create(model);// Store documents
vectorStore.add(List.of(
new Document("Spring AI is a framework...", metadata)
));
// Similarity search
List<Document> results = vectorStore.similaritySearch(
SearchRequest.query("What is Spring AI?").withTopK(5)
);@Bean
public FunctionCallback weatherFunction() {
return FunctionCallback.builder()
.function("getCurrentWeather", this::getWeather)
.description("Get current weather for a location")
.build();
}For detailed examples and implementation guides, please refer to the source code and inline documentation.
Contributions are welcome! However, since this is a proprietary educational project, please follow these guidelines:
- Fork the repository for personal learning and experimentation
- Open an issue to discuss proposed changes before submitting
- Submit pull requests with detailed descriptions of your improvements
- Follow the code style and conventions used in the project
- Add tests for new features when applicable
Please note that all contributions will be subject to the project's proprietary license.
Copyright (c) 2024 ROHAN UKE
This project and its source code are the exclusive property of the author.
**Unauthorized copying, modification, distribution, or commercial use is strictly prohibited.**
Limited use is granted for learning, reviewing, and non-commercial demonstration purposes only.
No warranties are provided; use at your own risk.
For permissions beyond this notice, contact: rohanuke1@example.com
This project was made possible thanks to:
- Spring AI Team at Pivotal for creating an excellent framework for AI integration
- OpenAI for providing powerful and accessible LLM APIs
- PgVector maintainers for the PostgreSQL vector similarity search extension
- Ollama team for making local LLM deployment simple and efficient
- The open-source community for continuous innovation and knowledge sharing
- Docker for simplifying development environment setup
- All contributors who have helped improve this educational resource
Special thanks to the broader Java and Spring ecosystem for their invaluable tools and resources.
For questions, feedback, or permissions, please reach out:
- Email: rohanuke1@example.com
- GitHub: @ARONAGENT
- Repository: Spring_AI
π Star this repo if you find it helpful! β
Your support motivates continued development and helps others discover this resource.
Built with β€οΈ by ARONAGENT
Making AI accessible, one Spring application at a time.