Spring AI Alibaba 集成使用指南
本文介绍了如何在Spring Boot项目中集成阿里巴巴的Spring AI Alibaba框架,实现与Qwen大模型的交互。主要内容包括:环境准备(Java 17+、Spring Boot 3.x)、Maven/Gradle依赖配置、阿里云API密钥设置,以及创建Service调用Qwen模型的代码示例。文章还展示了流式响应和提示词模板的使用方法,重点演示了函数调用功能,通过定义天气查询工具类并
前言
在现代的微服务架构中,Spring Boot 和 Spring Cloud 是构建分布式系统的核心框架。随着人工智能技术的不断发展,阿里巴巴推出了 Spring AI Alibaba 项目,旨在将大模型(如 Qwen、通义千问等)与 Spring 生态无缝集成,让开发者可以更方便地在企业级应用中引入 AI 能力。
本文将介绍如何在 Spring Boot 项目中集成 Spring AI Alibaba,并使用 Qwen 大模型进行文本生成和对话交互。
一、项目背景
1. 什么是 Spring AI Alibaba?
spring-ai-alibaba
是阿里巴巴开源的一个项目,它为 Spring 开发者提供了访问阿里云大模型(如 Qwen)的能力。它封装了调用大模型 API 的逻辑,支持流式输出、提示工程、函数调用等高级功能,并与 Spring Boot 自动装配机制兼容。
2. 适用场景
- 智能客服
- 自动摘要生成
- 对话机器人
- 文本翻译与改写
- 内容创作助手
二、环境准备
1. 技术栈
- Java 17+
- Spring Boot 3.x 或 Spring Boot 2.7+
- Maven / Gradle
- Qwen Access Key(通过阿里云平台申请)
2. 添加依赖
使用 Maven:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>spring-ai-alibaba-spring-boot-starter</artifactId>
<version>0.8.0</version> <!-- 请确认最新版本 -->
</dependency>
使用 Gradle:
implementation 'com.alibaba:spring-ai-alibaba-spring-boot-starter:0.8.0'
三、配置 Qwen 访问密钥
你需要先前往 阿里云百炼平台 注册账号并获取 API_KEY
。
在 application.yml
中添加如下配置:
spring:
ai:
alibaba:
qwen:
api-key: your_api_key_here
model-name: qwen-max # 可选:qwen-plus, qwen-turbo 等
base-url: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
四、编写代码示例
1. 创建一个 Service 来调用 Qwen 模型
import com.alibaba.spring.ai.alibaba.qwen.QwenChatClient;
import org.springframework.stereotype.Service;
@Service
public class QwenService {
private final QwenChatClient chatClient;
public QwenService(QwenChatClient chatClient) {
this.chatClient = chatClient;
}
public String generateResponse(String prompt) {
return chatClient.call(prompt);
}
}
2. 创建一个 Controller 接口
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/qwen")
public class QwenController {
private final QwenService qwenService;
public QwenController(QwenService qwenService) {
this.qwenService = qwenService;
}
@GetMapping("/ask")
public String askQwen(@RequestParam String question) {
return qwenService.generateResponse(question);
}
}
3. 启动项目并测试接口
启动 Spring Boot 应用后,访问如下地址:
http://localhost:8080/api/qwen/ask?question=今天天气怎么样?
即可看到 Qwen 返回的回答内容。
五、进阶功能
1. 流式响应(Streaming)
Spring AI Alibaba 支持流式输出,适用于长文本生成或聊天机器人场景。
@GetMapping("/stream")
public Flux<String> streamQwen(@RequestParam String question) {
return Flux.create(sink -> {
QwenChatClient.StreamCallback callback = new QwenChatClient.StreamCallback() {
@Override
public void onToken(String token) {
sink.next(token);
}
@Override
public void onError(Throwable throwable) {
sink.error(throwable);
}
@Override
public void onComplete() {
sink.complete();
}
};
chatClient.streamCall(question, callback);
});
}
2. 提示词模板(Prompt Template)
你可以使用 PromptTemplate
构建结构化提示:
var template = PromptTemplate.from("请根据以下问题回答:{question}");
var prompt = template.apply(Map.of("question", "量子计算的基本原理是什么?"));
String response = chatClient.call(prompt);
好的,我们来优化这部分内容:移除日志监控部分的描述,并在“函数调用(Function Calling)”中添加一个完整的示例。
3. 函数调用(Function Calling)
函数调用(Function Calling)是大模型与外部系统交互的重要方式。通过该功能,Qwen 可以根据用户输入智能判断是否需要调用某个工具函数,并将参数传入,最终整合结果返回给用户。
spring-ai-alibaba
提供了对函数调用的支持,开发者可以定义 Java 方法作为工具函数并注册到 QwenChatClient
中。
示例:天气查询工具
假设我们要实现一个天气查询助手,当用户询问“明天北京天气怎么样?”时,Qwen 能自动调用本地的天气接口获取数据并返回。
1. 定义工具类
import com.alibaba.spring.ai.tools.Tool;
import org.springframework.stereotype.Component;
@Component
public class WeatherTool {
@Tool("get_weather")
public String getWeather(String location) {
// 模拟调用真实天气服务
return "当前" + location + "的天气是晴天,温度25°C";
}
}
2. 注册工具并调用
我们需要在配置或启动时将工具注册进 Qwen 客户端:
import com.alibaba.spring.ai.alibaba.qwen.QwenChatClient;
import com.alibaba.spring.ai.tools.ToolProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class QwenService {
private final QwenChatClient chatClient;
public QwenService(QwenChatClient chatClient, ToolProvider toolProvider) {
this.chatClient = chatClient;
this.chatClient.setToolProviders(toolProvider); // 注册工具
}
public String generateResponse(String prompt) {
return chatClient.call(prompt);
}
}
注意:
ToolProvider
是 Spring AI 自动扫描并收集所有标注了@Component
和@Tool
的工具类。
3. 使用方式
访问接口:
http://localhost:8080/api/qwen/ask?question=明天上海天气如何?
Qwen 将自动识别需要调用 get_weather
工具,并传入参数 "location": "上海"
,最终返回整合后的天气信息。
小结
- 使用
@Tool
注解标记方法为可被大模型调用的工具。 - 工具类需加入 Spring 管理(如
@Component
)。 QwenChatClient
支持注册多个工具,实现灵活扩展。- 大模型会根据上下文自动决定是否调用工具,并传递参数。
这使得你可以构建更智能化、具备实际业务能力的 AI 应用程序,例如集成数据库查询、订单系统、支付接口等。
七、总结
通过 spring-ai-alibaba
,我们可以非常轻松地将 Qwen 等大模型能力集成到 Spring Boot 应用中,从而快速实现 AI 助手、智能问答等功能。该项目不仅简化了调用流程,还提供了良好的扩展性,非常适合企业级 AI 应用开发。
参考资料
- GitHub 官方仓库:https://github.com/alibaba/spring-ai-alibaba
- 阿里云百炼平台文档:https://help.aliyun.com/zh/bailian
- Spring Boot 官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/
更多推荐
所有评论(0)