关于java 调用阿里千问大模型,流式返回,并返回给前端

aaff

FLux曹,找了那么多案例,没一个靠谱的,还得东拼西凑。具体方法类

maven 这两个应该就够用

java 复制代码
   <!--ai调用-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dashscope-sdk-java</artifactId>
            <version>2.19.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
java 复制代码
  @GetMapping(value = "/aiTestTwo", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> simpleStream() throws NoApiKeyException, InputRequiredException {
        // 直接返回一个 Flux,每个元素会立即发送给前端
        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("You are a helpful assistant.")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content("你是谁?")
                .build();
        GenerationParam param = GenerationParam.builder()
                // 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))//DASHSCOPE_API_KEY
                // 模型列表:https://help.aliyun.com/model-studio/getting-started/models
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .incrementalOutput(true)
                .build();

        Flowable<GenerationResult> flowable  = gen.streamCall(param);
        return Flux.from(flowable)
                .publishOn(Schedulers.boundedElastic())
                .map(result -> result.getOutput().getChoices().get(0).getMessage().getContent())
                .filter(content -> content != null && !content.isEmpty());


    }

或者sse调用

复制代码
@GetMapping(value = "/aiTestTwo", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter simpleStream() throws NoApiKeyException, InputRequiredException {
        SSE sse = new SSE(60000L);
        // 直接返回一个 Flux,每个元素会立即发送给前端
        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("You are a helpful assistant.")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content("你是谁?")
                .build();
        GenerationParam param = GenerationParam.builder()
                // 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))//DASHSCOPE_API_KEY
                // 模型列表:https://help.aliyun.com/model-studio/getting-started/models
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .incrementalOutput(true)
                .build();

        /*Flowable<GenerationResult> flowable = gen.streamCall(param);*/
        gen.streamCall(param).forEach(result -> {
            System.out.println(result);
            System.out.println(result.getOutput());
            System.out.println(result.getOutput().getChoices());
            System.out.println(result.getOutput().getChoices().get(0));
            System.out.println(result.getOutput().getChoices().get(0).getMessage());
            System.out.println("122212121212"+result.getOutput().getChoices().get(0).getMessage().getContent());

            String chunk = result.getOutput().getChoices().get(0).getMessage().getContent();
            System.out.print(chunk); // 逐字输出,模拟打字机效果
            sse.send(chunk);
           /* sse.send(SseEmitter.event()
                    .name("message")
                    .data(chunk));*/
        });
        /*sse.complete();*/
        return sse;

    }

html

html 复制代码
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>SSE 流式测试</title>
</head>
<body>
<h1>SSE 流式数据接收测试</h1>
<button onclick="startConnect()">开始连接</button>
<button onclick="stopConnect()">断开连接</button>
<hr/>
<div id="message-box" style="border:1px solid #ccc; padding:10px; min-height:100px;"></div>

<script>
    let eventSource = null;

    function startConnect() {
        const box = document.getElementById('message-box');
        box.innerText = "正在连接...\n";

        // 1. 创建 EventSource 对象,指向后端接口
        eventSource = new EventSource('/user/aiTestTwo');

        // 2. 监听消息事件 (默认事件名为 'message')
        eventSource.onmessage = function(event) {
            // event.data 就是后端 emitter.send() 发送的内容
            console.log("收到数据:", event.data);
            box.innerText += event.data;
        };

        // 3. 监听自定义事件 (如果后端用了 .name("error"))
        eventSource.addEventListener('error', function(event) {
            console.error("收到错误:", event.data);
            box.innerText += "\n[错误]: " + event.data;
        });

        // 4. 监听连接打开事件
        eventSource.onopen = function(event) {
            console.log("连接已建立");
        };

        // 5. 监听连接关闭/错误事件
        eventSource.onerror = function(event) {
            console.log("连接出错或关闭");
            // EventSource 会自动重连,如果需要手动停止,需调用 close()
            // eventSource.close();
        };
    }

    function stopConnect() {
        if (eventSource) {
            eventSource.close();
            eventSource = null;
            document.getElementById('message-box').innerText += "\n[连接已断开]";
        }
    }
</script>
</body>
</html>
相关推荐
·薯条大王6 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
用户938515635077 小时前
从零在浏览器里跑 DeepSeek-R1:WebGPU + Transformer.js 全链路实战
前端·设计模式·typescript
CodeSheep7 小时前
稚晖君公司人事大变动,来了!
前端·后端·程序员
鱼樱前端7 小时前
用 AI 做内容变收入
前端·人工智能·ai编程
浮生望7 小时前
React + TypeScript 组件设计进阶:从类型约束到无状态组件的三次进化
前端
90后的晨仔8 小时前
Mac 开发 uni-app 终极方案:让安卓模拟器彻底脱离 Android Studio 独立运行
前端·vue.js
90后的晨仔8 小时前
别再搞混了!uni-app 中 node_modules 和 uni_modules 到底是什么关系?
前端
kyriewen9 小时前
面试官问"这段逻辑为什么这样写"——我才发现,这半年我写的代码,我自己都解释不了
前端·javascript·面试
谢小飞9 小时前
大文件上传很难?学会这招,GB文件秒传不是梦
前端·node.js
ZJH__GO9 小时前
网络编程v4pro--实现聊天室文件传输功能
java·服务器·网络·计算机网络