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

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

复制代码
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的属性

相关推荐
考虑考虑18 小时前
Jpa使用union all
java·spring boot·后端
用户37215742613519 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊20 小时前
Java学习第22天 - 云原生与容器化
java
渣哥1 天前
原来 Java 里线程安全集合有这么多种
java
间彧1 天前
Spring Boot集成Spring Security完整指南
java
间彧1 天前
Spring Secutiy基本原理及工作流程
java
Java水解1 天前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆1 天前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试
前端小张同学1 天前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole1 天前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端