Add Generative AI in Your Spring Boot Application - Spring AI - by Zeeshan Adil - Nov, 2023 - Level Up Coding
Add Generative AI in Your Spring Boot Application - Spring AI - by Zeeshan Adil - Nov, 2023 - Level Up Coding
10:37 Add Generative AI in your Spring Boot Application -Spring AI | by Zeeshan Adil | Nov, 2023 | Level Up Coding
In this tutorial, we’ll be putting AI to work in some fascinating scenarios using Spring
Boot.
Think of Spring AI as a toolkit for making AI applications. It gives you basic building
blocks that can be easily swapped out with different options. This flexibility lets you tweak
and customize your AI program without having to do a lot of complicated reprogramming.
Apart from the basic building blocks, Spring AI wants to offer extra features to handle
everyday situations like “asking questions about your documents” or “having a
conversation with your documents.” As the tasks become more complicated, Spring AI will
work together with other projects in the Spring family, like Spring Integration, Spring
Batch, and Spring Data.
Spring-AI is extremely fresh and currently lacks a stable version. However, we anticipate
the release of a stable version in the future.
Spring AI introduces the AiClient interface with implementations for OpenAI and Azure
OpenAI.
To know more about OpenAI action in spring boot. Check out my below article:
https://levelup.gitconnected.com/add-generative-ai-in-your-spring-boot-application-spring-ai-12561b1adf08 1/9
2023. 12. 18. 10:37 Add Generative AI in your Spring Boot Application -Spring AI | by Zeeshan Adil | Nov, 2023 | Level Up Coding
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<dependency>
<groupId>org.springframework.experimental.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>0.2.0-SNAPSHOT</version>
</dependency>
To employ Azure OpenAI instead of OpenAI, you can include the following dependency:
<dependency>
<groupId>org.springframework.experimental.ai</groupId>
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
<version>0.2.0-SNAPSHOT</version>
</dependency>
https://levelup.gitconnected.com/add-generative-ai-in-your-spring-boot-application-spring-ai-12561b1adf08 2/9
2023. 12. 18. 10:37 Add Generative AI in your Spring Boot Application -Spring AI | by Zeeshan Adil | Nov, 2023 | Level Up Coding
Add API Key and OpenAI Image Generator URL in application. properties file:
spring.ai.openai.apikey=REPLACE_WITH_YOUR_API_KEY
spring.ai.openai.imageUrl=https://api.openai.com/v1/images/generations
Project Structure :
https://levelup.gitconnected.com/add-generative-ai-in-your-spring-boot-application-spring-ai-12561b1adf08 3/9
2023. 12. 18. 10:37 Add Generative AI in your Spring Boot Application -Spring AI | by Zeeshan Adil | Nov, 2023 | Level Up Coding
@Service
public class SpringAIService {
@Autowired
AiClient aiClient;
@Value("{spring.ai.openai.apikey}")
private String apiKey;
@Value("{spring.ai.openai.imageUrl}")
private String openAIImageUrl;
https://levelup.gitconnected.com/add-generative-ai-in-your-spring-boot-application-spring-ai-12561b1adf08 4/9
2023. 12. 18. 10:37 Add Generative AI in your Spring Boot Application -Spring AI | by Zeeshan Adil | Nov, 2023 | Level Up Coding
public String getJoke(String topic){
PromptTemplate promptTemplate = new PromptTemplate("""
Crafting a compilation of programming jokes for my website. Would you like me to create a joke about {t
""");
promptTemplate.add("topic", topic);
return this.aiClient.generate(promptTemplate.create()).getGeneration().getText();
}
1. Autowired Dependencies:
The class uses the @Autowired annotation to inject an instance of the AiClient class.
2. getJoke Method:
The {topic} placeholder in the template is replaced with the actual topic parameter.
The AI client generates a response based on this prompt, and the text of the response
is returned.
3. getBestBook Method:
It follows a similar pattern, using a template to request information about the best
book in a certain category and year.
The placeholders {category} and {year} are replaced with the provided parameters.
The AI client generates a response, and the text of the response is returned.
4. getImage Method:
It uses a prompt template to form a request for image creation, replacing {topic} with
the actual topic.
The generated prompt is sent to an AI service using AiClient , and the resulting image
URL is obtained.
The image is then fetched from the URL and converted into an InputStreamResource for
further processing.
https://levelup.gitconnected.com/add-generative-ai-in-your-spring-boot-application-spring-ai-12561b1adf08 6/9
2023. 12. 18. 10:37 Add Generative AI in your Spring Boot Application -Spring AI | by Zeeshan Adil | Nov, 2023 | Level Up Coding
public void setUrl(String url) {
this.url = url;
}
}
}
@RestController
@RequestMapping("/api/v1")
public class SpringAIController {
@Autowired
SpringAIService aiService;
@GetMapping("/joke")
public String getJoke(@RequestParam String topic) {
return aiService.getJoke(topic);
}
@GetMapping("/book")
public String getBook(@RequestParam(name = "category") String category, @RequestParam(name = "year") String year) {
return aiService.getBestBook(category, year);
}
1. /api/v1/joke : It will give joke based on your topic provided. (Spring AI in action)
https://levelup.gitconnected.com/add-generative-ai-in-your-spring-boot-application-spring-ai-12561b1adf08 7/9
2023. 12. 18. 10:37 Add Generative AI in your Spring Boot Application -Spring AI | by Zeeshan Adil | Nov, 2023 | Level Up Coding
2. /api/v1/book : It will give you a book details based on category and year provided by you
(Spring Ai in Action)
😊
3. /api/v1/image : It will generate an image for you what you have asked from AI. (Spring
AI in Action Again … )
https://levelup.gitconnected.com/add-generative-ai-in-your-spring-boot-application-spring-ai-12561b1adf08 8/9
2023. 12. 18. 10:37 Add Generative AI in your Spring Boot Application -Spring AI | by Zeeshan Adil | Nov, 2023 | Level Up Coding
👏
Spring AI is awesome. If you find this article informative and beneficial, please consider
showing your appreciation by giving it a clap . Feel free to share this article with your
peers. Your support and knowledge sharing within the developer community are highly
valued.
GitHub - zees007/spring-ai-in-action
Contribute to zees007/spring-ai-in-action development by creating an
account on GitHub.
github.com
https://levelup.gitconnected.com/add-generative-ai-in-your-spring-boot-application-spring-ai-12561b1adf08 9/9