fix:知识库增加适配器

This commit is contained in:
rainv123
2025-11-20 09:38:31 +08:00
parent 5e48a2274a
commit 3caa4ec6e8
13 changed files with 2016 additions and 916 deletions
@@ -228,4 +228,16 @@ public interface ErrorCode {
// 智能体模板相关错误码(补充)
int AGENT_TEMPLATE_NOT_FOUND = 10183; // 默认智能体未找到
// 知识库适配器相关错误码
int RAG_ADAPTER_TYPE_NOT_SUPPORTED = 10184; // 不支持的适配器类型
int RAG_CONFIG_VALIDATION_FAILED = 10185; // RAG配置验证失败
int RAG_ADAPTER_CREATION_FAILED = 10186; // 适配器创建失败
int RAG_ADAPTER_INIT_FAILED = 10187; // 适配器初始化失败
int RAG_ADAPTER_CONNECTION_FAILED = 10188; // 适配器连接测试失败
int RAG_ADAPTER_OPERATION_FAILED = 10189; // 适配器操作失败
int RAG_ADAPTER_NOT_FOUND = 10190; // 适配器未找到
int RAG_ADAPTER_CACHE_ERROR = 10191; // 适配器缓存错误
int RAG_ADAPTER_TYPE_NOT_FOUND = 10192; // 适配器类型未找到
}
@@ -0,0 +1,22 @@
package xiaozhi.modules.knowledge.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xiaozhi.modules.knowledge.rag.KnowledgeBaseAdapterFactory;
/**
* 知识库配置类
* 配置知识库相关的Bean
*/
@Configuration
public class KnowledgeBaseConfig {
/**
* 提供KnowledgeBaseAdapterFactory的Bean实例
* @return KnowledgeBaseAdapterFactory实例
*/
@Bean
public KnowledgeBaseAdapterFactory knowledgeBaseAdapterFactory() {
return new KnowledgeBaseAdapterFactory();
}
}
@@ -77,7 +77,7 @@ public class KnowledgeFilesDTO implements Serializable {
return STATUS_UNSTART;
}
// 根据run字段的值直接映射到对应的状态码
// RAGFlow根据run字段的值直接映射到对应的状态码
switch (run.toUpperCase()) {
case "RUNNING":
return STATUS_RUNNING;
@@ -0,0 +1,200 @@
package xiaozhi.modules.knowledge.rag;
import java.util.List;
import java.util.Map;
import org.springframework.web.multipart.MultipartFile;
import xiaozhi.common.page.PageData;
import xiaozhi.modules.knowledge.dto.KnowledgeFilesDTO;
/**
* 知识库API适配器抽象基类
* 定义通用的知识库操作接口,支持多种后端API实现
*/
public abstract class KnowledgeBaseAdapter {
/**
* 获取适配器类型标识
*
* @return 适配器类型(如:ragflow, milvus, pinecone等)
*/
public abstract String getAdapterType();
/**
* 初始化适配器配置
*
* @param config 配置参数
*/
public abstract void initialize(Map<String, Object> config);
/**
* 验证配置是否有效
*
* @param config 配置参数
* @return 验证结果
*/
public abstract boolean validateConfig(Map<String, Object> config);
/**
* 分页查询文档列表
*
* @param datasetId 知识库ID
* @param queryParams 查询参数
* @param page 页码
* @param limit 每页数量
* @return 分页数据
*/
public abstract PageData<KnowledgeFilesDTO> getDocumentList(String datasetId,
Map<String, Object> queryParams,
Integer page,
Integer limit);
/**
* 根据文档ID获取文档详情
*
* @param datasetId 知识库ID
* @return 文档详情
*/
public abstract KnowledgeFilesDTO getDocumentById(String datasetId, String documentId);
/**
* 上传文档到知识库
*
* @param datasetId 知识库ID
* @param file 上传的文件
* @param name 文档名称
* @param metaFields 元数据字段
* @param chunkMethod 分块方法
* @param parserConfig 解析器配置
* @return 上传的文档信息
*/
public abstract KnowledgeFilesDTO uploadDocument(String datasetId,
MultipartFile file,
String name,
Map<String, Object> metaFields,
String chunkMethod,
Map<String, Object> parserConfig);
/**
* 根据状态分页查询文档列表
*
* @param datasetId 知识库ID
* @param status 文档解析状态
* @param page 页码
* @param limit 每页数量
* @return 分页数据
*/
public abstract PageData<KnowledgeFilesDTO> getDocumentListByStatus(String datasetId,
Integer status,
Integer page,
Integer limit);
/**
* 删除文档
*
* @param datasetId 知识库ID
* @param documentId 文档ID
*/
public abstract void deleteDocument(String datasetId, String documentId);
/**
* 解析文档(切块)
*
* @param datasetId 知识库ID
* @param documentIds 文档ID列表
* @return 解析结果
*/
public abstract boolean parseDocuments(String datasetId, List<String> documentIds);
/**
* 列出指定文档的切片
*
* @param datasetId 知识库ID
* @param documentId 文档ID
* @param keywords 关键词过滤
* @param page 页码
* @param pageSize 每页数量
* @param chunkId 切片ID
* @return 切片列表信息
*/
public abstract Map<String, Object> listChunks(String datasetId,
String documentId,
String keywords,
Integer page,
Integer pageSize,
String chunkId);
/**
* 召回测试 - 从知识库中检索相关切片
*
* @param question 用户查询
* @param datasetIds 数据集ID列表
* @param documentIds 文档ID列表
* @param retrievalParams 检索参数
* @return 召回测试结果
*/
public abstract Map<String, Object> retrievalTest(String question,
List<String> datasetIds,
List<String> documentIds,
Map<String, Object> retrievalParams);
/**
* 测试连接
*
* @return 连接测试结果
*/
public abstract boolean testConnection();
/**
* 获取适配器状态信息
*
* @return 状态信息
*/
public abstract Map<String, Object> getStatus();
/**
* 获取支持的配置参数
*
* @return 配置参数说明
*/
public abstract Map<String, Object> getSupportedConfig();
/**
* 获取默认配置
*
* @return 默认配置
*/
public abstract Map<String, Object> getDefaultConfig();
/**
* 创建数据集
*
* @param createParams 创建参数
* @return 数据集ID
*/
public abstract String createDataset(Map<String, Object> createParams);
/**
* 更新数据集
*
* @param datasetId 数据集ID
* @param updateParams 更新参数
*/
public abstract void updateDataset(String datasetId, Map<String, Object> updateParams);
/**
* 删除数据集
*
* @param datasetId 数据集ID
*/
public abstract void deleteDataset(String datasetId);
/**
* 获取数据集的文档数量
*
* @param datasetId 数据集ID
* @return 文档数量
*/
public abstract Integer getDocumentCount(String datasetId);
}
@@ -0,0 +1,197 @@
package xiaozhi.modules.knowledge.rag;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
import xiaozhi.common.exception.ErrorCode;
import xiaozhi.common.exception.RenException;
/**
* 知识库适配器工厂类
* 负责创建和管理不同类型的知识库API适配器
*/
@Slf4j
public class KnowledgeBaseAdapterFactory {
// 注册的适配器类型映射
private static final Map<String, Class<? extends KnowledgeBaseAdapter>> adapterRegistry = new HashMap<>();
// 适配器实例缓存
private static final Map<String, KnowledgeBaseAdapter> adapterCache = new ConcurrentHashMap<>();
static {
// 注册内置适配器类型
registerAdapter("ragflow", xiaozhi.modules.knowledge.rag.impl.RAGFlowAdapter.class);
// 可以在这里注册更多适配器类型
}
/**
* 注册新的适配器类型
*
* @param adapterType 适配器类型标识
* @param adapterClass 适配器类
*/
public static void registerAdapter(String adapterType, Class<? extends KnowledgeBaseAdapter> adapterClass) {
if (adapterRegistry.containsKey(adapterType)) {
log.warn("适配器类型 '{}' 已存在,将被覆盖", adapterType);
}
adapterRegistry.put(adapterType, adapterClass);
log.info("注册适配器类型: {} -> {}", adapterType, adapterClass.getSimpleName());
}
/**
* 获取适配器实例
*
* @param adapterType 适配器类型
* @param config 配置参数
* @return 适配器实例
*/
public static KnowledgeBaseAdapter getAdapter(String adapterType, Map<String, Object> config) {
String cacheKey = buildCacheKey(adapterType, config);
// 检查缓存中是否已存在实例
if (adapterCache.containsKey(cacheKey)) {
log.debug("从缓存获取适配器实例: {}", cacheKey);
return adapterCache.get(cacheKey);
}
// 创建新的适配器实例
KnowledgeBaseAdapter adapter = createAdapter(adapterType, config);
// 缓存适配器实例
adapterCache.put(cacheKey, adapter);
log.info("创建并缓存适配器实例: {}", cacheKey);
return adapter;
}
/**
* 获取适配器实例(无配置)
*
* @param adapterType 适配器类型
* @return 适配器实例
*/
public static KnowledgeBaseAdapter getAdapter(String adapterType) {
return getAdapter(adapterType, null);
}
/**
* 获取所有已注册的适配器类型
*
* @return 适配器类型集合
*/
public static Set<String> getRegisteredAdapterTypes() {
return adapterRegistry.keySet();
}
/**
* 检查适配器类型是否已注册
*
* @param adapterType 适配器类型
* @return 是否已注册
*/
public static boolean isAdapterTypeRegistered(String adapterType) {
return adapterRegistry.containsKey(adapterType);
}
/**
* 清除适配器缓存
*/
public static void clearCache() {
int cacheSize = adapterCache.size();
adapterCache.clear();
log.info("清除适配器缓存,共清除 {} 个实例", cacheSize);
}
/**
* 移除特定适配器类型的缓存
*
* @param adapterType 适配器类型
*/
public static void removeCacheByType(String adapterType) {
int removedCount = 0;
for (String cacheKey : adapterCache.keySet()) {
if (cacheKey.startsWith(adapterType + "@")) {
adapterCache.remove(cacheKey);
removedCount++;
}
}
log.info("移除适配器类型 '{}' 的缓存,共移除 {} 个实例", adapterType, removedCount);
}
/**
* 获取适配器工厂状态信息
*
* @return 状态信息
*/
public static Map<String, Object> getFactoryStatus() {
Map<String, Object> status = new HashMap<>();
status.put("registeredAdapterTypes", adapterRegistry.keySet());
status.put("cachedAdapterCount", adapterCache.size());
status.put("cacheKeys", adapterCache.keySet());
return status;
}
/**
* 创建适配器实例
*
* @param adapterType 适配器类型
* @param config 配置参数
* @return 适配器实例
*/
private static KnowledgeBaseAdapter createAdapter(String adapterType, Map<String, Object> config) {
if (!adapterRegistry.containsKey(adapterType)) {
throw new RenException(ErrorCode.RAG_ADAPTER_TYPE_NOT_SUPPORTED,
"不支持的适配器类型: " + adapterType);
}
try {
Class<? extends KnowledgeBaseAdapter> adapterClass = adapterRegistry.get(adapterType);
KnowledgeBaseAdapter adapter = adapterClass.getDeclaredConstructor().newInstance();
// 初始化适配器
if (config != null) {
adapter.initialize(config);
// 验证配置
if (!adapter.validateConfig(config)) {
throw new RenException(ErrorCode.RAG_CONFIG_VALIDATION_FAILED,
"适配器配置验证失败: " + adapterType);
}
}
log.info("成功创建适配器实例: {}", adapterType);
return adapter;
} catch (Exception e) {
log.error("创建适配器实例失败: {}", adapterType, e);
throw new RenException(ErrorCode.RAG_ADAPTER_CREATION_FAILED,
"创建适配器失败: " + adapterType + ", 错误: " + e.getMessage());
}
}
/**
* 构建缓存键
*
* @param adapterType 适配器类型
* @param config 配置参数
* @return 缓存键
*/
private static String buildCacheKey(String adapterType, Map<String, Object> config) {
if (config == null || config.isEmpty()) {
return adapterType + "@default";
}
// 基于配置参数生成缓存键
StringBuilder keyBuilder = new StringBuilder(adapterType + "@");
// 使用配置的哈希值作为缓存键的一部分
int configHash = config.hashCode();
keyBuilder.append(configHash);
return keyBuilder.toString();
}
}
@@ -35,6 +35,8 @@ import xiaozhi.common.utils.MessageUtils;
import xiaozhi.modules.knowledge.dao.KnowledgeBaseDao;
import xiaozhi.modules.knowledge.dto.KnowledgeBaseDTO;
import xiaozhi.modules.knowledge.entity.KnowledgeBaseEntity;
import xiaozhi.modules.knowledge.rag.KnowledgeBaseAdapter;
import xiaozhi.modules.knowledge.rag.KnowledgeBaseAdapterFactory;
import xiaozhi.modules.knowledge.service.KnowledgeBaseService;
import xiaozhi.modules.model.dao.ModelConfigDao;
import xiaozhi.modules.model.entity.ModelConfigEntity;
@@ -106,7 +108,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
if (pageData != null && pageData.getList() != null) {
for (KnowledgeBaseDTO knowledgeBase : pageData.getList()) {
try {
Integer documentCount = getDocumentCountFromRAGFlow(knowledgeBase.getDatasetId(),
Integer documentCount = getDocumentCountFromRAG(knowledgeBase.getDatasetId(),
knowledgeBase.getRagModelId());
knowledgeBase.setDocumentCount(documentCount);
} catch (Exception e) {
@@ -146,10 +148,10 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
checkDuplicateKnowledgeBaseName(knowledgeBaseDTO, null);
String datasetId = null;
// 调用RAGFlow API创建数据集
// 调用RAG API创建数据集
try {
Map<String, Object> ragConfig = getValidatedRAGConfig(knowledgeBaseDTO.getRagModelId());
datasetId = createDatasetInRAGFlow(
datasetId = createDatasetInRAG(
knowledgeBaseDTO.getName(),
knowledgeBaseDTO.getDescription(),
ragConfig);
@@ -162,13 +164,13 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
KnowledgeBaseEntity existingEntity = knowledgeBaseDao.selectOne(
new QueryWrapper<KnowledgeBaseEntity>().eq("dataset_id", datasetId));
if (existingEntity != null) {
// 如果datasetId已存在,删除RAGFlow中的数据集并抛出异常
// 如果datasetId已存在,删除RAG中的数据集并抛出异常
try {
Map<String, Object> ragConfig = getValidatedRAGConfig(knowledgeBaseDTO.getRagModelId());
deleteDatasetInRAGFlow(datasetId, ragConfig);
deleteDatasetInRAG(datasetId, ragConfig);
} catch (Exception deleteException) {
// 提供更详细的错误信息,包括异常类型和消息
String errorMessage = "删除重复datasetId的RAGFlow数据集失败: " + deleteException.getClass().getSimpleName();
String errorMessage = "删除重复datasetId的RAG数据集失败: " + deleteException.getClass().getSimpleName();
if (deleteException.getMessage() != null) {
errorMessage += " - " + deleteException.getMessage();
}
@@ -219,17 +221,17 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
// 先校验RAG配置
Map<String, Object> ragConfig = getValidatedRAGConfig(knowledgeBaseDTO.getRagModelId());
// 调用RAGFlow API更新数据集
updateDatasetInRAGFlow(
// 调用RAG API更新数据集
updateDatasetInRAG(
knowledgeBaseDTO.getDatasetId(),
knowledgeBaseDTO.getName(),
knowledgeBaseDTO.getDescription(),
ragConfig);
log.info("RAGFlow API更新成功,datasetId: {}", knowledgeBaseDTO.getDatasetId());
log.info("RAG API更新成功,datasetId: {}", knowledgeBaseDTO.getDatasetId());
} catch (Exception e) {
// 提供更详细的错误信息,包括异常类型和消息
String errorMessage = "更新RAGFlow数据集失败: " + e.getClass().getSimpleName();
String errorMessage = "更新RAG数据集失败: " + e.getClass().getSimpleName();
if (e.getMessage() != null) {
errorMessage += " - " + e.getMessage();
}
@@ -237,7 +239,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
throw e;
}
} else {
log.warn("datasetId或ragModelId为空,跳过RAGFlow更新");
log.warn("datasetId或ragModelId为空,跳过RAG更新");
}
KnowledgeBaseEntity entity = ConvertUtils.sourceToTarget(knowledgeBaseDTO, KnowledgeBaseEntity.class);
@@ -288,19 +290,19 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
log.info("找到记录: ID={}, datasetId={}, ragModelId={}",
entity.getId(), entity.getDatasetId(), entity.getRagModelId());
// 先调用RAGFlow API删除数据集
// 先调用RAG API删除数据集
boolean apiDeleteSuccess = false;
if (StringUtils.isNotBlank(entity.getDatasetId()) && StringUtils.isNotBlank(entity.getRagModelId())) {
try {
log.info("开始调用RAGFlow API删除数据集");
log.info("开始调用RAG API删除数据集");
// 在删除前进行RAG配置校验
Map<String, Object> ragConfig = getValidatedRAGConfig(entity.getRagModelId());
deleteDatasetInRAGFlow(entity.getDatasetId(), ragConfig);
log.info("RAGFlow API删除调用完成");
deleteDatasetInRAG(entity.getDatasetId(), ragConfig);
log.info("RAG API删除调用完成");
apiDeleteSuccess = true;
} catch (Exception e) {
// 提供更详细的错误信息,包括异常类型和消息
String errorMessage = "删除RAGFlow数据集失败: " + e.getClass().getSimpleName();
String errorMessage = "删除RAG数据集失败: " + e.getClass().getSimpleName();
if (e.getMessage() != null) {
errorMessage += " - " + e.getMessage();
}
@@ -308,7 +310,7 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
throw e;
}
} else {
log.warn("datasetId或ragModelId为空,跳过RAGFlow删除");
log.warn("datasetId或ragModelId为空,跳过RAG删除");
apiDeleteSuccess = true; // 没有RAG数据集,视为成功
}
@@ -432,289 +434,133 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
}
/**
* 调用RAGFlow API创建数据集
* RAG配置中提取适配器类型
*
* @param config RAG配置
* @return 适配器类型
*/
private String createDatasetInRAGFlow(String name, String description, Map<String, Object> ragConfig) {
String datasetId = null;
String baseUrl = (String) ragConfig.get("base_url");
String apiKey = (String) ragConfig.get("api_key");
log.info("开始调用RAGFlow API创建数据集, name: {}", name);
log.debug("RAGFlow配置 - baseUrl: {}, apiKey: {}", baseUrl, StringUtils.isBlank(apiKey) ? "未配置" : "已配置");
// 构建请求URL
String url = baseUrl + "/api/v1/datasets";
log.debug("请求URL: {}", url);
// 构建请求体
Map<String, Object> requestBody = new HashMap<>();
String username = SecurityUser.getUser().getUsername();
requestBody.put("name", username + "_" + name);
if (StringUtils.isNotBlank(description)) {
requestBody.put("description", description);
}
log.debug("请求体: {}", requestBody);
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + apiKey);
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
// 发送POST请求
log.info("发送POST请求到RAGFlow API...");
ResponseEntity<String> response;
try {
response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
} catch (Exception e) {
String errorMessage = url + e.getMessage();
log.error(errorMessage, e);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
private String extractAdapterType(Map<String, Object> config) {
if (config == null) {
throw new RenException(ErrorCode.RAG_CONFIG_NOT_FOUND);
}
log.info("RAGFlow API响应状态码: {}", response.getStatusCode());
log.debug("RAGFlow API响应内容: {}", response.getBody());
// 从配置中提取适配器类型
String adapterType = (String) config.get("type");
if (!response.getStatusCode().is2xxSuccessful()) {
String errorMessage = response.getStatusCode() + ", 响应内容: " + response.getBody();
log.error(errorMessage);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
// 验证适配器类型是否存在且非空
if (StringUtils.isBlank(adapterType)) {
throw new RenException(ErrorCode.RAG_ADAPTER_TYPE_NOT_FOUND);
}
// 解析响应体,提取datasetId
String responseBody = response.getBody();
if (StringUtils.isNotBlank(responseBody)) {
try {
// 解析RAGFlow API响应,支持多种可能的字段名
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> responseMap = objectMapper.readValue(responseBody, Map.class);
log.debug("RAGFlow API响应解析结果: {}", responseMap);
// 首先检查响应码
Integer code = (Integer) responseMap.get("code");
String message = (String) responseMap.get("message");
if (code != null && code == 0) {
// 响应码为0表示成功,从data字段中获取datasetId
Object dataObj = responseMap.get("data");
if (dataObj instanceof Map) {
Map<String, Object> dataMap = (Map<String, Object>) dataObj;
datasetId = (String) dataMap.get("id");
if (StringUtils.isBlank(datasetId)) {
// 如果id字段为空,尝试其他可能的字段名
datasetId = (String) dataMap.get("dataset_id");
datasetId = (String) dataMap.get("datasetId");
}
}
} else {
// 如果响应码不为0,说明API调用失败
String errorMessage = code + (message != null ? message : "null");
log.error(errorMessage);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
}
log.info("从RAGFlow API响应中解析出datasetId: {}", datasetId);
log.debug("完整响应内容: {}", responseBody);
} catch (IOException e) {
// 构建详细的错误信息,包含异常类型和消息
String baseErrorMessage = e.getClass().getSimpleName();
String errorMessage = baseErrorMessage + (e.getMessage() != null ? ": " + e.getMessage() : "");
log.error(errorMessage, e);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
} catch (Exception e) {
// 构建详细的错误信息,包含异常类型和消息
String baseErrorMessage = e.getClass().getSimpleName();
String errorMessage = baseErrorMessage + (e.getMessage() != null ? ": " + e.getMessage() : "");
log.error(errorMessage, e);
// 如果解析失败,但响应体不为空,尝试直接使用响应体作为错误信息
String finalErrorMessage = responseBody;
if (e.getMessage() != null) {
finalErrorMessage += errorMessage;
}
throw new RenException(ErrorCode.RAG_API_ERROR, finalErrorMessage);
}
// 验证适配器类型是否已注册
if (!KnowledgeBaseAdapterFactory.isAdapterTypeRegistered(adapterType)) {
throw new RenException(ErrorCode.RAG_ADAPTER_TYPE_NOT_SUPPORTED,
"不支持的适配器类型: " + adapterType);
}
if (StringUtils.isBlank(datasetId)) {
log.error("无法从RAGFlow API响应中获取datasetId,响应内容: {}", responseBody);
throw new RenException(ErrorCode.RAG_DATASET_ID_NOT_NULL);
}
log.info("RAGFlow数据集创建成功,datasetId: {}", datasetId);
return datasetId;
return adapterType;
}
/**
* 调用RAGFlow API更新数据集
* 使用适配器创建数据集
*/
private void updateDatasetInRAGFlow(String datasetId, String name, String description,
private String createDatasetInRAG(String name, String description, Map<String, Object> ragConfig) {
log.info("开始使用适配器创建数据集, name: {}", name);
try {
// 从RAG配置中提取适配器类型
String adapterType = extractAdapterType(ragConfig);
// 使用适配器工厂获取适配器实例
KnowledgeBaseAdapter adapter = KnowledgeBaseAdapterFactory.getAdapter(adapterType, ragConfig);
// 构建数据集创建参数
Map<String, Object> createParams = new HashMap<>();
String username = SecurityUser.getUser().getUsername();
createParams.put("name", username + "_" + name);
if (StringUtils.isNotBlank(description)) {
createParams.put("description", description);
}
// 调用适配器的创建数据集方法
String datasetId = adapter.createDataset(createParams);
log.info("数据集创建成功,datasetId: {}", datasetId);
return datasetId;
} catch (Exception e) {
// 直接传递底层适配器的详细错误信息
log.error("创建数据集失败", e);
if (e instanceof RenException) {
throw (RenException) e;
}
throw new RenException(ErrorCode.RAG_API_ERROR, e.getMessage());
}
}
/**
* 使用适配器更新数据集
*/
private void updateDatasetInRAG(String datasetId, String name, String description,
Map<String, Object> ragConfig) {
String baseUrl = (String) ragConfig.get("base_url");
String apiKey = (String) ragConfig.get("api_key");
log.info("开始使用适配器更新数据集,datasetId: {}, name: {}", datasetId, name);
log.info("开始调用RAGFlow API更新数据集,datasetId: {}, name: {}", datasetId, name);
log.debug("RAGFlow配置 - baseUrl: {}, apiKey: {}", baseUrl, StringUtils.isBlank(apiKey) ? "未配置" : "已配置");
// 构建请求URL
String url = baseUrl + "/api/v1/datasets/" + datasetId;
log.debug("请求URL: {}", url);
// 构建请求体
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("dataset_id", datasetId);
String username = SecurityUser.getUser().getUsername();
requestBody.put("name", username + "_" + name);
if (StringUtils.isNotBlank(description)) {
requestBody.put("description", description);
}
log.debug("请求体: {}", requestBody);
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + apiKey);
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
// 发送PUT请求
log.info("发送PUT请求到RAGFlow API...");
ResponseEntity<String> response;
try {
response = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, String.class);
} catch (Exception e) {
String errorMessage = url + e.getMessage();
log.error(errorMessage, e);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
}
// 从RAG配置中提取适配器类型
String adapterType = extractAdapterType(ragConfig);
log.info("RAGFlow API响应状态码: {}", response.getStatusCode());
log.debug("RAGFlow API响应内容: {}", response.getBody());
// 使用适配器工厂获取适配器实例
KnowledgeBaseAdapter adapter = KnowledgeBaseAdapterFactory.getAdapter(adapterType, ragConfig);
if (!response.getStatusCode().is2xxSuccessful()) {
String errorMessage = response.getStatusCode() + ", 响应内容: " + response.getBody();
log.error(errorMessage);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
}
// 解析响应体,验证操作是否真正成功
String responseBody = response.getBody();
if (responseBody != null) {
try {
Map<String, Object> responseMap = objectMapper.readValue(responseBody, Map.class);
Integer code = (Integer) responseMap.get("code");
String message = (String) responseMap.get("message");
if (code != null && code != 0) {
String errorMessage = code + (message != null ? message : "null");
log.error(errorMessage);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
}
} catch (IOException e) {
// 构建详细的错误信息,包含异常类型和消息
String baseErrorMessage = e.getClass().getSimpleName();
String errorMessage = baseErrorMessage + (e.getMessage() != null ? ": " + e.getMessage() : "");
log.error(errorMessage, e);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
} catch (Exception e) {
// 构建详细的错误信息,包含异常类型和消息
String baseErrorMessage = e.getClass().getSimpleName();
String errorMessage = baseErrorMessage + (e.getMessage() != null ? ": " + e.getMessage() : "");
log.error(errorMessage, e);
// 如果解析失败,但响应体不为空,尝试直接使用响应体作为错误信息
String finalErrorMessage = responseBody;
if (e.getMessage() != null) {
finalErrorMessage += errorMessage;
}
throw new RenException(ErrorCode.RAG_API_ERROR, finalErrorMessage);
// 构建数据集更新参数
Map<String, Object> updateParams = new HashMap<>();
String username = SecurityUser.getUser().getUsername();
updateParams.put("name", username + "_" + name);
if (StringUtils.isNotBlank(description)) {
updateParams.put("description", description);
}
// 调用适配器的更新数据集方法
adapter.updateDataset(datasetId, updateParams);
log.info("数据集更新成功,datasetId: {}", datasetId);
} catch (Exception e) {
// 直接传递底层适配器的详细错误信息
log.error("更新数据集失败", e);
if (e instanceof RenException) {
throw (RenException) e;
}
throw new RenException(ErrorCode.RAG_API_ERROR, e.getMessage());
}
log.info("RAGFlow数据集更新成功,datasetId: {}", datasetId);
}
/**
* 调用RAGFlow API删除数据集
* 使用适配器删除数据集
*/
private void deleteDatasetInRAGFlow(String datasetId, Map<String, Object> ragConfig) {
String baseUrl = (String) ragConfig.get("base_url");
String apiKey = (String) ragConfig.get("api_key");
private void deleteDatasetInRAG(String datasetId, Map<String, Object> ragConfig) {
log.info("开始使用适配器删除数据集,datasetId: {}", datasetId);
log.info("开始调用RAGFlow API删除数据集,datasetId: {}", datasetId);
log.debug("RAGFlow配置 - baseUrl: {}, apiKey: {}", baseUrl, StringUtils.isBlank(apiKey) ? "未配置" : "已配置");
// 构建请求URL
String url = baseUrl + "/api/v1/datasets";
log.debug("请求URL: {}", url);
// 构建请求体
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("ids", List.of(datasetId));
log.debug("请求体: {}", requestBody);
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + apiKey);
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
// 发送DELETE请求
log.info("发送DELETE请求到RAGFlow API...");
ResponseEntity<String> response;
try {
response = restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, String.class);
// 从RAG配置中提取适配器类型
String adapterType = extractAdapterType(ragConfig);
// 使用适配器工厂获取适配器实例
KnowledgeBaseAdapter adapter = KnowledgeBaseAdapterFactory.getAdapter(adapterType, ragConfig);
// 调用适配器的删除数据集方法
adapter.deleteDataset(datasetId);
log.info("数据集删除成功,datasetId: {}", datasetId);
} catch (Exception e) {
String errorMessage = url + e.getMessage();
log.error(errorMessage, e);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
}
log.info("RAGFlow API响应状态码: {}", response.getStatusCode());
log.debug("RAGFlow API响应内容: {}", response.getBody());
if (!response.getStatusCode().is2xxSuccessful()) {
String errorMessage = response.getStatusCode() + response.getBody();
log.error(errorMessage);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
}
// 解析响应体,验证操作是否真正成功
String responseBody = response.getBody();
if (responseBody != null) {
try {
Map<String, Object> responseMap = objectMapper.readValue(responseBody, Map.class);
Integer code = (Integer) responseMap.get("code");
String message = (String) responseMap.get("message");
if (code != null && code != 0) {
String errorMessage = code + (message != null ? message : "null");
log.error(errorMessage);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
}
} catch (IOException e) {
// 构建详细的错误信息,包含异常类型和消息
String baseErrorMessage = e.getClass().getSimpleName();
String errorMessage = baseErrorMessage + (e.getMessage() != null ? ": " + e.getMessage() : "");
log.error(errorMessage, e);
throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage);
} catch (Exception e) {
// 构建详细的错误信息,包含异常类型和消息
String baseErrorMessage = e.getClass().getSimpleName();
String errorMessage = baseErrorMessage + (e.getMessage() != null ? ": " + e.getMessage() : "");
log.error(errorMessage, e);
// 如果解析失败,但响应体不为空,尝试直接使用响应体作为错误信息
String finalErrorMessage = responseBody;
if (e.getMessage() != null) {
finalErrorMessage += errorMessage;
}
throw new RenException(ErrorCode.RAG_API_ERROR, finalErrorMessage);
// 直接传递底层适配器的详细错误信息
log.error("删除数据集失败", e);
if (e instanceof RenException) {
throw (RenException) e;
}
throw new RenException(ErrorCode.RAG_API_ERROR, e.getMessage());
}
log.info("RAGFlow数据集删除成功,datasetId: {}", datasetId);
}
/**
@@ -760,9 +606,9 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
}
/**
* 从RAGFlow API获取知识库的文档数量
* 从适配器获取知识库的文档数量
*/
private Integer getDocumentCountFromRAGFlow(String datasetId, String ragModelId) {
private Integer getDocumentCountFromRAG(String datasetId, String ragModelId) {
if (StringUtils.isBlank(datasetId) || StringUtils.isBlank(ragModelId)) {
log.warn("datasetId或ragModelId为空,无法获取文档数量");
return 0;
@@ -770,79 +616,29 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
log.info("开始获取知识库 {} 的文档数量", datasetId);
// 获取RAG配置
Map<String, Object> ragConfig = getValidatedRAGConfig(ragModelId);
String baseUrl = (String) ragConfig.get("base_url");
String apiKey = (String) ragConfig.get("api_key");
// 构建请求URL - 调用RAGFlow API获取文档列表,但不返回文档详情,只获取总数
String url = baseUrl + "/api/v1/datasets/" + datasetId + "/documents?page=1&size=1";
log.debug("请求URL: {}", url);
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + apiKey);
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
// 发送GET请求
log.info("发送GET请求到RAGFlow API获取文档数量...");
ResponseEntity<String> response;
try {
response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
// 获取RAG配置
Map<String, Object> ragConfig = getValidatedRAGConfig(ragModelId);
// 从RAG配置中提取适配器类型
String adapterType = extractAdapterType(ragConfig);
// 使用适配器工厂获取适配器实例
KnowledgeBaseAdapter adapter = KnowledgeBaseAdapterFactory.getAdapter(adapterType, ragConfig);
// 调用适配器的获取文档数量方法
Integer documentCount = adapter.getDocumentCount(datasetId);
log.info("获取知识库 {} 的文档数量成功: {}", datasetId, documentCount);
return documentCount;
} catch (Exception e) {
String errorMessage = url + e.getMessage();
// 构建详细的错误信息,包含异常类型和消息
String baseErrorMessage = e.getClass().getSimpleName() + " - 获取知识库文档数量失败";
String errorMessage = baseErrorMessage + (e.getMessage() != null ? ": " + e.getMessage() : "");
log.error(errorMessage, e);
return 0;
}
log.info("RAGFlow API响应状态码: {}", response.getStatusCode());
if (!response.getStatusCode().is2xxSuccessful()) {
log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody());
return 0;
}
String responseBody = response.getBody();
log.debug("RAGFlow API响应内容: {}", responseBody);
// 解析响应
try {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> responseMap = objectMapper.readValue(responseBody, Map.class);
Integer code = (Integer) responseMap.get("code");
if (code != null && code == 0) {
Object dataObj = responseMap.get("data");
if (dataObj instanceof Map) {
Map<String, Object> dataMap = (Map<String, Object>) dataObj;
Object totalObj = dataMap.get("total");
if (totalObj instanceof Integer) {
Integer documentCount = (Integer) totalObj;
log.info("获取知识库 {} 的文档数量成功: {}", datasetId, documentCount);
return documentCount;
} else if (totalObj instanceof Long) {
Long documentCount = (Long) totalObj;
log.info("获取知识库 {} 的文档数量成功: {}", datasetId, documentCount);
return documentCount.intValue();
}
}
} else {
log.error("RAGFlow API调用失败,响应码: {}, 响应内容: {}", code, responseBody);
}
} catch (IOException e) {
// 构建详细的错误信息,包含异常类型和消息
String baseErrorMessage = e.getClass().getSimpleName() + " - 解析RAGFlow API响应时发生IO异常";
String errorMessage = baseErrorMessage + (e.getMessage() != null ? ": " + e.getMessage() : "");
log.error(errorMessage, e);
} catch (Exception e) {
// 构建详细的错误信息,包含异常类型和消息
String baseErrorMessage = e.getClass().getSimpleName() + " - 解析RAGFlow API响应失败";
String errorMessage = baseErrorMessage + (e.getMessage() != null ? ": " + e.getMessage() : "");
log.error(errorMessage, e);
}
return 0;
}
}
@@ -189,4 +189,13 @@
10180=Dateiinhalt darf nicht leer sein
10181=Stimmenklon-Name darf nicht leer sein
10182=Stimmenklon-Audio nicht gefunden
10183=Standard-Agent-Vorlage nicht gefunden
10183=Standard-Agent-Vorlage nicht gefunden
10184=Nicht unterstützter Adaptertyp
10185=RAG-Konfigurationsvalidierung fehlgeschlagen
10186=Adapter-Erstellung fehlgeschlagen
10187=Adapter-Initialisierung fehlgeschlagen
10188=Adapter-Verbindungstest fehlgeschlagen
10189=Adapter-Operation fehlgeschlagen
10190=Adapter nicht gefunden
10191=Adapter-Cache-Fehler
10192=Adaptertyp nicht gefunden
@@ -189,4 +189,13 @@
10180=File content cannot be empty
10181=Voice clone name cannot be empty
10182=Voice clone audio not found
10183=Default agent template not found
10183=Default agent template not found
10184=Unsupported adapter type
10185=RAG configuration validation failed
10186=Adapter creation failed
10187=Adapter initialization failed
10188=Adapter connection test failed
10189=Adapter operation failed
10190=Adapter not found
10191=Adapter cache error
10192=Adapter type not found
@@ -189,4 +189,13 @@
10180=Nội dung tệp không thể để trống
10181=Tên nhân bản giọng nói không thể để trống
10182=Không tìm thấy âm thanh nhân bản giọng nói
10183=Không tìm thấy mẫu agent mặc định
10183=Không tìm thấy mẫu agent mặc định
10184=Loại bộ chuyển đổi không được hỗ trợ
10185=Kiểm tra cấu hình RAG thất bại
10186=Tạo bộ chuyển đổi thất bại
10187=Khởi tạo bộ chuyển đổi thất bại
10188=Kiểm tra kết nối bộ chuyển đổi thất bại
10189=Thao tác bộ chuyển đổi thất bại
10190=Không tìm thấy bộ chuyển đổi
10191=Lỗi bộ nhớ đệm bộ chuyển đổi
10192=Không tìm thấy loại bộ chuyển đổi
@@ -189,4 +189,13 @@
10180=\u6587\u4EF6\u5185\u5BB9\u4E0D\u80FD\u4E3A\u7A7A
10181=\u97F3\u8272\u514B\u9686\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A
10182=\u97F3\u8272\u514B\u9686\u97F3\u9891\u4E0D\u5B58\u5728
10183=\u9ED8\u8BA4\u667A\u80FD\u4F53\u672A\u627E\u5230
10183=\u9ED8\u8BA4\u667A\u80FD\u4F53\u672A\u627E\u5230
10184=\u4E0D\u652F\u6301\u7684\u9002\u914D\u5668\u7C7B\u578B
10185=RAG\u914D\u7F6E\u9A8C\u8BC1\u5931\u8D25
10186=\u9002\u914D\u5668\u521B\u5EFA\u5931\u8D25
10187=\u9002\u914D\u5668\u521D\u59CB\u5316\u5931\u8D25
10188=\u9002\u914D\u5668\u8FDE\u63A5\u6D4B\u8BD5\u5931\u8D25
10189=\u9002\u914D\u5668\u64CD\u4F5C\u5931\u8D25
10190=\u9002\u914D\u5668\u672A\u627E\u5230
10191=\u9002\u914D\u5668\u7F13\u5B58\u9519\u8BEF
10192=\u9002\u914D\u5668\u7C7B\u578B\u672A\u627E\u5230
@@ -189,4 +189,13 @@
10180=\u6587\u4ef6\u5185\u5bb9\u4e0d\u80fd\u70ba\u7a7a
10181=\u97f3\u8272\u514b\u9686\u540d\u7a31\u4e0d\u80fd\u70ba\u7a7a
10182=\u97f3\u8272\u514b\u9686\u97f3\u983b\u4e0d\u5b58\u5728
10183=\u9ed8\u8ba4\u667a\u80fd\u4f53\u672a\u627e\u5230
10183=\u9ed8\u8ba4\u667a\u80fd\u4f53\u672a\u627e\u5230
10184=\u4E0D\u652F\u6301\u7684\u9002\u914D\u5668\u985E\u578B
10185=RAG\u914D\u7F6E\u9A57\u8B49\u5931\u6557
10186=\u9002\u914D\u5668\u5275\u5EFA\u5931\u6557
10187=\u9002\u914D\u5668\u521D\u59CB\u5316\u5931\u6557
10188=\u9002\u914D\u5668\u9023\u63A5\u6E2C\u8A66\u5931\u6557
10189=\u9002\u914D\u5668\u64CD\u4F5C\u5931\u6557
10190=\u9002\u914D\u5668\u672A\u627E\u5230
10191=\u9002\u914D\u5668\u7F13\u5B58\u932F\u8AA4
10192=\u9002\u914D\u5668\u985E\u578B\u672A\u627E\u5230