This project showcases the use of Spring AI to bring AI features to your Spring application.
Using Spring AI, you can leverage a wide range of AI providers (such as OpenAI, Mistral AI, Google Gemini and many others) along with additional services such as vector databases.
Spring AI provides an abstraction layer atop AI providers API: this way you can focus on your code by using the same API, no matter which AI provider you pick.
Spring AI is to AI providers what Spring Data is to databases: the best abstraction layer to build portable Spring apps.
This repository includes a few demos you can run to learn about Spring AI.
You need Java 21+ to build and run this app.
Use this command to build the app:
./mvnw clean package
Before running this app, make sure you have Docker daemon running, along with Docker Compose.
Use this command to bootstrap a Redis database (which is used by some demos available in this app):
docker compose up
You can run this app using this command:
./mvnw spring-boot:run
The app will automatically bind to the Redis database running as a container.
This application has been tested with 3 AI providers:
- OpenAI with
gpt4o
- Azure OpenAI with
gpt4o
- Mistral AI with
open-mixtral-8x22b
- Ollama (local) running Mistral AI with model
mistral
Please note that those models are not created equal: some features (such as function calling) may not properly work depending on the model you pick.
You may run this app with additional AI providers and/or models, providing you add the right Spring AI dependency provider in the file pom.xml along with the configuration.
You need an OpenAI API key to run this app. Refer to this page to create and get an API key.
Set your API key as an environment variable:
export OPENAI_API_KEY=xxxxxx
Set openai
as the AI provider:
export APP_AI_PROVIDER=openai
You need an Azure OpenAI API key to run this app.
The deployment name
should be gpt-4o
Set your API key as an environment variable:
export AZURE_OPENAI_API_KEY=xxxxxx
Set azure
as the AI provider:
export APP_AI_PROVIDER=azure
You need a Mistral AI API key to run this app. Refer to this page to create and get an API key.
Set your API key as an environment variable:
export MISTRALAI_API_KEY=xxxxxx
Set mistralai
as the AI provider:
export APP_AI_PROVIDER=mistralai
Using Ollama you can run a LLM on your workstation. Follow these instructions to download and install Ollama.
Start Ollama using this command:
ollama serve
Use this command to download the Mistral AI model for Ollama
ollama pull mistral
This app relies on OpenWeatherMap to get live weather information for a given city.
Prior to running this app, you need to set the OPENWEATHERMAP_API_KEY
environment variable.
See this page to create an OpenWeatherMap account for free and get an API key.
Use this command to set the environment variable:
export OPENWEATHERMAP_API_KEY=xxxxx
🎉 You're ready to go!
Basic use
of the Spring AI ChatClient
:
http ":8080/chat/v1?q=How are you today?"
http ":8080/chat/v2?topic=devops"
http ":8080/hello"
Stateful chat conversations (keep track of previous messages):
http ":8080/hello-memory"
Converting chat responses to Java entities:
http ":8080/chat/v3?topic=devops"
Same as above with content negotiation (prioritizing XML output):
http ":8080/chat/v3?topic=devops" Accept:text/xml
Transform a text into a set of Nodes & Relationships:
http ":8080/chat/v4"
Load and index data (this task actually processes input files in background):
http ":8080/movies/init"
Create a new movie using 2 movie titles:
http ":8080/movies?titles=Lord of the Rings,Despicable Me"
Same as above but using genre drama (default is comedy):
http ":8080/movies?titles=Furiosa,Snow White&genre=drama"
Same as above but translating the result into french:
http ":8080/movies?titles=Ghostbusters,Despicable Me&genre=drama&lang=fr"
Clear the database (make sure you restart the app after running this command):
redis-cli flushdb
For this section please enable OpenAI as the code uses DALL-E image model. You may use other AI providers as long as you pick the right image model.
open "http://localhost:8080/imagegen?q=A vibrant photograph of a Java programmer working in his office with the Eiffel tower in background, wide shot, soft focus"
Verify that the AI provider has no way to figure out current weather if no function is registered:
http ":8080/chat/v1?q=Should I bring my sunglasses when I'll get to Paris later today?"
http ":8080/weather/v1?city=Paris"
Call a function and map the result to a Java entity:
http ":8080/weather/v2?city=Paris"
Using functions, verify that the AI provider can now answer weather related questions:
http ":8080/weather/v3?q=Should I bring my sunglasses when I'll get to Paris later today?"
Same as above but using a complex query involving many cities:
http ":8080/weather/v3?q=Sort the top 5 most populated cities in France by temperature."
Combining text, image and functions to answer a question, mapping the result to a JSON structure:
http ":8080/weather/v4?u=https://live.staticflickr.com/65535/54135349734_8503393ea0_h.jpg"
Contributions are always welcome!
Feel free to open issues & send PR.
Copyright © 2024 Broadcom, Inc. or its affiliates.
This project is licensed under the Apache Software License version 2.0.