737fc66e32da789ed20a72d510788a90880787b1
[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 lombok.extern.slf4j.Slf4j;
6 import org.onap.usecaseui.llmadaptation.bean.Application;
7 import org.onap.usecaseui.llmadaptation.bean.ResultHeader;
8 import org.onap.usecaseui.llmadaptation.bean.ServiceResult;
9 import org.onap.usecaseui.llmadaptation.bean.bisheng.BiShengCreateDatasetResponse;
10 import org.onap.usecaseui.llmadaptation.constant.BiShengConstant;
11 import org.onap.usecaseui.llmadaptation.constant.CommonConstant;
12 import org.onap.usecaseui.llmadaptation.constant.ServerConstant;
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     @Autowired
37     private ServerConstant serverConstant;
38
39     @Override
40     public Mono<ServiceResult> createApplication(Application application) {
41         JSONObject createParam = new JSONObject();
42         createParam.put("logo", "");
43         createParam.put("name", application.getApplicationName());
44         createParam.put("prompt", application.getPrompt());
45         return webClient.post()
46                 .uri(serverConstant.getBiShengServer() + BiShengConstant.APPLICATION_URL)
47                 .contentType(APPLICATION_JSON)
48                 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
49                 .bodyValue(createParam)
50                 .retrieve()
51                 .bodyToMono(BiShengCreateDatasetResponse.class)
52                 .flatMap(createResponse -> {
53                     JSONObject data = createResponse.getData();
54                     if (data == null) {
55                         return Mono.just(new ServiceResult(new ResultHeader(createResponse.getStatus_code(), createResponse.getStatus_message())));
56                     }
57                     String applicationId = data.getString("id");
58                     data.put("model_name", application.getLargeModelId());
59                     data.put("temperature", application.getTemperature() / 10);
60                     List<Integer> list = new ArrayList<>();
61                     list.add(Integer.valueOf(application.getKnowledgeBaseId()));
62                     data.put("knowledge_list", list);
63                     data.put("guide_word", application.getOpeningRemarks());
64                     data.put("update_time", TimeUtil.getNowTime());
65                     return webClient.put()
66                             .uri(serverConstant.getBiShengServer() + BiShengConstant.APPLICATION_URL)
67                             .contentType(APPLICATION_JSON)
68                             .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
69                             .bodyValue(data)
70                             .retrieve()
71                             .bodyToMono(BiShengCreateDatasetResponse.class)
72                             .flatMap(updateResponse -> {
73                                 application.setApplicationId(applicationId);
74                                 applicationMapper.insertApplication(application);
75                                 return Mono.just(new ServiceResult(new ResultHeader(200, "Application created successfully")));
76                             });
77                 }).onErrorResume(e -> {
78                     log.error("Error occurred while creating application: {}", e.getMessage());
79                     return Mono.just(new ServiceResult(new ResultHeader(500, "Application creation failed")));
80                 });
81     }
82
83     @Override
84     public Flux<String> chat(JSONObject question) {
85         JSONObject param = new JSONObject();
86         param.put("model", question.getString("applicationId"));
87         param.put("temperature", 0);
88         param.put("stream", true);
89         JSONObject message = new JSONObject();
90         message.put("role", "user");
91         message.put("content", question.getString("question"));
92         JSONArray jsonArray = new JSONArray();
93         jsonArray.add(message);
94         param.put("messages", jsonArray);
95         return webClient.post()
96                 .uri(serverConstant.getBiShengServer() + BiShengConstant.APPLICATION_CHAT_URL)
97                 .bodyValue(param)
98                 .retrieve()
99                 .bodyToFlux(String.class)
100                 .flatMap(response -> {
101                     if("[DONE]".equals(response)){
102                         return Flux.just(response);
103                     }
104                     JSONArray choices = JSONObject.parseObject(response).getJSONArray("choices");
105                     String jsonString = JSONObject.toJSONString(choices.get(0));
106                     JSONObject jsonObject = JSONObject.parseObject(jsonString);
107                     String string = jsonObject.getJSONObject("delta").getString("content");
108                     return Flux.just(string);
109                 })
110                 .onErrorResume(e -> {
111                     log.error("An error occurred {}", e.getMessage());
112                     return Flux.just("Network Error");
113                 });
114     }
115
116     @Override
117     public Mono<ServiceResult> removeApplication(String applicationId) {
118         String url = serverConstant.getBiShengServer() + BiShengConstant.DELETE_APPLICATION + applicationId;
119         return webClient.post()
120                 .uri(url)
121                 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
122                 .retrieve()
123                 .bodyToMono(BiShengCreateDatasetResponse.class)
124                 .flatMap(response -> {
125                     if (response.getStatus_code() == 200) {
126                         return Mono.fromRunnable(() -> {
127                             try {
128                                 applicationMapper.deleteApplicationById(applicationId);
129                             } catch (Exception dbException) {
130                                 throw new RuntimeException("Database operation failed", dbException);
131                             }
132                         }).then(Mono.just(new ServiceResult(new ResultHeader(200, "delete success"))));
133                     } else {
134                         return Mono.just(new ServiceResult(new ResultHeader(500, response.getStatus_message())));
135                     }
136                 })
137                 .onErrorResume(e -> {
138                     log.error("Error occurred while delete dataset: {}", e.getMessage());
139                     return Mono.just(new ServiceResult(new ResultHeader(500, "delete failed")));
140                 });
141     }
142
143     @Override
144     public Mono<ServiceResult> editApplication(Application application) {
145         String url = serverConstant.getBiShengServer() + BiShengConstant.GET_APPLICATION_URL + application.getApplicationId();
146         return webClient.get()
147                 .uri(url)
148                 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
149                 .retrieve()
150                 .bodyToMono(BiShengCreateDatasetResponse.class)
151                 .flatMap(createResponse -> {
152                     JSONObject data = createResponse.getData();
153                     if (data == null) {
154                         return Mono.just(new ServiceResult(new ResultHeader(createResponse.getStatus_code(), createResponse.getStatus_message())));
155                     }
156                     data.put("desc", application.getApplicationDescription());
157                     data.put("name", application.getApplicationName());
158                     data.put("update_time", TimeUtil.getNowTime());
159                     List<Integer> list = new ArrayList<>();
160                     list.add(Integer.valueOf(application.getKnowledgeBaseId()));
161                     data.put("knowledge_list", list);
162                     return webClient.put()
163                             .uri(serverConstant.getBiShengServer() + BiShengConstant.APPLICATION_URL)
164                             .contentType(APPLICATION_JSON)
165                             .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
166                             .bodyValue(data)
167                             .retrieve()
168                             .bodyToMono(BiShengCreateDatasetResponse.class)
169                             .flatMap(updateResponse -> {
170                                 applicationMapper.updateApplication(application);
171                                 return Mono.just(new ServiceResult(new ResultHeader(200, "Application update successfully")));
172                             });
173                 }).onErrorResume(e -> {
174                     log.error("Error occurred while update application: {}", e.getMessage());
175                     return Mono.just(new ServiceResult(new ResultHeader(500, "Application update failed")));
176                 });
177     }
178 }