百炼推出的智能体应用、工作流应用和智能体编排应用,有效解决了大模型在处理私有领域问题、获取最新信息、遵循固定流程以及自动规划复杂项目等方面的局限,显著拓展了其应用范围。
本文介绍如何通过Spring AI Alibaba集成百炼大模型应用。
前期准备
操作流程
1. 初始化Spring Boot工程
环境要求
Spring Boot 3.x
JDK 17 或更高版本
您可以通过以下两种方式初始化工程:
下载完整的示例工程快速上手(推荐)
完整示例工程请参见bailian-agent。下载代码到本地后,跳转到步骤【3. 配置参数】完成操作,然后执行步骤【5. 启动Spring Boot工程并测试】。
从零开始搭建基础工程
请按后续步骤依次执行。
2. 添加依赖
在pom.xml
中配置Spring AI Alibaba:
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>1.0.0-M5.1</version>
</dependency>
3. 配置参数
在application.yml
中配置百炼大模型应用ID、百炼API Key和业务空间ID(仅在子业务空间创建百炼大模型应用时需要)。
spring:
ai:
dashscope:
agent:
app-id: ${APP_ID} # 大模型应用ID
api-key: ${DASHSCOPE_API_KEY} # 百炼API Key
#workspace-id: ${WORKSPACE_ID} # 业务空间ID,可选,未配置时使用主账号空间
4. 调用百炼大模型应用
Spring AI Alibaba使用DashScopeAgent
调用百炼大模型应用。
非流式调用
流式调用
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentOptions;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RestController
@RequestMapping("/ai")
public class BailianAgentRagController {
private static final Logger logger = LoggerFactory.getLogger(BailianAgentRagController.class);
private DashScopeAgent agent;
@Value("${spring.ai.dashscope.agent.app-id}")
private String appId;
public BailianAgentRagController(DashScopeAgentApi dashscopeAgentApi) {
this.agent = new DashScopeAgent(dashscopeAgentApi);
}
@GetMapping("/bailian/agent/call")
public String call(@RequestParam(value = "message",
defaultValue = "如何使用SDK快速调用阿里云百炼的应用?") String message) {
ChatResponse response = agent.call(new Prompt(message, DashScopeAgentOptions.builder().withAppId(appId).build()));
if (response == null || response.getResult() == null) {
logger.error("chat response is null");
return "chat response is null";
}
AssistantMessage app_output = response.getResult().getOutput();
return app_output.getContent();
}
}
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentOptions;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
@RestController
@RequestMapping("/ai")
public class BailianAgentRagStreamController {
private static final Logger logger = LoggerFactory.getLogger(BailianAgentRagStreamController.class);
private DashScopeAgent agent;
@Value("${spring.ai.dashscope.agent.app-id}")
private String appId;
public BailianAgentRagStreamController(DashScopeAgentApi dashscopeAgentApi) {
this.agent = new DashScopeAgent(dashscopeAgentApi,
DashScopeAgentOptions.builder()
.withSessionId("current_session_id")
.withIncrementalOutput(true)
.withHasThoughts(true)
.build());
}
@GetMapping(value="/bailian/agent/stream", produces="text/event-stream")
public Flux<String> stream(@RequestParam(value = "message",
defaultValue = "你好,请问你的知识库文档主要是关于什么内容的?") String message) {
return agent.stream(new Prompt(message, DashScopeAgentOptions.builder().withAppId(appId).build())).map(response -> {
if (response == null || response.getResult() == null) {
logger.error("chat response is null");
return "chat response is null";
}
AssistantMessage app_output = response.getResult().getOutput();
String content = app_output.getContent();
return content;
});
}
}
5. 启动Spring Boot工程并测试
启动Spring Boot工程并测试(例如使用Postman进行测试)。
计费说明
百炼应用本身不收取费用,但通过应用调用模型时会产生模型推理(调用)的相关费用。有关模型推理(调用)费用详情,请参见计费项。
错误码
通用错误码信息请参见错误信息。
了解更多
Spring AI Alibaba:提供文档教程、实战博客和开发者社区,帮助您快速开发Java生成式AI应用。
应用调用:提供应用调用相关的接口说明与调用示例。
该文章对您有帮助吗?
- 本页导读 (1)
- 前期准备
- 操作流程
- 1. 初始化Spring Boot工程
- 2. 添加依赖
- 3. 配置参数
- 4. 调用百炼大模型应用
- 5. 启动Spring Boot工程并测试
- 计费说明
- 错误码
- 了解更多