Skip to content

Comprehensive Spring AI guide covering LLM integration, ChatClient implementation, embeddings, PgVector, RAG systems, MCP, memory advisors, and tool calling with OpenAI/Ollama models. Complete with 15 demos.

Notifications You must be signed in to change notification settings

ARONAGENT/Spring-AI-Orchestrator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Spring AI Comprehensive Guide

License Spring Boot Spring AI Java Docker OpenAI PgVector Visitors


"The future of AI is not just about making machines smarter, but about making them more accessible and integrated into our everyday applications." β€” Unknown

πŸ“– Overview

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.


✨ Features

πŸ€– 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


πŸ› οΈ Technologies

Tech Stack

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

πŸ“Έ Screenshots

1. ChatClient Successfully Implemented - Basic ChatClient integration with Spring AI framework

1 ChatClient successfully implemented

2. Fast Response with OpenAI - Performance demonstration with OpenAI models showing rapid response times

2 Fast Response when using model Like Open Ai

3. PromptTemplate Variables -Dynamic variable injection into prompts for flexible prompt engineering

3 PromptTemplate - we can pass variables to prompt

4. Vector Store Embedding - Data storage in embedded format using PgVector database

4 Data Store in Vector Store in Embedded Format

5. Similarity Search - Semantic search from vector store based on input text queries

5 Similarity Search from Vector Store from Input Text

6. RAG Implementation - Custom vector store search integration for Retrieval-Augmented Generation

6 Implement Rag and search for own vector Store

7. VectorStoreChatMemoryAdvisor - Long-term conversation memory persistence using vector storage

7 VectorStoreChatMemoryAdvisor that Stores Long Term Data

8. VectorMemory Response - LLM answers using GPT-4o-mini model with vector memory context

8 Answer From VectorStoreChatMemoryAdvisor via LLM chatgpt 4o mini model

9. MessageChatMemoryAdvisor - Short-term session memory implementation for active conversations

9 MessageChatMemoryAdvisor that stores Short Term Memory

10. Hybrid Memory Response - Intelligent fallback between memory layers (MessageChatMemory β†’ VectorStore)

10 Response From Model Answers Varies which one is take if not in MessageChatMemory then take from VectorStore

11. Chat Memory in Database - Persistent conversation storage and lookup in database

11 Chat Memory Stored in DB for Lookup

12. QuestionAnswer Advisor - Specialized advisor optimized for RAG-based Q&A systems

12 QuestionAnswerAdvisor Best for RAG system

13. SafeGuard Advisor - Policy enforcement and governance to prevent irrelevant conversations

12 SafeGuardAdvisor for Policies and Governance for prevent disrelevant chats

14. Custom Token Usage Advisor - Token tracking and usage monitoring for cost optimization

13 Custom TokenUsage Advisor

15. Tool Calling with OpenWeatherMap - External API integration using function calling capabilities

14 Tool Calling with Open Weather Map

πŸš€ Installation

Prerequisites

  • Java 17 or higher
  • Docker and Docker Compose
  • Maven 3.8+
  • OpenAI API Key (or Ollama for local models)
  • Git

Steps

  1. Clone the repository

    git clone https://github.com/ARONAGENT/Spring_AI.git
    cd Spring_AI
  2. Set up PgVector with Docker

    docker-compose up -d
  3. Configure environment variables

    Create an application.properties or application.yml file:

    spring.ai.openai.api-key=${OPENAI_API_KEY}
    spring.datasource.url=jdbc:postgresql://localhost:5432/vectordb
    spring.datasource.username=postgres
    spring.datasource.password=postgres
  4. Build the project

    mvn clean install
  5. Run the application

    mvn spring-boot:run
  6. Access the application

    The application will be available at http://localhost:8080


πŸ’‘ Usage

Basic ChatClient Example

@Autowired
private ChatClient chatClient;

public String chat(String userMessage) {
    return chatClient.prompt()
            .user(userMessage)
            .call()
            .content();
}

PromptTemplate with Variables

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

Vector Store and RAG

// 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)
);

Tool Calling

@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.


🀝 Contributing

Contributions are welcome! However, since this is a proprietary educational project, please follow these guidelines:

  1. Fork the repository for personal learning and experimentation
  2. Open an issue to discuss proposed changes before submitting
  3. Submit pull requests with detailed descriptions of your improvements
  4. Follow the code style and conventions used in the project
  5. Add tests for new features when applicable

Please note that all contributions will be subject to the project's proprietary license.


πŸ“„ 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


πŸ™ Acknowledgments

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.


πŸ“¬ Contact

For questions, feedback, or permissions, please reach out:


⭐ Show Your Support

🌟 Star this repo if you find it helpful! ⭐ GitHub Stars

Your support motivates continued development and helps others discover this resource.


Built with ❀️ by ARONAGENT

Making AI accessible, one Spring application at a time.

About

Comprehensive Spring AI guide covering LLM integration, ChatClient implementation, embeddings, PgVector, RAG systems, MCP, memory advisors, and tool calling with OpenAI/Ollama models. Complete with 15 demos.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages