I came across the Spring framework's AI module while browsing short videos, and as a Java programmer, I had no idea about it. I quickly went to the official website to take a look and find some tutorials to try it out.
The current version of Spring AI is 0.8.1, but there is already a stable version 1.0 SNAPSHOT, so the API is unlikely to undergo major changes. In summary, the current version is usable and there is no need to worry about compatibility.
Initialization#
You can use IntelliJ IDEA to quickly initialize a Spring AI project. However, please note that the minimum required JDK version is >=17 and the Spring Boot version should be >3.
You can also create an initial project with Spring AI using https://start.spring.io/:
As shown in the above image, we have added the most common OpenAI dependency. Spring AI mainly adds the following BOM:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>0.8.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
And the OpenAI dependency we selected:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
Quick Start#
Preparation#
Next, you need to provide the interface address of the model, using OpenAI as an example.
In fact, the base URL can be omitted, and the default is the official address.
If you are using the official API interface, you may also need to add a proxy to the program.
System.setProperty("https.proxyHost", "localhost");
System.setProperty("https.proxyPort", "7890");
Client Object#
If you have already configured the information in the configuration file, you can directly initialize the object.
private final OpenAiChatClient chatClient;
Another method is to create it in the code.
var openAiApi = new OpenAiApi("https://api.openai.com", "sk-xxxxx");
OpenAiChatClient chatClient = new OpenAiChatClient(openAiApi, OpenAiChatOptions.builder()
.withModel("gpt-3.5-turbo-1106")
.withTemperature(0.4F)
.build());
The advantage of using code is that you can customize the object flexibly. withModel("gpt-3.5-turbo-1106")
specifies which model to use, with the default being gpt-3.5-turbo. withTemperature(0.4F)
specifies the random attribute of the model (the default value of Temperature is 0.8, the larger the value, the more diverse, creative, and random the reply content; set to 0 to answer based on facts, if you want precise answers, you should lower this parameter; for daily chat, it is recommended to use 0.5-0.8).
However, you can still specify these parameters in the configuration file. You can refer to the Configuration Properties in the official documentation for more information.
Prompt#
Great, we already have the object, can we call it now? Actually, we can.
As you can see, the call method accepts two different types of parameters.
prompt is the prompt, similar to the character setting of the model. In most cases, we need to assign a value to the model.
But why can we call it directly by passing message?
As you can see in the source code, it is still wrapped with a prompt.
Creating a Prompt is also simple:
private static Prompt getPrompt(String message) {
String systemPrompt = "{prompt}";
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemPrompt);
Message userMessage = new UserMessage(message);
Message systemMessage = systemPromptTemplate.createMessage(MapUtil.of("prompt", "you are a helpful AI assistant"));
return new Prompt(List.of(userMessage, systemMessage));
}
Here, the message is wrapped with a prompt, but there is an additional systemMessage, which is similar to the character setting of the model.
In fact, there are many types of Prompts and various ways to use them. They are not only prompts but also key to multi-turn conversations.
Here is the complete code:
@RestController
@RequestMapping("ai")
@RequiredArgsConstructor
@CrossOrigin
public class OpenAIController {
private final OpenAiChatClient chatClient;
@GetMapping("chat/{message}")
public String opChat(@PathVariable String message) {
Prompt prompt = getPrompt(message);
List<Generation> response = chatClient.call(prompt).getResults();
StringBuilder result = new StringBuilder();
for (Generation generation : response) {
String content = generation.getOutput().getContent();
result.append(content);
}
return result.toString();
}
private static Prompt getPrompt(String message) {
String systemPrompt = "{prompt}";
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemPrompt);
Message userMessage = new UserMessage(message);
Message systemMessage = systemPromptTemplate.createMessage(MapUtil.of("prompt", "you are a helpful AI assistant"));
return new Prompt(List.of(userMessage, systemMessage));
}
}
The result of the call is as follows:
Spring AI
Simple Example of Spring AI Chat
Quick Integration of ChatGPT using spring-ai