update:格式调整

This commit is contained in:
rainv123
2025-11-05 09:13:44 +08:00
parent 0008ca6335
commit a82cf6e300
18 changed files with 733 additions and 1003 deletions
@@ -150,12 +150,12 @@ public interface Constant {
* 火山引擎双声道语音克隆 * 火山引擎双声道语音克隆
*/ */
String VOICE_CLONE_HUOSHAN_DOUBLE_STREAM = "huoshan_double_stream"; String VOICE_CLONE_HUOSHAN_DOUBLE_STREAM = "huoshan_double_stream";
/** /**
* RAG配置类型 * RAG配置类型
*/ */
String RAG_CONFIG_TYPE = "RAG"; String RAG_CONFIG_TYPE = "RAG";
/** /**
* 默认RAG模型配置ID * 默认RAG模型配置ID
*/ */
@@ -202,13 +202,13 @@ public interface ErrorCode {
// 知识库数据集相关错误码 // 知识库数据集相关错误码
int Knowledge_Base_RECORD_NOT_EXISTS = 10163; // 知识库记录不存在 int Knowledge_Base_RECORD_NOT_EXISTS = 10163; // 知识库记录不存在
// RAG配置相关错误码 // RAG配置相关错误码
int RAG_CONFIG_NOT_FOUND = 10164; // RAG配置未找到 int RAG_CONFIG_NOT_FOUND = 10164; // RAG配置未找到
int RAG_CONFIG_TYPE_ERROR = 10165; // RAG配置类型错误 int RAG_CONFIG_TYPE_ERROR = 10165; // RAG配置类型错误
int RAG_DEFAULT_CONFIG_NOT_FOUND = 10166; // 默认RAG配置未找到 int RAG_DEFAULT_CONFIG_NOT_FOUND = 10166; // 默认RAG配置未找到
int RAG_CONFIG_MISSING_PARAMS = 10167; // RAG配置缺少必要参数 int RAG_CONFIG_MISSING_PARAMS = 10167; // RAG配置缺少必要参数
// RAG API调用相关错误码 // RAG API调用相关错误码
int RAG_API_CREATE_FAILED = 10168; // RAG API创建数据集失败 int RAG_API_CREATE_FAILED = 10168; // RAG API创建数据集失败
int RAG_API_UPDATE_FAILED = 10169; // RAG API更新数据集失败 int RAG_API_UPDATE_FAILED = 10169; // RAG API更新数据集失败
@@ -57,7 +57,7 @@ public class FieldMetaObjectHandler implements MetaObjectHandler {
@Override @Override
public void updateFill(MetaObject metaObject) { public void updateFill(MetaObject metaObject) {
Date date = new Date(); Date date = new Date();
// 更新者 // 更新者
strictUpdateFill(metaObject, UPDATER, Long.class, SecurityUser.getUserId()); strictUpdateFill(metaObject, UPDATER, Long.class, SecurityUser.getUserId());
// 更新时间 - 支持updateDate和updatedAt两种字段名 // 更新时间 - 支持updateDate和updatedAt两种字段名
@@ -378,8 +378,9 @@ public class ConfigServiceImpl implements ConfigService {
boolean isCache) { boolean isCache) {
Map<String, String> selectedModule = new HashMap<>(); Map<String, String> selectedModule = new HashMap<>();
String[] modelTypes = { "VAD", "ASR", "TTS", "Memory", "Intent", "LLM", "VLLM","RAG" }; String[] modelTypes = { "VAD", "ASR", "TTS", "Memory", "Intent", "LLM", "VLLM", "RAG" };
String[] modelIds = { vadModelId, asrModelId, ttsModelId, memModelId, intentModelId, llmModelId, vllmModelId, ragModelId }; String[] modelIds = { vadModelId, asrModelId, ttsModelId, memModelId, intentModelId, llmModelId, vllmModelId,
ragModelId };
String intentLLMModelId = null; String intentLLMModelId = null;
String memLocalShortLLMModelId = null; String memLocalShortLLMModelId = null;
@@ -25,7 +25,6 @@ import xiaozhi.modules.knowledge.dto.KnowledgeBaseDTO;
import xiaozhi.modules.knowledge.service.KnowledgeBaseService; import xiaozhi.modules.knowledge.service.KnowledgeBaseService;
import java.util.Map; import java.util.Map;
@AllArgsConstructor @AllArgsConstructor
@RestController @RestController
@RequestMapping("/api/v1") @RequestMapping("/api/v1")
@@ -47,7 +46,8 @@ public class KnowledgeBaseController {
KnowledgeBaseDTO knowledgeBaseDTO = new KnowledgeBaseDTO(); KnowledgeBaseDTO knowledgeBaseDTO = new KnowledgeBaseDTO();
knowledgeBaseDTO.setName(name); knowledgeBaseDTO.setName(name);
knowledgeBaseDTO.setDatasetId(id); knowledgeBaseDTO.setDatasetId(id);
PageData<KnowledgeBaseDTO> pageData = knowledgeBaseService.getPageList(knowledgeBaseDTO, String.valueOf(page), String.valueOf(page_size)); PageData<KnowledgeBaseDTO> pageData = knowledgeBaseService.getPageList(knowledgeBaseDTO, String.valueOf(page),
String.valueOf(page_size));
return new Result<PageData<KnowledgeBaseDTO>>().ok(pageData); return new Result<PageData<KnowledgeBaseDTO>>().ok(pageData);
} }
@@ -70,8 +70,8 @@ public class KnowledgeBaseController {
@PutMapping("/datasets/{dataset_id}") @PutMapping("/datasets/{dataset_id}")
@Operation(summary = "更新知识库") @Operation(summary = "更新知识库")
@RequiresPermissions("sys:role:normal") @RequiresPermissions("sys:role:normal")
public Result<KnowledgeBaseDTO> update(@PathVariable("dataset_id") String datasetId, public Result<KnowledgeBaseDTO> update(@PathVariable("dataset_id") String datasetId,
@RequestBody @Validated KnowledgeBaseDTO knowledgeBaseDTO) { @RequestBody @Validated KnowledgeBaseDTO knowledgeBaseDTO) {
knowledgeBaseDTO.setDatasetId(datasetId); knowledgeBaseDTO.setDatasetId(datasetId);
KnowledgeBaseDTO resp = knowledgeBaseService.update(knowledgeBaseDTO); KnowledgeBaseDTO resp = knowledgeBaseService.update(knowledgeBaseDTO);
return new Result<KnowledgeBaseDTO>().ok(resp); return new Result<KnowledgeBaseDTO>().ok(resp);
@@ -85,7 +85,7 @@ public class KnowledgeBaseController {
knowledgeBaseService.deleteByDatasetId(datasetId); knowledgeBaseService.deleteByDatasetId(datasetId);
return new Result<>(); return new Result<>();
} }
@DeleteMapping("/datasets/batch") @DeleteMapping("/datasets/batch")
@Operation(summary = "批量删除知识库") @Operation(summary = "批量删除知识库")
@Parameter(name = "ids", description = "知识库ID列表,用逗号分隔", required = true) @Parameter(name = "ids", description = "知识库ID列表,用逗号分隔", required = true)
@@ -94,7 +94,7 @@ public class KnowledgeBaseController {
if (StringUtils.isBlank(ids)) { if (StringUtils.isBlank(ids)) {
throw new RenException(ErrorCode.PARAMS_GET_ERROR); throw new RenException(ErrorCode.PARAMS_GET_ERROR);
} }
String[] idArray = ids.split(","); String[] idArray = ids.split(",");
for (String datasetId : idArray) { for (String datasetId : idArray) {
if (StringUtils.isNotBlank(datasetId)) { if (StringUtils.isNotBlank(datasetId)) {
@@ -103,7 +103,7 @@ public class KnowledgeBaseController {
} }
return new Result<>(); return new Result<>();
} }
@GetMapping("/rag-config/default") @GetMapping("/rag-config/default")
@Operation(summary = "获取默认RAG配置") @Operation(summary = "获取默认RAG配置")
@RequiresPermissions("sys:role:normal") @RequiresPermissions("sys:role:normal")
@@ -53,11 +53,11 @@ public class KnowledgeFilesController {
@RequestParam(required = false) String chunkMethod, @RequestParam(required = false) String chunkMethod,
@RequestParam(required = false) String metaFields, @RequestParam(required = false) String metaFields,
@RequestParam(required = false) String parserConfig) { @RequestParam(required = false) String parserConfig) {
KnowledgeFilesDTO resp = knowledgeFilesService.uploadDocument(datasetId, file, name, KnowledgeFilesDTO resp = knowledgeFilesService.uploadDocument(datasetId, file, name,
metaFields != null ? parseJsonMap(metaFields) : null, metaFields != null ? parseJsonMap(metaFields) : null,
chunkMethod, chunkMethod,
parserConfig != null ? parseJsonMap(parserConfig) : null); parserConfig != null ? parseJsonMap(parserConfig) : null);
return new Result<KnowledgeFilesDTO>().ok(resp); return new Result<KnowledgeFilesDTO>().ok(resp);
} }
@@ -66,21 +66,21 @@ public class KnowledgeFilesController {
@Parameter(name = "document_id", description = "文档ID", required = true) @Parameter(name = "document_id", description = "文档ID", required = true)
@RequiresPermissions("sys:role:normal") @RequiresPermissions("sys:role:normal")
public Result<Void> delete(@PathVariable("dataset_id") String datasetId, public Result<Void> delete(@PathVariable("dataset_id") String datasetId,
@PathVariable("document_id") String documentId) { @PathVariable("document_id") String documentId) {
knowledgeFilesService.deleteByDocumentId(documentId, datasetId); knowledgeFilesService.deleteByDocumentId(documentId, datasetId);
return new Result<>(); return new Result<>();
} }
@PostMapping("/chunks") @PostMapping("/chunks")
@Operation(summary = "批量解析文档(切块)") @Operation(summary = "批量解析文档(切块)")
@RequiresPermissions("sys:role:normal") @RequiresPermissions("sys:role:normal")
public Result<Void> parseDocuments(@PathVariable("dataset_id") String datasetId, public Result<Void> parseDocuments(@PathVariable("dataset_id") String datasetId,
@RequestBody Map<String, List<String>> requestBody) { @RequestBody Map<String, List<String>> requestBody) {
List<String> documentIds = requestBody.get("document_ids"); List<String> documentIds = requestBody.get("document_ids");
if (documentIds == null || documentIds.isEmpty()) { if (documentIds == null || documentIds.isEmpty()) {
return new Result<Void>().error("document_ids参数不能为空"); return new Result<Void>().error("document_ids参数不能为空");
} }
boolean success = knowledgeFilesService.parseDocuments(datasetId, documentIds); boolean success = knowledgeFilesService.parseDocuments(datasetId, documentIds);
if (success) { if (success) {
return new Result<Void>(); return new Result<Void>();
@@ -88,14 +88,14 @@ public class KnowledgeFilesController {
return new Result<Void>().error("文档解析失败,文档可能正在处理中"); return new Result<Void>().error("文档解析失败,文档可能正在处理中");
} }
} }
@PostMapping("/documents/{document_id}/parse") @PostMapping("/documents/{document_id}/parse")
@Operation(summary = "解析单个文档(切块)") @Operation(summary = "解析单个文档(切块)")
@RequiresPermissions("sys:role:normal") @RequiresPermissions("sys:role:normal")
public Result<Void> parseDocument(@PathVariable("dataset_id") String datasetId, public Result<Void> parseDocument(@PathVariable("dataset_id") String datasetId,
@PathVariable("document_id") String documentId) { @PathVariable("document_id") String documentId) {
List<String> documentIds = java.util.Arrays.asList(documentId); List<String> documentIds = java.util.Arrays.asList(documentId);
boolean success = knowledgeFilesService.parseDocuments(datasetId, documentIds); boolean success = knowledgeFilesService.parseDocuments(datasetId, documentIds);
if (success) { if (success) {
return new Result<Void>(); return new Result<Void>();
@@ -108,13 +108,14 @@ public class KnowledgeFilesController {
@Operation(summary = "添加切片到指定文档") @Operation(summary = "添加切片到指定文档")
@RequiresPermissions("sys:role:normal") @RequiresPermissions("sys:role:normal")
public Result<Map<String, Object>> addChunk(@PathVariable("dataset_id") String datasetId, public Result<Map<String, Object>> addChunk(@PathVariable("dataset_id") String datasetId,
@PathVariable("document_id") String documentId, @PathVariable("document_id") String documentId,
@RequestBody Map<String, Object> requestBody) { @RequestBody Map<String, Object> requestBody) {
String content = (String) requestBody.get("content"); String content = (String) requestBody.get("content");
List<String> importantKeywords = (List<String>) requestBody.get("important_keywords"); List<String> importantKeywords = (List<String>) requestBody.get("important_keywords");
List<String> questions = (List<String>) requestBody.get("questions"); List<String> questions = (List<String>) requestBody.get("questions");
Map<String, Object> result = knowledgeFilesService.addChunk(datasetId, documentId, content, importantKeywords, questions); Map<String, Object> result = knowledgeFilesService.addChunk(datasetId, documentId, content, importantKeywords,
questions);
return new Result<Map<String, Object>>().ok(result); return new Result<Map<String, Object>>().ok(result);
} }
@@ -122,12 +123,13 @@ public class KnowledgeFilesController {
@Operation(summary = "列出指定文档的切片") @Operation(summary = "列出指定文档的切片")
@RequiresPermissions("sys:role:normal") @RequiresPermissions("sys:role:normal")
public Result<Map<String, Object>> listChunks(@PathVariable("dataset_id") String datasetId, public Result<Map<String, Object>> listChunks(@PathVariable("dataset_id") String datasetId,
@PathVariable("document_id") String documentId, @PathVariable("document_id") String documentId,
@RequestParam(required = false) String keywords, @RequestParam(required = false) String keywords,
@RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "1") Integer page,
@RequestParam(required = false, defaultValue = "1024") Integer page_size, @RequestParam(required = false, defaultValue = "1024") Integer page_size,
@RequestParam(required = false) String id) { @RequestParam(required = false) String id) {
Map<String, Object> result = knowledgeFilesService.listChunks(datasetId, documentId, keywords, page, page_size, id); Map<String, Object> result = knowledgeFilesService.listChunks(datasetId, documentId, keywords, page, page_size,
id);
return new Result<Map<String, Object>>().ok(result); return new Result<Map<String, Object>>().ok(result);
} }
@@ -138,7 +140,7 @@ public class KnowledgeFilesController {
@Operation(summary = "召回测试") @Operation(summary = "召回测试")
@RequiresPermissions("sys:role:normal") @RequiresPermissions("sys:role:normal")
public Result<Map<String, Object>> retrievalTest(@PathVariable("dataset_id") String datasetId, public Result<Map<String, Object>> retrievalTest(@PathVariable("dataset_id") String datasetId,
@RequestBody Map<String, Object> params) { @RequestBody Map<String, Object> params) {
try { try {
// 提取参数 // 提取参数
String question = (String) params.get("question"); String question = (String) params.get("question");
@@ -165,23 +167,23 @@ public class KnowledgeFilesController {
} }
Map<String, Object> result = knowledgeFilesService.retrievalTest( Map<String, Object> result = knowledgeFilesService.retrievalTest(
question, datasetIds, documentIds, page, pageSize, similarityThreshold, question, datasetIds, documentIds, page, pageSize, similarityThreshold,
vectorSimilarityWeight, topK, rerankId, keyword, highlight, crossLanguages, metadataCondition vectorSimilarityWeight, topK, rerankId, keyword, highlight, crossLanguages, metadataCondition);
);
return new Result<Map<String, Object>>().ok(result); return new Result<Map<String, Object>>().ok(result);
} catch (Exception e) { } catch (Exception e) {
return new Result<Map<String, Object>>().error("召回测试失败: " + e.getMessage()); return new Result<Map<String, Object>>().error("召回测试失败: " + e.getMessage());
} }
} }
/** /**
* 解析JSON字符串为Map对象 * 解析JSON字符串为Map对象
*/ */
private Map<String, Object> parseJsonMap(String jsonString) { private Map<String, Object> parseJsonMap(String jsonString) {
try { try {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(jsonString, new TypeReference<Map<String, Object>>() {}); return objectMapper.readValue(jsonString, new TypeReference<Map<String, Object>>() {
});
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("解析JSON字符串失败: " + jsonString, e); throw new RuntimeException("解析JSON字符串失败: " + jsonString, e);
} }
@@ -10,5 +10,5 @@ import xiaozhi.modules.knowledge.entity.KnowledgeBaseEntity;
*/ */
@Mapper @Mapper
public interface KnowledgeBaseDao extends BaseDao<KnowledgeBaseEntity> { public interface KnowledgeBaseDao extends BaseDao<KnowledgeBaseEntity> {
} }
@@ -16,7 +16,7 @@ public class KnowledgeFilesDTO implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Schema(description = "唯一标识") @Schema(description = "唯一标识")
private String id; private String id;
@Schema(description = "文档ID") @Schema(description = "文档ID")
private String documentId; private String documentId;
@@ -16,8 +16,8 @@ public interface KnowledgeBaseService extends BaseService<KnowledgeBaseEntity> {
* 分页查询知识库列表 * 分页查询知识库列表
* *
* @param knowledgeBaseDTO 查询条件 * @param knowledgeBaseDTO 查询条件
* @param page 页码 * @param page 页码
* @param limit 每页数量 * @param limit 每页数量
* @return 分页数据 * @return 分页数据
*/ */
PageData<KnowledgeBaseDTO> getPageList(KnowledgeBaseDTO knowledgeBaseDTO, String page, String limit); PageData<KnowledgeBaseDTO> getPageList(KnowledgeBaseDTO knowledgeBaseDTO, String page, String limit);
@@ -67,7 +67,7 @@ public interface KnowledgeBaseService extends BaseService<KnowledgeBaseEntity> {
* @param datasetId 知识库ID * @param datasetId 知识库ID
*/ */
void deleteByDatasetId(String datasetId); void deleteByDatasetId(String datasetId);
/** /**
* 获取RAG配置信息 * 获取RAG配置信息
* *
@@ -75,7 +75,7 @@ public interface KnowledgeBaseService extends BaseService<KnowledgeBaseEntity> {
* @return RAG配置信息 * @return RAG配置信息
*/ */
Map<String, Object> getRAGConfig(String ragModelId); Map<String, Object> getRAGConfig(String ragModelId);
/** /**
* 获取默认RAG配置信息 * 获取默认RAG配置信息
* *
@@ -13,121 +13,120 @@ import xiaozhi.modules.knowledge.dto.KnowledgeFilesDTO;
*/ */
public interface KnowledgeFilesService { public interface KnowledgeFilesService {
/** /**
* 分页查询文档列表 * 分页查询文档列表
* *
* @param knowledgeFilesDTO 查询条件 * @param knowledgeFilesDTO 查询条件
* @param page 页码 * @param page 页码
* @param limit 每页数量 * @param limit 每页数量
* @return 分页数据 * @return 分页数据
*/ */
PageData<KnowledgeFilesDTO> getPageList(KnowledgeFilesDTO knowledgeFilesDTO, Integer page, Integer limit); PageData<KnowledgeFilesDTO> getPageList(KnowledgeFilesDTO knowledgeFilesDTO, Integer page, Integer limit);
/** /**
* 根据文档ID和知识库ID获取文档详情 * 根据文档ID和知识库ID获取文档详情
* *
* @param documentId 文档ID * @param documentId 文档ID
* @param datasetId 知识库ID * @param datasetId 知识库ID
* @return 文档详情 * @return 文档详情
*/ */
KnowledgeFilesDTO getByDocumentId(String documentId, String datasetId); KnowledgeFilesDTO getByDocumentId(String documentId, String datasetId);
/** /**
* 上传文档到知识库 * 上传文档到知识库
* *
* @param datasetId 知识库ID * @param datasetId 知识库ID
* @param file 上传的文件 * @param file 上传的文件
* @param name 文档名称 * @param name 文档名称
* @param metaFields 元数据字段 * @param metaFields 元数据字段
* @param chunkMethod 分块方法 * @param chunkMethod 分块方法
* @param parserConfig 解析器配置 * @param parserConfig 解析器配置
* @return 上传的文档信息 * @return 上传的文档信息
*/ */
KnowledgeFilesDTO uploadDocument(String datasetId, MultipartFile file, String name, KnowledgeFilesDTO uploadDocument(String datasetId, MultipartFile file, String name,
Map<String, Object> metaFields, String chunkMethod, Map<String, Object> metaFields, String chunkMethod,
Map<String, Object> parserConfig); Map<String, Object> parserConfig);
/** /**
* 根据文档ID和知识库ID删除文档 * 根据文档ID和知识库ID删除文档
* *
* @param documentId 文档ID * @param documentId 文档ID
* @param datasetId 知识库ID * @param datasetId 知识库ID
*/ */
void deleteByDocumentId(String documentId, String datasetId); void deleteByDocumentId(String documentId, String datasetId);
/**
* 获取RAG配置信息
*
* @param ragModelId RAG模型配置ID
* @return RAG配置信息
*/
Map<String, Object> getRAGConfig(String ragModelId);
/** /**
* 获取RAG配置信息 * 获取默认RAG配置信息
* *
* @param ragModelId RAG模型配置ID * @return 默认RAG配置信息
* @return RAG配置信息 */
*/ Map<String, Object> getDefaultRAGConfig();
Map<String, Object> getRAGConfig(String ragModelId);
/**
* 获取默认RAG配置信息
*
* @return 默认RAG配置信息
*/
Map<String, Object> getDefaultRAGConfig();
/** /**
* 解析文档(切块) * 解析文档(切块)
* *
* @param datasetId 知识库ID * @param datasetId 知识库ID
* @param documentIds 文档ID列表 * @param documentIds 文档ID列表
* @return 解析结果 * @return 解析结果
*/ */
boolean parseDocuments(String datasetId, List<String> documentIds); boolean parseDocuments(String datasetId, List<String> documentIds);
/** /**
* 添加切片到指定文档 * 添加切片到指定文档
* *
* @param datasetId 知识库ID * @param datasetId 知识库ID
* @param documentId 文档ID * @param documentId 文档ID
* @param content 切片内容 * @param content 切片内容
* @param importantKeywords 重要关键词列表 * @param importantKeywords 重要关键词列表
* @param questions 问题列表 * @param questions 问题列表
* @return 添加的切片信息 * @return 添加的切片信息
*/ */
Map<String, Object> addChunk(String datasetId, String documentId, String content, Map<String, Object> addChunk(String datasetId, String documentId, String content,
List<String> importantKeywords, List<String> questions); List<String> importantKeywords, List<String> questions);
/** /**
* 列出指定文档的切片 * 列出指定文档的切片
* *
* @param datasetId 知识库ID * @param datasetId 知识库ID
* @param documentId 文档ID * @param documentId 文档ID
* @param keywords 关键词过滤 * @param keywords 关键词过滤
* @param page 页码 * @param page 页码
* @param pageSize 每页数量 * @param pageSize 每页数量
* @param chunkId 切片ID * @param chunkId 切片ID
* @return 切片列表信息 * @return 切片列表信息
*/ */
Map<String, Object> listChunks(String datasetId, String documentId, String keywords, Map<String, Object> listChunks(String datasetId, String documentId, String keywords,
Integer page, Integer pageSize, String chunkId); Integer page, Integer pageSize, String chunkId);
/** /**
* 召回测试 - 从指定数据集或文档中检索相关切片 * 召回测试 - 从指定数据集或文档中检索相关切片
* *
* @param question 用户查询或查询关键词 * @param question 用户查询或查询关键词
* @param datasetIds 数据集ID列表 * @param datasetIds 数据集ID列表
* @param documentIds 文档ID列表 * @param documentIds 文档ID列表
* @param page 页码 * @param page 页码
* @param pageSize 每页数量 * @param pageSize 每页数量
* @param similarityThreshold 最小相似度阈值 * @param similarityThreshold 最小相似度阈值
* @param vectorSimilarityWeight 向量相似度权重 * @param vectorSimilarityWeight 向量相似度权重
* @param topK 参与向量余弦计算的切片数量 * @param topK 参与向量余弦计算的切片数量
* @param rerankId 重排模型ID * @param rerankId 重排模型ID
* @param keyword 是否启用关键词匹配 * @param keyword 是否启用关键词匹配
* @param highlight 是否启用高亮显示 * @param highlight 是否启用高亮显示
* @param crossLanguages 跨语言翻译列表 * @param crossLanguages 跨语言翻译列表
* @param metadataCondition 元数据过滤条件 * @param metadataCondition 元数据过滤条件
* @return 召回测试结果 * @return 召回测试结果
*/ */
Map<String, Object> retrievalTest(String question, List<String> datasetIds, List<String> documentIds, Map<String, Object> retrievalTest(String question, List<String> datasetIds, List<String> documentIds,
Integer page, Integer pageSize, Float similarityThreshold, Integer page, Integer pageSize, Float similarityThreshold,
Float vectorSimilarityWeight, Integer topK, String rerankId, Float vectorSimilarityWeight, Integer topK, String rerankId,
Boolean keyword, Boolean highlight, List<String> crossLanguages, Boolean keyword, Boolean highlight, List<String> crossLanguages,
Map<String, Object> metadataCondition); Map<String, Object> metadataCondition);
} }
@@ -37,15 +37,14 @@ import xiaozhi.modules.model.service.ModelConfigService;
@Service @Service
@AllArgsConstructor @AllArgsConstructor
@Slf4j @Slf4j
public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao, KnowledgeBaseEntity> implements KnowledgeBaseService { public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao, KnowledgeBaseEntity>
implements KnowledgeBaseService {
private final KnowledgeBaseDao knowledgeBaseDao; private final KnowledgeBaseDao knowledgeBaseDao;
private final ModelConfigService modelConfigService; private final ModelConfigService modelConfigService;
private final ModelConfigDao modelConfigDao; private final ModelConfigDao modelConfigDao;
private RestTemplate restTemplate = new RestTemplate(); private RestTemplate restTemplate = new RestTemplate();
@Override @Override
public PageData<KnowledgeBaseDTO> getPageList(KnowledgeBaseDTO knowledgeBaseDTO, String page, String limit) { public PageData<KnowledgeBaseDTO> getPageList(KnowledgeBaseDTO knowledgeBaseDTO, String page, String limit) {
long curPage = Long.parseLong(page); long curPage = Long.parseLong(page);
@@ -53,11 +52,11 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
Page<KnowledgeBaseEntity> pageInfo = new Page<>(curPage, pageSize); Page<KnowledgeBaseEntity> pageInfo = new Page<>(curPage, pageSize);
QueryWrapper<KnowledgeBaseEntity> queryWrapper = new QueryWrapper<>(); QueryWrapper<KnowledgeBaseEntity> queryWrapper = new QueryWrapper<>();
// 添加查询条件 // 添加查询条件
if (knowledgeBaseDTO != null) { if (knowledgeBaseDTO != null) {
queryWrapper.like(StringUtils.isNotBlank(knowledgeBaseDTO.getName()), "name", knowledgeBaseDTO.getName()) queryWrapper.like(StringUtils.isNotBlank(knowledgeBaseDTO.getName()), "name", knowledgeBaseDTO.getName())
.eq(knowledgeBaseDTO.getStatus() != null, "status", knowledgeBaseDTO.getStatus()); .eq(knowledgeBaseDTO.getStatus() != null, "status", knowledgeBaseDTO.getStatus());
} }
// 添加排序规则:按创建时间降序 // 添加排序规则:按创建时间降序
@@ -67,12 +66,13 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
// 获取分页数据 // 获取分页数据
PageData<KnowledgeBaseDTO> pageData = getPageData(knowledgeBaseEntityIPage, KnowledgeBaseDTO.class); PageData<KnowledgeBaseDTO> pageData = getPageData(knowledgeBaseEntityIPage, KnowledgeBaseDTO.class);
// 为每个知识库获取文档数量 // 为每个知识库获取文档数量
if (pageData != null && pageData.getList() != null) { if (pageData != null && pageData.getList() != null) {
for (KnowledgeBaseDTO knowledgeBase : pageData.getList()) { for (KnowledgeBaseDTO knowledgeBase : pageData.getList()) {
try { try {
Integer documentCount = getDocumentCountFromRAGFlow(knowledgeBase.getDatasetId(), knowledgeBase.getRagModelId()); Integer documentCount = getDocumentCountFromRAGFlow(knowledgeBase.getDatasetId(),
knowledgeBase.getRagModelId());
knowledgeBase.setDocumentCount(documentCount); knowledgeBase.setDocumentCount(documentCount);
} catch (Exception e) { } catch (Exception e) {
log.warn("获取知识库 {} 的文档数量失败: {}", knowledgeBase.getDatasetId(), e.getMessage()); log.warn("获取知识库 {} 的文档数量失败: {}", knowledgeBase.getDatasetId(), e.getMessage());
@@ -109,19 +109,17 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
try { try {
Map<String, Object> ragConfig = getValidatedRAGConfig(knowledgeBaseDTO.getRagModelId()); Map<String, Object> ragConfig = getValidatedRAGConfig(knowledgeBaseDTO.getRagModelId());
datasetId = createDatasetInRAGFlow( datasetId = createDatasetInRAGFlow(
knowledgeBaseDTO.getName(), knowledgeBaseDTO.getName(),
knowledgeBaseDTO.getDescription(), knowledgeBaseDTO.getDescription(),
ragConfig ragConfig);
);
} catch (Exception e) { } catch (Exception e) {
// 如果RAG API调用失败,直接抛出异常,无需回滚(因为还没有插入本地数据库) // 如果RAG API调用失败,直接抛出异常,无需回滚(因为还没有插入本地数据库)
throw e; throw e;
} }
// 验证数据集ID是否已存在 // 验证数据集ID是否已存在
KnowledgeBaseEntity existingEntity = knowledgeBaseDao.selectOne( KnowledgeBaseEntity existingEntity = knowledgeBaseDao.selectOne(
new QueryWrapper<KnowledgeBaseEntity>().eq("dataset_id", datasetId) new QueryWrapper<KnowledgeBaseEntity>().eq("dataset_id", datasetId));
);
if (existingEntity != null) { if (existingEntity != null) {
// 如果datasetId已存在,删除RAGFlow中的数据集并抛出异常 // 如果datasetId已存在,删除RAGFlow中的数据集并抛出异常
try { try {
@@ -132,7 +130,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
} }
throw new RenException(ErrorCode.DB_RECORD_EXISTS); throw new RenException(ErrorCode.DB_RECORD_EXISTS);
} }
// 创建本地实体并保存 // 创建本地实体并保存
KnowledgeBaseEntity entity = ConvertUtils.sourceToTarget(knowledgeBaseDTO, KnowledgeBaseEntity.class); KnowledgeBaseEntity entity = ConvertUtils.sourceToTarget(knowledgeBaseDTO, KnowledgeBaseEntity.class);
entity.setDatasetId(datasetId); entity.setDatasetId(datasetId);
@@ -156,10 +154,9 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
// 验证数据集ID是否与其他记录冲突 // 验证数据集ID是否与其他记录冲突
if (StringUtils.isNotBlank(knowledgeBaseDTO.getDatasetId())) { if (StringUtils.isNotBlank(knowledgeBaseDTO.getDatasetId())) {
KnowledgeBaseEntity conflictEntity = knowledgeBaseDao.selectOne( KnowledgeBaseEntity conflictEntity = knowledgeBaseDao.selectOne(
new QueryWrapper<KnowledgeBaseEntity>() new QueryWrapper<KnowledgeBaseEntity>()
.eq("dataset_id", knowledgeBaseDTO.getDatasetId()) .eq("dataset_id", knowledgeBaseDTO.getDatasetId())
.ne("id", knowledgeBaseDTO.getId()) .ne("id", knowledgeBaseDTO.getId()));
);
if (conflictEntity != null) { if (conflictEntity != null) {
throw new RenException(ErrorCode.DB_RECORD_EXISTS); throw new RenException(ErrorCode.DB_RECORD_EXISTS);
} }
@@ -173,11 +170,10 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
try { try {
Map<String, Object> ragConfig = getValidatedRAGConfig(knowledgeBaseDTO.getRagModelId()); Map<String, Object> ragConfig = getValidatedRAGConfig(knowledgeBaseDTO.getRagModelId());
updateDatasetInRAGFlow( updateDatasetInRAGFlow(
knowledgeBaseDTO.getDatasetId(), knowledgeBaseDTO.getDatasetId(),
knowledgeBaseDTO.getName(), knowledgeBaseDTO.getName(),
knowledgeBaseDTO.getDescription(), knowledgeBaseDTO.getDescription(),
ragConfig ragConfig);
);
} catch (Exception e) { } catch (Exception e) {
// 如果RAG API调用失败,回滚本地数据库操作 // 如果RAG API调用失败,回滚本地数据库操作
knowledgeBaseDao.updateById(existingEntity); knowledgeBaseDao.updateById(existingEntity);
@@ -203,8 +199,8 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
throw new RenException(ErrorCode.Knowledge_Base_RECORD_NOT_EXISTS); throw new RenException(ErrorCode.Knowledge_Base_RECORD_NOT_EXISTS);
} }
log.info("找到记录: ID={}, datasetId={}, ragModelId={}", log.info("找到记录: ID={}, datasetId={}, ragModelId={}",
entity.getId(), entity.getDatasetId(), entity.getRagModelId()); entity.getId(), entity.getDatasetId(), entity.getRagModelId());
// 先调用RAGFlow API删除数据集 // 先调用RAGFlow API删除数据集
boolean apiDeleteSuccess = false; boolean apiDeleteSuccess = false;
@@ -229,7 +225,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
int deleteCount = knowledgeBaseDao.deleteById(id); int deleteCount = knowledgeBaseDao.deleteById(id);
log.info("本地数据库删除结果: {}", deleteCount > 0 ? "成功" : "失败"); log.info("本地数据库删除结果: {}", deleteCount > 0 ? "成功" : "失败");
} }
log.info("=== 删除操作结束 ==="); log.info("=== 删除操作结束 ===");
} }
@@ -240,8 +236,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
} }
KnowledgeBaseEntity entity = knowledgeBaseDao.selectOne( KnowledgeBaseEntity entity = knowledgeBaseDao.selectOne(
new QueryWrapper<KnowledgeBaseEntity>().eq("dataset_id", datasetId) new QueryWrapper<KnowledgeBaseEntity>().eq("dataset_id", datasetId));
);
if (entity == null) { if (entity == null) {
throw new RenException(ErrorCode.Knowledge_Base_RECORD_NOT_EXISTS); throw new RenException(ErrorCode.Knowledge_Base_RECORD_NOT_EXISTS);
@@ -260,16 +255,15 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
log.info("删除datasetId: {}", datasetId); log.info("删除datasetId: {}", datasetId);
KnowledgeBaseEntity entity = knowledgeBaseDao.selectOne( KnowledgeBaseEntity entity = knowledgeBaseDao.selectOne(
new QueryWrapper<KnowledgeBaseEntity>().eq("dataset_id", datasetId) new QueryWrapper<KnowledgeBaseEntity>().eq("dataset_id", datasetId));
);
if (entity == null) { if (entity == null) {
log.warn("记录不存在,datasetId: {}", datasetId); log.warn("记录不存在,datasetId: {}", datasetId);
throw new RenException(ErrorCode.Knowledge_Base_RECORD_NOT_EXISTS); throw new RenException(ErrorCode.Knowledge_Base_RECORD_NOT_EXISTS);
} }
log.info("找到记录: ID={}, datasetId={}, ragModelId={}", log.info("找到记录: ID={}, datasetId={}, ragModelId={}",
entity.getId(), entity.getDatasetId(), entity.getRagModelId()); entity.getId(), entity.getDatasetId(), entity.getRagModelId());
// 先删除本地数据库记录 // 先删除本地数据库记录
int deleteCount = knowledgeBaseDao.deleteById(entity.getId()); int deleteCount = knowledgeBaseDao.deleteById(entity.getId());
@@ -288,62 +282,62 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
} else { } else {
log.warn("datasetId或ragModelId为空,跳过RAGFlow删除"); log.warn("datasetId或ragModelId为空,跳过RAGFlow删除");
} }
log.info("=== 通过datasetId删除操作结束 ==="); log.info("=== 通过datasetId删除操作结束 ===");
} }
@Override @Override
public Map<String, Object> getRAGConfig(String ragModelId) { public Map<String, Object> getRAGConfig(String ragModelId) {
if (StringUtils.isBlank(ragModelId)) { if (StringUtils.isBlank(ragModelId)) {
throw new RenException(ErrorCode.PARAMS_GET_ERROR); throw new RenException(ErrorCode.PARAMS_GET_ERROR);
} }
// 从缓存获取模型配置 // 从缓存获取模型配置
ModelConfigEntity modelConfig = modelConfigService.getModelByIdFromCache(ragModelId); ModelConfigEntity modelConfig = modelConfigService.getModelByIdFromCache(ragModelId);
if (modelConfig == null || modelConfig.getConfigJson() == null) { if (modelConfig == null || modelConfig.getConfigJson() == null) {
throw new RenException(ErrorCode.RAG_CONFIG_NOT_FOUND); throw new RenException(ErrorCode.RAG_CONFIG_NOT_FOUND);
} }
// 验证是否为RAG类型配置 // 验证是否为RAG类型配置
if (!Constant.RAG_CONFIG_TYPE.equals(modelConfig.getModelType().toUpperCase())) { if (!Constant.RAG_CONFIG_TYPE.equals(modelConfig.getModelType().toUpperCase())) {
throw new RenException(ErrorCode.RAG_CONFIG_TYPE_ERROR); throw new RenException(ErrorCode.RAG_CONFIG_TYPE_ERROR);
} }
Map<String, Object> config = modelConfig.getConfigJson(); Map<String, Object> config = modelConfig.getConfigJson();
// 验证必要的配置参数 // 验证必要的配置参数
validateRagConfig(config); validateRagConfig(config);
// 返回配置信息 // 返回配置信息
return config; return config;
} }
@Override @Override
public Map<String, Object> getDefaultRAGConfig() { public Map<String, Object> getDefaultRAGConfig() {
// 获取默认RAG模型配置 // 获取默认RAG模型配置
QueryWrapper<ModelConfigEntity> queryWrapper = new QueryWrapper<>(); QueryWrapper<ModelConfigEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("model_type", Constant.RAG_CONFIG_TYPE) queryWrapper.eq("model_type", Constant.RAG_CONFIG_TYPE)
.eq("is_default", 1) .eq("is_default", 1)
.eq("is_enabled", 1); .eq("is_enabled", 1);
List<ModelConfigEntity> modelConfigs = modelConfigDao.selectList(queryWrapper); List<ModelConfigEntity> modelConfigs = modelConfigDao.selectList(queryWrapper);
if (modelConfigs == null || modelConfigs.isEmpty()) { if (modelConfigs == null || modelConfigs.isEmpty()) {
throw new RenException(ErrorCode.RAG_DEFAULT_CONFIG_NOT_FOUND); throw new RenException(ErrorCode.RAG_DEFAULT_CONFIG_NOT_FOUND);
} }
ModelConfigEntity defaultConfig = modelConfigs.get(0); ModelConfigEntity defaultConfig = modelConfigs.get(0);
if (defaultConfig.getConfigJson() == null) { if (defaultConfig.getConfigJson() == null) {
throw new RenException(ErrorCode.RAG_CONFIG_NOT_FOUND); throw new RenException(ErrorCode.RAG_CONFIG_NOT_FOUND);
} }
Map<String, Object> config = defaultConfig.getConfigJson(); Map<String, Object> config = defaultConfig.getConfigJson();
// 验证必要的配置参数 // 验证必要的配置参数
validateRagConfig(config); validateRagConfig(config);
return config; return config;
} }
/** /**
* 验证RAG配置中是否包含必要的参数 * 验证RAG配置中是否包含必要的参数
*/ */
@@ -351,17 +345,17 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
if (config == null) { if (config == null) {
throw new RenException(ErrorCode.RAG_CONFIG_NOT_FOUND); throw new RenException(ErrorCode.RAG_CONFIG_NOT_FOUND);
} }
// 从配置中提取必要的参数 // 从配置中提取必要的参数
String baseUrl = (String) config.get("base_url"); String baseUrl = (String) config.get("base_url");
String apiKey = (String) config.get("api_key"); String apiKey = (String) config.get("api_key");
// 验证base_url是否存在且非空 // 验证base_url是否存在且非空
if (StringUtils.isBlank(baseUrl)) { if (StringUtils.isBlank(baseUrl)) {
throw new RenException(ErrorCode.RAG_CONFIG_MISSING_PARAMS); throw new RenException(ErrorCode.RAG_CONFIG_MISSING_PARAMS);
} }
} }
/** /**
* 调用RAGFlow API创建数据集 * 调用RAGFlow API创建数据集
*/ */
@@ -370,14 +364,14 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
try { try {
String baseUrl = (String) ragConfig.get("base_url"); String baseUrl = (String) ragConfig.get("base_url");
String apiKey = (String) ragConfig.get("api_key"); String apiKey = (String) ragConfig.get("api_key");
log.info("开始调用RAGFlow API创建数据集, name: {}", name); log.info("开始调用RAGFlow API创建数据集, name: {}", name);
log.debug("RAGFlow配置 - baseUrl: {}, apiKey: {}", baseUrl, StringUtils.isBlank(apiKey) ? "未配置" : "已配置"); log.debug("RAGFlow配置 - baseUrl: {}, apiKey: {}", baseUrl, StringUtils.isBlank(apiKey) ? "未配置" : "已配置");
// 构建请求URL // 构建请求URL
String url = baseUrl + "/api/v1/datasets"; String url = baseUrl + "/api/v1/datasets";
log.debug("请求URL: {}", url); log.debug("请求URL: {}", url);
// 构建请求体 // 构建请求体
Map<String, Object> requestBody = new HashMap<>(); Map<String, Object> requestBody = new HashMap<>();
requestBody.put("name", name); requestBody.put("name", name);
@@ -385,26 +379,26 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
requestBody.put("description", description); requestBody.put("description", description);
} }
log.debug("请求体: {}", requestBody); log.debug("请求体: {}", requestBody);
// 设置请求头 // 设置请求头
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + apiKey); headers.set("Authorization", "Bearer " + apiKey);
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers); HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
// 发送POST请求 // 发送POST请求
log.info("发送POST请求到RAGFlow API..."); log.info("发送POST请求到RAGFlow API...");
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
log.info("RAGFlow API响应状态码: {}", response.getStatusCode()); log.info("RAGFlow API响应状态码: {}", response.getStatusCode());
log.debug("RAGFlow API响应内容: {}", response.getBody()); log.debug("RAGFlow API响应内容: {}", response.getBody());
if (!response.getStatusCode().is2xxSuccessful()) { if (!response.getStatusCode().is2xxSuccessful()) {
log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody()); log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody());
throw new RenException(ErrorCode.RAG_API_CREATE_FAILED); throw new RenException(ErrorCode.RAG_API_CREATE_FAILED);
} }
// 解析响应体,提取datasetId // 解析响应体,提取datasetId
String responseBody = response.getBody(); String responseBody = response.getBody();
if (StringUtils.isNotBlank(responseBody)) { if (StringUtils.isNotBlank(responseBody)) {
@@ -412,9 +406,9 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
// 解析RAGFlow API响应,支持多种可能的字段名 // 解析RAGFlow API响应,支持多种可能的字段名
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> responseMap = objectMapper.readValue(responseBody, Map.class); Map<String, Object> responseMap = objectMapper.readValue(responseBody, Map.class);
log.debug("RAGFlow API响应解析结果: {}", responseMap); log.debug("RAGFlow API响应解析结果: {}", responseMap);
// 首先检查响应码 // 首先检查响应码
Integer code = (Integer) responseMap.get("code"); Integer code = (Integer) responseMap.get("code");
if (code != null && code == 0) { if (code != null && code == 0) {
@@ -423,7 +417,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
if (dataObj instanceof Map) { if (dataObj instanceof Map) {
Map<String, Object> dataMap = (Map<String, Object>) dataObj; Map<String, Object> dataMap = (Map<String, Object>) dataObj;
datasetId = (String) dataMap.get("id"); datasetId = (String) dataMap.get("id");
if (StringUtils.isBlank(datasetId)) { if (StringUtils.isBlank(datasetId)) {
// 如果id字段为空,尝试其他可能的字段名 // 如果id字段为空,尝试其他可能的字段名
datasetId = (String) dataMap.get("dataset_id"); datasetId = (String) dataMap.get("dataset_id");
@@ -435,7 +429,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
log.error("RAGFlow API调用失败,响应码: {}, 响应内容: {}", code, responseBody); log.error("RAGFlow API调用失败,响应码: {}, 响应内容: {}", code, responseBody);
throw new RenException(ErrorCode.RAG_API_CREATE_FAILED, "RAGFlow API调用失败,响应码: " + code); throw new RenException(ErrorCode.RAG_API_CREATE_FAILED, "RAGFlow API调用失败,响应码: " + code);
} }
log.info("从RAGFlow API响应中解析出datasetId: {}", datasetId); log.info("从RAGFlow API响应中解析出datasetId: {}", datasetId);
log.debug("完整响应内容: {}", responseBody); log.debug("完整响应内容: {}", responseBody);
} catch (Exception e) { } catch (Exception e) {
@@ -443,22 +437,24 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
throw new RenException(ErrorCode.RAG_API_CREATE_FAILED, "解析RAGFlow响应失败: " + e.getMessage()); throw new RenException(ErrorCode.RAG_API_CREATE_FAILED, "解析RAGFlow响应失败: " + e.getMessage());
} }
} }
if (StringUtils.isBlank(datasetId)) { if (StringUtils.isBlank(datasetId)) {
log.error("无法从RAGFlow API响应中获取datasetId,响应内容: {}", responseBody); log.error("无法从RAGFlow API响应中获取datasetId,响应内容: {}", responseBody);
throw new RenException(ErrorCode.RAG_API_CREATE_FAILED, "RAGFlow API响应中未包含datasetId"); throw new RenException(ErrorCode.RAG_API_CREATE_FAILED, "RAGFlow API响应中未包含datasetId");
} }
log.info("RAGFlow数据集创建成功,datasetId: {}", datasetId); log.info("RAGFlow数据集创建成功,datasetId: {}", datasetId);
} catch (HttpClientErrorException e) { } catch (HttpClientErrorException e) {
log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}",
e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e);
throw new RenException(ErrorCode.RAG_API_CREATE_FAILED, "创建RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString()); throw new RenException(ErrorCode.RAG_API_CREATE_FAILED,
"创建RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString());
} catch (HttpServerErrorException e) { } catch (HttpServerErrorException e) {
log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}",
e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e);
throw new RenException(ErrorCode.RAG_API_CREATE_FAILED, "创建RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString()); throw new RenException(ErrorCode.RAG_API_CREATE_FAILED,
"创建RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString());
} catch (ResourceAccessException e) { } catch (ResourceAccessException e) {
log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e);
throw new RenException(ErrorCode.RAG_API_CREATE_FAILED, "创建RAGFlow数据集失败: 网络连接错误 - " + e.getMessage()); throw new RenException(ErrorCode.RAG_API_CREATE_FAILED, "创建RAGFlow数据集失败: 网络连接错误 - " + e.getMessage());
@@ -468,22 +464,23 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
} }
return datasetId; return datasetId;
} }
/** /**
* 调用RAGFlow API更新数据集 * 调用RAGFlow API更新数据集
*/ */
private void updateDatasetInRAGFlow(String datasetId, String name, String description, Map<String, Object> ragConfig) { private void updateDatasetInRAGFlow(String datasetId, String name, String description,
Map<String, Object> ragConfig) {
try { try {
String baseUrl = (String) ragConfig.get("base_url"); String baseUrl = (String) ragConfig.get("base_url");
String apiKey = (String) ragConfig.get("api_key"); String apiKey = (String) ragConfig.get("api_key");
log.info("开始调用RAGFlow API更新数据集,datasetId: {}, name: {}", datasetId, name); log.info("开始调用RAGFlow API更新数据集,datasetId: {}, name: {}", datasetId, name);
log.debug("RAGFlow配置 - baseUrl: {}, apiKey: {}", baseUrl, StringUtils.isBlank(apiKey) ? "未配置" : "已配置"); log.debug("RAGFlow配置 - baseUrl: {}, apiKey: {}", baseUrl, StringUtils.isBlank(apiKey) ? "未配置" : "已配置");
// 构建请求URL // 构建请求URL
String url = baseUrl + "/api/v1/datasets/" + datasetId; String url = baseUrl + "/api/v1/datasets/" + datasetId;
log.debug("请求URL: {}", url); log.debug("请求URL: {}", url);
// 构建请求体 // 构建请求体
Map<String, Object> requestBody = new HashMap<>(); Map<String, Object> requestBody = new HashMap<>();
requestBody.put("dataset_id", datasetId); requestBody.put("dataset_id", datasetId);
@@ -492,36 +489,38 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
requestBody.put("description", description); requestBody.put("description", description);
} }
log.debug("请求体: {}", requestBody); log.debug("请求体: {}", requestBody);
// 设置请求头 // 设置请求头
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + apiKey); headers.set("Authorization", "Bearer " + apiKey);
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers); HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
// 发送PUT请求 // 发送PUT请求
log.info("发送PUT请求到RAGFlow API..."); log.info("发送PUT请求到RAGFlow API...");
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, String.class); ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, String.class);
log.info("RAGFlow API响应状态码: {}", response.getStatusCode()); log.info("RAGFlow API响应状态码: {}", response.getStatusCode());
log.debug("RAGFlow API响应内容: {}", response.getBody()); log.debug("RAGFlow API响应内容: {}", response.getBody());
if (!response.getStatusCode().is2xxSuccessful()) { if (!response.getStatusCode().is2xxSuccessful()) {
log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody()); log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody());
throw new RenException(ErrorCode.RAG_API_UPDATE_FAILED); throw new RenException(ErrorCode.RAG_API_UPDATE_FAILED);
} }
log.info("RAGFlow数据集更新成功,datasetId: {}", datasetId); log.info("RAGFlow数据集更新成功,datasetId: {}", datasetId);
} catch (HttpClientErrorException e) { } catch (HttpClientErrorException e) {
log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}",
e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e);
throw new RenException(ErrorCode.RAG_API_UPDATE_FAILED, "更新RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString()); throw new RenException(ErrorCode.RAG_API_UPDATE_FAILED,
"更新RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString());
} catch (HttpServerErrorException e) { } catch (HttpServerErrorException e) {
log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}",
e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e);
throw new RenException(ErrorCode.RAG_API_UPDATE_FAILED, "更新RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString()); throw new RenException(ErrorCode.RAG_API_UPDATE_FAILED,
"更新RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString());
} catch (ResourceAccessException e) { } catch (ResourceAccessException e) {
log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e);
throw new RenException(ErrorCode.RAG_API_UPDATE_FAILED, "更新RAGFlow数据集失败: 网络连接错误 - " + e.getMessage()); throw new RenException(ErrorCode.RAG_API_UPDATE_FAILED, "更新RAGFlow数据集失败: 网络连接错误 - " + e.getMessage());
@@ -530,7 +529,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
throw new RenException(ErrorCode.RAG_API_UPDATE_FAILED, "更新RAGFlow数据集失败: " + e.getMessage()); throw new RenException(ErrorCode.RAG_API_UPDATE_FAILED, "更新RAGFlow数据集失败: " + e.getMessage());
} }
} }
/** /**
* 调用RAGFlow API删除数据集 * 调用RAGFlow API删除数据集
*/ */
@@ -538,48 +537,51 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
try { try {
String baseUrl = (String) ragConfig.get("base_url"); String baseUrl = (String) ragConfig.get("base_url");
String apiKey = (String) ragConfig.get("api_key"); String apiKey = (String) ragConfig.get("api_key");
log.info("开始调用RAGFlow API删除数据集,datasetId: {}", datasetId); log.info("开始调用RAGFlow API删除数据集,datasetId: {}", datasetId);
log.debug("RAGFlow配置 - baseUrl: {}, apiKey: {}", baseUrl, StringUtils.isBlank(apiKey) ? "未配置" : "已配置"); log.debug("RAGFlow配置 - baseUrl: {}, apiKey: {}", baseUrl, StringUtils.isBlank(apiKey) ? "未配置" : "已配置");
// 构建请求URL // 构建请求URL
String url = baseUrl + "/api/v1/datasets"; String url = baseUrl + "/api/v1/datasets";
log.debug("请求URL: {}", url); log.debug("请求URL: {}", url);
// 构建请求体 // 构建请求体
Map<String, Object> requestBody = new HashMap<>(); Map<String, Object> requestBody = new HashMap<>();
requestBody.put("ids", List.of(datasetId)); requestBody.put("ids", List.of(datasetId));
log.debug("请求体: {}", requestBody); log.debug("请求体: {}", requestBody);
// 设置请求头 // 设置请求头
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + apiKey); headers.set("Authorization", "Bearer " + apiKey);
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers); HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
// 发送DELETE请求 // 发送DELETE请求
log.info("发送DELETE请求到RAGFlow API..."); log.info("发送DELETE请求到RAGFlow API...");
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, String.class); ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.DELETE, requestEntity,
String.class);
log.info("RAGFlow API响应状态码: {}", response.getStatusCode()); log.info("RAGFlow API响应状态码: {}", response.getStatusCode());
log.debug("RAGFlow API响应内容: {}", response.getBody()); log.debug("RAGFlow API响应内容: {}", response.getBody());
if (!response.getStatusCode().is2xxSuccessful()) { if (!response.getStatusCode().is2xxSuccessful()) {
log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody()); log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody());
throw new RenException(ErrorCode.RAG_API_DELETE_FAILED); throw new RenException(ErrorCode.RAG_API_DELETE_FAILED);
} }
log.info("RAGFlow数据集删除成功,datasetId: {}", datasetId); log.info("RAGFlow数据集删除成功,datasetId: {}", datasetId);
} catch (HttpClientErrorException e) { } catch (HttpClientErrorException e) {
log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}",
e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e);
throw new RenException(ErrorCode.RAG_API_DELETE_FAILED, "删除RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString()); throw new RenException(ErrorCode.RAG_API_DELETE_FAILED,
"删除RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString());
} catch (HttpServerErrorException e) { } catch (HttpServerErrorException e) {
log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}",
e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e);
throw new RenException(ErrorCode.RAG_API_DELETE_FAILED, "删除RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString()); throw new RenException(ErrorCode.RAG_API_DELETE_FAILED,
"删除RAGFlow数据集失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString());
} catch (ResourceAccessException e) { } catch (ResourceAccessException e) {
log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e);
throw new RenException(ErrorCode.RAG_API_DELETE_FAILED, "删除RAGFlow数据集失败: 网络连接错误 - " + e.getMessage()); throw new RenException(ErrorCode.RAG_API_DELETE_FAILED, "删除RAGFlow数据集失败: 网络连接错误 - " + e.getMessage());
@@ -588,7 +590,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
throw new RenException(ErrorCode.RAG_API_DELETE_FAILED, "删除RAGFlow数据集失败: " + e.getMessage()); throw new RenException(ErrorCode.RAG_API_DELETE_FAILED, "删除RAGFlow数据集失败: " + e.getMessage());
} }
} }
/** /**
* 获取RAG配置并验证 * 获取RAG配置并验证
*/ */
@@ -599,7 +601,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
return getDefaultRAGConfig(); return getDefaultRAGConfig();
} }
} }
/** /**
* 从RAGFlow API获取知识库的文档数量 * 从RAGFlow API获取知识库的文档数量
*/ */
@@ -611,7 +613,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
try { try {
log.info("开始获取知识库 {} 的文档数量", datasetId); log.info("开始获取知识库 {} 的文档数量", datasetId);
// 获取RAG配置 // 获取RAG配置
Map<String, Object> ragConfig = getValidatedRAGConfig(ragModelId); Map<String, Object> ragConfig = getValidatedRAGConfig(ragModelId);
String baseUrl = (String) ragConfig.get("base_url"); String baseUrl = (String) ragConfig.get("base_url");
@@ -646,7 +648,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> responseMap = objectMapper.readValue(responseBody, Map.class); Map<String, Object> responseMap = objectMapper.readValue(responseBody, Map.class);
Integer code = (Integer) responseMap.get("code"); Integer code = (Integer) responseMap.get("code");
if (code != null && code == 0) { if (code != null && code == 0) {
Object dataObj = responseMap.get("data"); Object dataObj = responseMap.get("data");
if (dataObj instanceof Map) { if (dataObj instanceof Map) {
@@ -669,11 +671,11 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
} catch (IOException e) { } catch (IOException e) {
log.error("解析RAGFlow API响应失败: {}", e.getMessage(), e); log.error("解析RAGFlow API响应失败: {}", e.getMessage(), e);
} catch (HttpClientErrorException e) { } catch (HttpClientErrorException e) {
log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}",
e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e);
} catch (HttpServerErrorException e) { } catch (HttpServerErrorException e) {
log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}",
e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e);
} catch (ResourceAccessException e) { } catch (ResourceAccessException e) {
log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e);
} catch (Exception e) { } catch (Exception e) {
@@ -683,7 +685,4 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
return 0; return 0;
} }
} }
+164 -265
View File
@@ -8,6 +8,54 @@ function getAuthToken() {
return localStorage.getItem('token') || ''; return localStorage.getItem('token') || '';
} }
/**
* 通用API请求包装器
* @param {Object} config - 请求配置
* @param {string} config.url - 请求URL
* @param {string} config.method - 请求方法
* @param {Object} [config.data] - 请求数据
* @param {Object} [config.headers] - 额外请求头
* @param {Function} config.callback - 成功回调
* @param {Function} [config.errorCallback] - 错误回调
* @param {string} [config.errorMessage] - 错误消息
* @param {Function} [config.retryFunction] - 重试函数
*/
function makeApiRequest(config) {
const token = getAuthToken();
const { url, method, data, headers, callback, errorCallback, errorMessage, retryFunction } = config;
const requestBuilder = RequestService.sendRequest()
.url(url)
.method(method)
.header({
'Authorization': `Bearer ${token}`,
...headers
});
if (data) {
requestBuilder.data(data);
}
requestBuilder
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail((err) => {
console.error(errorMessage || '操作失败', err);
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
if (retryFunction) {
RequestService.reAjaxFun(() => {
retryFunction();
});
}
}).send();
}
/** /**
* 知识库管理相关API * 知识库管理相关API
*/ */
@@ -19,33 +67,20 @@ export default {
* @param {Function} errorCallback - 错误回调 * @param {Function} errorCallback - 错误回调
*/ */
getKnowledgeBaseList(params, callback, errorCallback) { getKnowledgeBaseList(params, callback, errorCallback) {
const token = getAuthToken();
const queryParams = new URLSearchParams({ const queryParams = new URLSearchParams({
page: params.page, page: params.page,
page_size: params.page_size, page_size: params.page_size,
name: params.name || '' name: params.name || ''
}).toString(); }).toString();
RequestService.sendRequest() makeApiRequest({
.url(`${getServiceUrl()}/api/v1/datasets?${queryParams}`) url: `${getServiceUrl()}/api/v1/datasets?${queryParams}`,
.method('GET') method: 'GET',
.header({ 'Authorization': `Bearer ${token}` }) callback: callback,
.success((res) => { errorCallback: errorCallback,
RequestService.clearRequestTime(); errorMessage: '获取知识库列表失败',
callback(res); retryFunction: () => this.getKnowledgeBaseList(params, callback, errorCallback)
}) });
.fail((err) => {
console.error('获取知识库列表失败:', err);
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
const self = this;
RequestService.reAjaxFun(() => {
self.getKnowledgeBaseList(params, callback, errorCallback);
});
}).send();
}, },
/** /**
@@ -56,20 +91,18 @@ export default {
*/ */
createKnowledgeBase(data, callback, errorCallback) { createKnowledgeBase(data, callback, errorCallback) {
console.log('createKnowledgeBase called with data:', data); console.log('createKnowledgeBase called with data:', data);
const token = getAuthToken();
console.log('Token exists:', !!token);
console.log('API URL:', `${getServiceUrl()}/api/v1/datasets`); console.log('API URL:', `${getServiceUrl()}/api/v1/datasets`);
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/datasets`) makeApiRequest({
.method('POST') url: `${getServiceUrl()}/api/v1/datasets`,
.data(data) method: 'POST',
.header({ 'Authorization': `Bearer ${token}` }) data: data,
.success((res) => { headers: { 'Content-Type': 'application/json' },
callback: (res) => {
console.log('createKnowledgeBase success response:', res); console.log('createKnowledgeBase success response:', res);
RequestService.clearRequestTime();
callback(res); callback(res);
}) },
.fail((err) => { errorCallback: (err) => {
console.error('创建知识库失败:', err); console.error('创建知识库失败:', err);
if (err.response) { if (err.response) {
console.error('Error response data:', err.response.data); console.error('Error response data:', err.response.data);
@@ -78,14 +111,10 @@ export default {
if (errorCallback) { if (errorCallback) {
errorCallback(err); errorCallback(err);
} }
}) },
.networkFail(() => { errorMessage: '创建知识库失败',
console.log('Network failure, retrying...'); retryFunction: () => this.createKnowledgeBase(data, callback, errorCallback)
const self = this; });
RequestService.reAjaxFun(() => {
self.createKnowledgeBase(data, callback, errorCallback);
});
}).send();
}, },
/** /**
@@ -97,30 +126,18 @@ export default {
*/ */
updateKnowledgeBase(datasetId, data, callback, errorCallback) { updateKnowledgeBase(datasetId, data, callback, errorCallback) {
console.log('updateKnowledgeBase called with datasetId:', datasetId, 'data:', data); console.log('updateKnowledgeBase called with datasetId:', datasetId, 'data:', data);
const token = getAuthToken();
console.log('Token exists:', !!token);
console.log('API URL:', `${getServiceUrl()}/api/v1/datasets/${datasetId}`); console.log('API URL:', `${getServiceUrl()}/api/v1/datasets/${datasetId}`);
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/datasets/${datasetId}`) makeApiRequest({
.method('PUT') url: `${getServiceUrl()}/api/v1/datasets/${datasetId}`,
.data(data) method: 'PUT',
.header({ 'Authorization': `Bearer ${token}` }) data: data,
.success((res) => { headers: { 'Content-Type': 'application/json' },
RequestService.clearRequestTime(); callback: callback,
callback(res); errorCallback: errorCallback,
}) errorMessage: '更新知识库失败',
.fail((err) => { retryFunction: () => this.updateKnowledgeBase(datasetId, data, callback, errorCallback)
console.error('更新知识库失败:', err); });
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
const self = this;
RequestService.reAjaxFun(() => {
self.updateKnowledgeBase(datasetId, data, callback, errorCallback);
});
}).send();
}, },
/** /**
@@ -131,29 +148,16 @@ export default {
*/ */
deleteKnowledgeBase(datasetId, callback, errorCallback) { deleteKnowledgeBase(datasetId, callback, errorCallback) {
console.log('deleteKnowledgeBase called with datasetId:', datasetId); console.log('deleteKnowledgeBase called with datasetId:', datasetId);
const token = getAuthToken();
console.log('Token exists:', !!token);
console.log('API URL:', `${getServiceUrl()}/api/v1/datasets/${datasetId}`); console.log('API URL:', `${getServiceUrl()}/api/v1/datasets/${datasetId}`);
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/datasets/${datasetId}`) makeApiRequest({
.method('DELETE') url: `${getServiceUrl()}/api/v1/datasets/${datasetId}`,
.header({ 'Authorization': `Bearer ${token}` }) method: 'DELETE',
.success((res) => { callback: callback,
RequestService.clearRequestTime(); errorCallback: errorCallback,
callback(res); errorMessage: '删除知识库失败',
}) retryFunction: () => this.deleteKnowledgeBase(datasetId, callback, errorCallback)
.fail((err) => { });
console.error('删除知识库失败:', err);
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
const self = this;
RequestService.reAjaxFun(() => {
self.deleteKnowledgeBase(datasetId, callback, errorCallback);
});
}).send();
}, },
/** /**
@@ -163,29 +167,17 @@ export default {
* @param {Function} errorCallback - 错误回调 * @param {Function} errorCallback - 错误回调
*/ */
deleteKnowledgeBases(ids, callback, errorCallback) { deleteKnowledgeBases(ids, callback, errorCallback) {
const token = getAuthToken();
// 确保ids是正确格式的字符串 // 确保ids是正确格式的字符串
const idsStr = Array.isArray(ids) ? ids.join(',') : ids; const idsStr = Array.isArray(ids) ? ids.join(',') : ids;
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/datasets/batch?ids=${idsStr}`) makeApiRequest({
.method('DELETE') url: `${getServiceUrl()}/api/v1/datasets/batch?ids=${idsStr}`,
.header({ 'Authorization': `Bearer ${token}` }) method: 'DELETE',
.success((res) => { callback: callback,
RequestService.clearRequestTime(); errorCallback: errorCallback,
callback(res); errorMessage: '批量删除知识库失败',
}) retryFunction: () => this.deleteKnowledgeBases(ids, callback, errorCallback)
.fail((err) => { });
console.error('批量删除知识库失败:', err);
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
const self = this;
RequestService.reAjaxFun(() => {
self.deleteKnowledgeBases(ids, callback, errorCallback);
});
}).send();
}, },
/** /**
@@ -196,33 +188,20 @@ export default {
* @param {Function} errorCallback - 错误回调 * @param {Function} errorCallback - 错误回调
*/ */
getDocumentList(datasetId, params, callback, errorCallback) { getDocumentList(datasetId, params, callback, errorCallback) {
const token = getAuthToken();
const queryParams = new URLSearchParams({ const queryParams = new URLSearchParams({
page: params.page, page: params.page,
page_size: params.page_size, page_size: params.page_size,
name: params.name || '' name: params.name || ''
}).toString(); }).toString();
RequestService.sendRequest() makeApiRequest({
.url(`${getServiceUrl()}/api/v1/datasets/${datasetId}/documents?${queryParams}`) url: `${getServiceUrl()}/api/v1/datasets/${datasetId}/documents?${queryParams}`,
.method('GET') method: 'GET',
.header({ 'Authorization': `Bearer ${token}` }) callback: callback,
.success((res) => { errorCallback: errorCallback,
RequestService.clearRequestTime(); errorMessage: '获取文档列表失败',
callback(res); retryFunction: () => this.getDocumentList(datasetId, params, callback, errorCallback)
}) });
.fail((err) => {
console.error('获取文档列表失败:', err);
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
const self = this;
RequestService.reAjaxFun(() => {
self.getDocumentList(datasetId, params, callback, errorCallback);
});
}).send();
}, },
/** /**
@@ -233,31 +212,16 @@ export default {
* @param {Function} errorCallback - 错误回调 * @param {Function} errorCallback - 错误回调
*/ */
uploadDocument(datasetId, formData, callback, errorCallback) { uploadDocument(datasetId, formData, callback, errorCallback) {
const token = getAuthToken(); makeApiRequest({
RequestService.sendRequest() url: `${getServiceUrl()}/api/v1/datasets/${datasetId}/documents`,
.url(`${getServiceUrl()}/api/v1/datasets/${datasetId}/documents`) method: 'POST',
.method('POST') data: formData,
.data(formData) headers: { 'Content-Type': 'multipart/form-data' },
.header({ callback: callback,
'Authorization': `Bearer ${token}`, errorCallback: errorCallback,
'Content-Type': 'multipart/form-data' errorMessage: '上传文档失败',
}) retryFunction: () => this.uploadDocument(datasetId, formData, callback, errorCallback)
.success((res) => { });
RequestService.clearRequestTime();
callback(res);
})
.fail((err) => {
console.error('上传文档失败:', err);
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
const self = this;
RequestService.reAjaxFun(() => {
self.uploadDocument(datasetId, formData, callback, errorCallback);
});
}).send();
}, },
/** /**
@@ -268,35 +232,20 @@ export default {
* @param {Function} errorCallback - 错误回调 * @param {Function} errorCallback - 错误回调
*/ */
parseDocument(datasetId, documentId, callback, errorCallback) { parseDocument(datasetId, documentId, callback, errorCallback) {
const token = getAuthToken();
const requestBody = { const requestBody = {
document_ids: [documentId] document_ids: [documentId]
}; };
RequestService.sendRequest() makeApiRequest({
.url(`${getServiceUrl()}/api/v1/datasets/${datasetId}/chunks`) url: `${getServiceUrl()}/api/v1/datasets/${datasetId}/chunks`,
.method('POST') method: 'POST',
.data(requestBody) data: requestBody,
.header({ headers: { 'Content-Type': 'application/json' },
'Authorization': `Bearer ${token}`, callback: callback,
'Content-Type': 'application/json' errorCallback: errorCallback,
}) errorMessage: '解析文档失败',
.success((res) => { retryFunction: () => this.parseDocument(datasetId, documentId, callback, errorCallback)
RequestService.clearRequestTime(); });
callback(res);
})
.fail((err) => {
console.error('解析文档失败:', err);
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
const self = this;
RequestService.reAjaxFun(() => {
self.parseDocument(datasetId, documentId, callback, errorCallback);
});
}).send();
}, },
/** /**
@@ -307,27 +256,14 @@ export default {
* @param {Function} errorCallback - 错误回调 * @param {Function} errorCallback - 错误回调
*/ */
deleteDocument(datasetId, documentId, callback, errorCallback) { deleteDocument(datasetId, documentId, callback, errorCallback) {
const token = getAuthToken(); makeApiRequest({
RequestService.sendRequest() url: `${getServiceUrl()}/api/v1/datasets/${datasetId}/documents/${documentId}`,
.url(`${getServiceUrl()}/api/v1/datasets/${datasetId}/documents/${documentId}`) method: 'DELETE',
.method('DELETE') callback: callback,
.header({ 'Authorization': `Bearer ${token}` }) errorCallback: errorCallback,
.success((res) => { errorMessage: '删除文档失败',
RequestService.clearRequestTime(); retryFunction: () => this.deleteDocument(datasetId, documentId, callback, errorCallback)
callback(res); });
})
.fail((err) => {
console.error('删除文档失败:', err);
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
const self = this;
RequestService.reAjaxFun(() => {
self.deleteDocument(datasetId, documentId, callback, errorCallback);
});
}).send();
}, },
/** /**
@@ -339,8 +275,7 @@ export default {
* @param {Function} errorCallback - 错误回调 * @param {Function} errorCallback - 错误回调
*/ */
listChunks(datasetId, documentId, params, callback, errorCallback) { listChunks(datasetId, documentId, params, callback, errorCallback) {
const token = getAuthToken(); let queryParams = new URLSearchParams({
const queryParams = new URLSearchParams({
page: params.page || 1, page: params.page || 1,
page_size: params.page_size || 10 page_size: params.page_size || 10
}).toString(); }).toString();
@@ -350,26 +285,14 @@ export default {
queryParams += `&keywords=${encodeURIComponent(params.keywords)}`; queryParams += `&keywords=${encodeURIComponent(params.keywords)}`;
} }
RequestService.sendRequest() makeApiRequest({
.url(`${getServiceUrl()}/api/v1/datasets/${datasetId}/documents/${documentId}/chunks?${queryParams}`) url: `${getServiceUrl()}/api/v1/datasets/${datasetId}/documents/${documentId}/chunks?${queryParams}`,
.method('GET') method: 'GET',
.header({ 'Authorization': `Bearer ${token}` }) callback: callback,
.success((res) => { errorCallback: errorCallback,
RequestService.clearRequestTime(); errorMessage: '获取切片列表失败',
callback(res); retryFunction: () => this.listChunks(datasetId, documentId, params, callback, errorCallback)
}) });
.fail((err) => {
console.error('获取切片列表失败:', err);
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
const self = this;
RequestService.reAjaxFun(() => {
self.listChunks(datasetId, documentId, params, callback, errorCallback);
});
}).send();
}, },
/** /**
@@ -381,28 +304,16 @@ export default {
* @param {Function} errorCallback - 错误回调 * @param {Function} errorCallback - 错误回调
*/ */
addChunk(datasetId, documentId, data, callback, errorCallback) { addChunk(datasetId, documentId, data, callback, errorCallback) {
const token = getAuthToken(); makeApiRequest({
RequestService.sendRequest() url: `${getServiceUrl()}/api/v1/datasets/${datasetId}/documents/${documentId}/chunks`,
.url(`${getServiceUrl()}/api/v1/datasets/${datasetId}/documents/${documentId}/chunks`) method: 'POST',
.method('POST') data: data,
.data(data) headers: { 'Content-Type': 'application/json' },
.header({ 'Authorization': `Bearer ${token}` }) callback: callback,
.success((res) => { errorCallback: errorCallback,
RequestService.clearRequestTime(); errorMessage: '添加切片失败',
callback(res); retryFunction: () => this.addChunk(datasetId, documentId, data, callback, errorCallback)
}) });
.fail((err) => {
console.error('添加切片失败:', err);
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
const self = this;
RequestService.reAjaxFun(() => {
self.addChunk(datasetId, documentId, data, callback, errorCallback);
});
}).send();
}, },
/** /**
@@ -413,28 +324,16 @@ export default {
* @param {Function} errorCallback - 错误回调 * @param {Function} errorCallback - 错误回调
*/ */
retrievalTest(datasetId, data, callback, errorCallback) { retrievalTest(datasetId, data, callback, errorCallback) {
const token = getAuthToken(); makeApiRequest({
RequestService.sendRequest() url: `${getServiceUrl()}/api/v1/datasets/${datasetId}/retrieval-test`,
.url(`${getServiceUrl()}/api/v1/datasets/${datasetId}/retrieval-test`) method: 'POST',
.method('POST') data: data,
.data(data) headers: { 'Content-Type': 'application/json' },
.header({ 'Authorization': `Bearer ${token}` }) callback: callback,
.success((res) => { errorCallback: errorCallback,
RequestService.clearRequestTime(); errorMessage: '召回测试失败',
callback(res); retryFunction: () => this.retrievalTest(datasetId, data, callback, errorCallback)
}) });
.fail((err) => {
console.error('召回测试失败:', err);
if (errorCallback) {
errorCallback(err);
}
})
.networkFail(() => {
const self = this;
RequestService.reAjaxFun(() => {
self.retrievalTest(datasetId, data, callback, errorCallback);
});
}).send();
} }
}; };
+1 -1
View File
@@ -1206,7 +1206,7 @@ export default {
'knowledgeFileUpload.documentNamePlaceholder': 'Please enter document name', 'knowledgeFileUpload.documentNamePlaceholder': 'Please enter document name',
'knowledgeFileUpload.file': 'File', 'knowledgeFileUpload.file': 'File',
'knowledgeFileUpload.clickToUpload': 'Click to upload', 'knowledgeFileUpload.clickToUpload': 'Click to upload',
'knowledgeFileUpload.uploadTip': 'Only pdf, doc, docx, txt files are supported, and file size should not exceed 10MB', 'knowledgeFileUpload.uploadTip': 'Supported file types: PDF, DOC, DOCX, TXT, MD, CSV, XLS, XLSX, PPT, PPTX. Maximum 32 files per upload, each file size up to 10MB',
'knowledgeFileUpload.dragOrClick': 'Drag file here, or click to upload', 'knowledgeFileUpload.dragOrClick': 'Drag file here, or click to upload',
'knowledgeFileUpload.cancel': 'Cancel', 'knowledgeFileUpload.cancel': 'Cancel',
'knowledgeFileUpload.confirm': 'Confirm', 'knowledgeFileUpload.confirm': 'Confirm',
+1 -1
View File
@@ -1206,7 +1206,7 @@ export default {
'knowledgeFileUpload.documentNamePlaceholder': '请输入文档名称', 'knowledgeFileUpload.documentNamePlaceholder': '请输入文档名称',
'knowledgeFileUpload.file': '文件', 'knowledgeFileUpload.file': '文件',
'knowledgeFileUpload.clickToUpload': '点击上传', 'knowledgeFileUpload.clickToUpload': '点击上传',
'knowledgeFileUpload.uploadTip': '只能上传pdf、doc、docx、txt文件,且不超过10MB', 'knowledgeFileUpload.uploadTip': '支持的文档类型:PDF、DOC、DOCX、TXT、MD、CSV、XLS、XLSX、PPT、PPTX,单次批量上传文件数不超过 32 个',
'knowledgeFileUpload.dragOrClick': '将文件拖到此处,或点击上传', 'knowledgeFileUpload.dragOrClick': '将文件拖到此处,或点击上传',
'knowledgeFileUpload.cancel': '取消', 'knowledgeFileUpload.cancel': '取消',
'knowledgeFileUpload.confirm': '确定', 'knowledgeFileUpload.confirm': '确定',
+1 -1
View File
@@ -1206,7 +1206,7 @@ export default {
'knowledgeFileUpload.documentNamePlaceholder': '請輸入文檔名稱', 'knowledgeFileUpload.documentNamePlaceholder': '請輸入文檔名稱',
'knowledgeFileUpload.file': '文件', 'knowledgeFileUpload.file': '文件',
'knowledgeFileUpload.clickToUpload': '點擊上傳', 'knowledgeFileUpload.clickToUpload': '點擊上傳',
'knowledgeFileUpload.uploadTip': '只能上傳pdf、doc、docx、txt文件,且不超過10MB', 'knowledgeFileUpload.uploadTip': '支持的文件類型:PDF、DOC、DOCX、TXT、MD、CSV、XLS、XLSX、PPT、PPTX,单次批量上傳文件數不超過 32 個,每個文件大小不超過 10MB',
'knowledgeFileUpload.dragOrClick': '將文件拖到此處,或點擊上傳', 'knowledgeFileUpload.dragOrClick': '將文件拖到此處,或點擊上傳',
'knowledgeFileUpload.cancel': '取消', 'knowledgeFileUpload.cancel': '取消',
'knowledgeFileUpload.confirm': '確定', 'knowledgeFileUpload.confirm': '確定',
@@ -15,7 +15,7 @@
<div class="content-panel"> <div class="content-panel">
<div class="content-area"> <div class="content-area">
<el-card class="params-card" shadow="never"> <el-card class="params-card" shadow="never">
<div class="table-wrapper"> <div>
<el-table ref="paramsTable" :data="knowledgeBaseList" class="transparent-table" v-loading="loading" <el-table ref="paramsTable" :data="knowledgeBaseList" class="transparent-table" v-loading="loading"
:element-loading-text="$t('common.loading')" element-loading-spinner="el-icon-loading" :element-loading-text="$t('common.loading')" element-loading-spinner="el-icon-loading"
element-loading-background="rgba(255, 255, 255, 0.7)" element-loading-background="rgba(255, 255, 255, 0.7)"
@@ -310,7 +310,6 @@ export default {
uploadDialogVisible: false, uploadDialogVisible: false,
uploading: false, uploading: false,
uploadForm: { uploadForm: {
name: '',
file: null file: null
}, },
uploadUrl: '', uploadUrl: '',
@@ -323,7 +322,6 @@ export default {
currentDocumentName: '', currentDocumentName: '',
sliceList: [], sliceList: [],
sliceLoading: false, sliceLoading: false,
sliceSearchKeyword: '',
sliceCurrentPage: 1, sliceCurrentPage: 1,
slicePageSize: 10, slicePageSize: 10,
sliceTotal: 0, sliceTotal: 0,
@@ -977,17 +975,6 @@ export default {
this.fetchSlices(); this.fetchSlices();
}, },
handleSliceDialogClose: function(done) {
this.sliceDialogVisible = false;
this.currentDocumentId = null;
this.currentDocumentName = '';
this.sliceList = [];
this.sliceTotal = 0;
if (done) {
done();
}
},
// 跳转到切片管理第一页 // 跳转到切片管理第一页
goToSliceFirstPage: function() { goToSliceFirstPage: function() {
if (this.sliceCurrentPage !== 1) { if (this.sliceCurrentPage !== 1) {
@@ -1433,9 +1420,9 @@ export default {
/* 拖拽上传区域样式 */ /* 拖拽上传区域样式 */
.document-uploader { .document-uploader {
:deep(.el-upload-dragger) { :deep(.el-upload-dragger) {
width: 100%; width: 600px;
height: 200px; height: 300px;
min-height: 200px; min-height: 300px;
border: 2px dashed #c0c4cc; border: 2px dashed #c0c4cc;
border-radius: 16px; border-radius: 16px;
cursor: pointer; cursor: pointer;
@@ -1993,10 +1980,6 @@ export default {
gap: 10px; gap: 10px;
} }
.slice-table {
margin-bottom: 20px;
}
.slice-pagination { .slice-pagination {
text-align: right; text-align: right;
margin-top: 20px; margin-top: 20px;
@@ -2007,172 +1990,6 @@ export default {
justify-content: flex-end; justify-content: flex-end;
gap: 5px; gap: 5px;
} }
:deep(.el-pagination) {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 8px;
.el-pagination__sizes {
margin-right: 8px;
.el-input__inner {
height: 32px;
line-height: 32px;
border-radius: 4px;
border: 1px solid #e4e7ed;
background: #dee7ff;
color: #606266;
font-size: 14px;
}
.el-input__suffix {
right: 6px;
width: 15px;
height: 20px;
display: flex;
justify-content: center;
align-items: center;
top: 6px;
border-radius: 4px;
}
.el-input__suffix-inner {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
}
.el-icon-arrow-up:before {
content: "";
display: inline-block;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-top: 9px solid #606266;
position: relative;
transform: rotate(0deg);
transition: transform 0.3s;
}
}
.btn-prev, .btn-next {
min-width: 60px;
height: 32px;
padding: 0 12px;
border-radius: 4px;
border: 1px solid #e4e7ed;
background: #dee7ff;
color: #606266;
font-size: 14px;
cursor: pointer;
transition: all 0.3s ease;
&:hover {
background: #d7dce6;
}
&:disabled {
opacity: 0.6;
cursor: not-allowed;
}
}
.el-pager {
display: flex;
gap: 4px;
.number {
min-width: 28px;
height: 32px;
padding: 0;
border-radius: 4px;
border: 1px solid transparent;
background: transparent;
color: #606266;
font-size: 14px;
cursor: pointer;
transition: all 0.3s ease;
&:hover {
background: rgba(245, 247, 250, 0.3);
}
&.active {
background: #5f70f3 !important;
color: #ffffff !important;
border-color: #5f70f3 !important;
&:hover {
background: #6d7cf5 !important;
}
}
}
}
.el-pagination__jump {
margin-left: 8px;
color: #606266;
font-size: 14px;
.el-pagination__editor {
width: 50px;
margin: 0 8px;
.el-input__inner {
height: 32px;
line-height: 32px;
border-radius: 4px;
border: 1px solid #e4e7ed;
background: #dee7ff;
color: #606266;
font-size: 14px;
}
}
}
.el-pagination__total {
margin-right: 8px;
color: #606266;
font-size: 14px;
}
}
}
.slice-content {
max-height: 300px;
min-height: 60px;
overflow-y: auto;
word-break: break-all;
line-height: 1.4;
padding: 8px 12px;
border: 1px solid #e4e7ed;
border-radius: 4px;
background-color: #f8f9fa;
white-space: pre-wrap;
}
.slice-keywords, .slice-questions {
max-height: 60px;
overflow-y: auto;
}
.slice-tag {
margin: 2px;
}
.add-slice-form {
padding: 20px 0;
}
.form-item {
margin-bottom: 20px;
}
.form-actions {
text-align: right;
margin-top: 20px;
} }
@media (min-width: 1144px) { @media (min-width: 1144px) {