| | |
| | | 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"; |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 使用阿里云百炼服务创建知识库。 |
| | | * 使用阿里云百炼服务上传应用数据。 |
| | | * |
| | | * @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"); |
| | |
| | | |
| | | // 步骤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(); |
| | |
| | | |
| | | // 步骤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); |
| | | |
| | | 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 addResponse.getBody().getData().getFileId(); |
| | | } catch (Exception e) { |
| | | System.out.println("发生错误:" + e.getMessage()); |
| | | e.printStackTrace(); |
| | | 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; |
| | | |
| | |
| | | /** |
| | | * 使用阿里云百炼服务更新知识库 |
| | | * |
| | | * @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 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 { |