1486a9917c393071893c50087911509d4a1b96ce
[usecase-ui/llm-adaptation.git] /
1 package org.onap.usecaseui.llmadaptation.service.impl;
2
3 import com.alibaba.fastjson2.JSONArray;
4 import com.alibaba.fastjson2.JSONObject;
5 import io.netty.util.internal.StringUtil;
6 import lombok.extern.slf4j.Slf4j;
7 import org.onap.usecaseui.llmadaptation.bean.Application;
8 import org.onap.usecaseui.llmadaptation.bean.ResultHeader;
9 import org.onap.usecaseui.llmadaptation.bean.ServiceResult;
10 import org.onap.usecaseui.llmadaptation.bean.bisheng.BiShengCreateDatasetResponse;
11 import org.onap.usecaseui.llmadaptation.constant.BiShengConstant;
12 import org.onap.usecaseui.llmadaptation.constant.CommonConstant;
13 import org.onap.usecaseui.llmadaptation.mapper.ApplicationMapper;
14 import org.onap.usecaseui.llmadaptation.service.BiShengApplicationService;
15 import org.onap.usecaseui.llmadaptation.util.TimeUtil;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.stereotype.Service;
18 import org.springframework.web.reactive.function.client.WebClient;
19 import reactor.core.publisher.Flux;
20 import reactor.core.publisher.Mono;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import static org.springframework.http.MediaType.APPLICATION_JSON;
26
27 @Slf4j
28 @Service
29 public class BiShengApplicationServiceImpl implements BiShengApplicationService {
30     @Autowired
31     private ApplicationMapper applicationMapper;
32
33     @Autowired
34     private WebClient webClient;
35
36     @Override
37     public Mono<ServiceResult> createApplication(Application application, String serverIp) {
38         JSONObject createParam = new JSONObject();
39         createParam.put("logo", "");
40         createParam.put("name", application.getApplicationName());
41         createParam.put("prompt", application.getPrompt());
42         return webClient.post()
43                 .uri(serverIp + BiShengConstant.APPLICATION_URL)
44                 .contentType(APPLICATION_JSON)
45                 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
46                 .bodyValue(createParam)
47                 .retrieve()
48                 .bodyToMono(BiShengCreateDatasetResponse.class)
49                 .flatMap(createResponse -> {
50                     JSONObject data = createResponse.getData();
51                     if (data == null) {
52                         return Mono.just(new ServiceResult(new ResultHeader(createResponse.getStatus_code(), createResponse.getStatus_message())));
53                     }
54                     String applicationId = data.getString("id");
55                     String applicationDescription = application.getApplicationDescription();
56                     if (!StringUtil.isNullOrEmpty(applicationDescription)) {
57                         data.put("desc", applicationDescription);
58                     }
59                     data.put("model_name", application.getLargeModelId());
60                     data.put("temperature", application.getTemperature() / 10);
61                     List<Integer> list = new ArrayList<>();
62                     list.add(Integer.valueOf(application.getKnowledgeBaseId()));
63                     data.put("knowledge_list", list);
64                     data.put("guide_word", application.getOpeningRemarks());
65                     data.put("update_time", TimeUtil.getNowTime());
66                     return webClient.put()
67                             .uri(serverIp + BiShengConstant.APPLICATION_URL)
68                             .contentType(APPLICATION_JSON)
69                             .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
70                             .bodyValue(data)
71                             .retrieve()
72                             .bodyToMono(BiShengCreateDatasetResponse.class)
73                             .flatMap(updateResponse -> {
74                                 if (updateResponse.getStatus_code() == 200) {
75                                     application.setApplicationId(applicationId);
76                                     applicationMapper.insertApplication(application);
77                                     return Mono.just(new ServiceResult(new ResultHeader(200, "Application created successfully")));
78                                 }
79                                 log.error("error is {}",updateResponse.getStatus_message());
80                                 return Mono.just(new ServiceResult(new ResultHeader(updateResponse.getStatus_code(), "Application created failed")));
81                             });
82                 }).onErrorResume(e -> {
83                     log.error("Error occurred while creating application: {}", e.getMessage());
84                     return Mono.just(new ServiceResult(new ResultHeader(500, "Application created failed")));
85                 });
86     }
87
88     @Override
89     public Flux<String> chat(JSONObject question, String serverIp) {
90         JSONObject param = new JSONObject();
91         param.put("model", question.getString("applicationId"));
92         param.put("temperature", 0);
93         param.put("stream", true);
94         JSONObject message = new JSONObject();
95         message.put("role", "user");
96         message.put("content", question.getString("question"));
97         JSONArray jsonArray = new JSONArray();
98         jsonArray.add(message);
99         param.put("messages", jsonArray);
100         return webClient.post()
101                 .uri(serverIp + BiShengConstant.APPLICATION_CHAT_URL)
102                 .bodyValue(param)
103                 .retrieve()
104                 .bodyToFlux(String.class)
105                 .flatMap(response -> {
106                     if ("[DONE]".equals(response)) {
107                         return Flux.just(response);
108                     }
109                     JSONArray choices = JSONObject.parseObject(response).getJSONArray("choices");
110                     String jsonString = JSONObject.toJSONString(choices.get(0));
111                     JSONObject jsonObject = JSONObject.parseObject(jsonString);
112                     String string = jsonObject.getJSONObject("delta").getString("content");
113                     return Flux.just(string);
114                 })
115                 .onErrorResume(e -> {
116                     log.error("An error occurred {}", e.getMessage());
117                     return Flux.just("Network Error");
118                 });
119     }
120
121     @Override
122     public Mono<ServiceResult> removeApplication(String applicationId, String serverIp) {
123         String url = serverIp + BiShengConstant.DELETE_APPLICATION + applicationId;
124         return webClient.post()
125                 .uri(url)
126                 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
127                 .retrieve()
128                 .bodyToMono(BiShengCreateDatasetResponse.class)
129                 .flatMap(response -> {
130                     if (response.getStatus_code() == 200 || response.getStatus_code() == 10400) {
131                         return Mono.fromRunnable(() -> {
132                             try {
133                                 applicationMapper.deleteApplicationById(applicationId);
134                             } catch (Exception dbException) {
135                                 throw new RuntimeException("Database operation failed", dbException);
136                             }
137                         }).then(Mono.just(new ServiceResult(new ResultHeader(200, "delete success"))));
138                     } else {
139                         return Mono.just(new ServiceResult(new ResultHeader(500, response.getStatus_message())));
140                     }
141                 })
142                 .onErrorResume(e -> {
143                     log.error("Error occurred while delete dataset: {}", e.getMessage());
144                     return Mono.just(new ServiceResult(new ResultHeader(500, "delete failed")));
145                 });
146     }
147
148     @Override
149     public Mono<ServiceResult> editApplication(Application application, String serverIp) {
150         String url = serverIp + BiShengConstant.GET_APPLICATION_URL + application.getApplicationId();
151         return webClient.get()
152                 .uri(url)
153                 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
154                 .retrieve()
155                 .bodyToMono(BiShengCreateDatasetResponse.class)
156                 .flatMap(createResponse -> {
157                     JSONObject data = createResponse.getData();
158                     if (data == null) {
159                         return Mono.just(new ServiceResult(new ResultHeader(createResponse.getStatus_code(), createResponse.getStatus_message())));
160                     }
161                     data.put("desc", application.getApplicationDescription());
162                     data.put("name", application.getApplicationName());
163                     data.put("update_time", TimeUtil.getNowTime());
164                     List<Integer> list = new ArrayList<>();
165                     list.add(Integer.valueOf(application.getKnowledgeBaseId()));
166                     data.put("knowledge_list", list);
167                     data.put("model_name", application.getLargeModelId());
168                     data.put("temperature", application.getTemperature() / 10);
169                     data.put("prompt", application.getPrompt());
170                     data.put("guide_word", application.getOpeningRemarks());
171                     return webClient.put()
172                             .uri(serverIp + BiShengConstant.APPLICATION_URL)
173                             .contentType(APPLICATION_JSON)
174                             .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
175                             .bodyValue(data)
176                             .retrieve()
177                             .bodyToMono(BiShengCreateDatasetResponse.class)
178                             .flatMap(updateResponse -> {
179                                 if (updateResponse.getStatus_code() == 200) {
180                                     applicationMapper.insertApplication(application);
181                                     return Mono.just(new ServiceResult(new ResultHeader(200, "Application update successfully")));
182                                 }
183                                 log.error("error is {}", updateResponse.getStatus_message());
184                                 return Mono.just(new ServiceResult(new ResultHeader(updateResponse.getStatus_code(), "Application update failed")));
185                             });
186                 }).onErrorResume(e -> {
187                     log.error("Error occurred while update application: {}", e.getMessage());
188                     return Mono.just(new ServiceResult(new ResultHeader(500, "Application update failed")));
189                 });
190     }
191 }