1 package org.onap.usecaseui.llmadaptation.service.impl;
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.mapper.ApplicationMapper;
13 import org.onap.usecaseui.llmadaptation.service.BiShengApplicationService;
14 import org.onap.usecaseui.llmadaptation.util.TimeUtil;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.stereotype.Service;
17 import org.springframework.web.reactive.function.client.WebClient;
18 import reactor.core.publisher.Flux;
19 import reactor.core.publisher.Mono;
21 import java.util.ArrayList;
22 import java.util.List;
24 import static org.springframework.http.MediaType.APPLICATION_JSON;
28 public class BiShengApplicationServiceImpl implements BiShengApplicationService {
30 private ApplicationMapper applicationMapper;
33 private WebClient webClient;
36 public Mono<ServiceResult> createApplication(Application application, String serverIp) {
37 JSONObject createParam = new JSONObject();
38 createParam.put("logo", "");
39 createParam.put("name", application.getApplicationName());
40 createParam.put("prompt", application.getPrompt());
41 return webClient.post()
42 .uri(serverIp + BiShengConstant.APPLICATION_URL)
43 .contentType(APPLICATION_JSON)
44 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
45 .bodyValue(createParam)
47 .bodyToMono(BiShengCreateDatasetResponse.class)
48 .flatMap(createResponse -> {
49 JSONObject data = createResponse.getData();
51 return Mono.just(new ServiceResult(new ResultHeader(createResponse.getStatus_code(), createResponse.getStatus_message())));
53 String applicationId = data.getString("id");
54 data.put("desc", application.getApplicationDescription());
55 data.put("model_name", application.getLargeModelId());
56 data.put("temperature", application.getTemperature() / 10);
57 List<Integer> list = new ArrayList<>();
58 list.add(Integer.valueOf(application.getKnowledgeBaseId()));
59 data.put("knowledge_list", list);
60 data.put("guide_word", application.getOpeningRemarks());
61 data.put("update_time", TimeUtil.getNowTime());
62 return webClient.put()
63 .uri(serverIp + BiShengConstant.APPLICATION_URL)
64 .contentType(APPLICATION_JSON)
65 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
68 .bodyToMono(BiShengCreateDatasetResponse.class)
69 .flatMap(updateResponse -> {
70 application.setApplicationId(applicationId);
71 applicationMapper.insertApplication(application);
72 return Mono.just(new ServiceResult(new ResultHeader(200, "Application created successfully")));
74 }).onErrorResume(e -> {
75 log.error("Error occurred while creating application: {}", e.getMessage());
76 return Mono.just(new ServiceResult(new ResultHeader(500, "Application creation failed")));
81 public Flux<String> chat(JSONObject question, String serverIp) {
82 JSONObject param = new JSONObject();
83 param.put("model", question.getString("applicationId"));
84 param.put("temperature", 0);
85 param.put("stream", true);
86 JSONObject message = new JSONObject();
87 message.put("role", "user");
88 message.put("content", question.getString("question"));
89 JSONArray jsonArray = new JSONArray();
90 jsonArray.add(message);
91 param.put("messages", jsonArray);
92 return webClient.post()
93 .uri(serverIp + BiShengConstant.APPLICATION_CHAT_URL)
96 .bodyToFlux(String.class)
97 .flatMap(response -> {
98 if("[DONE]".equals(response)){
99 return Flux.just(response);
101 JSONArray choices = JSONObject.parseObject(response).getJSONArray("choices");
102 String jsonString = JSONObject.toJSONString(choices.get(0));
103 JSONObject jsonObject = JSONObject.parseObject(jsonString);
104 String string = jsonObject.getJSONObject("delta").getString("content");
105 return Flux.just(string);
107 .onErrorResume(e -> {
108 log.error("An error occurred {}", e.getMessage());
109 return Flux.just("Network Error");
114 public Mono<ServiceResult> removeApplication(String applicationId, String serverIp) {
115 String url = serverIp + BiShengConstant.DELETE_APPLICATION + applicationId;
116 return webClient.post()
118 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
120 .bodyToMono(BiShengCreateDatasetResponse.class)
121 .flatMap(response -> {
122 if (response.getStatus_code() == 200) {
123 return Mono.fromRunnable(() -> {
125 applicationMapper.deleteApplicationById(applicationId);
126 } catch (Exception dbException) {
127 throw new RuntimeException("Database operation failed", dbException);
129 }).then(Mono.just(new ServiceResult(new ResultHeader(200, "delete success"))));
131 return Mono.just(new ServiceResult(new ResultHeader(500, response.getStatus_message())));
134 .onErrorResume(e -> {
135 log.error("Error occurred while delete dataset: {}", e.getMessage());
136 return Mono.just(new ServiceResult(new ResultHeader(500, "delete failed")));
141 public Mono<ServiceResult> editApplication(Application application, String serverIp) {
142 String url = serverIp + BiShengConstant.GET_APPLICATION_URL + application.getApplicationId();
143 return webClient.get()
145 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
147 .bodyToMono(BiShengCreateDatasetResponse.class)
148 .flatMap(createResponse -> {
149 JSONObject data = createResponse.getData();
151 return Mono.just(new ServiceResult(new ResultHeader(createResponse.getStatus_code(), createResponse.getStatus_message())));
153 data.put("desc", application.getApplicationDescription());
154 data.put("name", application.getApplicationName());
155 data.put("update_time", TimeUtil.getNowTime());
156 List<Integer> list = new ArrayList<>();
157 list.add(Integer.valueOf(application.getKnowledgeBaseId()));
158 data.put("knowledge_list", list);
159 data.put("model_name", application.getLargeModelId());
160 data.put("temperature", application.getTemperature() / 10);
161 data.put("prompt",application.getPrompt());
162 data.put("guide_word", application.getOpeningRemarks());
163 return webClient.put()
164 .uri(serverIp + BiShengConstant.APPLICATION_URL)
165 .contentType(APPLICATION_JSON)
166 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
169 .bodyToMono(BiShengCreateDatasetResponse.class)
170 .flatMap(updateResponse -> {
171 applicationMapper.updateApplication(application);
172 return Mono.just(new ServiceResult(new ResultHeader(200, "Application update successfully")));
174 }).onErrorResume(e -> {
175 log.error("Error occurred while update application: {}", e.getMessage());
176 return Mono.just(new ServiceResult(new ResultHeader(500, "Application update failed")));