refactor(yudao-module-llm): 重构模型下载列表功能
- 增加参数校验,提高代码健壮性 - 优化日志记录,增加请求和响应的详细日志 - 改进异常处理,确保在请求失败时返回空列表 - 重构方法结构,提高代码可读性和维护性
This commit is contained in:
parent
da2d548009
commit
6b6b9f8b9d
@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.llm.service.http;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.llm.dal.dataobject.servername.ServerNameDO;
|
||||
import cn.iocoder.yudao.module.llm.framework.backend.config.LLMBackendProperties;
|
||||
import cn.iocoder.yudao.module.llm.service.http.vo.*;
|
||||
import cn.iocoder.yudao.module.llm.service.servername.ServerNameService;
|
||||
@ -16,9 +17,7 @@ import javax.annotation.Resource;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@ -108,21 +107,63 @@ public class ModelService {
|
||||
return serverNameService.getServerName(gpuType).getHost();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 GPU 类型获取对应的主机
|
||||
* <p>ModelService GpuType 是 ServerName 表ID
|
||||
*
|
||||
* @param gpuType gpuType
|
||||
* @return host
|
||||
*/
|
||||
public ServerNameDO getServerByType (Long gpuType) {
|
||||
return serverNameService.getServerName(gpuType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模型下载列表
|
||||
*
|
||||
* @param gpuType GPU 类型,用于确定下载地址的主机
|
||||
* @param fileName 模型名称,用于拼接下载地址
|
||||
* @return 模型下载文件列表,如果获取失败则返回空列表
|
||||
*/
|
||||
public List<String> getFileList (Long gpuType, String fileName) {
|
||||
String baseUrl = getHostByType(gpuType);
|
||||
String url = baseUrl + llmBackendProperties.getModelFileList() + fileName;
|
||||
|
||||
String res = HttpUtils.get(url, null);
|
||||
log.info(" getFileList:{}", res);
|
||||
try {
|
||||
return JSONArray.parseArray(res, String.class);
|
||||
} catch (Exception e) {
|
||||
log.info("error getFileList:{}", e.getMessage());
|
||||
// 1. 参数是否为空
|
||||
if (gpuType == null || fileName == null || fileName.trim().isEmpty()) {
|
||||
log.warn("参数: gpuType={}, fileName={}", gpuType, fileName);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// 2. 根据 GPU 类型获取主机地址
|
||||
ServerNameDO server = getServerByType(gpuType);
|
||||
String baseUrl = server.getHost();
|
||||
if (baseUrl == null || baseUrl.trim().isEmpty()) {
|
||||
log.warn("GPU: Type: {} , Name: {} , Host: {}", gpuType, server.getCardServerName(), baseUrl);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// 3. 拼接完整的请求 URL
|
||||
String url = baseUrl + llmBackendProperties.getModelFileList() + fileName;
|
||||
log.info("Request URL: {}", url);
|
||||
|
||||
// 4. 发起 HTTP GET 请求
|
||||
String response = null;
|
||||
try {
|
||||
response = HttpUtils.get(url, null);
|
||||
log.info("Response from server: {}", response);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to send HTTP request to URL: {}, error: {}", url, e.getMessage());
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// 5. 解析响应数据
|
||||
try {
|
||||
return Optional.ofNullable(response)
|
||||
.map(res -> JSONArray.parseArray(res, String.class))
|
||||
.orElse(Collections.emptyList());
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to parse response JSON: {}, error: {}", response, e.getMessage());
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ModelCompletionsRespVO modelPrivateCompletions (Map<String, String> headers, ModelCompletionsReqVO req) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user