Skip to main content

For the past few years, Artificial Intelligence (AI) has been the hottest topic in our lives. It has permeated every aspect of our digital world, from coding to art, articles, presentations, and more. Many of us have dreamed about creating our own applications with pre-trained AI models, whether it be for a chat game with anime girls or generating images with the help of Dall-E. Fortunately, with the newly released Spring AI, these dreams can now become a reality.

To begin using Spring AI, you'll need to create a simple Spring Web project. There's no need for any extra dependencies for this tutorial. You can easily create a Spring project using IntelliJ Idea or any other integrated development environment (IDE) of your choice.

To integrate Spring AI into your project, you'll need to add the following dependency manually to your POM.XML:

<dependency>
    <groupId>org.springframework.experimental.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
    <version>0.2.0-SNAPSHOT</version>
</dependency>

Additionally, you'll need to add a different repository to download this dependency by adding the following code to your POM.XML:

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>

Before diving into the code, there's one important step you need to take. Spring AI currently supports two APIs: OpenAI and Azure OpenAI. For this article, we'll be using OpenAI. To provide Spring with the necessary API token, you can add the following line to your application.properties file:

spring.ai.openai.api-key= {your-token}
open.api-key = {same token you entered above}

Now let's take a closer look at the getJoke() method and understand how it works. There are several classes that come with Spring AI, and the first one we'll focus on is the AiClient class. This class is responsible for executing your prompt. To create a prompt, you'll need to utilize the Prompt class, which has three constructors. You can pass a String, a Message object (which also comes with the AI package), or a List of Messages as parameters.
Another important class is PromptTemplate, which allows you to use placeholders in your prompts. In the getJoke() method example above, we used {topic} as a placeholder and retrieved its value from the user. You can then use the .add method to replace the placeholder with the desired value. Once you've set up your prompt, generating a message is straightforward. The result is simply amazing! While it's subjective whether the jokes are actually funny, the AI's performance is undeniably impressive.


Now let's explore another method called getBestMovie(), where we expand our AI inquiries. In this method, we provide the AI with a year and genre to generate the best movie suggestion, along with additional information such as a summary. The best part is that the output is in JSON format, allowing you to convert it into your own custom objects easily. In my personal experience, the plots suggested by the AI were consistently awesome, even when I tried it multiple times with different selections due to the integration of IMDB ratings.

While Spring AI primarily relies on text-based engines, we can use the Dall-E API to create images. However, to generate the prompt for image generation, we need the assistance of AI. Luckily, the same API token we used for OpenAI can be used to access Dall-E as well.
To return an image in Spring, we can utilize ResponseEntity<InputStreamResource>. Dall-E will provide you with a URL for the created image. You'll then use RestTemplate to fetch the image and convert it into a byte array. Finally, wrap the byte array in an InputStreamResource and return it.

Imagine a sales team seeking a robust dashboard to monitor their performance and make data-driven decisions. With Spring AI, a web application can be developed to turn this vision into reality.

The application would gather data from various data sources such as CRM systems, sales data, and marketing campaigns. Utilizing Spring AI's data integration capabilities, this information can be seamlessly brought together in a single interface.

AI algorithms can be employed to analyze the data and provide actionable insights. For example, the application can identify sales trends, highlight underperforming products or regions, and suggest strategies for improvement. These insights empower the sales team to take proactive measures to boost performance.

To enhance user experience, the dashboard can be personalized based on the individual user's role and preferences. Each sales team member can have a customized view that displays the most relevant metrics and reports. This level of personalization ensures that users can quickly access the information they need without being overwhelmed by irrelevant data.

Additionally, the application can be designed to provide real-time updates. By leveraging AI algorithms, the dashboard can continuously monitor and analyze sales data, updating metrics and insights in real-time. This live data stream enables the sales team to respond swiftly to changes in the market and make informed decisions on the go.

Spring AI opens up a world of possibilities for developers, allowing them to harness the power of AI within their applications. With its seamless integration and user-friendly methods, Spring AI empowers developers to create innovative AI-driven solutions. The potential applications of AI are vast: from generating jokes and movie recommendations to creating custom images, Spring AI provides the tools necessary to explore and experiment with AI technology. So, embark on this exciting journey and embrace the future with Spring AI!

Integrate People, Process and Technology

Related Posts