| | |
| | | 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 { |
| | | public static final String ACCESS_KEY_ID = "LTAI5tCyQRwhZ2eimxCFKbdq"; |
| | |
| | | // 调用添加分类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; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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); |
| | |
| | | } |
| | | } |
| | | |
| | | 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(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 向一个文档类知识库追加导入已解析的文件 |
| | | * |