refactor(module-llm):重构知识库向量嵌入功能并添加日志记录

-移除了 try-catch 块,简化了错误处理逻辑
- 在 KnowledgeRagEmbedReqVO 中添加了 fileBytes 字段
- 引入了 okhttp3 和 ok2curl 依赖,用于生成 curl 命令
- 在 RagHttpService 中添加了详细的日志记录,包括请求参数、生成的 curl 命令和响应内容
- 优化了代码结构,提高了可读性和可维护性
This commit is contained in:
Liuyang 2025-02-20 14:02:56 +08:00
parent 05841ce69c
commit c7e4e71e48
4 changed files with 59 additions and 24 deletions

View File

@ -97,6 +97,16 @@
<version>portable-1.3.4</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>com.github.mrmike</groupId>
<artifactId>ok2curl</artifactId>
<version>0.4.5</version>
</dependency>
</dependencies>
</project>

View File

@ -103,25 +103,20 @@ public class AsyncKnowledgeBase {
*/
public void knowledgeEmbed (KnowledgeDocumentsDO knowledge, Long id) {
try {
// TODO:本地调试时打开
// String tmpUrl = "http://xhllm.xinnuojinzhi.com/admin-api/infra/file/29/get/ca3d06d24f80c127ec0300408a035176f5e0bf90ce319fda17018303226e2298.doc";
// log.info("knowledge url {}", tmpUrl);
// knowledge.setFileUrl(tmpUrl);
// TODO:本地调试时打开
// String tmpUrl = "http://xhllm.xinnuojinzhi.com/admin-api/infra/file/29/get/ca3d06d24f80c127ec0300408a035176f5e0bf90ce319fda17018303226e2298.doc";
// log.info("knowledge url {}", tmpUrl);
// knowledge.setFileUrl(tmpUrl);
// 创建知识向量
KnowledgeRagEmbedReqVO ragEmbedReqVo = new KnowledgeRagEmbedReqVO()
.setFileId(String.valueOf(knowledge.getId()))
.setFileName(knowledge.getDocumentName())
.setFileInputStream(new ByteArrayInputStream(Objects.requireNonNull(getFileByte(knowledge.getFileUrl()))))
.setFileBytes(getFileByte(knowledge.getFileUrl()));
// 创建知识向量
KnowledgeRagEmbedReqVO ragEmbedReqVo = new KnowledgeRagEmbedReqVO()
.setFileId(String.valueOf(knowledge.getId()))
.setFileName(knowledge.getDocumentName())
.setFileInputStream(new ByteArrayInputStream(Objects.requireNonNull(getFileByte(knowledge.getFileUrl()))));
ragHttpService.knowledgeEmbed(ragEmbedReqVo, id);
ragHttpService.knowledgeEmbed(ragEmbedReqVo, id);
} catch (Exception e) {
log.error("the creation of the knowledge base error : {}", e.getMessage());
throw e;
}
}
/**

View File

@ -21,8 +21,9 @@ import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
@ -152,19 +153,34 @@ public class RagHttpService {
inputStream.close(); // 关闭InputStream
outputStream.close(); // 关闭ByteArrayOutputStream
String ragEmbed = ragUploadReqVO.getUrl();
log.info("知识库向量嵌入接口URL: {}", ragEmbed);
// 打印请求参数
log.info("请求参数 - file_id: {}", ragUploadReqVO.getFileId());
log.info("请求参数 - file: {}", ragUploadReqVO.getFileName());
// 生成并打印 curl 命令
String curlCommand = String.format("curl -X POST -F \"file_id=%s\" -F \"file=@%s\" \"%s\"", ragUploadReqVO.getFileId(), ragUploadReqVO.getFileName(), ragEmbed);
log.info("生成的 curl 命令: {}", curlCommand);
String body = HttpRequest.post(ragUploadReqVO.getUrl())
.form("file", result, ragUploadReqVO.getFileName())
.form("file_id", ragUploadReqVO.getFileId())
.execute().body();
// // 发送上传请求
// HttpResponse<String> uploadResponse = Unirest.post(ragUploadReqVO.getUrl())
// .field("file_id", ragUploadReqVO.getFileId())
// .field("file", new ByteArrayInputStream(utf8Bytes), ragUploadReqVO.getFileName())
// .asString();
// 打印响应内容
log.info("响应原始内容: {}", body);
// // 发送上传请求
// 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());
// log.info("Response Body: {}", uploadResponse.getBody());
ragEmbedRespVO = JSON.parseObject(body, RagEmbedRespVO.class);
log.info("ragEmbedRespVO:{}", ragEmbedRespVO);
@ -340,7 +356,7 @@ public class RagHttpService {
if (parseObject.containsKey("status") && parseObject.getBoolean("status")) {
// 修改状态为 上传成功
updateFileState(documents, KnowledgeStatusEnum.UPLOAD_SUCCESS);
} else {
} else {
String errorMsg = parseObject.getString("error");
updateFileState(documents, KnowledgeStatusEnum.UPLOAD_FAILED);
throw exception(new ErrorCode(10047, errorMsg));
@ -360,6 +376,15 @@ public class RagHttpService {
throw new RuntimeException(errorMsg);
}
/**
* 处理失败逻辑
*/
private void handleFailure (KnowledgeDocumentsDO documents, String errorMsg) {
updateFileState(documents, KnowledgeStatusEnum.UPLOAD_FAILED);
log.error("Exception {} : {}", FILE_UPLOAD_FAILED_MSG, errorMsg);
throw new RuntimeException(errorMsg);
}
/**
* 重置 Unirest 连接
*/

View File

@ -31,4 +31,9 @@ public class KnowledgeRagEmbedReqVO {
* 文件流
*/
private ByteArrayInputStream fileInputStream;
/**
* 文件字节数组
*/
private byte[] fileBytes;
}