2424import org .springframework .web .client .RestTemplate ;
2525import org .springframework .web .multipart .MultipartFile ;
2626
27- import com .baomidou .mybatisplus .core .conditions .query .QueryWrapper ;
2827import com .fasterxml .jackson .databind .ObjectMapper ;
2928
3029import lombok .AllArgsConstructor ;
3130import lombok .extern .slf4j .Slf4j ;
32- import xiaozhi .common .constant .Constant ;
3331import xiaozhi .common .exception .ErrorCode ;
3432import xiaozhi .common .exception .RenException ;
3533import xiaozhi .common .page .PageData ;
3634import xiaozhi .modules .knowledge .dto .KnowledgeFilesDTO ;
35+ import xiaozhi .modules .knowledge .service .KnowledgeBaseService ;
3736import xiaozhi .modules .knowledge .service .KnowledgeFilesService ;
38- import xiaozhi .modules .model .dao .ModelConfigDao ;
39- import xiaozhi .modules .model .entity .ModelConfigEntity ;
40- import xiaozhi .modules .model .service .ModelConfigService ;
4137
4238@ Service
4339@ AllArgsConstructor
4440@ Slf4j
4541public class KnowledgeFilesServiceImpl implements KnowledgeFilesService {
4642
47- private final ModelConfigService modelConfigService ;
48- private final ModelConfigDao modelConfigDao ;
43+ private final KnowledgeBaseService knowledgeBaseService ;
4944 private RestTemplate restTemplate = new RestTemplate ();
5045 private ObjectMapper objectMapper = new ObjectMapper ();
5146
47+ @ Override
48+ public Map <String , Object > getRAGConfig (String ragModelId ) {
49+ return knowledgeBaseService .getRAGConfig (ragModelId );
50+ }
51+
5252 @ Override
5353 public PageData <KnowledgeFilesDTO > getPageList (KnowledgeFilesDTO knowledgeFilesDTO , Integer page , Integer limit ) {
5454 try {
@@ -60,17 +60,17 @@ public PageData<KnowledgeFilesDTO> getPageList(KnowledgeFilesDTO knowledgeFilesD
6060 knowledgeFilesDTO != null ? knowledgeFilesDTO .getStatus () : null ,
6161 page , limit );
6262
63- // 获取RAG配置
64- Map <String , Object > ragConfig = getDefaultRAGConfig ();
65- String baseUrl = (String ) ragConfig .get ("base_url" );
66- String apiKey = (String ) ragConfig .get ("api_key" );
67-
6863 // 构建请求URL - 根据RAGFlow API文档,获取文档列表的接口
6964 String datasetId = knowledgeFilesDTO != null ? knowledgeFilesDTO .getDatasetId () : null ;
7065 if (StringUtils .isBlank (datasetId )) {
7166 throw new RenException (ErrorCode .PARAMS_GET_ERROR , "datasetId不能为空" );
7267 }
7368
69+ // 获取RAG配置
70+ Map <String , Object > ragConfig = knowledgeBaseService .getRAGConfigByDatasetId (datasetId );
71+ String baseUrl = (String ) ragConfig .get ("base_url" );
72+ String apiKey = (String ) ragConfig .get ("api_key" );
73+
7474 String url = baseUrl + "/api/v1/datasets/" + datasetId + "/documents" ;
7575
7676 // 添加查询参数
@@ -253,7 +253,8 @@ private void syncDocumentStatusWithRAGFlow(KnowledgeFilesDTO dto) {
253253 long currentTime = System .currentTimeMillis ();
254254
255255 // 调用RAGFlow API获取文档切片信息
256- Map <String , Object > ragConfig = getDefaultRAGConfig ();
256+ String datasetId = dto .getDatasetId ();
257+ Map <String , Object > ragConfig = knowledgeBaseService .getRAGConfigByDatasetId (datasetId );
257258 String baseUrl = (String ) ragConfig .get ("base_url" );
258259 String apiKey = (String ) ragConfig .get ("api_key" );
259260
@@ -449,7 +450,7 @@ public KnowledgeFilesDTO getByDocumentId(String documentId, String datasetId) {
449450
450451 try {
451452 // 获取RAG配置
452- Map <String , Object > ragConfig = getDefaultRAGConfig ( );
453+ Map <String , Object > ragConfig = knowledgeBaseService . getRAGConfigByDatasetId ( datasetId );
453454 String baseUrl = (String ) ragConfig .get ("base_url" );
454455 String apiKey = (String ) ragConfig .get ("api_key" );
455456
@@ -528,7 +529,7 @@ public PageData<KnowledgeFilesDTO> getPageListByStatus(String datasetId, Integer
528529
529530 try {
530531 // 获取RAG配置
531- Map <String , Object > ragConfig = getDefaultRAGConfig ( );
532+ Map <String , Object > ragConfig = knowledgeBaseService . getRAGConfigByDatasetId ( datasetId );
532533 String baseUrl = (String ) ragConfig .get ("base_url" );
533534 String apiKey = (String ) ragConfig .get ("api_key" );
534535
@@ -748,58 +749,6 @@ private String getFileType(String fileName) {
748749 }
749750 }
750751
751- @ Override
752- public Map <String , Object > getRAGConfig (String ragModelId ) {
753- if (StringUtils .isBlank (ragModelId )) {
754- throw new RenException (ErrorCode .PARAMS_GET_ERROR );
755- }
756-
757- // 从缓存获取模型配置
758- ModelConfigEntity modelConfig = modelConfigService .getModelByIdFromCache (ragModelId );
759- if (modelConfig == null || modelConfig .getConfigJson () == null ) {
760- throw new RenException (ErrorCode .RAG_CONFIG_NOT_FOUND );
761- }
762-
763- // 验证是否为RAG类型配置
764- if (!Constant .RAG_CONFIG_TYPE .equals (modelConfig .getModelType ().toUpperCase ())) {
765- throw new RenException (ErrorCode .RAG_CONFIG_TYPE_ERROR );
766- }
767-
768- Map <String , Object > config = modelConfig .getConfigJson ();
769-
770- // 验证必要的配置参数
771- validateRagConfig (config );
772-
773- // 返回配置信息
774- return config ;
775- }
776-
777- @ Override
778- public Map <String , Object > getDefaultRAGConfig () {
779- // 获取默认RAG模型配置
780- QueryWrapper <ModelConfigEntity > queryWrapper = new QueryWrapper <>();
781- queryWrapper .eq ("model_type" , Constant .RAG_CONFIG_TYPE )
782- .eq ("is_default" , 1 )
783- .eq ("is_enabled" , 1 );
784-
785- List <ModelConfigEntity > modelConfigs = modelConfigDao .selectList (queryWrapper );
786- if (modelConfigs == null || modelConfigs .isEmpty ()) {
787- throw new RenException (ErrorCode .RAG_DEFAULT_CONFIG_NOT_FOUND );
788- }
789-
790- ModelConfigEntity defaultConfig = modelConfigs .get (0 );
791- if (defaultConfig .getConfigJson () == null ) {
792- throw new RenException (ErrorCode .RAG_CONFIG_NOT_FOUND );
793- }
794-
795- Map <String , Object > config = defaultConfig .getConfigJson ();
796-
797- // 验证必要的配置参数
798- validateRagConfig (config );
799-
800- return config ;
801- }
802-
803752 /**
804753 * 验证RAG配置中是否包含必要的参数
805754 */
@@ -826,7 +775,7 @@ private String uploadDocumentToRAGFlow(String datasetId, MultipartFile file, Str
826775 Map <String , Object > parserConfig ) {
827776 try {
828777 // 获取RAG配置
829- Map <String , Object > ragConfig = getDefaultRAGConfig ( );
778+ Map <String , Object > ragConfig = knowledgeBaseService . getRAGConfigByDatasetId ( datasetId );
830779 String baseUrl = (String ) ragConfig .get ("base_url" );
831780 String apiKey = (String ) ragConfig .get ("api_key" );
832781
@@ -1017,7 +966,7 @@ private String extractDocumentIdFromRoot(Map<String, Object> responseMap) {
1017966 private void deleteDocumentInRAGFlow (String documentId , String datasetId ) {
1018967 try {
1019968 // 获取RAG配置
1020- Map <String , Object > ragConfig = getDefaultRAGConfig ( );
969+ Map <String , Object > ragConfig = knowledgeBaseService . getRAGConfigByDatasetId ( datasetId );
1021970 String baseUrl = (String ) ragConfig .get ("base_url" );
1022971 String apiKey = (String ) ragConfig .get ("api_key" );
1023972
@@ -1141,7 +1090,7 @@ public boolean parseDocuments(String datasetId, List<String> documentIds) {
11411090
11421091 try {
11431092 // 获取RAG配置
1144- Map <String , Object > ragConfig = getDefaultRAGConfig ( );
1093+ Map <String , Object > ragConfig = knowledgeBaseService . getRAGConfigByDatasetId ( datasetId );
11451094 String baseUrl = (String ) ragConfig .get ("base_url" );
11461095 String apiKey = (String ) ragConfig .get ("api_key" );
11471096
@@ -1217,7 +1166,7 @@ public Map<String, Object> listChunks(String datasetId, String documentId, Strin
12171166
12181167 try {
12191168 // 获取RAG配置
1220- Map <String , Object > ragConfig = getDefaultRAGConfig ( );
1169+ Map <String , Object > ragConfig = knowledgeBaseService . getRAGConfigByDatasetId ( datasetId );
12211170 String baseUrl = (String ) ragConfig .get ("base_url" );
12221171 String apiKey = (String ) ragConfig .get ("api_key" );
12231172
@@ -1616,7 +1565,7 @@ public Map<String, Object> retrievalTest(String question, List<String> datasetId
16161565
16171566 try {
16181567 // 获取RAG配置
1619- Map <String , Object > ragConfig = getDefaultRAGConfig ( );
1568+ Map <String , Object > ragConfig = knowledgeBaseService . getRAGConfigByDatasetId ( datasetIds . get ( 0 ) );
16201569 String baseUrl = (String ) ragConfig .get ("base_url" );
16211570 String apiKey = (String ) ragConfig .get ("api_key" );
16221571
0 commit comments