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.FastGptConstant;
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 public Mono<ServiceResult> createApplication(Application application) {
41 List<Application> applications = applicationMapper.getAllApplication();
42 if (!CollectionUtils.isEmpty(applications)) {
43 List<Application> collect = applications.stream().filter(app -> app.getApplicationName().equals(application.getApplicationName())).toList();
44 if (!collect.isEmpty()) {
45 return Mono.just(new ServiceResult(new ResultHeader(500, "name exists"), applications));
48 MaaSPlatform maaSPlatformById = getMaaSPlatFormById(application.getMaaSPlatformId());
49 if (maaSPlatformById == null) {
50 return Mono.just(new ServiceResult(new ResultHeader(500, "maas is not exist")));
52 String maaSType = maaSPlatformById.getMaaSType();
53 if (FastGptConstant.FAST_GPT.equals(maaSType)) {
54 return fastGptApplicationService.createApplication(application, maaSPlatformById.getServerIp());
56 return biShengApplicationService.createApplication(application, maaSPlatformById.getServerIp());
60 public Mono<ServiceResult> removeApplication(String applicationId) {
61 MaaSPlatform maaSPlatform = getMaaSPlatFormByAppId(applicationId);
62 if (FastGptConstant.FAST_GPT.equals(maaSPlatform.getMaaSType())) {
63 return fastGptApplicationService.removeApplication(applicationId, maaSPlatform.getServerIp());
65 return biShengApplicationService.removeApplication(applicationId, maaSPlatform.getServerIp());
69 public Flux<String> chat(JSONObject question) {
70 String applicationId = question.getString("applicationId");
71 MaaSPlatform maaSPlatform = getMaaSPlatFormByAppId(applicationId);
72 if (FastGptConstant.FAST_GPT.equals(maaSPlatform.getMaaSType())) {
73 return fastGptApplicationService.chat(question, maaSPlatform.getServerIp());
75 return biShengApplicationService.chat(question, maaSPlatform.getServerIp());
79 public ServiceResult getApplications() {
80 List<Application> allApplication = applicationMapper.getAllApplication();
81 if (CollectionUtils.isEmpty(allApplication)) {
82 return new ServiceResult(new ResultHeader(200, "no application"), allApplication);
84 allApplication.forEach(application -> {
85 KnowledgeBase knowledgeBaseRecordById = datasetMapper.getKnowledgeBaseRecordById(application.getKnowledgeBaseId());
86 if (knowledgeBaseRecordById != null) {
87 application.setOperatorId(knowledgeBaseRecordById.getOperatorId());
88 application.setOperatorName(knowledgeBaseRecordById.getOperatorName());
89 application.setMaaSPlatformId(knowledgeBaseRecordById.getMaaSPlatformId());
90 application.setMaaSPlatformName(knowledgeBaseRecordById.getMaaSPlatformName());
91 application.setKnowledgeBaseName(knowledgeBaseRecordById.getKnowledgeBaseName());
94 return new ServiceResult(new ResultHeader(200, "success"), allApplication);
98 public ServiceResult getApplicationById(String applicationId) {
99 Application application = applicationMapper.getApplicationById(applicationId);
100 if (application == null) {
101 return new ServiceResult(new ResultHeader(500, "no application"), application);
103 KnowledgeBase knowledgeBaseRecordById = datasetMapper.getKnowledgeBaseRecordById(application.getKnowledgeBaseId());
104 application.setOperatorId(knowledgeBaseRecordById.getOperatorId());
105 application.setOperatorName(knowledgeBaseRecordById.getOperatorName());
106 application.setMaaSPlatformId(knowledgeBaseRecordById.getMaaSPlatformId());
107 application.setMaaSPlatformName(knowledgeBaseRecordById.getMaaSPlatformName());
108 application.setKnowledgeBaseName(knowledgeBaseRecordById.getKnowledgeBaseName());
109 return new ServiceResult(new ResultHeader(200, "success"), application);
113 public Mono<ServiceResult> editApplication(Application application) {
114 MaaSPlatform maaSPlatformById = getMaaSPlatFormById(application.getMaaSPlatformId());
115 if (maaSPlatformById == null) {
116 return Mono.just(new ServiceResult(new ResultHeader(500, "maas is not exist")));
118 String maaSType = maaSPlatformById.getMaaSType();
119 if (FastGptConstant.FAST_GPT.equals(maaSType)) {
120 return fastGptApplicationService.editApplication(application, maaSPlatformById.getServerIp());
122 return biShengApplicationService.editApplication(application, maaSPlatformById.getServerIp());
125 private MaaSPlatform getMaaSPlatFormByAppId(String applicationId) {
126 Application applicationById = applicationMapper.getApplicationById(applicationId);
127 KnowledgeBase knowledgeBaseRecordById = datasetMapper.getKnowledgeBaseRecordById(applicationById.getKnowledgeBaseId());
128 return getMaaSPlatFormById(knowledgeBaseRecordById.getMaaSPlatformId());
131 private MaaSPlatform getMaaSPlatFormById(String maaSPlatformId) {
132 return maaSPlatformMapper.getMaaSPlatformById(maaSPlatformId);