mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 23:23:55 +08:00
update:意图识别插件增加知识库
This commit is contained in:
@@ -152,4 +152,11 @@ public class RedisKeys {
|
||||
public static String getVoiceCloneAudioIdKey(String uuid) {
|
||||
return "voiceClone:audio:id:" + uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取知识库缓存key
|
||||
*/
|
||||
public static String getKnowledgeBaseCacheKey(String datasetId) {
|
||||
return "knowledge:base:" + datasetId;
|
||||
}
|
||||
}
|
||||
|
||||
+38
-1
@@ -1,9 +1,13 @@
|
||||
package xiaozhi.modules.agent.service.impl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.alibaba.druid.support.json.JSONUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
|
||||
@@ -11,6 +15,10 @@ import lombok.RequiredArgsConstructor;
|
||||
import xiaozhi.modules.agent.dao.AgentPluginMappingMapper;
|
||||
import xiaozhi.modules.agent.entity.AgentPluginMapping;
|
||||
import xiaozhi.modules.agent.service.AgentPluginMappingService;
|
||||
import xiaozhi.modules.knowledge.entity.KnowledgeBaseEntity;
|
||||
import xiaozhi.modules.knowledge.service.KnowledgeBaseService;
|
||||
import xiaozhi.modules.model.entity.ModelConfigEntity;
|
||||
import xiaozhi.modules.model.service.ModelConfigService;
|
||||
|
||||
/**
|
||||
* @description 针对表【ai_agent_plugin_mapping(Agent与插件的唯一映射表)】的数据库操作Service实现
|
||||
@@ -21,10 +29,39 @@ import xiaozhi.modules.agent.service.AgentPluginMappingService;
|
||||
public class AgentPluginMappingServiceImpl extends ServiceImpl<AgentPluginMappingMapper, AgentPluginMapping>
|
||||
implements AgentPluginMappingService {
|
||||
private final AgentPluginMappingMapper agentPluginMappingMapper;
|
||||
private final KnowledgeBaseService knowledgeBaseService;
|
||||
private final ModelConfigService modelConfigService;
|
||||
|
||||
@Override
|
||||
public List<AgentPluginMapping> agentPluginParamsByAgentId(String agentId) {
|
||||
return agentPluginMappingMapper.selectPluginsByAgentId(agentId);
|
||||
List<AgentPluginMapping> list = agentPluginMappingMapper.selectPluginsByAgentId(agentId);
|
||||
int index = 0;
|
||||
for (int i = list.size() - 1; i >= 0; i--) {
|
||||
AgentPluginMapping mapping = list.get(i);
|
||||
if (StringUtils.isBlank(mapping.getProviderCode())) {
|
||||
// 查询知识库插件参数
|
||||
KnowledgeBaseEntity knowledgeBaseEntity = knowledgeBaseService.selectById(mapping.getPluginId());
|
||||
if (knowledgeBaseEntity == null) {
|
||||
list.remove(i);
|
||||
continue;
|
||||
}
|
||||
ModelConfigEntity modelConfigEntity = modelConfigService
|
||||
.getModelByIdFromCache(knowledgeBaseEntity.getRagModelId());
|
||||
if (modelConfigEntity == null) {
|
||||
list.remove(i);
|
||||
continue;
|
||||
}
|
||||
Map<String, String> paramInfo = new HashMap<>(2);
|
||||
paramInfo.put("name", knowledgeBaseEntity.getName());
|
||||
paramInfo.put("description", knowledgeBaseEntity.getDescription());
|
||||
mapping.setParamInfo(JSONUtils.toJSONString(paramInfo));
|
||||
String providerCode = "xzmcp_search_from_knowledgeBase_" + modelConfigEntity.getModelCode() + "_"
|
||||
+ index;
|
||||
index++;
|
||||
mapping.setProviderCode(providerCode);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+40
@@ -1,6 +1,7 @@
|
||||
package xiaozhi.modules.knowledge.service.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -28,6 +29,8 @@ import xiaozhi.common.constant.Constant;
|
||||
import xiaozhi.common.exception.ErrorCode;
|
||||
import xiaozhi.common.exception.RenException;
|
||||
import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.common.redis.RedisKeys;
|
||||
import xiaozhi.common.redis.RedisUtils;
|
||||
import xiaozhi.common.service.impl.BaseServiceImpl;
|
||||
import xiaozhi.common.utils.ConvertUtils;
|
||||
import xiaozhi.modules.knowledge.dao.KnowledgeBaseDao;
|
||||
@@ -47,8 +50,34 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
private final KnowledgeBaseDao knowledgeBaseDao;
|
||||
private final ModelConfigService modelConfigService;
|
||||
private final ModelConfigDao modelConfigDao;
|
||||
private final RedisUtils redisUtils;
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
@Override
|
||||
public KnowledgeBaseEntity selectById(Serializable datasetId) {
|
||||
if (datasetId == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 先从Redis获取缓存
|
||||
String key = RedisKeys.getKnowledgeBaseCacheKey(datasetId.toString());
|
||||
KnowledgeBaseEntity cachedEntity = (KnowledgeBaseEntity) redisUtils.get(key);
|
||||
if (cachedEntity != null) {
|
||||
return cachedEntity;
|
||||
}
|
||||
|
||||
// 如果缓存中没有,则从数据库获取
|
||||
KnowledgeBaseEntity entity = knowledgeBaseDao.selectById(datasetId);
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 存入Redis缓存
|
||||
redisUtils.set(key, entity);
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<KnowledgeBaseDTO> getPageList(KnowledgeBaseDTO knowledgeBaseDTO, Integer page, Integer limit) {
|
||||
long curPage = page;
|
||||
@@ -169,6 +198,11 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
KnowledgeBaseEntity entity = ConvertUtils.sourceToTarget(knowledgeBaseDTO, KnowledgeBaseEntity.class);
|
||||
knowledgeBaseDao.updateById(entity);
|
||||
|
||||
// 删除缓存
|
||||
if (entity.getDatasetId() != null) {
|
||||
redisUtils.delete(RedisKeys.getKnowledgeBaseCacheKey(entity.getDatasetId()));
|
||||
}
|
||||
|
||||
// 调用RAGFlow API更新数据集
|
||||
if (StringUtils.isNotBlank(knowledgeBaseDTO.getDatasetId())) {
|
||||
try {
|
||||
@@ -194,6 +228,12 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
||||
throw new RenException(ErrorCode.IDENTIFIER_NOT_NULL);
|
||||
}
|
||||
|
||||
// 先获取实体以获取datasetId用于删除缓存
|
||||
KnowledgeBaseEntity tempEntity = knowledgeBaseDao.selectById(id);
|
||||
if (tempEntity != null && tempEntity.getDatasetId() != null) {
|
||||
redisUtils.delete(RedisKeys.getKnowledgeBaseCacheKey(tempEntity.getDatasetId()));
|
||||
}
|
||||
|
||||
log.info("=== 开始删除操作 ===");
|
||||
log.info("删除ID: {}", id);
|
||||
|
||||
|
||||
+33
-1
@@ -22,6 +22,8 @@ import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.common.service.impl.BaseServiceImpl;
|
||||
import xiaozhi.common.user.UserDetail;
|
||||
import xiaozhi.common.utils.ConvertUtils;
|
||||
import xiaozhi.modules.knowledge.dao.KnowledgeBaseDao;
|
||||
import xiaozhi.modules.knowledge.entity.KnowledgeBaseEntity;
|
||||
import xiaozhi.modules.model.dao.ModelProviderDao;
|
||||
import xiaozhi.modules.model.dto.ModelProviderDTO;
|
||||
import xiaozhi.modules.model.entity.ModelProviderEntity;
|
||||
@@ -34,13 +36,43 @@ public class ModelProviderServiceImpl extends BaseServiceImpl<ModelProviderDao,
|
||||
implements ModelProviderService {
|
||||
|
||||
private final ModelProviderDao modelProviderDao;
|
||||
private final KnowledgeBaseDao knowledgeBaseDao;
|
||||
|
||||
@Override
|
||||
public List<ModelProviderDTO> getPluginList() {
|
||||
// 1. 获取插件列表
|
||||
LambdaQueryWrapper<ModelProviderEntity> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(ModelProviderEntity::getModelType, "Plugin");
|
||||
List<ModelProviderEntity> providerEntities = modelProviderDao.selectList(queryWrapper);
|
||||
return ConvertUtils.sourceToTarget(providerEntities, ModelProviderDTO.class);
|
||||
List<ModelProviderDTO> resultList = ConvertUtils.sourceToTarget(providerEntities, ModelProviderDTO.class);
|
||||
|
||||
// 2. 获取当前用户的知识库列表并追加到结果中
|
||||
UserDetail userDetail = SecurityUser.getUser();
|
||||
if (userDetail != null && userDetail.getId() != null) {
|
||||
// 查询当前用户的知识库
|
||||
LambdaQueryWrapper<KnowledgeBaseEntity> kbQueryWrapper = new LambdaQueryWrapper<>();
|
||||
kbQueryWrapper.eq(KnowledgeBaseEntity::getCreator, userDetail.getId());
|
||||
kbQueryWrapper.eq(KnowledgeBaseEntity::getStatus, 1); // 只获取启用状态的知识库
|
||||
List<KnowledgeBaseEntity> knowledgeBases = knowledgeBaseDao.selectList(kbQueryWrapper);
|
||||
|
||||
// 将知识库转换为ModelProviderDTO格式并添加到结果列表
|
||||
for (KnowledgeBaseEntity kb : knowledgeBases) {
|
||||
ModelProviderDTO dto = new ModelProviderDTO();
|
||||
dto.setId(kb.getId());
|
||||
dto.setModelType("Rag");
|
||||
dto.setName("[知识库]" + kb.getName());
|
||||
dto.setProviderCode("ragflow"); // 假设所有RAG都使用ragflow
|
||||
dto.setFields("[]");
|
||||
dto.setSort(0);
|
||||
dto.setCreateDate(kb.getCreatedAt());
|
||||
dto.setUpdateDate(kb.getUpdatedAt());
|
||||
dto.setCreator(0L);
|
||||
dto.setUpdater(0L);
|
||||
resultList.add(dto);
|
||||
}
|
||||
}
|
||||
|
||||
return resultList;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user