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

Spring AI Alibaba集成阿里云百炼大模型应用

文章目录

    • 1.准备工作
    • 2.引入maven依赖
    • 3.application.yml
    • 4.调用
      • 4.1.非流式调用
      • 4.2.流式调用

阿里云百炼推出的智能体应用工作流应用智能体编排应用,有效解决了大模型在处理私有领域问题、获取最新信息、遵循固定流程以及自动规划复杂项目等方面的局限,显著拓展了其应用范围。

Spring AI Alibaba框架

1.准备工作

  1. 登录阿里百炼平台,新建一个appkey,然后创建一个智能体应用,获取一个appId
  2. 开发环境:JDK17+、SpringBoot3.x+

2.引入maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.8</version></parent><groupId>com.linging</groupId><artifactId>spring-ai-alibaba</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter</artifactId><version>1.0.0-M5.1</version></dependency></dependencies></project>

3.application.yml

spring:ai:dashscope:agent:app-id: xxxx # 大模型应用ID,需要在平台发布(创建时会指定使用的模型)api-key: xxxx # 百炼API Keyworkspace-id: xxxx # 业务空间ID,可选,未配置时使用主账号空间

4.调用

4.1.非流式调用

/*** 非流式调用*/
@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:xxx}")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();}
}

4.2.流式调用

/*** 流式调用*/
@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:xxx}")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;charset=utf-8")public Flux<String> stream(@RequestParam(value = "message",defaultValue = "你好,请问你的知识库文档主要是关于什么内容的?") String message, HttpServletResponse re) {re.setContentType("text/event-stream;charset=utf-8");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;});}@GetMapping(value="/bailian/agent/stream/html", produces="text/html;charset=utf-8")public Flux<String> streamHtml(@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;});}
}
http://www.lryc.cn/news/2383977.html

相关文章:

  • 22. 用例依赖装饰器的实现思路和方法
  • 支持向量存储:PostgresSQL及pgvector扩展详细安装步骤!老工程接入RAG功能必备!
  • 【部署】如何离线环境创建docker容器执行python命令行程序
  • idea常用配置 properties中文输出乱码
  • 【Bluedroid】蓝牙 HID Host connect全流程源码解析
  • day1 大模型学习 Qwen系列学习
  • Unity3D仿星露谷物语开发47之砍树时落叶特效
  • 第十节第六部分:常见API:DateTimeFormatter、Period、Duration
  • 如何在VSCode中更换默认浏览器:完整指南
  • B2160 病人排队
  • 【机器人】复现 3D-Mem 具身探索和推理 | 3D场景记忆 CVPR 2025
  • 鸿蒙进阶——CMakelist、GN语法简介及三方库通用移植指南
  • CSS-5.1 Transition 过渡
  • TTS:VITS-fast-fine-tuning 快速微调 VITS
  • 从虚拟仿真到行业实训再到具身智能--华清远见嵌入式物联网人工智能全链路教学方案
  • 告别手动绘图!2分钟用 AI 生成波士顿矩阵
  • GraphPad Prism工作表的管理
  • UE 材质几个输出向量节点
  • 【modelscope/huggingface 通过colab将huggingface 模型/数据集/空间转移到 modelscope并下载】
  • 告别静态UI!Guineration用AI打造用户专属动态界面
  • 第六届电子通讯与人工智能国际学术会议(ICECAI 2025)
  • 【C/C++】C++并发编程:std::async与std::thread深度对比
  • 每日算法刷题Day11 5.20:leetcode不定长滑动窗口求最长/最大6道题,结束不定长滑动窗口求最长/最大,用时1h20min
  • STL中的Vector(顺序表)
  • iOS Runtime与RunLoop的对比和使用
  • 解决vscode在任务栏显示白色图标
  • 架构思维:构建高并发扣减服务_分布式无主架构
  • Vue 3 官方 Hooks 的用法与实现原理
  • Vue3 打印表格、Element Plus 打印、前端打印、表格导出打印、打印插件封装、JavaScript 打印、打印预览
  • 湖北理元理律师事务所:专业债务优化如何助力负债者重获生活掌控权