Administrator
20 hours ago 26472c5a333f7ee98d9366aa2344e15c9234cc67
feat(ai): 增加知识库创建与更新时的分类ID参数支持

- 在 createKnowledgeBase 方法中新增 categoryId 参数,并设置默认值逻辑
- 在 updateKnowledgeBase 方法中新增 categoryId 参数,并设置默认值逻辑
- 优化删除旧文件逻辑,避免空文件ID导致异常
- 更新主方法测试用例中的调用参数,适配新接口签名
1 files modified
21 ■■■■■ changed files
src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java 21 ●●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/ai/util/KnowledgeBaseUtil.java
@@ -1,5 +1,6 @@
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;
@@ -273,9 +274,11 @@
     * @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";
@@ -376,9 +379,11 @@
     * @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 {
@@ -450,8 +455,10 @@
            }
            // 步骤9:删除旧文件
            if(StrUtil.isNotBlank(oldFileId)){
            System.out.println("步骤9:删除旧文件");
            deleteIndexDocument(client, workspaceId, indexId, oldFileId);
            }
            System.out.println("阿里云百炼知识库更新成功!");
            return indexId;
@@ -507,11 +514,11 @@
    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 {