From b4889fd52299f6862aa1e7044cbcb5fd3427b4f2 Mon Sep 17 00:00:00 2001 From: Liuyang <2746366019@qq.com> Date: Wed, 26 Feb 2025 17:06:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor(yudao-module-llm):=20ModelService=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=20modelCompletions=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -增加日志记录 --- .../module/llm/service/http/ModelService.java | 69 +++++++++++++------ 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java index 660b08eef..017daec5a 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java @@ -65,41 +65,68 @@ public class ModelService { * @return */ public ModelCompletionsRespVO modelCompletions (String url, ModelCompletionsReqVO req) { - if (StringUtils.isBlank(req.getModel())) { - req.setModel(DEFAULT_MODEL_ID); - } - - log.info("request: {}", req); - String result; - if (StringUtils.isBlank(url)) { - log.info("url1: {}", llmBackendProperties.getModelCompletions()); - result = HttpUtils.post(llmBackendProperties.getModelCompletions(), null, JSON.toJSONString(req)); - } else { - log.info("url2: {}", url); - result = HttpUtils.post(url, null, JSON.toJSONString(req)); - } - log.info("11 response: {}", result); - if (StringUtils.isBlank(result)) { - return null; - } try { + log.info("开始处理模型补全请求..."); + + // 检查模型是否为空,若为空则设置默认模型 + if (StringUtils.isBlank(req.getModel())) { + log.debug("模型ID为空,设置为默认模型: {}", DEFAULT_MODEL_ID); + req.setModel(DEFAULT_MODEL_ID); + } + + // 记录请求信息 + log.info("请求参数: {}", JSON.toJSONString(req)); + + // 发起 HTTP POST 请求 + String result; + if (StringUtils.isBlank(url)) { + log.info("URL为空,使用默认URL: {}", llmBackendProperties.getModelCompletions()); + result = HttpUtils.post(llmBackendProperties.getModelCompletions(), null, JSON.toJSONString(req)); + } else { + log.info("使用指定URL: {}", url); + result = HttpUtils.post(url, null, JSON.toJSONString(req)); + } + + log.info("HTTP 请求完成。响应内容: {}", result); + + // 检查响应是否为空 + if (StringUtils.isBlank(result)) { + log.warn("响应内容为空,返回 null"); + return null; + } + + // 解析响应内容 + log.info("正在解析响应内容..."); ChatCompletion chatCompletion = JSON.parseObject(result, ChatCompletion.class); + + // 检查响应内容是否包含错误信息 if (StringUtils.isBlank(chatCompletion.getDetail())) { + log.info("响应内容无错误信息,提取回答内容..."); + + // 提取回答内容 String respContent = chatCompletion.getChoices().get(0).getMessage().getContent(); String patternString = "(.*?)"; Pattern pattern = Pattern.compile(patternString, Pattern.DOTALL); Matcher matcher = pattern.matcher(respContent); String answerContent = matcher.replaceAll(""); - // 没有detail,就是没有错误 + + // 构建返回对象 ModelCompletionsRespVO respVO = new ModelCompletionsRespVO(); respVO.setSystem("助手"); respVO.setQuestion(req.getMessages().get(req.getMessages().size() - 1).getContent()); respVO.setAnswer(answerContent); + + log.info("模型补全请求处理成功。返回结果: {}", JSON.toJSONString(respVO)); return respVO; + + } else { + log.warn("响应内容包含错误信息,返回 null"); + return null; } - return null; + } catch (Exception e) { - throw new RuntimeException(e); + log.error("处理模型补全请求时发生异常。", e); + throw new RuntimeException("模型补全请求处理失败", e); } } @@ -144,7 +171,7 @@ public class ModelService { ServerNameDO server = getServerByType(gpuType); String baseUrl = server.getHost(); if (baseUrl == null || baseUrl.trim().isEmpty()) { - log.warn("GPU: Type: {} , Name: {} , Host: {}", gpuType, server.getCardServerName(), baseUrl); + log.info("GPU: Type: {} , Name: {} , Host: {}", gpuType, server.getCardServerName(), baseUrl); return Collections.emptyList(); }