Getting Started with Spring AI: Build ChatGPT‑Powered Apps in Java
This guide introduces Spring AI, shows how to add dependencies, configure OpenAI credentials, and demonstrates chat, image, and prompt APIs with code examples, then explores advanced function calling to build business‑level large language model integrations in Java.
Spring AI Introduction
Spring AI is an application framework for AI engineers that offers a friendly API and abstractions to simplify the development of AI applications. It supports common models and is currently available on https://start.spring.io, hosted in the Spring private repository.
Using the ChatGPT Model
1. Add Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<!-- Configure Spring repository -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>2. Configure OpenAI Parameters
spring:
ai:
openai:
base-url: # optional proxy such as openai-sb, openai-hk
api-key: sk-xxxxAPI Usage
1. Chat API
@Autowired
private OpenAiChatClient chatClient;
@Test
void testChatClient() {
String message = "树上有 9 只鸟,猎人开枪打死一只,树上还剩下多少只鸟?";
System.out.println(chatClient.call(message));
}2. Image Generation
@Autowired
private OpenAiImageClient openaiImageClient;
@Test
void testImageClient() {
ImageResponse response = openaiImageClient.call(
new ImagePrompt("A light cream colored mini golden doodle",
OpenAiImageOptions.builder()
.withQuality("hd")
.withN(4)
.withHeight(1024)
.withWidth(1024)
.build())
);
}3. Prompt API
@Test
void testPrompts() {
String systemText = "You are a helpful AI assistant that helps people find information.
Your name is {name}
You should reply to the user's request with your name.
";
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemText);
String userText = "Tell me about three famous pirates from the Golden Age of Piracy and why they did.
Write at least a sentence for each pirate.
";
Message userMessage = new UserMessage(userText);
Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "lengleng"));
Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
List<Generation> response = chatClient.call(prompt).getResults();
for (Generation generation : response) {
System.out.println(generation.getOutput().getContent());
}
}Advanced: Building Business Large Models
Goal: In natural language interaction, call external tools to answer questions; convert natural language to API call parameters or database query conditions; extract structured data.
1. Create Function Calling
public class MockMathScoreService implements Function<MockMathScoreService.Request, MockMathScoreService.Response> {
/** request dimensions: username */
@JsonInclude(Include.NON_NULL)
@JsonClassDescription("数学行业")
public record Request(@JsonProperty(required = true, value = "username")
@JsonPropertyDescription("学生姓名") String username) {}
/** response dimensions: username, score */
public record Response(String username, double score) {}
/** data source logic: query local DB by username */
@Override
public Response apply(Request request) {
if (request.username.contains("张三")) {
return new Response("张三", 100);
} else if (request.username.contains("李四")) {
return new Response("李四", 99.5);
}
return new Response("null", 0);
}
}2. Invoke
@Test
void testCallFunc() {
String promptText = "张三同学的数学成绩怎么样?";
UserMessage userMessage = new UserMessage(promptText);
var promptOptions = OpenAiChatOptions.builder()
.withFunctionCallbacks(List.of(new FunctionCallbackWrapper<>(
"MathScoreService",
"教育行业AI机器人",
new MockMathScoreService())))
.build();
ChatResponse response = chatClient.call(new Prompt(List.of(userMessage), promptOptions));
System.out.println(response.getResult().getOutput().getContent());
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
