| | |
| | | package cc.mrbird.febs.ai.util; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.aliyun.bailian20231229.models.*; |
| | | import com.aliyun.teautil.models.RuntimeOptions; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | |
| | | 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 ENDPOINT = "bailian.cn-beijing.aliyuncs.com"; |
| | | |
| | | /** |
| | | * <b>description</b> : |
| | |
| | | * @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(); |
| | | // 构造添加分类请求参数 |
| | |
| | | * @param name 知识库名称 |
| | | * @return 如果成功,返回知识库ID;否则返回 null |
| | | */ |
| | | public static String createKnowledgeBase(String filePath, String workspaceId, String name) { |
| | | public static String createKnowledgeBase(String filePath, String workspaceId, String name,String categoryId) { |
| | | // 设置默认值 |
| | | String categoryId = "default"; |
| | | if (StrUtil.isBlank(categoryId)){ |
| | | categoryId = "default"; |
| | | } |
| | | String parser = "DASHSCOPE_DOCMIND"; |
| | | String sourceType = "DATA_CENTER_FILE"; |
| | | String structureType = "unstructured"; |
| | |
| | | * @param oldFileId 需要更新的文件的FileID |
| | | * @return 如果成功,返回知识库ID;否则返回 null |
| | | */ |
| | | public static String updateKnowledgeBase(String filePath, String workspaceId, String indexId, String oldFileId) { |
| | | public static String updateKnowledgeBase(String filePath, String workspaceId, String indexId, String oldFileId, String categoryId) { |
| | | // 设置默认值 |
| | | String categoryId = "default"; |
| | | if (StrUtil.isBlank(categoryId)){ |
| | | categoryId = "default"; |
| | | } |
| | | String parser = "DASHSCOPE_DOCMIND"; |
| | | String sourceType = "DATA_CENTER_FILE"; |
| | | try { |
| | |
| | | } |
| | | |
| | | // 步骤9:删除旧文件 |
| | | System.out.println("步骤9:删除旧文件"); |
| | | deleteIndexDocument(client, workspaceId, indexId, oldFileId); |
| | | if(StrUtil.isNotBlank(oldFileId)){ |
| | | System.out.println("步骤9:删除旧文件"); |
| | | deleteIndexDocument(client, workspaceId, indexId, oldFileId); |
| | | } |
| | | |
| | | System.out.println("阿里云百炼知识库更新成功!"); |
| | | return indexId; |
| | |
| | | public static void main(String[] args) { |
| | | |
| | | String filePath = "D:\\项目\\大模型\\阿里云百炼\\知识库\\薪资谈判常见100问与答.md"; |
| | | String indexId = "xlmj6e7ix1"; // 即 AddFile 接口返回的 FileId。您也可以在阿里云百炼控制台的应用数据页面,单击文件名称旁的 ID 图标获取。 |
| | | String oldFileId = "file_5c9f7e4e0f3e4b4ea2bd208a1b4f5e6f_12629554"; |
| | | String indexId = "xlmj6e7ix1"; |
| | | String oldFileId = "file_e943bb6d305a49a5acb7781ca00d70dd_12629554"; |
| | | |
| | | String workspaceId = WORKSPACE_ID; |
| | | String result = updateKnowledgeBase(filePath, workspaceId, indexId, oldFileId); |
| | | String result = updateKnowledgeBase(filePath, workspaceId, indexId, oldFileId,null); |
| | | if (result != null) { |
| | | System.out.println("知识库更新成功,返回知识库ID: " + result); |
| | | } else { |