By Sagar Anchal
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.
<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.
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!