当前位置: 首页 > news >正文

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-集成百炼知识库》

http://www.lryc.cn/news/614284.html

相关文章:

  • 用browse实现菜单功能的方法
  • 《在 Spring Boot 中安全使用 Qwen API-KEY:环境变量替代明文配置的最佳实践》
  • 一文可视化分析2025年6月计算机视觉顶刊IJCV前沿热点
  • 数据结构(16)排序(上)
  • 代理模式在C++中的实现及面向对象设计原则的满足
  • vscode无法跳转到定义引用
  • 以下是使用这款ePub编辑器将指定章节转换为TXT文本文档的操作方法
  • JAVA基础-NIO
  • flutter TLS protocol versions: (TLSv1.2, TLSv1.3)
  • 【数据结构】排序(sort) -- 计数排序
  • 在 Elasticsearch/Kibana (ELK Stack) 中搜索包含竖线 (|)​​ 这类特殊字符的日志消息 (msg 字段) ​确实需要转义
  • 软件包管理、缓存、自定义 YUM 源
  • Vulnhub drippingblues 靶场复现 详细攻略
  • 强光干扰下误报率↓82%!陌讯多模态融合算法在高空抛物检测的实战优化
  • 自适应反步控制:理论与设计
  • 分布式微服务--GateWay的断言以及如何自定义一个断言
  • MySQL 配置性能优化赛:核心策略与实战技巧
  • 分布式系统性能优化实战:从瓶颈定位到架构升级
  • 前端后端之争?JavaScript和Java的特性与应用场景解析
  • Microsoft Dynamics AX 性能优化解决方案
  • 用JOIN替代子查询的查询性能优化
  • 深入解析基于Zookeeper分布式锁在高并发场景下的性能优化实践指南
  • DataFun联合开源AllData社区和开源Gravitino社区将在8月9日相聚数据治理峰会论坛
  • AI漫画翻译器-上传图片自动翻译,支持多语言
  • 分享超图提供的、很不错的WebGIS学习资源
  • 从安卓兼容性困境到腾讯Bugly的救赎:全链路崩溃监控解决方案-卓伊凡|bigniu
  • 什么是局放?局放在线智能传感器,敏锐洞察电气设备中的隐形故障!
  • bytearray和bytes
  • 进程管理、系统高负载、cpu超过800%等实战问题处理
  • 【Mybatis入门】配置Mybatis(IDEA)