文心一言的流式接口数据进行处理 增加属性

需求:需要对文心一言的流式接口数据进行处理 增加属性

return ResponseEntity.ok()
                .header("Access-Control-Allow-Origin", "*")
                .contentType(org.springframework.http.MediaType.TEXT_EVENT_STREAM)
                .cacheControl(org.springframework.http.CacheControl.noCache())
                .body(outputStream -> {
                        try (Response response = client.newCall(request).execute();
                             ResponseBody responseBody = response.body();
                             InputStream inputStream = responseBody.byteStream()) {
                            if (!response.isSuccessful()) {
                                throw new IOException("Failed to fetch streaming data, HTTP error code: " + response.code());
                            }
                            byte[] buffer = new byte[4096];
                            int bytesRead;
                            while ((bytesRead = inputStream.read(buffer)) != -1) {
                                /*outputStream.write(buffer, 0, bytesRead);
                                outputStream.flush();*/
                                String data = new String(buffer, 0, bytesRead, StandardCharsets.UTF_8);
                                String[] lines = data.split("\n");
                                for (String line : lines) {
                                    if (line.startsWith("data: ")) {
                                        // 记录流输出结果,用于后续持久化
                                        com.zbIntel.integration.wenxin.entity.ChatResponse bean =
                                                JSONUtil.parseObj(line.substring(6).getBytes(StandardCharsets.UTF_8))
                                                        .toBean(com.zbIntel.integration.wenxin.entity.ChatResponse.class);
                                        bean.setSessionId(req.getSessionId());

                                        // 序列化bean并发送
                                        String serializedBean = JSONUtil.toJsonStr(bean);
                                        outputStream.write(("data: " + serializedBean + "\n\n").getBytes(StandardCharsets.UTF_8));
                                        outputStream.flush();

                                        log.info("返回数据:{}", bean);
                                        String content = bean.getResult();
                                        if (bean.getIs_end()) {
                                            isEndflag.set(Boolean.TRUE);
                                        }
                                        // 记录流输出结果,用于后续持久化
                                        respContent.append(content);
                                    }
                                }
                            }
                        } catch (IOException e) {
                            log.error("Error during streaming data: ", e);
                            outputStream.write(("{" +
                                    "  \"error_code\": 112," +
                                    "  \"error_msg\": \"" + e.getMessage() + "\"" +
                                    "}").getBytes(StandardCharsets.UTF_8));
                            outputStream.flush();
                        }

                    if(isEndflag.get()) {
                        // 构造回复数据对象,持久化
                        String respContentStr = respContent.toString();
                        SessionChatRecordEntity replyRecord = new SessionChatRecordEntity(
                                finalAskRecord.getSessionId(), Role.ASSISTANT.name,
                                respContentStr, ChatGPTApi.getMessageTokenNum(respContentStr));
                        sessionChatRecordService.saveBatch(ImmutableList.of(finalAskRecord, replyRecord));

                        // 刷新缓存
                        chatService.refreshWindowRecordCache(finalAskRecord.getSessionId());
                    }
                }
        );

主要时这段:

// 序列化bean并发送

String serializedBean = JSONUtil.toJsonStr(bean);

outputStream.write(("data: " + serializedBean + "\n\n").getBytes(StandardCharsets.UTF_8));

outputStream.flush();

返回的结果:

增加了 sessionId的属性

相关推荐
suweijie7682 小时前
SpringCloudAlibaba | Sentinel从基础到进阶
java·大数据·sentinel
公贵买其鹿3 小时前
List深拷贝后,数据还是被串改
java
xlsw_6 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹7 小时前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭7 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫7 小时前
泛型(2)
java
超爱吃士力架8 小时前
邀请逻辑
java·linux·后端
南宫生8 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石8 小时前
12/21java基础
java
李小白668 小时前
Spring MVC(上)
java·spring·mvc