From dcfbf44fcde3b8f4551de9ed45bb9ab8ffb6c6db Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 30 Sep 2025 13:37:56 +0800
Subject: [PATCH] feat(ai): 更新知识文件时自动设置更新时间
---
src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java | 251 +++++++++++++++++++++++---------------------------
1 files changed, 115 insertions(+), 136 deletions(-)
diff --git a/src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java b/src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java
index 0ba8f5a..ff05bbd 100644
--- a/src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java
+++ b/src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java
@@ -9,18 +9,18 @@
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
-import java.nio.file.Paths;
import java.security.MessageDigest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
-import java.util.concurrent.TimeUnit;
public class KnowledgeBaseUtil {
- private static String ACCESS_KEY_ID = "LTAI5tCyQRwhZ2eimxCFKbdq";
- private static String ACCESS_KEY_SECRET = "fs1mEwLXg2j9XuKJsFoW8ThQbJFqHl";
- private static String WORKSPACE_ID = "llm-4bcr09yfxlgz0b0t";
- private static String ENDPOINT = "bailian.cn-beijing.aliyuncs.com";
+ public static final String ACCESS_KEY_ID = "LTAI5tCyQRwhZ2eimxCFKbdq";
+ public static final String ACCESS_KEY_SECRET = "fs1mEwLXg2j9XuKJsFoW8ThQbJFqHl";
+ public static final String WORKSPACE_ID = "llm-4bcr09yfxlgz0b0t";
+ public static final String DEFAULT_CATEGORY_ID = "cate_e6a47dd4d7cd4062a452eddccac76ad6_12629554";
+ public static final String DEFAULT_KNOWLEDGE_ID = "9rxd16p56z";
+ public static final String ENDPOINT = "bailian.cn-beijing.aliyuncs.com";
/**
* <b>description</b> :
@@ -44,6 +44,9 @@
* @throws Exception 当API调用失败或其他异常情况时抛出
*/
public static String AddCategory(String categoryName, String parentCategoryId) throws Exception {
+ if (StrUtil.isBlank(parentCategoryId)){
+ parentCategoryId = "";
+ }
// 创建阿里云百炼客户端
com.aliyun.bailian20231229.Client client = KnowledgeBaseUtil.createClient();
// 构造添加分类请求参数
@@ -56,6 +59,20 @@
// 调用添加分类API接口
AddCategoryResponse addCategoryResponse = client.addCategoryWithOptions(WORKSPACE_ID, addCategoryRequest, headers, runtime);
return addCategoryResponse.getBody().getData().getCategoryId();
+ }
+
+
+ /**
+ * 永久删除应用数据中的指定文件
+ * @param fileId 文件 ID
+ * @throws Exception 当API调用失败或其他异常情况时抛出
+ */
+ public static void deleteFile(String fileId) throws Exception {
+ com.aliyun.bailian20231229.Client client = KnowledgeBaseUtil.createClient();
+ com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
+ java.util.Map<String, String> headers = new java.util.HashMap<>();
+ client.deleteFileWithOptions(fileId, WORKSPACE_ID, headers, runtime);
+ return;
}
/**
@@ -237,7 +254,7 @@
* @param indexId 知识库ID
* @return 阿里云百炼服务的响应对象
*/
- public static GetIndexJobStatusResponse getIndexJobStatus(com.aliyun.bailian20231229.Client client, String workspaceId, String jobId, String indexId) throws Exception {
+ public static GetIndexJobStatusResponse getFileJobStatus(com.aliyun.bailian20231229.Client client, String workspaceId, String jobId, String indexId) throws Exception {
Map<String, String> headers = new HashMap<>();
com.aliyun.bailian20231229.models.GetIndexJobStatusRequest getIndexJobStatusRequest = new com.aliyun.bailian20231229.models.GetIndexJobStatusRequest();
getIndexJobStatusRequest.setIndexId(indexId);
@@ -267,22 +284,18 @@
}
/**
- * 使用阿里云百炼服务创建知识库。
+ * 使用阿里云百炼服务上传应用数据。
*
* @param filePath 文件本地路径
- * @param workspaceId 业务空间ID
- * @param name 知识库名称
+ * @param categoryId 应用数据目录ID
* @return 如果成功,返回知识库ID;否则返回 null
*/
- public static String createKnowledgeBase(String filePath, String workspaceId, String name,String categoryId) {
+ public static String uploadFileToAppData(String filePath, String categoryId) {
// 设置默认值
if (StrUtil.isBlank(categoryId)){
- categoryId = "default";
+ return null;
}
String parser = "DASHSCOPE_DOCMIND";
- String sourceType = "DATA_CENTER_FILE";
- String structureType = "unstructured";
- String sinkType = "DEFAULT";
try {
// 步骤1:初始化客户端(Client)
System.out.println("步骤1:初始化Client");
@@ -296,7 +309,7 @@
// 步骤3:申请上传租约
System.out.println("步骤3:向阿里云百炼申请上传租约");
- ApplyFileUploadLeaseResponse leaseResponse = applyLease(client, categoryId, fileName, fileMd5, fileSize, workspaceId);
+ ApplyFileUploadLeaseResponse leaseResponse = applyLease(client, categoryId, fileName, fileMd5, fileSize, WORKSPACE_ID);
String leaseId = leaseResponse.getBody().getData().getFileUploadLeaseId();
String uploadUrl = leaseResponse.getBody().getData().getParam().getUrl();
Object uploadHeaders = leaseResponse.getBody().getData().getParam().getHeaders();
@@ -311,53 +324,36 @@
// 步骤5:将文件添加到服务器
System.out.println("步骤5:将文件添加到阿里云百炼服务器");
- AddFileResponse addResponse = addFile(client, leaseId, parser, categoryId, workspaceId);
- String fileId = addResponse.getBody().getData().getFileId();
+ AddFileResponse addResponse = addFile(client, leaseId, parser, categoryId, WORKSPACE_ID);
- // 步骤6:检查文件状态
- System.out.println("步骤6:检查阿里云百炼中的文件状态");
- while (true) {
- DescribeFileResponse describeResponse = describeFile(client, workspaceId, fileId);
- String status = describeResponse.getBody().getData().getStatus();
- System.out.println("当前文件状态:" + status);
+ return addResponse.getBody().getData().getFileId();
+ } catch (Exception e) {
+ System.out.println("发生错误:" + e.getMessage());
+ e.printStackTrace();
+ return null;
+ }
+ }
- if (status.equals("INIT")) {
- System.out.println("文件待解析,请稍候...");
- } else if (status.equals("PARSING")) {
- System.out.println("文件解析中,请稍候...");
- } else if (status.equals("PARSE_SUCCESS")) {
- System.out.println("文件解析完成!");
- break;
- } else {
- System.out.println("未知的文件状态:" + status + ",请联系技术支持。");
- return null;
- }
- TimeUnit.SECONDS.sleep(5);
- }
+ /**
+ * 使用阿里云百炼服务创建知识库。
+ *
+ * @param fileId 应用数据ID
+ * @param name 知识库名称
+ * @return 如果成功,返回知识库ID;否则返回 null
+ */
+ public static String createKnowledgeBase(String fileId, String name) {
+ String sourceType = "DATA_CENTER_FILE";
+ String structureType = "unstructured";
+ String sinkType = "DEFAULT";
+ try {
+ // 步骤1:初始化客户端(Client)
+ System.out.println("步骤1:初始化Client");
+ com.aliyun.bailian20231229.Client client = KnowledgeBaseUtil.createClient();
// 步骤7:初始化知识库
System.out.println("步骤7:在阿里云百炼中创建知识库");
- CreateIndexResponse indexResponse = createIndex(client, workspaceId, fileId, name, structureType, sourceType, sinkType);
+ CreateIndexResponse indexResponse = createIndex(client, WORKSPACE_ID, fileId, name, structureType, sourceType, sinkType);
String indexId = indexResponse.getBody().getData().getId();
-
- // 步骤8:提交索引任务
- System.out.println("步骤8:向阿里云百炼提交索引任务");
- SubmitIndexJobResponse submitResponse = submitIndex(client, workspaceId, indexId);
- String jobId = submitResponse.getBody().getData().getId();
-
- // 步骤9:获取索引任务状态
- System.out.println("步骤9:获取阿里云百炼索引任务状态");
- while (true) {
- GetIndexJobStatusResponse getStatusResponse = getIndexJobStatus(client, workspaceId, jobId, indexId);
- String status = getStatusResponse.getBody().getData().getStatus();
- System.out.println("当前索引任务状态:" + status);
-
- if (status.equals("COMPLETED")) {
- break;
- }
- TimeUnit.SECONDS.sleep(5);
- }
-
System.out.println("阿里云百炼知识库创建成功!");
return indexId;
@@ -373,98 +369,82 @@
/**
* 使用阿里云百炼服务更新知识库
*
- * @param filePath 文件(更新后的)的实际本地路径
- * @param workspaceId 业务空间ID
+ * @param fileId 文件(更新后的)的实际本地路径
* @param indexId 需要更新的知识库ID
* @param oldFileId 需要更新的文件的FileID
* @return 如果成功,返回知识库ID;否则返回 null
*/
- public static String updateKnowledgeBase(String filePath, String workspaceId, String indexId, String oldFileId, String categoryId) {
- // 设置默认值
- if (StrUtil.isBlank(categoryId)){
- categoryId = "default";
- }
- String parser = "DASHSCOPE_DOCMIND";
+ public static String updateKnowledgeBase(String fileId, String indexId, String oldFileId) {
String sourceType = "DATA_CENTER_FILE";
try {
// 步骤1:初始化客户端(Client)
System.out.println("步骤1:创建Client");
com.aliyun.bailian20231229.Client client = createClient();
- // 步骤2:准备文件信息(更新后的文件)
- System.out.println("步骤2:准备文件信息");
- String fileName = Paths.get(filePath).getFileName().toString();
- String fileMd5 = calculateMD5(filePath);
- String fileSize = getFileSize(filePath);
-
- // 步骤3:申请上传租约
- System.out.println("步骤3:向阿里云百炼申请上传租约");
- ApplyFileUploadLeaseResponse leaseResponse = applyLease(client, categoryId, fileName, fileMd5, fileSize, workspaceId);
- String leaseId = leaseResponse.getBody().getData().getFileUploadLeaseId();
- String uploadUrl = leaseResponse.getBody().getData().getParam().getUrl();
- Object uploadHeaders = leaseResponse.getBody().getData().getParam().getHeaders();
-
- // 步骤4:上传文件到临时存储
- System.out.println("步骤4:上传文件到临时存储");
- // 请自行安装jackson-databind
- // 将上一步的uploadHeaders转换为Map(Key-Value形式)
- ObjectMapper mapper = new ObjectMapper();
- Map<String, String> uploadHeadersMap = (Map<String, String>) mapper.readValue(mapper.writeValueAsString(uploadHeaders), Map.class);
- uploadFile(uploadUrl, uploadHeadersMap, filePath);
-
- // 步骤5:添加文件到类目中
- System.out.println("步骤5:添加文件到类目中");
- AddFileResponse addResponse = addFile(client, leaseId, parser, categoryId, workspaceId);
- String fileId = addResponse.getBody().getData().getFileId();
-
- // 步骤6:检查更新后的文件状态
- System.out.println("步骤6:检查阿里云百炼中的文件状态");
- while (true) {
- DescribeFileResponse describeResponse = describeFile(client, workspaceId, fileId);
- String status = describeResponse.getBody().getData().getStatus();
- System.out.println("当前文件状态:" + status);
- if ("INIT".equals(status)) {
- System.out.println("文件待解析,请稍候...");
- } else if ("PARSING".equals(status)) {
- System.out.println("文件解析中,请稍候...");
- } else if ("PARSE_SUCCESS".equals(status)) {
- System.out.println("文件解析完成!");
- break;
- } else {
- System.out.println("未知的文件状态:" + status + ",请联系技术支持。");
- return null;
- }
- Thread.sleep(5000);
- }
-
// 步骤7:提交追加文件任务
- System.out.println("步骤7:提交追加文件任务");
- SubmitIndexAddDocumentsJobResponse indexAddResponse = submitIndexAddDocumentsJob(client, workspaceId, indexId, fileId, sourceType);
+ System.out.println("步骤7:提交文件任务");
+ SubmitIndexAddDocumentsJobResponse indexAddResponse = submitIndexAddDocumentsJob(client, WORKSPACE_ID, indexId, fileId, sourceType);
String jobId = indexAddResponse.getBody().getData().getId();
-
- // 步骤8:等待追加任务完成
- System.out.println("步骤8:等待追加任务完成");
- while (true) {
- GetIndexJobStatusResponse jobStatusResponse = getIndexJobStatus(client, workspaceId, jobId, indexId);
- String status = jobStatusResponse.getBody().getData().getStatus();
- System.out.println("当前索引任务状态:" + status);
- if ("COMPLETED".equals(status)) {
- break;
- }
- Thread.sleep(5000);
- }
-
- // 步骤9:删除旧文件
- if(StrUtil.isNotBlank(oldFileId)){
- System.out.println("步骤9:删除旧文件");
- deleteIndexDocument(client, workspaceId, indexId, oldFileId);
- }
-
System.out.println("阿里云百炼知识库更新成功!");
- return indexId;
+ return jobId;
} catch (Exception e) {
System.out.println("发生错误:" + e.getMessage());
return null;
+ }
+ }
+
+ public static int getFileJobStatus(String fileId) {
+ int state = 0;
+ // 步骤1:初始化客户端(Client)
+ System.out.println("步骤1:创建Client");
+ com.aliyun.bailian20231229.Client client = null;
+ try {
+ client = createClient();
+ // 步骤6:检查更新后的文件状态
+ System.out.println("步骤6:检查阿里云百炼中的文件状态");
+ DescribeFileResponse describeResponse = describeFile(client, WORKSPACE_ID, fileId);
+ String status = describeResponse.getBody().getData().getStatus();
+ System.out.println("当前文件状态:" + status);
+ if ("PARSE_SUCCESS".equals(status)) {
+ state = 2;
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return state;
+ }
+
+ public static int getIndexKnowledgeJobStatus(String jobId, String indexId) {
+ int state = 0;
+ // 步骤1:初始化客户端(Client)
+ System.out.println("步骤1:创建Client");
+ com.aliyun.bailian20231229.Client client = null;
+ try {
+ client = createClient();
+ System.out.println("步骤8:等待追加任务完成");
+ GetIndexJobStatusResponse jobStatusResponse = getFileJobStatus(client, WORKSPACE_ID, jobId, indexId);
+ String status = jobStatusResponse.getBody().getData().getStatus();
+ System.out.println("当前索引任务状态:" + status);
+ if ("COMPLETED".equals(status)) {
+ state = 3;
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return state;
+ }
+
+
+ public static void knowledgeFileDelete(String fileId, String indexId) {
+ com.aliyun.bailian20231229.Client client = null;
+ try {
+ System.out.println("步骤1:创建Client");
+ client = createClient();
+ // 步骤9:删除旧文件
+ System.out.println("步骤9:删除旧文件");
+ deleteIndexDocument(client, WORKSPACE_ID, indexId, fileId);
+ } catch (Exception e) {
+ e.printStackTrace();
}
}
@@ -513,12 +493,11 @@
*/
public static void main(String[] args) {
- String filePath = "D:\\项目\\大模型\\阿里云百炼\\知识库\\薪资谈判常见100问与答.md";
+ String filePath = "updateKnowledgeBase";
String indexId = "xlmj6e7ix1";
String oldFileId = "file_e943bb6d305a49a5acb7781ca00d70dd_12629554";
- String workspaceId = WORKSPACE_ID;
- String result = updateKnowledgeBase(filePath, workspaceId, indexId, oldFileId,null);
+ String result = updateKnowledgeBase(filePath, indexId, oldFileId);
if (result != null) {
System.out.println("知识库更新成功,返回知识库ID: " + result);
} else {
--
Gitblit v1.9.1