This project demonstrates the integration of AI capabilities within a Spring Boot application, utilizing the Spring AI framework.
- Getting started with Spring AI Chat Model and easily switch between different AI providers including OpenAI, Mistral AI and Ollama. The example is available in the branch master. A detailed guide may be found in the following article: Getting Started with Spring AI and Chat Model
- Getting started with Spring AI Function Calling for OpenAI chat models. The example is available in the branch master. A detailed guide may be found in the following article: Getting Started with Spring AI Function Calling
- Using RAG (Retrieval Augmented Generation) and Vector Store with Spring AI. The example is available in the branch master. A detailed guide may be found in the following article: Using RAG and Vector Store with Spring AI
- Using Multimodality feature and Image Model with Spring AI and OpenAI. The example is available in the branch master. A detailed guide may be found in the following article: Spring AI with Multimodality and Images
- Running multiple models with Ollama and integration through Spring AI. The example is available in the branch master. A detailed guide may be found in the following article: Using Ollama with Spring AI
- Getting started with Spring AI Tool Calling for OpenAI/MistralAI chat models. The example is available in the branch master. A detailed guide may be found in the following article: Tool Calling with Spring AI
- Integrate Spring AI with Azure OpenAI for chat models, image generation, tool calling and RAG. The example is available in the branch master. A detailed guide may be found in the following article: Spring AI with Azure OpenAI
Currently, there are four @RestController
s that show Spring AI features:
pl.piomin.services.controller.PersonController
- prompt template, chat memory, and structured output based on a simple example that asks AI model to generate some persons
pl.piomin.services.controller.WalletController
- function calling that calculates a value of our wallet stored in local database in conjunction with the latest stock prices
pl.piomin.services.controller.StockController
- RAG with a Pinecone vector store and OpenAI based on stock prices API
pl.piomin.services.controller.ImageController
- image model and multimodality
The architecture is designed to be modular and scalable, focusing on demonstrating how AI features can be incorporated into Spring-based applications.
Follow these steps to run the application locally.
git clone https://github.com/piomin/spring-ai-showcase.git
cd spring-ai-showcase
By default, this sample Spring AI app connects to OpenAI. So, before running the app you must set a token:
export OPEN_AI_TOKEN=<YOUR_API_TOKEN>
mvn spring-boot:run
To enable integration with Mistral, we should activate the mistral-ai
profile:
export MISTRAL_AI_TOKEN=<YOUR_API_TOKEN>
mvn spring-boot:run -Pmistral-ai
To enable integration with Ollama, we should activate the ollama-ai
profile:
mvn spring-boot:run -Pollama-ai
Before that, we must run the model on Ollama, e.g.:
ollama run llava
To enable integration with Azure OpenAI, we should activate the azure-ai
profile and activate the Spring Boot azure-ai
profile:
mvn spring-boot:run -Pazure-ai -Dspring-boot.run.profiles=azure-ai
You should also export the Azure OpenAI credentials:
export AZURE_OPENAI_API_KEY=<YOUR_AZURE_OPENAI_API_KEY>
For scenarios with a vector store (StockController
, ImageController
) you need to export the following ENV:
export PINECONE_TOKEN=<YOUR_PINECONE_TOKEN>
For scenarios with a stock API (StockController
, WalletController
) you need to export the following ENV:
export STOCK_API_KEY=<YOUR_STOCK_API_KEY>
More details in the articles.
The application exposes several REST API endpoints organized by functionality. Below is a comprehensive list of all available endpoints:
Demonstrates prompt templates, chat memory, and structured output generation.
Method | Endpoint | Description | Response Type |
---|---|---|---|
GET | /persons |
Generate or return a list of 10 persons with random values | List<Person> |
GET | /persons/{id} |
Find and return person by ID from current list | Person |
Demonstrates function calling with stock price calculations and wallet value analysis.
Method | Endpoint | Description | Response Type |
---|---|---|---|
GET | /wallet/with-tools |
Calculate current wallet value using latest stock prices with AI tools | String |
GET | /wallet/highest-day/{days} |
Find the day with highest wallet value in the last N days | String |
Demonstrates RAG (Retrieval Augmented Generation) with Pinecone vector store for stock market analysis.
Method | Endpoint | Description | Response Type |
---|---|---|---|
GET | /stocks/load-data |
Load stock data for major companies (AAPL, MSFT, GOOG, AMZN, META, NVDA) into vector store | void |
GET | /stocks/docs |
Query vector store documents for growth trends | List<Document> |
GET/POST | /stocks/v1/most-growth-trend |
Find stock with most percentage growth (version 1) | String |
GET/POST | /stocks/v1-1/most-growth-trend |
Find stock with most percentage growth with enhanced search (version 1.1) | String |
GET/POST | /stocks/v2/most-growth-trend |
Find stock with most percentage growth using advanced RAG (version 2) | String |
Demonstrates image model capabilities, multimodality, and image generation/analysis.
Method | Endpoint | Description | Response Type |
---|---|---|---|
GET | /images/find/{object} |
Find and return image containing the specified object | byte[] (PNG) |
GET | /images/generate/{object} |
Generate a new image containing the specified object | byte[] (PNG) |
GET | /images/describe |
Describe all images (static + dynamically generated) | String[] |
GET | /images/describe/{image} |
Describe items in a specific image and categorize them | List<Item> |
GET | /images/load |
Load image descriptions into vector store for similarity search | void |
GET | /images/generate-and-match/{object} |
Generate image with object and find similar images in vector store | List<Document> |
Demonstrates dynamic API generation with chat memory for any entity type.
Method | Endpoint | Description | Response Type |
---|---|---|---|
GET/POST | /api/{entity} |
Generate a dynamic list of any entity type with random values | String |
GET/POST | /api/{entity}/{id} |
Find and return specific entity by ID from current list | String |
# Generate a list of persons
curl http://localhost:8080/persons
# Calculate wallet value with AI tools
curl http://localhost:8080/wallet/with-tools
# Load stock data into vector store
curl http://localhost:8080/stocks/load-data
# Find stock with most growth
curl http://localhost:8080/stocks/v2/most-growth-trend
# Generate an image with cats
curl http://localhost:8080/images/generate/cats --output cats.png
# Generate dynamic list of cars
curl http://localhost:8080/api/cars
- OpenAI/Mistral/Ollama: Required for all chat-based endpoints
- Stock API Key: Required for
/wallet/*
and/stocks/*
endpoints - Pinecone Token: Required for vector store operations (
/stocks/*
,/images/load
,/images/generate-and-match/*
) - Image Model: Required for
/images/generate/*
and/images/generate-and-match/*
endpoints