refactor(module-llm):重构文件上传逻辑

- 移除了不必要的重试机制
-简化了上传流程,提高了代码可读性
- 优化了异常处理,确保上传失败时能够正确抛出异常
This commit is contained in:
Liuyang 2025-02-12 17:34:40 +08:00
parent 2f2e1257cc
commit 1de6a87303

View File

@ -126,49 +126,38 @@ public class RagHttpService {
String fileContent = fileContentBuilder.toString();
byte[] utf8Bytes = fileContent.getBytes(StandardCharsets.UTF_8);
int retryCount = 0;
boolean uploadSuccess = false;
while (retryCount < MAX_RETRIES && !uploadSuccess) {
try {
// 配置 Unirest
Unirest.config().reset();
Unirest.config().socketTimeout(86400000);
try {
// 配置 Unirest
Unirest.config().reset();
Unirest.config().socketTimeout(86400000);
// 发送上传请求
HttpResponse<String> uploadResponse = Unirest.post(ragUploadReqVO.getUrl())
.field("file_id", ragUploadReqVO.getFileId())
.field("file", new ByteArrayInputStream(utf8Bytes), ragUploadReqVO.getFileName())
.asString();
// 发送上传请求
HttpResponse<String> uploadResponse = Unirest.post(ragUploadReqVO.getUrl())
.field("file_id", ragUploadReqVO.getFileId())
.field("file", new ByteArrayInputStream(utf8Bytes), ragUploadReqVO.getFileName())
.asString();
// 检查响应状态
// 检查响应状态
log.info("Response Body: {}", uploadResponse.getBody());
ragEmbedRespVO = JSON.parseObject(uploadResponse.getBody(), RagEmbedRespVO.class);
log.info("ragEmbedRespVO:{}", ragEmbedRespVO);
log.info("Response Body: {}", uploadResponse.getBody());
ragEmbedRespVO = JSON.parseObject(uploadResponse.getBody(), RagEmbedRespVO.class);
log.info("ragEmbedRespVO:{}", ragEmbedRespVO);
if (ragEmbedRespVO.isStatus()) {
// 修改状态为 上传成功
updateFileState(documents, KnowledgeStatusEnum.UPLOAD_SUCCESS);
} else {
// 修改状态为 上传失败
updateFileState(documents, KnowledgeStatusEnum.UPLOAD_FAILED);
throw new RuntimeException("文件上传失败:" + ragEmbedRespVO.getMessage());
}
} catch (UnirestException e) {
if (isSocketClosedException(e)) {
log.warn("Socket 连接已关闭,尝试重新上传...");
retryCount++;
} else {
throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
}
if (ragEmbedRespVO.isStatus()) {
// 修改状态为 上传成功
updateFileState(documents, KnowledgeStatusEnum.UPLOAD_SUCCESS);
} else {
// 修改状态为 上传失败
updateFileState(documents, KnowledgeStatusEnum.UPLOAD_FAILED);
throw new RuntimeException("文件上传失败:" + ragEmbedRespVO.getMessage());
}
} catch (UnirestException e) {
throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
}
if (!uploadSuccess) {
throw new RuntimeException("文件上传失败,已达到最大重试次数: " + MAX_RETRIES);
}
}
}
}
@ -298,39 +287,28 @@ public class RagHttpService {
Unirest.config().socketTimeout(86400000);
// 发送 POST 请求
for (int retryCount = 0; retryCount < MAX_RETRIES; retryCount++) {
try {
HttpResponse<String> response = Unirest.post(ragEmbed)
.header(CONTENT_TYPE_HEADER, WORD_DOCUMENT_CONTENT_TYPE)
.field("file_id", fileId)
.field("file", reqVO.getFileInputStream(), fileName)
.asString();
try {
HttpResponse<String> response = Unirest.post(ragEmbed)
.header(CONTENT_TYPE_HEADER, WORD_DOCUMENT_CONTENT_TYPE)
.field("file_id", fileId)
.field("file", reqVO.getFileInputStream(), fileName)
.asString();
// 检查 HTTP 状态码
if (response.getStatus() != HttpStatus.SC_OK) {
log.error("HTTP请求失败状态码: {}", response.getStatus());
throw new RuntimeException("HTTP请求失败状态码: " + response.getStatus());
}
String responseBody = response.getBody();
log.info("响应原始内容: {}", responseBody);
processResponse(responseBody, documents);
return;
} catch (UnirestException e) {
if (isSocketClosedException(e) && retryCount < MAX_RETRIES - 1) {
log.warn("Socket连接异常尝试重试 ({}/{})", retryCount + 1, MAX_RETRIES);
resetUnirestConnection();
} else {
handleFailure(documents, FILE_UPLOAD_FAILED_MSG, e);
}
} catch (Exception e) {
handleFailure(documents, FILE_UPLOAD_FAILED_MSG, e);
// 检查 HTTP 状态码
if (response.getStatus() != HttpStatus.SC_OK) {
log.error("HTTP请求失败状态码: {}", response.getStatus());
throw new RuntimeException("HTTP请求失败状态码: " + response.getStatus());
}
String responseBody = response.getBody();
log.info("响应原始内容: {}", responseBody);
processResponse(responseBody, documents);
} catch (Exception e) {
handleFailure(documents, FILE_UPLOAD_FAILED_MSG, e);
}
throw new RuntimeException("文件上传失败,已达到最大重试次数: " + MAX_RETRIES);
}
/**