mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
uptate:根据知识库id获取rag配置,删除获取默认rag配置方法
This commit is contained in:
+4
-3
@@ -71,11 +71,12 @@ public interface KnowledgeBaseService extends BaseService<KnowledgeBaseEntity> {
|
||||
Map<String, Object> getRAGConfig(String ragModelId);
|
||||
|
||||
/**
|
||||
* 获取默认RAG配置信息
|
||||
* 根据知识库ID获取对应的RAG配置
|
||||
*
|
||||
* @return 默认RAG配置信息
|
||||
* @param datasetId 知识库ID
|
||||
* @return RAG配置
|
||||
*/
|
||||
Map<String, Object> getDefaultRAGConfig();
|
||||
Map<String, Object> getRAGConfigByDatasetId(String datasetId);
|
||||
|
||||
/**
|
||||
* 获取RAG模型列表
|
||||
|
||||
-7
@@ -74,13 +74,6 @@ public interface KnowledgeFilesService {
|
||||
*/
|
||||
Map<String, Object> getRAGConfig(String ragModelId);
|
||||
|
||||
/**
|
||||
* 获取默认RAG配置信息
|
||||
*
|
||||
* @return 默认RAG配置信息
|
||||
*/
|
||||
Map<String, Object> getDefaultRAGConfig();
|
||||
|
||||
/**
|
||||
* 解析文档(切块)
|
||||
*
|
||||
|
||||
+21
-24
@@ -315,7 +315,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
// API删除成功后再删除本地记录
|
||||
if (apiDeleteSuccess) {
|
||||
log.info("开始删除ai_agent_plugin_mapping表中与知识库ID '{}' 相关的映射记录", entity.getId());
|
||||
|
||||
|
||||
// 先删除相关的插件映射记录
|
||||
knowledgeBaseDao.deletePluginMappingByKnowledgeBaseId(entity.getId());
|
||||
log.info("插件映射记录删除完成");
|
||||
@@ -354,29 +354,27 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getDefaultRAGConfig() {
|
||||
// 获取默认RAG模型配置
|
||||
QueryWrapper<ModelConfigEntity> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("model_type", Constant.RAG_CONFIG_TYPE)
|
||||
.eq("is_default", 1)
|
||||
.eq("is_enabled", 1);
|
||||
|
||||
List<ModelConfigEntity> modelConfigs = modelConfigDao.selectList(queryWrapper);
|
||||
if (modelConfigs == null || modelConfigs.isEmpty()) {
|
||||
throw new RenException(ErrorCode.RAG_DEFAULT_CONFIG_NOT_FOUND);
|
||||
public Map<String, Object> getRAGConfigByDatasetId(String datasetId) {
|
||||
if (StringUtils.isBlank(datasetId)) {
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR, "datasetId不能为空");
|
||||
}
|
||||
|
||||
ModelConfigEntity defaultConfig = modelConfigs.get(0);
|
||||
if (defaultConfig.getConfigJson() == null) {
|
||||
throw new RenException(ErrorCode.RAG_CONFIG_NOT_FOUND);
|
||||
// 根据datasetId查询知识库信息
|
||||
KnowledgeBaseDTO knowledgeBase = getByDatasetId(datasetId);
|
||||
if (knowledgeBase == null) {
|
||||
log.warn("未找到datasetId为{}的知识库", datasetId);
|
||||
throw new RenException(ErrorCode.Knowledge_Base_RECORD_NOT_EXISTS);
|
||||
}
|
||||
|
||||
Map<String, Object> config = defaultConfig.getConfigJson();
|
||||
// 如果知识库指定了ragModelId,使用该配置
|
||||
String ragModelId = knowledgeBase.getRagModelId();
|
||||
if (StringUtils.isBlank(ragModelId)) {
|
||||
log.warn("知识库datasetId为{}未配置ragModelId", datasetId);
|
||||
throw new RenException(ErrorCode.RAG_CONFIG_NOT_FOUND, "知识库未配置RAG模型");
|
||||
}
|
||||
|
||||
// 验证必要的配置参数
|
||||
validateRagConfig(config);
|
||||
|
||||
return config;
|
||||
// 获取并返回RAG配置
|
||||
return getRAGConfig(ragModelId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -726,13 +724,12 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
* 获取RAG配置并验证
|
||||
*/
|
||||
private Map<String, Object> getValidatedRAGConfig(String ragModelId) {
|
||||
Map<String, Object> ragConfig;
|
||||
if (StringUtils.isNotBlank(ragModelId)) {
|
||||
ragConfig = getRAGConfig(ragModelId);
|
||||
} else {
|
||||
ragConfig = getDefaultRAGConfig();
|
||||
if (StringUtils.isBlank(ragModelId)) {
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR, "ragModelId不能为空");
|
||||
}
|
||||
|
||||
Map<String, Object> ragConfig = getRAGConfig(ragModelId);
|
||||
|
||||
// 验证RAG配置参数
|
||||
validateRagConfig(ragConfig);
|
||||
|
||||
|
||||
+21
-72
@@ -24,31 +24,31 @@ import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xiaozhi.common.constant.Constant;
|
||||
import xiaozhi.common.exception.ErrorCode;
|
||||
import xiaozhi.common.exception.RenException;
|
||||
import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.modules.knowledge.dto.KnowledgeFilesDTO;
|
||||
import xiaozhi.modules.knowledge.service.KnowledgeBaseService;
|
||||
import xiaozhi.modules.knowledge.service.KnowledgeFilesService;
|
||||
import xiaozhi.modules.model.dao.ModelConfigDao;
|
||||
import xiaozhi.modules.model.entity.ModelConfigEntity;
|
||||
import xiaozhi.modules.model.service.ModelConfigService;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@Slf4j
|
||||
public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
private final ModelConfigService modelConfigService;
|
||||
private final ModelConfigDao modelConfigDao;
|
||||
private final KnowledgeBaseService knowledgeBaseService;
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
private ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getRAGConfig(String ragModelId) {
|
||||
return knowledgeBaseService.getRAGConfig(ragModelId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<KnowledgeFilesDTO> getPageList(KnowledgeFilesDTO knowledgeFilesDTO, Integer page, Integer limit) {
|
||||
try {
|
||||
@@ -60,17 +60,17 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
knowledgeFilesDTO != null ? knowledgeFilesDTO.getStatus() : null,
|
||||
page, limit);
|
||||
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
// 构建请求URL - 根据RAGFlow API文档,获取文档列表的接口
|
||||
String datasetId = knowledgeFilesDTO != null ? knowledgeFilesDTO.getDatasetId() : null;
|
||||
if (StringUtils.isBlank(datasetId)) {
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR, "datasetId不能为空");
|
||||
}
|
||||
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = knowledgeBaseService.getRAGConfigByDatasetId(datasetId);
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
String url = baseUrl + "/api/v1/datasets/" + datasetId + "/documents";
|
||||
|
||||
// 添加查询参数
|
||||
@@ -253,7 +253,8 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
// 调用RAGFlow API获取文档切片信息
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
String datasetId = dto.getDatasetId();
|
||||
Map<String, Object> ragConfig = knowledgeBaseService.getRAGConfigByDatasetId(datasetId);
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -449,7 +450,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
Map<String, Object> ragConfig = knowledgeBaseService.getRAGConfigByDatasetId(datasetId);
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -528,7 +529,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
Map<String, Object> ragConfig = knowledgeBaseService.getRAGConfigByDatasetId(datasetId);
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -748,58 +749,6 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getRAGConfig(String ragModelId) {
|
||||
if (StringUtils.isBlank(ragModelId)) {
|
||||
throw new RenException(ErrorCode.PARAMS_GET_ERROR);
|
||||
}
|
||||
|
||||
// 从缓存获取模型配置
|
||||
ModelConfigEntity modelConfig = modelConfigService.getModelByIdFromCache(ragModelId);
|
||||
if (modelConfig == null || modelConfig.getConfigJson() == null) {
|
||||
throw new RenException(ErrorCode.RAG_CONFIG_NOT_FOUND);
|
||||
}
|
||||
|
||||
// 验证是否为RAG类型配置
|
||||
if (!Constant.RAG_CONFIG_TYPE.equals(modelConfig.getModelType().toUpperCase())) {
|
||||
throw new RenException(ErrorCode.RAG_CONFIG_TYPE_ERROR);
|
||||
}
|
||||
|
||||
Map<String, Object> config = modelConfig.getConfigJson();
|
||||
|
||||
// 验证必要的配置参数
|
||||
validateRagConfig(config);
|
||||
|
||||
// 返回配置信息
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getDefaultRAGConfig() {
|
||||
// 获取默认RAG模型配置
|
||||
QueryWrapper<ModelConfigEntity> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("model_type", Constant.RAG_CONFIG_TYPE)
|
||||
.eq("is_default", 1)
|
||||
.eq("is_enabled", 1);
|
||||
|
||||
List<ModelConfigEntity> modelConfigs = modelConfigDao.selectList(queryWrapper);
|
||||
if (modelConfigs == null || modelConfigs.isEmpty()) {
|
||||
throw new RenException(ErrorCode.RAG_DEFAULT_CONFIG_NOT_FOUND);
|
||||
}
|
||||
|
||||
ModelConfigEntity defaultConfig = modelConfigs.get(0);
|
||||
if (defaultConfig.getConfigJson() == null) {
|
||||
throw new RenException(ErrorCode.RAG_CONFIG_NOT_FOUND);
|
||||
}
|
||||
|
||||
Map<String, Object> config = defaultConfig.getConfigJson();
|
||||
|
||||
// 验证必要的配置参数
|
||||
validateRagConfig(config);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证RAG配置中是否包含必要的参数
|
||||
*/
|
||||
@@ -826,7 +775,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
Map<String, Object> parserConfig) {
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
Map<String, Object> ragConfig = knowledgeBaseService.getRAGConfigByDatasetId(datasetId);
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -1017,7 +966,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
private void deleteDocumentInRAGFlow(String documentId, String datasetId) {
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
Map<String, Object> ragConfig = knowledgeBaseService.getRAGConfigByDatasetId(datasetId);
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -1141,7 +1090,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
Map<String, Object> ragConfig = knowledgeBaseService.getRAGConfigByDatasetId(datasetId);
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -1217,7 +1166,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
Map<String, Object> ragConfig = knowledgeBaseService.getRAGConfigByDatasetId(datasetId);
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
@@ -1616,7 +1565,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
|
||||
|
||||
try {
|
||||
// 获取RAG配置
|
||||
Map<String, Object> ragConfig = getDefaultRAGConfig();
|
||||
Map<String, Object> ragConfig = knowledgeBaseService.getRAGConfigByDatasetId(datasetIds.get(0));
|
||||
String baseUrl = (String) ragConfig.get("base_url");
|
||||
String apiKey = (String) ragConfig.get("api_key");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user