Spring AI 系列之四十 - Spring AI Alibaba-集成百炼智能体
之前做个几个大模型的应用,都是使用Python语言,后来有一个项目使用了Java,并使用了Spring AI框架。随着Spring AI不断地完善,最近它发布了1.0正式版,意味着它已经能很好的作为企业级生产环境的使用。对于Java开发者来说真是一个福音,其功能已经能满足基于大模型开发企业级应用。借着这次机会,给大家分享一下Spring AI框架。
注意:由于框架不同版本改造会有些使用的不同,因此本次系列中使用基本框架是 Spring AI-1.0.0,JDK版本使用的是19,Spring-AI-Alibaba-1.0.0.3-SNAPSHOT。
代码参考: https://github.com/forever1986/springai-study
目录
- 1 百炼智能体应用
- 2 代码演示
- 2.1 百炼搭建智能体
- 2.2 代码实现
- 2.3 演示效果
上一章讲了如何使用Spring AI Alibaba的集成百炼平台的知识库,这一章利用阿里的云上百炼平台构建智能体、工作流应用以及智能体编排,然后在Spring AI Alibaba中调用创建好的智能体应用。
1 百炼智能体应用
阿里云百炼是一款可视化 AI 智能体应用开发平台,它提供了三种大模型应用开发模式:智能体、工作流与智能体编排,支持知识库检索、互联网搜索、工作流设计及智能体协作等功能。(如果用过其它在线构建Agent的应该就很熟悉),如下图:
其官方网站是:https://bailian.console.aliyun.com/?tab=app#/app-market
可以看出百炼平台的智能体构建已经很强大,因此可以利用其智能体构建能力快速搭建自己的应用,然后再集成Spring AI Alibaba应用到自己项目中,这个对于开发应用来说是非常迅速的。下面就演示如何使用Spring AI Alibaba集成百炼平台的智能体。
2 代码演示
代码参考lesson30子模块
示例说明:通过在阿里的百炼平台构建一个智能体,该智能体引用一个“庆余年小说”知识库,回答用户有关庆余年的相关内容
2.1 百炼搭建智能体
1)登录百炼平台:https://bailian.console.aliyun.com/?tab=app#/app-center
2)创建一下智能体应用。其知识库自己可以创建一个,并上传文件。这里利用上一章的同一个知识库:庆余年小说
3)发布应用
4)发布成功后,会有一个应用ID,这个会在程序中使用到
2.2 代码实现
1)在lesson30子模块下,创建baili-agent子模块,其pom引入如下:
<dependencies><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter-dashscope</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
2)创建application.properties配置文件
# 百炼模型的API KEY
spring.ai.dashscope.api-key=你的阿里百炼API KEY
# 百炼平台的智能体应用ID
spring.ai.dashscope.agent.app-id=你的百炼平台的智能体应用ID
3)创建BailianAgentRagController演示类
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class BailianAgentRagController {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 = "你是谁?") String message) {ChatResponse response = agent.call(new Prompt(message, DashScopeAgentOptions.builder().withAppId(appId).build()));if (response == null || response.getResult() == null) {System.err.println("chat response is null");return "chat response is null";}AssistantMessage app_output = response.getResult().getOutput();String content = app_output.getText();DashScopeAgentApi.DashScopeAgentResponse.DashScopeAgentResponseOutput output = (DashScopeAgentApi.DashScopeAgentResponse.DashScopeAgentResponseOutput) app_output.getMetadata().get("output");List<DashScopeAgentApi.DashScopeAgentResponse.DashScopeAgentResponseOutput.DashScopeAgentResponseOutputDocReference> docReferences = output.docReferences();List<DashScopeAgentApi.DashScopeAgentResponse.DashScopeAgentResponseOutput.DashScopeAgentResponseOutputThoughts> thoughts = output.thoughts();System.err.printf("content:\n{}\n\n", content);if (docReferences != null && !docReferences.isEmpty()) {for (DashScopeAgentApi.DashScopeAgentResponse.DashScopeAgentResponseOutput.DashScopeAgentResponseOutputDocReference docReference : docReferences) {System.err.printf("{}\n\n", docReference);}}if (thoughts != null && !thoughts.isEmpty()) {for (DashScopeAgentApi.DashScopeAgentResponse.DashScopeAgentResponseOutput.DashScopeAgentResponseOutputThoughts thought : thoughts) {System.err.printf("{}\n\n", thought);}}return content;}}
4)创建Lesson30AgentApplication 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Lesson30AgentApplication {public static void main(String[] args) {SpringApplication.run(Lesson30AgentApplication.class, args);}}
2.3 演示效果
1)访问
http://localhost:8080/bailian/agent/call?message=你是谁
2)访问其中小说细节
http://localhost:8080/bailian/agent/call?message=滕子京的死出现在第几章
3)可以使用百炼平台的应用观测查看调用记录
结语:本章演示了如何使用Spring AI Alibaba集成百炼平台的智能体,这样对于实际应用开发来说是非常迅速的,这就是Spring AI Alibaba框架的优势之一。好了,本次对于Spring AI Alibaba的内容已经讲完了,虽然还有些内容没有讲到,但是相信你看文档也能够很快掌握。关于Spring AI的章节也就完整讲完了,本次系列就到此结束。
Spring AI系列上一章:《Spring AI 系列之三十九 - Spring AI Alibaba-集成百炼知识库》