refactor(yudao-module-llm): ModelService 中的 modelCompletions 方法

-增加日志记录
This commit is contained in:
Liuyang 2025-02-26 17:06:34 +08:00
parent acc34b7a1a
commit b4889fd522

View File

@ -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 = "(<think>.*?</think>)";
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();
}