Home > Article > Technology peripherals > A new programming paradigm, when Spring Boot meets OpenAI
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.
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.
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\_OPENAI\_API\_KEY>
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;}}
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();}}
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!