In 2023, AI technology has become a hot topic and has had a huge impact on various industries, especially in the programming field. People are increasingly aware of the importance of AI technology, and the Spring community is no exception.
With the continuous advancement of GenAI (General Artificial Intelligence) technology, it has become crucial and urgent to simplify the creation of applications with AI functions. Against this background, "Spring AI" emerged, aiming to simplify the process of developing AI functional applications, making it simple and intuitive and avoiding unnecessary complexity. Through "Spring AI", developers can more easily build applications with AI functions, making them easier to use and operate. This not only helps improve development efficiency, but also accelerates the popularization and application of AI technology. In short, "Spring AI" brings new possibilities to the development of AI applications, providing developers with simpler and more intuitive tools and frameworks.
This article will briefly introduce the Spring AI framework and some engineering tips for using the framework. Developers can use these tips to better structure prompt information and fully utilize the capabilities of Spring AI.
1 Introduction to Spring AI
Spring AI is created and written by M K Pavan Kumar
Spring AI is a tool designed to simplify AI applications Developed project inspired by the Python projects LangChain and LlamaIndex. However, Spring AI is not a simple copy. Its core idea is to open generative AI applications to users of various programming languages, not just Python language enthusiasts. This means developers can build AI applications using a language they are familiar with without having to learn the Python language. With Spring AI, developers can more easily harness the power of AI to solve a variety of problems, regardless of which programming language they use. This will facilitate broader AI application development and provide developers with more flexibility and choice.
The core goal of Spring AI is to provide the basic building blocks for building AI-driven applications. These building blocks are highly flexible and components can be easily swapped with virtually no modifications to the code. One example is that Spring AI introduces a component called the ChatClient interface, which is compatible with OpenAI and Azure OpenAI technologies. This allows developers to switch between different AI service providers without changing the code, making development and integration more convenient.
At its core, Spring AI provides reliable building blocks for developing artificial intelligence-based applications. The elasticity of these modules enables smooth swapping of components without requiring extensive modifications to the coding. One example is Spring AI's introduction of the ChatClient interface, which is compatible with OpenAI and Azure OpenAI, allowing developers to easily talk to both platforms. This compatibility allows developers to choose the appropriate platform based on actual needs without having to rewrite code. With Spring AI, developers can build AI-driven applications more efficiently.
Spring AI goes beyond basic building blocks and focuses on providing more advanced solutions. For example, it can support typical scenarios such as "questions and answers about one's own documents" or "interactive chat using documents". As application needs grow, Spring AI plans to work closely with other components of the Spring ecosystem such as Spring Integration, Spring Batch and Spring Data to meet more complex business needs.
2 Create a Spring Boot project and write an OpenAI controller example
First generate the Spring Boot project in the IDE and keep the following content in the application.properties file:
spring.ai.openai.api-key=<your></your>
Below Write a controller named OpenAIController.java:
package com.vas.springai.controller;import org.springframework.ai.client.AiClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/api/v1")public class OpenAIController {private final AiClient aiClient;public OpenAIController(AiClient aiClient) {this.aiClient = aiClient;}}
3 Use the Prompt class to build prompt information
The prompt class is a structured holder of a sequence of message objects, each message represents a prompt a part of. These messages have different roles and purposes in the prompt, and their content varies. Includes user questions, AI-generated responses, relevant contextual details, and more. This setup facilitates complex and sophisticated human-computer interactions since the prompt consists of multiple messages with specific functions.
@GetMapping("/completion")public String completion(@RequestParam(value = "message") String message){return this.aiClient.generate(message);}
However, aiClient's generate method does not only accept plain text as a parameter, it can also accept objects of the Prompt class as parameters, as shown below. Now, this method returns an instance of type AiResponse, not simple text.
@GetMapping("/completion")public AiResponse completion(@RequestParam(value = "message") String message){ PromptTemplate promptTemplate = new PromptTemplate("translate the given english sentence sentence into french {query}"); Prompt prompt = promptTemplate.create(Map.of("query", message)); return this.aiClient.generate(prompt);}
In addition, the Prompt class also provides an overloaded constructor that can accept a sequence of Message type instances with different roles and intentions as parameters. This can better organize and manage prompt information and facilitate subsequent processing and use. Below is a sample code showing how to use this overloaded constructor to merge everything.
package com.vas.springai.controller;import org.springframework.ai.client.AiClient;import org.springframework.ai.client.Generation;import org.springframework.ai.prompt.Prompt;import org.springframework.ai.prompt.PromptTemplate;import org.springframework.ai.prompt.SystemPromptTemplate;import org.springframework.ai.prompt.messages.Message;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.Map;@RestController@RequestMapping("/api/v1")public class OpenAIController {private final AiClient aiClient;public OpenAIController(AiClient aiClient) {this.aiClient = aiClient;}@GetMapping("/completion")public List<generation> completion(@RequestParam(value = "message") String message) {String systemPrompt = """You are a helpful AI assistant that helps people translate given text from english to french.Your name is TranslateProYou should reply to the user's request with your name and also in the style of a professional.""";SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemPrompt);Message systemMessage = systemPromptTemplate.createMessage();PromptTemplate promptTemplate = new PromptTemplate("translate the given english sentence sentence into french {query}");Message userMessage = promptTemplate.createMessage(Map.of("query", message));Prompt prompt = new Prompt(List.of(systemMessage, userMessage));return this.aiClient.generate(prompt).getGenerations();}}</generation>
4 Testing the application
You can use any open tool available on the market to test the application, such as postman, insomnia, Httpie, etc.
picture
The above is the detailed content of A new programming paradigm, when Spring Boot meets OpenAI. For more information, please follow other related articles on the PHP Chinese website!

Harnessing the Power of Data Visualization with Microsoft Power BI Charts In today's data-driven world, effectively communicating complex information to non-technical audiences is crucial. Data visualization bridges this gap, transforming raw data i

Expert Systems: A Deep Dive into AI's Decision-Making Power Imagine having access to expert advice on anything, from medical diagnoses to financial planning. That's the power of expert systems in artificial intelligence. These systems mimic the pro

First of all, it’s apparent that this is happening quickly. Various companies are talking about the proportions of their code that are currently written by AI, and these are increasing at a rapid clip. There’s a lot of job displacement already around

The film industry, alongside all creative sectors, from digital marketing to social media, stands at a technological crossroad. As artificial intelligence begins to reshape every aspect of visual storytelling and change the landscape of entertainment

ISRO's Free AI/ML Online Course: A Gateway to Geospatial Technology Innovation The Indian Space Research Organisation (ISRO), through its Indian Institute of Remote Sensing (IIRS), is offering a fantastic opportunity for students and professionals to

Local Search Algorithms: A Comprehensive Guide Planning a large-scale event requires efficient workload distribution. When traditional approaches fail, local search algorithms offer a powerful solution. This article explores hill climbing and simul

The release includes three distinct models, GPT-4.1, GPT-4.1 mini and GPT-4.1 nano, signaling a move toward task-specific optimizations within the large language model landscape. These models are not immediately replacing user-facing interfaces like

Chip giant Nvidia said on Monday it will start manufacturing AI supercomputers— machines that can process copious amounts of data and run complex algorithms— entirely within the U.S. for the first time. The announcement comes after President Trump si


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)