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

文章目录

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

Spring AI Alibaba框架

1.准备工作

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

2.引入maven依赖

xml 复制代码
<?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

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

4.调用

4.1.非流式调用

java 复制代码
/**
 * 非流式调用
 */
@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.流式调用

java 复制代码
/**
 * 流式调用
 */
@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;
        });
    }
}
相关推荐
红尘炼丹客6 分钟前
《DeepSeek-OCR: Contexts Optical Compression》速览
人工智能·python·自然语言处理·ocr
TiAmo zhang12 分钟前
现代C++的AI革命:C++20/C++23核心特性解析与实战应用
c++·人工智能·c++20
mwq3012318 分钟前
从傅里叶变换到 RoPE:解构位置编码的数学灵魂
人工智能
知兀32 分钟前
【Spring/SpringBoot】<dependencyManagement> + import 导入能继承父maven项目的所有依赖,类似parent
spring boot·spring·maven
源码宝1 小时前
企业项目级医院随访系统源码,患者随访管理系统,技术框架:Java+Spring boot,Vue,Ant-Design+MySQL5
java·vue.js·spring·程序·医院管理系统·随访·随访系统源码
深圳佛手1 小时前
AI 编程工具Claude Code 介绍
人工智能·python·机器学习·langchain
沃达德软件1 小时前
智能识别车辆驾驶人特征
人工智能·目标检测·计算机视觉·目标跟踪·视觉检测
金融小师妹2 小时前
多因子量化模型预警:美元强势因子压制金价失守4000关口,ADP数据能否重构黄金趋势?
人工智能·深度学习·1024程序员节
BJ_Bonree2 小时前
圆桌论坛精华实录 | AI是重构运维逻辑的颠覆性革命?博睿数据与行业大咖亲授“AI+可观测性”的破局之道
运维·人工智能·重构