ae9a515ab27461245ff0178765383684ce32d4f0
[usecase-ui/llm-adaptation.git] /
1 package org.onap.usecaseui.llmadaptation.service.impl;
2
3 import com.alibaba.fastjson2.JSONObject;
4 import lombok.extern.slf4j.Slf4j;
5 import org.onap.usecaseui.llmadaptation.bean.*;
6 import org.onap.usecaseui.llmadaptation.constant.ServerConstant;
7 import org.onap.usecaseui.llmadaptation.mapper.ApplicationMapper;
8 import org.onap.usecaseui.llmadaptation.mapper.DatasetMapper;
9 import org.onap.usecaseui.llmadaptation.mapper.MaaSPlatformMapper;
10 import org.onap.usecaseui.llmadaptation.service.ApplicationService;
11 import org.onap.usecaseui.llmadaptation.service.BiShengApplicationService;
12 import org.onap.usecaseui.llmadaptation.service.FastGptApplicationService;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.stereotype.Service;
15 import org.springframework.util.CollectionUtils;
16 import reactor.core.publisher.Flux;
17 import reactor.core.publisher.Mono;
18
19 import java.util.List;
20
21 @Slf4j
22 @Service
23 public class ApplicationServiceImpl implements ApplicationService {
24     @Autowired
25     private ApplicationMapper applicationMapper;
26
27     @Autowired
28     private DatasetMapper datasetMapper;
29
30     @Autowired
31     private FastGptApplicationService fastGptApplicationService;
32
33     @Autowired
34     private BiShengApplicationService biShengApplicationService;
35
36     @Autowired
37     private MaaSPlatformMapper maaSPlatformMapper;
38
39     @Autowired
40     private ServerConstant serverConstant;
41
42     @Override
43     public Mono<ServiceResult> createApplication(Application application) {
44         List<Application> applications = applicationMapper.getAllApplication();
45         if (!CollectionUtils.isEmpty(applications)) {
46             List<Application> collect = applications.stream().filter(app -> app.getApplicationName().equals(application.getApplicationName())).toList();
47             if (!collect.isEmpty()) {
48                 return Mono.just(new ServiceResult(new ResultHeader(500, "name exists"), applications));
49             }
50         }
51         MaaSPlatform maaSPlatformById = maaSPlatformMapper.getMaaSPlatformById(application.getMaaSPlatformId());
52         if (maaSPlatformById == null) {
53             return Mono.just(new ServiceResult(new ResultHeader(500, "maas is not exist")));
54         }
55         String maaSType = maaSPlatformById.getMaaSType();
56         String fastGptType = serverConstant.getFastGptType();
57         if (fastGptType.equals(maaSType)) {
58             return fastGptApplicationService.createApplication(application);
59         }
60         return biShengApplicationService.createApplication(application);
61     }
62
63     @Override
64     public Mono<ServiceResult> removeApplication(String applicationId) {
65         String maaSType = getMaaSType(applicationId);
66         String fastGptType = serverConstant.getFastGptType();
67         if (fastGptType.equals(maaSType)) {
68             return fastGptApplicationService.removeApplication(applicationId);
69         }
70         return biShengApplicationService.removeApplication(applicationId);
71     }
72
73     @Override
74     public Flux<String> chat(JSONObject question) {
75         String applicationId = question.getString("applicationId");
76         String maaSType = getMaaSType(applicationId);
77         String fastGptType = serverConstant.getFastGptType();
78         if (fastGptType.equals(maaSType)) {
79             return fastGptApplicationService.chat(question);
80         }
81         return biShengApplicationService.chat(question);
82     }
83
84     @Override
85     public ServiceResult getApplications() {
86         List<Application> allApplication = applicationMapper.getAllApplication();
87         if (CollectionUtils.isEmpty(allApplication)) {
88             return new ServiceResult(new ResultHeader(500, "no application"), allApplication);
89         }
90         allApplication.forEach(application -> {
91             KnowledgeBase knowledgeBaseRecordById = datasetMapper.getKnowledgeBaseRecordById(application.getKnowledgeBaseId());
92             if (knowledgeBaseRecordById != null) {
93                 application.setOperatorId(knowledgeBaseRecordById.getOperatorId());
94                 application.setOperatorName(knowledgeBaseRecordById.getOperatorName());
95                 application.setMaaSPlatformId(knowledgeBaseRecordById.getMaaSPlatformId());
96                 application.setMaaSPlatformName(knowledgeBaseRecordById.getMaaSPlatformName());
97                 application.setKnowledgeBaseName(knowledgeBaseRecordById.getKnowledgeBaseName());
98             }
99         });
100         return new ServiceResult(new ResultHeader(200, "success"), allApplication);
101     }
102
103     @Override
104     public ServiceResult getApplicationById(String applicationId) {
105         Application application = applicationMapper.getApplicationById(applicationId);
106         if (application == null) {
107             return new ServiceResult(new ResultHeader(500, "no application"), application);
108         }
109         KnowledgeBase knowledgeBaseRecordById = datasetMapper.getKnowledgeBaseRecordById(application.getKnowledgeBaseId());
110         application.setOperatorId(knowledgeBaseRecordById.getOperatorId());
111         application.setOperatorName(knowledgeBaseRecordById.getOperatorName());
112         application.setMaaSPlatformId(knowledgeBaseRecordById.getMaaSPlatformId());
113         application.setMaaSPlatformName(knowledgeBaseRecordById.getMaaSPlatformName());
114         application.setKnowledgeBaseName(knowledgeBaseRecordById.getKnowledgeBaseName());
115         return new ServiceResult(new ResultHeader(200, "success"), application);
116     }
117
118     @Override
119     public Mono<ServiceResult> editApplication(Application application) {
120         MaaSPlatform maaSPlatformById = maaSPlatformMapper.getMaaSPlatformById(application.getMaaSPlatformId());
121         if (maaSPlatformById == null) {
122             return Mono.just(new ServiceResult(new ResultHeader(500, "maas is not exist")));
123         }
124         String maaSType = maaSPlatformById.getMaaSType();
125         String fastGptType = serverConstant.getFastGptType();
126         if (fastGptType.equals(maaSType)) {
127             return fastGptApplicationService.editApplication(application);
128         }
129         return biShengApplicationService.editApplication(application);
130     }
131
132     private String getMaaSType(String applicationId) {
133         Application applicationById = applicationMapper.getApplicationById(applicationId);
134         KnowledgeBase knowledgeBaseRecordById = datasetMapper.getKnowledgeBaseRecordById(applicationById.getKnowledgeBaseId());
135         MaaSPlatform maaSPlatformById = maaSPlatformMapper.getMaaSPlatformById(knowledgeBaseRecordById.getMaaSPlatformId());
136         return maaSPlatformById.getMaaSType();
137     }
138 }