# Spring Boot 3.x 集成 AI 大模型实战指南
## 前言
随着 ChatGPT 的爆火,AI 大模型应用开发已成为程序员必备技能之一。本文将手把手教你如何在 Spring Boot 3.x 项目中集成主流 AI 大模型,实现智能对话、文本生成等功能。
## 一、环境准备
### 1.1 技术栈
- JDK 17+
- Spring Boot 3.x
- Spring AI (最新版)
- Maven / Gradle
### 1.2 添加依赖
```xml
<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>
```
### 1.3 配置 API Key
```yaml
spring:
ai:
openai:
api-key: your-api-key
base-url: https://api.openai.com
```
## 二、核心实现
### 2.1 创建 AI 服务类
```java
@Service
public class AiService {
private final ChatClient chatClient;
public AiService(ChatClient.Builder builder) {
this.chatClient = builder
.defaultSystem("你是一个专业的Java技术顾问")
.build();
}
public String chat(String message) {
return chatClient.prompt()
.user(message)
.call()
.content();
}
public Flux<String> streamChat(String message) {
return chatClient.prompt()
.user(message)
.stream()
.content();
}
}
```
### 2.2 对话控制器
```java
@RestController
@RequestMapping("/api/ai")
public class AiController {
private final AiService aiService;
@PostMapping("/chat")
public Map<String, String> chat(@RequestBody Map<String, String> request) {
String answer = aiService.chat(request.get("question"));
return Map.of("answer", answer);
}
}
```
## 三、实战案例:智能代码审查
### 3.1 需求背景
传统代码审查耗时费力,让 AI 自动审查代码问题。
### 3.2 实现方案
```java
public class CodeReviewService {
private final ChatClient chatClient;
public List<String> review(String code) {
String prompt = "请审查以下Java代码,指出潜在问题:\n" + code;
return chatClient.prompt()
.user(prompt)
.call()
.list(String.class);
}
}
```
## 四、多模型支持
Spring AI 支持多种大模型:
### 4.1 阿里通义千问
```yaml
spring:
ai:
tongyi:
chat:
options:
api-key: your-tongyi-api-key
```
### 4.2 百度文心一言
```yaml
spring:
ai:
wenxin:
chat:
options:
api-key: your-wenxin-api-key
```
## 五、注意事项
### 5.1 安全问题
- API Key 切勿硬编码,使用环境变量
- 生产环境增加调用频率限制
- 敏感数据脱敏后再发送给 AI
### 5.2 性能优化
- 使用流式响应提升用户体验
- 合理设置超时时间
- 考虑接入缓存层
### 5.3 成本控制
- AI 调用按 token 计费,优化 Prompt 长度
- 善用系统消息减少 token 消耗
## 六、总结
通过本文,我们学习了:
1. Spring Boot 3.x 与 Spring AI 的集成方式
2. 同步与流式对话的实现
3. 多模型支持的配置方法
4. 企业级应用的注意事项
AI 大模型正在改变软件开发方式,掌握这些技能能让你的项目更具竞争力。赶紧动手试试吧!Spring Boot 3.x 集成 AI 大模型实战指南
前言
随着 ChatGPT 的爆火,AI 大模型应用开发已成为程序员必备技能之一。本文将手把手教你如何在 Spring Boot 3.x 项目中集成主流 AI 大模型,实现智能对话、文本生成等功能。
一、环境准备
1.1 技术栈
- JDK 17+
- Spring Boot 3.x
- Spring AI (最新版)
- Maven / Gradle
1.2 添加依赖
<!-- Maven -->
<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>
1.3 配置 API Key
在 application.yml 中配置:
spring:
ai:
openai:
api-key: your-api-key
base-url: https://api.openai.com
二、核心实现
2.1 创建 AI 服务类
@Service
public class AiService {
private final ChatClient chatClient;
public AiService(ChatClient.Builder builder) {
this.chatClient = builder
.defaultSystem("你是一个专业的Java技术顾问")
.build();
}
public String chat(String message) {
return chatClient.prompt()
.user(message)
.call()
.content();
}
// 流式对话
public Flux<String> streamChat(String message) {
return chatClient.prompt()
.user(message)
.stream()
.content();
}
}
2.2 对话控制器
@RestController
@RequestMapping("/api/ai")
public class AiController {
private final AiService aiService;
public AiController(AiService aiService) {
this.aiService = aiService;
}
@PostMapping("/chat")
public Map<String, String> chat(@RequestBody Map<String, String> request) {
String question = request.get("question");
String answer = aiService.chat(question);
return Map.of("answer", answer);
}
@PostMapping("/stream-chat")
public Flux<ServerSentEvent<String>> streamChat(
@RequestBody Map<String, String> request) {
String question = request.get("question");
return aiService.streamChat(question)
.map(content -> ServerSentEvent.builder(content).build());
}
}
三、实战案例:智能代码审查
3.1 需求背景
传统代码审查耗时费力,能否让 AI 自动审查代码问题?
3.2 实现方案
public class CodeReviewService {
private final ChatClient chatClient;
public List<String> review(String code) {
String prompt = String.format("""
请审查以下Java代码,指出潜在问题和优化建议:
%s
请以JSON数组格式返回问题列表。
""", code);
return chatClient.prompt()
.user(prompt)
.call()
.list(String.class);
}
}
3.3 调用示例
@RestController
public class CodeReviewController {
private final CodeReviewService reviewService;
@PostMapping("/review")
public Map<String, Object> reviewCode(@RequestBody Map<String, String> request) {
String code = request.get("code");
List<String> issues = reviewService.review(code);
return Map.of("issues", issues, "count", issues.size());
}
}
四、多模型支持
Spring AI 支持多种大模型,只需简单配置即可切换:
4.1 阿里通义千问
spring:
ai:
tongyi:
chat:
options:
api-key: your-tongyi-api-key
4.2 百度文心一言
spring:
ai:
wenxin:
chat:
options:
api-key: your-wenxin-api-key
secret-key: your-secret-key
五、注意事项
5.1 安全问题
- API Key 切勿硬编码,使用环境变量或配置中心
- 生产环境建议增加调用频率限制
- 敏感数据脱敏后再发送给 AI
5.2 性能优化
- 使用流式响应提升用户体验
- 合理设置超时时间
- 考虑接入缓存层
5.3 成本控制
- AI 调用按 token 计费,优化 Prompt 长度
- 善用系统消息减少 token 消耗
六、总结
通过本文,我们学习了:
- Spring Boot 3.x 与 Spring AI 的集成方式
- 同步与流式对话的实现
- 多模型支持的配置方法
- 企业级应用的注意事项
AI 大模型正在改变软件开发方式,掌握这些技能能让你的项目更具竞争力。赶紧动手试试吧!
关注我,获取更多 Java & AI 技术干货!