Empowering Java-spring Developers in Building AI Applications
Objective:
The world of artificial intelligence (AI) is rapidly evolving, and Java developers no longer need to feel left out.
This blog focuses on showcasing that to develop AI applications or adding AI capabilities in your existing application ,one need not to learn Python conventionally but today java developers with spring framework can develop AI applications .Here we will talk about frameworks, modules and knowledge they need to seamlessly integrate AI capabilities into their applications.
We’ll cover:
- Spring AI — Overview? — An introduction to the Spring AI project and its role in simplifying AI integration for Java developers.
- Why Use Spring AI ?
- Getting Started
- Tech Stack
- Lets see this in Action — A hands-on guide to setting up Spring AI in your Java projects.
By the end of this blog, you’ll have the confidence and skills to leverage Spring AI and bring AI-powered solutions to life in your Java applications. Let’s bridge the gap between Java development and AI innovation!
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Spring AI — Overview: As raw as it can be . From the official spring ai site “Spring AI is an application framework for AI engineering. Its goal is to apply to the AI domain Spring ecosystem design principles such as portability and modular design and promote using POJOs as the building blocks of an application to the AI domain. At its core, Spring AI addresses the fundamental challenge of AI integration: Connecting your enterprise Data and APIs with the AI Models. ”
The beauty of spring boot projects is that they provide abstraction and let developers declare what they need and start using it. Spring framework in backend resolves all dependencies and makes sure the required objects are available before use.
Spring AI was no exception . Though it is still evolving, its goal is to make AI development as simple as adding a dependency to your Spring Boot project. With its growing community and documentation, it’s becoming easier than ever for Java developers to dive into the world of AI.
Why Use Spring AI?
Inclusiveness: Support for all major AI Model providers such as Anthropic, OpenAI, Microsoft, Amazon, Google, and Ollama.
2. Familiarity : To enable your app with Ai capabilities , you need not to learn other language. Spring AI feels like a natural extension of your existing spring toolkit.
3. Structured Output : Availability of out of box structured output functionality reduces error probability while presenting output data generally received as strings.
4. Observability — Provides insights into AI-related operations.
5. Productivity: By abstracting away the complexities of AI integration, Spring AI allows you to focus on building features rather than configuring models.
Getting Started:
To get started one need to understand below pointers.
1. The tech stack to use
2 The dependencies to declare .
3. Set up the project skeleton .
4. Generate model access api key
5. Code & Configurations
Tech stack:
In this experiment below techs were being used.
1. Java 17
2. Spring boot 3.4.2
3. VS code (Any IDE of your choice)
4. openAi api (The model `gpt-4o`)
5. Build tool- maven
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Necessary Dependencies to declare:
To develop an OpenAI-based Spring AI application, you’ll need to include specific dependencies in your project. Below are the details.
1. Spring-boot-starter : This deps helps us to bootstrap our project with spring framework support . As spring Ai has been built on top of Spring boot framework thus this is the mandatory dependency for the project.
2. Spring AI Dependency: Spring AI provides modules for integrating with OpenAI. You’ll need to include the spring-ai-openai-spring-boot-starter dependency to access OpenAI-specific functionalities.
3. Spring boot starter Web : To showcase the model access via Rest controller this dependency is required . If one has another way to invoke api e.g. postman , api client then this can be avoided. We have used it to showcase end to end workflow and how to consume open ai model api in web apps.
4. OpenAi key configuration : To access models the openAI need to authenticate you and for that the request must carry a key . As a spring boot developer we need to pass the key in application.properties file as key value pair
E.g. spring.ai.openai.api-key=${YOUR_OPEN_AI_KEY}
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Generate model access api key
To generate the key , one need to access https://platform.openai.com/
1. Click on “Sign Up” if you don’t already have an account, or “Log In” if you do.
2. If you’re new to OpenAI, you may need to verify your email address and complete your profile.
3. OpenAI API usage is not free. Ensure you have credits available , if not set up billing information.
4. Create organization then project inside organization.
5. Go to the OpenAI API Keys page. Then click “Create new secret key”
6. Copy that key in some private location as this wont be visible again in their site.
===========================================================
Lets see this in Action —
A hands-on guide to setting up Spring AI in your Java projects.
1. Set Up Project: This part is known to all , as to start a spring boot project easiest process is Visit spring.io and create a spring boot project with above dependencies and generate the project . Import the same into your IDE. General project skeleton will be something similar to below snapshot.
2. Create three packages of name configuration , controller and service at the same level where the main java file is containing the main method .
3. Create openAI API client: To access OpenAi gpt models API the spring AI provides one public interface ChatClient whose bean is to be created as a mandate configuration part as that bean will interact with APIs and bring results.
Inside configuration package created a java class and annotated with @configuration for spring boot to automatically process and created a bean of ChatClient using builder. It’s mandatory as simple autowired wont work and one needs to use builder to create the bean first.
4. Create Controller: Inside controller package created a java class annotated with @RestController , referring service class bean with @Autowired annotation and create some rest handler with relevant @RequestMapping. So that we can invoke those url from browser , Postman etc.
5. Create Service : Inside service package created a java class with @Service annotation and declare final variable of chatClient and using constructor of service class populating the variable from the bean we created using @Configuration annotation , refer section 2.
As this blog is to showcase the easiness the spring AI brought in accessing AI models thus primary focus is not in business logic but use simple code. In Service class , created a chat method which uses chatCleint object and prompt the message user gives and passed here from controller.
6. Boot the web app : When all above is set the using maven run we can start spring boot web app.
7. Testing: By default spring boot web app works on port 8080 so if no customization has been done ,then from the browser we can access our rest controller endpoint on the same 8080 ports and pass our conversation message as part of the url which will be used as a prompt for the openAI API .
Sample request / response is in the snapshot below.
Conclusion:
Unless you live under a rock , you must know that AI development has emerged as the poster boy of today’s IT and all major enterprises are investing in it . Conventional idea is that all AI development requires knowledge of Python . Spring AI delivers the abstraction in Java language and now Java programmers can easily add AI capabilities in their application. Abstractions provided by Spring AI are ChatClient , ImageCleint , SpeechClient etc are curated for self explanatory purposes and its out of box tools like OutputParsers and PromptTemplate etc enable developers to add AI capabilities very easily in their spring boot apps.
We have seen above that using so little code we consume the gpt4o model and start to chat with the AI systems and all complexities are managed by spring AI module.
Reference & Credits 🎉