🍃 Spring AI
"将 AI 的力量引入企业 Java 生态系统。"
Spring AI 提供了一致性的抽象层,用于将 AI 能力集成到 Spring Boot 应用中,通过统一 API 支持多个 AI 提供商。
🎯 为什么选择 Spring AI?
| 优势 | 说明 |
|---|---|
| 熟悉的模式 | Spring 约定、依赖注入 |
| 提供商无关 | 在 OpenAI、Anthropic、Ollama 等之间自由切换 |
| 生产就绪 | 内置重试、熔断器、可观测性 |
| 类型安全 | Java/Kotlin 类型安全,无需手动处理 JSON |
🏗️ 架构
🚀 快速开始
依赖配置
<!-- Spring AI BOM -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-M4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- OpenAI Starter -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
配置
# application.yml
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4o
temperature: 0.7
💬 Chat Client
基本用法
@Service
public class ChatService {
private final ChatClient chatClient;
public ChatService(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
public String chat(String userMessage) {
return chatClient.prompt()
.user(userMessage)
.call()
.content();
}
}
带系统提示词
public String chatWithContext(String userMessage) {
return chatClient.prompt()
.system("You are a helpful assistant specialized in Java programming.")
.user(userMessage)
.call()
.content();
}
流式响应
public Flux<String> streamChat(String userMessage) {
return chatClient.prompt()
.user(userMessage)
.stream()
.content();
}
📝 输出解析
结构化输出
// Define response structure
public record MovieRecommendation(
String title,
int year,
String genre,
String reason
) {}
// Parse LLM output to Java object
public MovieRecommendation getRecommendation(String preferences) {
return chatClient.prompt()
.user("Recommend a movie for someone who likes: " + preferences)
.call()
.entity(MovieRecommendation.class);
}
列表输出
public List<MovieRecommendation> getRecommendations(String preferences) {
return chatClient.prompt()
.user("Recommend 3 movies for: " + preferences)
.call()
.entity(new ParameterizedTypeReference<List<MovieRecommendation>>() {});
}
🔧 Function Calling
定义函数
@Configuration
public class FunctionConfig {
@Bean
@Description("Get current weather for a location")
public Function<WeatherRequest, WeatherResponse> currentWeather() {
return request -> {
// Call weather API
return new WeatherResponse(
request.city(),
72.0,
"Sunny"
);
};
}
}
record WeatherRequest(String city, String unit) {}
record WeatherResponse(String city, double temperature, String condition) {}
在对话中使用函数
public String chatWithWeather(String userMessage) {
return chatClient.prompt()
.user(userMessage)
.functions("currentWeather") // Enable the function
.call()
.content();
}
// 用户: "What's the weather in Seattle?"
// Agent: 调用 currentWeather("Seattle")
// 响应: "It's currently 72°F and sunny in Seattle."