1 package org.onap.usecaseui.llmadaptation.service.impl;
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;
19 import java.util.List;
23 public class ApplicationServiceImpl implements ApplicationService {
25 private ApplicationMapper applicationMapper;
28 private DatasetMapper datasetMapper;
31 private FastGptApplicationService fastGptApplicationService;
34 private BiShengApplicationService biShengApplicationService;
37 private MaaSPlatformMapper maaSPlatformMapper;
40 private ServerConstant serverConstant;
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));
51 MaaSPlatform maaSPlatformById = maaSPlatformMapper.getMaaSPlatformById(application.getMaaSPlatformId());
52 if (maaSPlatformById == null) {
53 return Mono.just(new ServiceResult(new ResultHeader(500, "maas is not exist")));
55 String maaSType = maaSPlatformById.getMaaSType();
56 String fastGptType = serverConstant.getFastGptType();
57 if (fastGptType.equals(maaSType)) {
58 return fastGptApplicationService.createApplication(application);
60 return biShengApplicationService.createApplication(application);
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);
70 return biShengApplicationService.removeApplication(applicationId);
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);
81 return biShengApplicationService.chat(question);
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);
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());
100 return new ServiceResult(new ResultHeader(200, "success"), allApplication);
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);
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);
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")));
124 String maaSType = maaSPlatformById.getMaaSType();
125 String fastGptType = serverConstant.getFastGptType();
126 if (fastGptType.equals(maaSType)) {
127 return fastGptApplicationService.editApplication(application);
129 return biShengApplicationService.editApplication(application);
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();