From 758b00fefb7d10651cef250c927e8bc8f08319a9 Mon Sep 17 00:00:00 2001 From: Liuyang <2746366019@qq.com> Date: Fri, 21 Feb 2025 17:05:39 +0800 Subject: [PATCH] =?UTF-8?q?refactor(module-llm):=E4=BC=98=E5=8C=96=20HTTP?= =?UTF-8?q?=20=E8=AF=B7=E6=B1=82=E5=92=8C=E5=93=8D=E5=BA=94=E7=9A=84?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除重复的日志记录代码 - 添加请求耗时的记录 - 优化日志输出格式 - 封装日志打印逻辑到单独的方法中 --- .../llm/service/http/RagHttpService.java | 62 ++++++++++--------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/RagHttpService.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/RagHttpService.java index 161051960..58a970d8f 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/RagHttpService.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/RagHttpService.java @@ -167,33 +167,22 @@ public class RagHttpService { String curlCommand = String.format("curl -X POST -F \"file_id=%s\" -F \"file=@%s\" \"%s\"", ragUploadReqVO.getFileId(), ragUploadReqVO.getFileName(), ragEmbed); log.info("生成的 curl 命令: {}", curlCommand); - + long startTime = System.currentTimeMillis(); // 记录开始时间 String body = HttpRequest.post(ragUploadReqVO.getUrl()) .form("file", result, ragUploadReqVO.getFileName()) .form("file_id", ragUploadReqVO.getFileId()) .timeout(60000) .executeAsync().body(); + // 打印响应内容 - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); + long endTime = System.currentTimeMillis(); + long duration = endTime - startTime; + + printLogs(); + log.info("请求耗时: {}", formatDuration(duration)); log.info("响应原始内容: {}", body); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - // // 发送上传请求 - // HttpResponse uploadResponse = Unirest.post(ragUploadReqVO.getUrl()) - // .field("file_id", ragUploadReqVO.getFileId()) - // .field("file", new ByteArrayInputStream(utf8Bytes), ragUploadReqVO.getFileName()) - // .asString(); + printLogs(); - // 检查响应状态 - - // log.info("Response Body: {}", uploadResponse.getBody()); ragEmbedRespVO = JSON.parseObject(body, RagEmbedRespVO.class); log.info("ragEmbedRespVO:{}", ragEmbedRespVO); @@ -217,6 +206,20 @@ public class RagHttpService { } } + public void printLogs(){ + for (int i = 0; i < 5; i++){ + log.info("===============================响应成功==============================="); + } + } + + public static String formatDuration(long durationMillis) { + long minutes = durationMillis / 60000; + long seconds = (durationMillis % 60000) / 1000; + long millis = durationMillis % 1000; + + return String.format("%d分%d秒%d毫秒", minutes, seconds, millis); + } + private static String detectCharset (InputStream inputStream) throws IOException { byte[] buffer = new byte[4096]; int nread; @@ -364,6 +367,9 @@ public class RagHttpService { ) .build(); + // 记录开始时间 + long startTime = System.currentTimeMillis(); + // 创建请求 Request sendRequest = new Request.Builder() .url(ragEmbed) @@ -375,17 +381,15 @@ public class RagHttpService { try (Response sendResponse = client.newCall(sendRequest).execute()) { if (sendResponse.body() != null) { String body = sendResponse.body().string(); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); + // 打印响应内容 + long endTime = System.currentTimeMillis(); + long duration = endTime - startTime; + + printLogs(); + log.info("请求耗时: {}", formatDuration(duration)); log.info("!!!!!!!!!! 响应原始内容 Response: {}", body); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); - log.info("===============================响应成功==============================="); + printLogs(); + JSONObject resJson = JSONObject.parseObject(body); // 1: 先判断是否存在 detail,