Spring AI Alibaba框架整合百炼大模型平台
文章目录
-
- [Spring AI Alibaba框架整合百炼大模型平台](#Spring AI Alibaba框架整合百炼大模型平台)
版本springboot 3.5.x, jdk17, springaiAlibaba1.1.2.2
快速入门
1.pom依赖文件
xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.0</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-bom</artifactId>
<version>1.1.2.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.1.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-extensions-bom</artifactId>
<version>1.1.2.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring AI Alibaba DashScope Starter(阿里云百炼平台适配) -->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-dashscope</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-agent-framework</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope> <!-- 必须为 provided,否则可能打包冲突 -->
</dependency>
</dependencies>
yml配置
yml
spring:
ai:
# 禁用默认的 Chat Client(如果需要同时使用多个模型,需禁用自动创建的bean改为手动自己去创建)
chat:
client:
enabled: false
dashscope:
api-key: ${AI_DASHSCOPE_API_KEY}
# 聊天模型
chat:
enabled: true
options:
# 指定使用的模型型号(如 qwen-turbo、qwen-plus、qwen-max 等)
model: qwen-plus-2025-07-28
配置Chat聊天模型
java
/**
* ChatClient 配置类
*
* ChatClient 是 Spring AI 推荐的现代调用方式,支持链式编程和插件机制。
* 相比直接使用 ChatModel,ChatClient 提供了更丰富的功能:
* - 链式调用 API
* - 默认系统提示词
* - Advisor 插件机制(日志、记忆、RAG 等)
*/
@Configuration
public class ChatClientConfig {
/**
* 创建 ChatClient Bean
*
* @param chatModel 自动注入的 ChatModel(由 DashScope Starter 提供)
* @return 配置好的 ChatClient 实例
*/
@Bean
public ChatClient chatClient(DashScopeChatModel chatModel) {
return ChatClient.builder(chatModel)
// 设置默认系统提示词,定义了 AI 的基础人设和行为规范
.defaultSystem("你是一个专业、友好的AI助手,请用中文回答问题。")
// 添加日志插件,自动打印每次请求和响应的详细信息,方便调试
// SimpleLoggerAdvisor 是 Spring AI 内置的日志 Advisor
.defaultAdvisors(
new SimpleLoggerAdvisor()
//自定义插件
//,new MyLoggerAdvisor()
)
// 设置默认选项(可选)
// 【设置默认参数】
.defaultOptions(ChatOptions.builder()
.temperature(0.7) // 温度:控制回答的随机性。0=确定,1=创造性高
.topP(0.9) // 核采样:控制词汇选择的多样性
.build())
.build();
}
/**
* 创建 ChatClient.Builder Bean,供需要动态构建 ChatClient 的场景使用
* (例如多轮对话中需要为每个会话创建独立的 ChatClient)
*/
@Bean
public ChatClient.Builder chatClientBuilder(DashScopeChatModel chatModel) {
return ChatClient.builder(chatModel);
}
}
测试
java
@Test
public void chatTest() {
String content = "讲一个笑话";
//String response2 = chatClient.prompt(content).call().content();
String response = chatClient
.prompt() // 1. 开始构建提示词请求
.user(content) // 2. 设置用户输入的问题
.call() // 3. 同步调用 AI 接口
.content(); // 4. 获取 AI 返回的文本内容
System.out.println("AI助手:" + response);
}