🌟 一、前言:什么是 Spring AI?
随着人工智能技术的快速发展,越来越多的企业开始尝试将 AI 能力集成到自己的业务系统中。Spring 作为 Java 开发中最流行的框架之一,也在积极拥抱 AI 技术,推出了 Spring AI 项目。
Spring AI 是一个用于简化在 Spring 应用程序中集成 AI 功能的项目,它提供了一系列开箱即用的接口和工具,帮助开发者快速接入主流的大语言模型(LLM),如 OpenAI、Anthropic、阿里云通义千问(Qwen)、百度文心一言等。
🔧 二、开发环境准备
1. JDK 版本
- 推荐使用 JDK 17+
2. IDE 工具
- IntelliJ IDEA / Eclipse / VS Code + Java 插件
3. 构建工具
- Maven 或 Gradle(本文以 Maven 为例)
4. Spring Boot 版本
- Spring Boot 3.x(兼容 Spring AI 最新版本)
5. 大模型 API Key
- 如你希望调用 Qwen,请前往 阿里云百炼平台 获取 API Key。
🧱 三、创建 Spring Boot 项目
你可以通过 Spring Initializr 创建一个基础项目,选择如下依赖:
- Spring Web
- Spring Boot DevTools(可选)
- Lombok(可选)
然后添加 Spring AI 的依赖。
Maven 配置(pom.xml
):
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Spring AI 核心依赖 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-core</artifactId>
<version>0.8.1</version> <!-- 注意查看最新版本 -->
</dependency>
<!-- 如果要调用阿里云 Qwen 模型 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>0.8.1</version>
</dependency>
</dependencies>
💡 四、调用大模型(以 Qwen 为例)
我们将演示如何通过 Spring AI 快速调用阿里云的 Qwen 大模型。
1. 配置 application.yml
spring:
ai:
alibaba:
qwen:
api-key: your_api_key_here
base-url: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
⚠️请将
your_api_key_here
替换为你的实际 DashScope API Key。
2. 创建一个服务类来调用模型
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Service;
@Service
public class AiService {
private final ChatClient chatClient;
public AiService(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}
public String askQuestion(String question) {
return chatClient.prompt()
.user(question)
.call()
.content();
}
}
3. 创建 Controller 提供 REST 接口
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/ai")
public class AiController {
private final AiService aiService;
public AiController(AiService aiService) {
this.aiService = aiService;
}
@GetMapping("/ask")
public String ask(@RequestParam String question) {
return aiService.askQuestion(question);
}
}
4. 启动项目并测试接口
启动 Spring Boot 项目后,访问如下 URL 测试接口:
http://localhost:8080/api/ai/ask?question=你好,请介绍一下你自己。
你会看到 Qwen 返回的回答!
🧪 五、进阶功能介绍
1. 支持多种模型
Spring AI 不仅支持 Qwen,还支持 OpenAI、Claude、Google Gemini 等主流大模型,只需更换配置和依赖即可切换模型。
2. 提示工程(Prompt Engineering)
可以通过自定义提示模板提升回答质量:
PromptTemplate promptTemplate = new PromptTemplate("请回答这个问题:{question}");
promptTemplate.apply(Map.of("question", "什么是量子计算?"));
3. 支持流式输出(Streaming)
对于需要实时返回结果的场景,可以使用 Streaming API:
chatClient.stream().user("讲个笑话").call().content().subscribe(System.out::print);
📚 六、参考资料与学习路径
资源名称 | 地址 |
---|---|
Spring AI 官方文档 | https://www.spring-doc.cn/projects/spring-ai |
Spring AI GitHub 仓库 | https://github.com/spring-projects/spring-ai |
阿里云 Qwen DashScope 平台 | https://dashscope.aliyun.com/ |
🎉 七、结语
Spring AI 正在迅速发展,成为连接 Spring 生态与 AI 技术的重要桥梁。通过本文的学习,你应该已经掌握了如何使用 Spring Boot 快速集成大模型,并实现简单的 AI 对话功能。
接下来你可以尝试:
- 集成更多 AI 模型
- 实现多轮对话上下文管理
- 构建 AI Agent 系统
- 结合前端展示 AI 输出内容
如果你觉得这篇博客对你有帮助,欢迎点赞、收藏、转发!也欢迎关注我后续发布的更多 AI 与 Spring 相关的技术文章。
✨ 持续更新 Spring AI、Java、微服务、架构等干货内容,记得关注不迷路哦~