文件上传后将fileId的UUID部分做为标签更新
This commit is contained in:
@@ -1,207 +1,215 @@
|
|||||||
//package com.gxwebsoft.ai.service.impl;
|
package com.gxwebsoft.ai.service.impl;
|
||||||
//
|
|
||||||
//import com.aliyun.bailian20231229.Client;
|
import com.aliyun.bailian20231229.Client;
|
||||||
//import com.aliyun.bailian20231229.models.CreateIndexResponse;
|
import com.aliyun.bailian20231229.models.AddFileResponse;
|
||||||
//import com.aliyun.bailian20231229.models.DeleteIndexDocumentResponse;
|
import com.aliyun.bailian20231229.models.CreateIndexResponse;
|
||||||
//import com.aliyun.bailian20231229.models.DeleteIndexResponse;
|
import com.aliyun.bailian20231229.models.DeleteIndexDocumentResponse;
|
||||||
//import com.aliyun.bailian20231229.models.ListIndexDocumentsResponse;
|
import com.aliyun.bailian20231229.models.DeleteIndexResponse;
|
||||||
//import com.aliyun.bailian20231229.models.ListIndicesResponse;
|
import com.aliyun.bailian20231229.models.ListIndexDocumentsResponse;
|
||||||
//import com.aliyun.bailian20231229.models.RetrieveResponse;
|
import com.aliyun.bailian20231229.models.ListIndicesResponse;
|
||||||
//import com.aliyun.bailian20231229.models.RetrieveResponseBody.RetrieveResponseBodyDataNodes;
|
import com.aliyun.bailian20231229.models.RetrieveResponse;
|
||||||
//import com.gxwebsoft.ai.config.KnowledgeBaseConfig;
|
import com.aliyun.bailian20231229.models.RetrieveResponseBody.RetrieveResponseBodyDataNodes;
|
||||||
//import com.gxwebsoft.ai.constants.KnowledgeBaseConstants;
|
import com.gxwebsoft.ai.config.KnowledgeBaseConfig;
|
||||||
//import com.gxwebsoft.ai.dto.KnowledgeBaseRequest;
|
import com.gxwebsoft.ai.constants.KnowledgeBaseConstants;
|
||||||
//import com.gxwebsoft.ai.factory.KnowledgeBaseClientFactory;
|
import com.gxwebsoft.ai.dto.KnowledgeBaseRequest;
|
||||||
//import com.gxwebsoft.ai.service.KnowledgeBaseService;
|
import com.gxwebsoft.ai.factory.KnowledgeBaseClientFactory;
|
||||||
//import com.gxwebsoft.ai.util.AiCloudKnowledgeBaseUtil;
|
import com.gxwebsoft.ai.service.KnowledgeBaseService;
|
||||||
//import com.gxwebsoft.ai.util.KnowledgeBaseUploader;
|
import com.gxwebsoft.ai.util.AiCloudDataCenterUtil;
|
||||||
//import com.gxwebsoft.ai.util.KnowledgeBaseUtil;
|
import com.gxwebsoft.ai.util.AiCloudKnowledgeBaseUtil;
|
||||||
//import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
//import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
//import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
//import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
//import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
//
|
|
||||||
//import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
//import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
//import java.util.Arrays;
|
import java.util.ArrayList;
|
||||||
//import java.util.HashMap;
|
import java.util.Arrays;
|
||||||
//import java.util.LinkedHashSet;
|
import java.util.HashMap;
|
||||||
//import java.util.List;
|
import java.util.LinkedHashSet;
|
||||||
//import java.util.Map;
|
import java.util.List;
|
||||||
//import java.util.Set;
|
import java.util.Map;
|
||||||
//
|
import java.util.Set;
|
||||||
//@Service
|
|
||||||
//public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
|
@Service
|
||||||
//
|
public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
|
||||||
// @Autowired
|
|
||||||
// private KnowledgeBaseConfig config;
|
@Autowired
|
||||||
//
|
private KnowledgeBaseConfig config;
|
||||||
// @Autowired
|
|
||||||
// private KnowledgeBaseClientFactory clientFactory;
|
@Autowired
|
||||||
//
|
private KnowledgeBaseClientFactory clientFactory;
|
||||||
// @Override
|
|
||||||
// public Set<String> queryKnowledgeBase(KnowledgeBaseRequest req) {
|
@Override
|
||||||
// return queryKnowledgeBase(req.getKbId(), req.getQuery(), req.getTopK(), req.getFormCommit());
|
public Set<String> queryKnowledgeBase(KnowledgeBaseRequest req) {
|
||||||
// }
|
return queryKnowledgeBase(req.getKbId(), req.getQuery(), req.getTopK(), req.getFormCommit());
|
||||||
//
|
}
|
||||||
// @Override
|
|
||||||
// public Set<String> queryKnowledgeBase(String kbId, String query, Integer topK, Integer formCommit) {
|
@Override
|
||||||
// Set<String> result = new LinkedHashSet<>();
|
public Set<String> queryKnowledgeBase(String kbId, String query, Integer topK, Integer formCommit) {
|
||||||
// String workspaceId = config.getWorkspaceId();
|
Set<String> result = new LinkedHashSet<>();
|
||||||
// List<String> keyWords = Arrays.asList(KnowledgeBaseConstants.KEY_WORDS);
|
String workspaceId = config.getWorkspaceId();
|
||||||
// String indexId = kbId;
|
List<String> keyWords = Arrays.asList(KnowledgeBaseConstants.KEY_WORDS);
|
||||||
// String searchQuery = StrUtil.isEmpty(query) ? keyWords.get(formCommit) : query;
|
String indexId = kbId;
|
||||||
// Integer searchTopK = topK == null ? 10 : topK;
|
String searchQuery = StrUtil.isEmpty(query) ? keyWords.get(formCommit) : query;
|
||||||
//
|
Integer searchTopK = topK == null ? 10 : topK;
|
||||||
// try {
|
|
||||||
// Client client = clientFactory.createClient();
|
try {
|
||||||
// RetrieveResponse resp = KnowledgeBaseUtil.retrieveIndex(client, workspaceId, indexId, searchQuery);
|
Client client = clientFactory.createClient();
|
||||||
// for (RetrieveResponseBodyDataNodes node : resp.getBody().getData().getNodes()) {
|
RetrieveResponse resp = AiCloudKnowledgeBaseUtil.retrieveIndex(client, workspaceId, indexId, searchQuery);
|
||||||
// result.add(node.getText());
|
for (RetrieveResponseBodyDataNodes node : resp.getBody().getData().getNodes()) {
|
||||||
// if (result.size() >= searchTopK) {
|
result.add(node.getText());
|
||||||
// break;
|
if (result.size() >= searchTopK) {
|
||||||
// }
|
break;
|
||||||
// }
|
}
|
||||||
// } catch (Exception e) {
|
}
|
||||||
// throw new RuntimeException("查询知识库失败: " + e.getMessage(), e);
|
} catch (Exception e) {
|
||||||
// }
|
throw new RuntimeException("查询知识库失败: " + e.getMessage(), e);
|
||||||
// return result;
|
}
|
||||||
// }
|
return result;
|
||||||
//
|
}
|
||||||
// @Override
|
|
||||||
// public String createKnowledgeBase(String companyName, String companyCode) {
|
@Override
|
||||||
// String workspaceId = config.getWorkspaceId();
|
public String createKnowledgeBase(String companyName, String companyCode) {
|
||||||
// try {
|
String workspaceId = config.getWorkspaceId();
|
||||||
// String kbId = getKnowledgeBaseIdByName(companyCode);
|
try {
|
||||||
// if(StrUtil.isNotEmpty(kbId)) {
|
String kbId = getKnowledgeBaseIdByName(companyCode);
|
||||||
// return kbId;
|
if(StrUtil.isNotEmpty(kbId)) {
|
||||||
// }
|
return kbId;
|
||||||
//
|
}
|
||||||
// Client client = clientFactory.createClient();
|
|
||||||
// CreateIndexResponse indexResponse = KnowledgeBaseUtil.createIndex(client, workspaceId, companyCode, companyName);
|
Client client = clientFactory.createClient();
|
||||||
// return indexResponse.getBody().getData().getId();
|
CreateIndexResponse indexResponse = AiCloudKnowledgeBaseUtil.createIndex(client, workspaceId, companyCode, companyName);
|
||||||
// } catch (Exception e) {
|
return indexResponse.getBody().getData().getId();
|
||||||
// throw new RuntimeException("创建知识库失败: " + e.getMessage(), e);
|
} catch (Exception e) {
|
||||||
// }
|
throw new RuntimeException("创建知识库失败: " + e.getMessage(), e);
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// @Override
|
|
||||||
// public String createKnowledgeBaseTemp() {
|
@Override
|
||||||
// String code = "Temp_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMddHHmmssSSS"));
|
public String createKnowledgeBaseTemp() {
|
||||||
// return createKnowledgeBase(code, code);
|
String code = "Temp_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMddHHmmssSSS"));
|
||||||
// }
|
return createKnowledgeBase(code, code);
|
||||||
//
|
}
|
||||||
// @Override
|
|
||||||
// public boolean existsKnowledgeBase(String companyCode) {
|
@Override
|
||||||
// String workspaceId = config.getWorkspaceId();
|
public boolean existsKnowledgeBase(String companyCode) {
|
||||||
// try {
|
String workspaceId = config.getWorkspaceId();
|
||||||
// Client client = clientFactory.createClient();
|
try {
|
||||||
// ListIndicesResponse indicesResponse = KnowledgeBaseUtil.listIndices(client, workspaceId);
|
Client client = clientFactory.createClient();
|
||||||
//
|
ListIndicesResponse indicesResponse = AiCloudKnowledgeBaseUtil.listIndices(client, workspaceId);
|
||||||
// return indicesResponse.getBody().getData().getIndices().stream()
|
|
||||||
// .anyMatch(index -> companyCode.equals(index.getName()));
|
return indicesResponse.getBody().getData().getIndices().stream()
|
||||||
// } catch (Exception e) {
|
.anyMatch(index -> companyCode.equals(index.getName()));
|
||||||
// throw new RuntimeException("检查知识库是否存在失败: " + e.getMessage(), e);
|
} catch (Exception e) {
|
||||||
// }
|
throw new RuntimeException("检查知识库是否存在失败: " + e.getMessage(), e);
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// @Override
|
|
||||||
// public String getKnowledgeBaseIdByName(String companyCode) {
|
@Override
|
||||||
// String workspaceId = config.getWorkspaceId();
|
public String getKnowledgeBaseIdByName(String companyCode) {
|
||||||
// try {
|
String workspaceId = config.getWorkspaceId();
|
||||||
// Client client = clientFactory.createClient();
|
try {
|
||||||
// ListIndicesResponse indicesResponse = KnowledgeBaseUtil.listIndices(client, workspaceId);
|
Client client = clientFactory.createClient();
|
||||||
//
|
ListIndicesResponse indicesResponse = AiCloudKnowledgeBaseUtil.listIndices(client, workspaceId);
|
||||||
// return indicesResponse.getBody().getData().getIndices().stream()
|
|
||||||
// .filter(index -> companyCode.equals(index.getName()))
|
return indicesResponse.getBody().getData().getIndices().stream()
|
||||||
// .findFirst()
|
.filter(index -> companyCode.equals(index.getName()))
|
||||||
// .map(index -> index.getId())
|
.findFirst()
|
||||||
// .orElse("");
|
.map(index -> index.getId())
|
||||||
// } catch (Exception e) {
|
.orElse("");
|
||||||
// throw new RuntimeException("查找知识库ID失败: " + e.getMessage(), e);
|
} catch (Exception e) {
|
||||||
// }
|
throw new RuntimeException("查找知识库ID失败: " + e.getMessage(), e);
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// @Override
|
|
||||||
// public Map<String,Object> listDocuments(String kbId, Integer pageSize, Integer pageNumber) {
|
@Override
|
||||||
// Map<String,Object> ret = new HashMap<>();
|
public Map<String,Object> listDocuments(String kbId, Integer pageSize, Integer pageNumber) {
|
||||||
// String workspaceId = config.getWorkspaceId();
|
Map<String,Object> ret = new HashMap<>();
|
||||||
// try {
|
String workspaceId = config.getWorkspaceId();
|
||||||
// Client client = clientFactory.createClient();
|
try {
|
||||||
// ListIndexDocumentsResponse indexDocumentsResponse = KnowledgeBaseUtil.listIndexDocuments(client, workspaceId, kbId, pageSize, pageNumber);
|
Client client = clientFactory.createClient();
|
||||||
// ret.put("data", indexDocumentsResponse.getBody().getData().getDocuments());
|
ListIndexDocumentsResponse indexDocumentsResponse = AiCloudKnowledgeBaseUtil.listIndexDocuments(client, workspaceId, kbId, pageSize, pageNumber);
|
||||||
// ret.put("total", indexDocumentsResponse.getBody().getData().getTotalCount());
|
ret.put("data", indexDocumentsResponse.getBody().getData().getDocuments());
|
||||||
// } catch (Exception e) {
|
ret.put("total", indexDocumentsResponse.getBody().getData().getTotalCount());
|
||||||
// throw new RuntimeException("查询知识库下的文档列表失败: " + e.getMessage(), e);
|
} catch (Exception e) {
|
||||||
// }
|
throw new RuntimeException("查询知识库下的文档列表失败: " + e.getMessage(), e);
|
||||||
// return ret;
|
}
|
||||||
// }
|
return ret;
|
||||||
//
|
}
|
||||||
// @Override
|
|
||||||
// public boolean deleteIndex(String kbId) {
|
@Override
|
||||||
// String workspaceId = config.getWorkspaceId();
|
public boolean deleteIndex(String kbId) {
|
||||||
// try {
|
String workspaceId = config.getWorkspaceId();
|
||||||
// Client client = clientFactory.createClient();
|
try {
|
||||||
// DeleteIndexResponse indexDocumentResponse = KnowledgeBaseUtil.deleteIndex(client, workspaceId, kbId);
|
Client client = clientFactory.createClient();
|
||||||
// return indexDocumentResponse.getBody().getSuccess();
|
DeleteIndexResponse indexDocumentResponse = AiCloudKnowledgeBaseUtil.deleteIndex(client, workspaceId, kbId);
|
||||||
// } catch (Exception e) {
|
return indexDocumentResponse.getBody().getSuccess();
|
||||||
// throw new RuntimeException("删除知识库失败: " + e.getMessage(), e);
|
} catch (Exception e) {
|
||||||
// }
|
throw new RuntimeException("删除知识库失败: " + e.getMessage(), e);
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// @Override
|
|
||||||
// public boolean deleteIndexDocument(String kbId, String fileIds) {
|
@Override
|
||||||
// String workspaceId = config.getWorkspaceId();
|
public boolean deleteIndexDocument(String kbId, String fileIds) {
|
||||||
// List<String> ids = StrUtil.splitTrim(fileIds, ",");
|
String workspaceId = config.getWorkspaceId();
|
||||||
// try {
|
List<String> ids = StrUtil.splitTrim(fileIds, ",");
|
||||||
// Client client = clientFactory.createClient();
|
try {
|
||||||
// DeleteIndexDocumentResponse indexDocumentResponse = KnowledgeBaseUtil.deleteIndexDocument(client, workspaceId, kbId, ids);
|
Client client = clientFactory.createClient();
|
||||||
// return indexDocumentResponse.getBody().getSuccess();
|
DeleteIndexDocumentResponse indexDocumentResponse = AiCloudKnowledgeBaseUtil.deleteIndexDocument(client, workspaceId, kbId, ids);
|
||||||
// } catch (Exception e) {
|
return indexDocumentResponse.getBody().getSuccess();
|
||||||
// throw new RuntimeException("删除知识库下的文档失败: " + e.getMessage(), e);
|
} catch (Exception e) {
|
||||||
// }
|
throw new RuntimeException("删除知识库下的文档失败: " + e.getMessage(), e);
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// @Override
|
|
||||||
// public boolean uploadDocuments(String kbId, MultipartFile[] files) {
|
@Override
|
||||||
// String workspaceId = config.getWorkspaceId();
|
public boolean uploadDocuments(String kbId, MultipartFile[] files) {
|
||||||
// int count = files.length;
|
String workspaceId = config.getWorkspaceId();
|
||||||
// try {
|
int count = files.length;
|
||||||
// Client client = clientFactory.createClient();
|
try {
|
||||||
// List<String> fileIds = KnowledgeBaseUploader.uploadDocuments(client, workspaceId, kbId, files);
|
Client client = clientFactory.createClient();
|
||||||
// //上传切片完成后删除原文档(释放云空间)
|
|
||||||
// for(String fileId : fileIds) {
|
List<String> fileIds = new ArrayList<>();
|
||||||
// KnowledgeBaseUtil.deleteAppDocument(client, workspaceId, fileId);
|
for(MultipartFile file : files) {
|
||||||
// }
|
AddFileResponse addFileResponse = AiCloudDataCenterUtil.uploadFile(client, workspaceId, "", file);
|
||||||
// return !fileIds.isEmpty() && fileIds.size() == count;
|
String fileId = addFileResponse.getBody().getData().getFileId();
|
||||||
// } catch (Exception e) {
|
fileIds.add(fileId);
|
||||||
// throw new RuntimeException("上传文档到知识库失败: " + e.getMessage(), e);
|
}
|
||||||
// }
|
// List<String> fileIds = AiCloudKnowledgeBaseUtil.uploadDocuments(client, workspaceId, kbId, files);
|
||||||
// }
|
//上传切片完成后删除原文档(释放云空间)
|
||||||
//
|
for(String fileId : fileIds) {
|
||||||
// @Async
|
AiCloudKnowledgeBaseUtil.deleteAppDocument(client, workspaceId, fileId);
|
||||||
// @Override
|
}
|
||||||
// public void submitDocuments(String kbId, String fileId) {
|
return !fileIds.isEmpty() && fileIds.size() == count;
|
||||||
// String workspaceId = config.getWorkspaceId();
|
} catch (Exception e) {
|
||||||
// try {
|
throw new RuntimeException("上传文档到知识库失败: " + e.getMessage(), e);
|
||||||
// Client client = clientFactory.createClient();
|
}
|
||||||
// AiCloudKnowledgeBaseUtil.submitIndexAddDocumentsJob(client, workspaceId, kbId, fileId);
|
}
|
||||||
// } catch (Exception e) {
|
|
||||||
// throw new RuntimeException("添加文档到知识库失败: " + e.getMessage(), e);
|
@Async
|
||||||
// }
|
@Override
|
||||||
// }
|
public void submitDocuments(String kbId, String fileId) {
|
||||||
//
|
String workspaceId = config.getWorkspaceId();
|
||||||
// @Override
|
try {
|
||||||
// public void submitDocuments(String kbId, List<String> fileIds) {
|
Client client = clientFactory.createClient();
|
||||||
// String workspaceId = config.getWorkspaceId();
|
AiCloudKnowledgeBaseUtil.submitIndexAddDocumentsJob(client, workspaceId, kbId, fileId);
|
||||||
// try {
|
} catch (Exception e) {
|
||||||
// Client client = clientFactory.createClient();
|
throw new RuntimeException("添加文档到知识库失败: " + e.getMessage(), e);
|
||||||
// AiCloudKnowledgeBaseUtil.submitIndexAddDocumentsJob(client, workspaceId, kbId, fileIds);
|
}
|
||||||
// } catch (Exception e) {
|
}
|
||||||
// throw new RuntimeException("添加文档到知识库失败: " + e.getMessage(), e);
|
|
||||||
// }
|
@Override
|
||||||
// }
|
public void submitDocuments(String kbId, List<String> fileIds) {
|
||||||
//
|
String workspaceId = config.getWorkspaceId();
|
||||||
//
|
try {
|
||||||
//}
|
Client client = clientFactory.createClient();
|
||||||
|
AiCloudKnowledgeBaseUtil.submitIndexAddDocumentsJob(client, workspaceId, kbId, fileIds);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("添加文档到知识库失败: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ import java.io.InputStream;
|
|||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.util.ArrayList;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -261,6 +261,11 @@ public class AiCloudDataCenterUtil {
|
|||||||
|
|
||||||
String fileId = addFileResponse.getBody().getData().getFileId();
|
String fileId = addFileResponse.getBody().getData().getFileId();
|
||||||
waitForFileParsing(client, workspaceId, fileId);
|
waitForFileParsing(client, workspaceId, fileId);
|
||||||
|
|
||||||
|
// fileId的UUID部分做为标签更新 file_df12ed21b7384353bd75868444c516ae_10377381 -> df12ed21b7384353bd75868444c516ae
|
||||||
|
String tag = StrUtil.subBetween(fileId, "_", "_").substring(0, 32);
|
||||||
|
updateFileTag(client, workspaceId, fileId, Arrays.asList(tag));
|
||||||
|
|
||||||
return addFileResponse;
|
return addFileResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ public class AiCloudKnowledgeBaseUtil {
|
|||||||
searchFiltersTags.put("tags", JSON.toJSONString(filesIds));
|
searchFiltersTags.put("tags", JSON.toJSONString(filesIds));
|
||||||
searchFilters.add(searchFiltersTags);
|
searchFilters.add(searchFiltersTags);
|
||||||
retrieveRequest.setSearchFilters(searchFilters);
|
retrieveRequest.setSearchFilters(searchFilters);
|
||||||
|
// retrieveRequest.setRerankMinScore(null);
|
||||||
RuntimeOptions runtime = new RuntimeOptions();
|
RuntimeOptions runtime = new RuntimeOptions();
|
||||||
return client.retrieveWithOptions(workspaceId, retrieveRequest, null, runtime);
|
return client.retrieveWithOptions(workspaceId, retrieveRequest, null, runtime);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user