1 package org.onap.usecaseui.llmadaptation.service.impl;
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;
22 import java.util.ArrayList;
23 import java.util.List;
25 import static org.springframework.http.MediaType.APPLICATION_JSON;
29 public class BiShengApplicationServiceImpl implements BiShengApplicationService {
31 private ApplicationMapper applicationMapper;
34 private WebClient webClient;
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)
48 .bodyToMono(BiShengCreateDatasetResponse.class)
49 .flatMap(createResponse -> {
50 JSONObject data = createResponse.getData();
52 return Mono.just(new ServiceResult(new ResultHeader(createResponse.getStatus_code(), createResponse.getStatus_message())));
54 String applicationId = data.getString("id");
55 String applicationDescription = application.getApplicationDescription();
56 if (!StringUtil.isNullOrEmpty(applicationDescription)) {
57 data.put("desc", applicationDescription);
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)
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")));
79 log.error("error is {}",updateResponse.getStatus_message());
80 return Mono.just(new ServiceResult(new ResultHeader(updateResponse.getStatus_code(), "Application created failed")));
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")));
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)
104 .bodyToFlux(String.class)
105 .flatMap(response -> {
106 if ("[DONE]".equals(response)) {
107 return Flux.just(response);
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);
115 .onErrorResume(e -> {
116 log.error("An error occurred {}", e.getMessage());
117 return Flux.just("Network Error");
122 public Mono<ServiceResult> removeApplication(String applicationId, String serverIp) {
123 String url = serverIp + BiShengConstant.DELETE_APPLICATION + applicationId;
124 return webClient.post()
126 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
128 .bodyToMono(BiShengCreateDatasetResponse.class)
129 .flatMap(response -> {
130 if (response.getStatus_code() == 200 || response.getStatus_code() == 10400) {
131 return Mono.fromRunnable(() -> {
133 applicationMapper.deleteApplicationById(applicationId);
134 } catch (Exception dbException) {
135 throw new RuntimeException("Database operation failed", dbException);
137 }).then(Mono.just(new ServiceResult(new ResultHeader(200, "delete success"))));
139 return Mono.just(new ServiceResult(new ResultHeader(500, response.getStatus_message())));
142 .onErrorResume(e -> {
143 log.error("Error occurred while delete dataset: {}", e.getMessage());
144 return Mono.just(new ServiceResult(new ResultHeader(500, "delete failed")));
149 public Mono<ServiceResult> editApplication(Application application, String serverIp) {
150 String url = serverIp + BiShengConstant.GET_APPLICATION_URL + application.getApplicationId();
151 return webClient.get()
153 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
155 .bodyToMono(BiShengCreateDatasetResponse.class)
156 .flatMap(createResponse -> {
157 JSONObject data = createResponse.getData();
159 return Mono.just(new ServiceResult(new ResultHeader(createResponse.getStatus_code(), createResponse.getStatus_message())));
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)
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")));
183 log.error("error is {}", updateResponse.getStatus_message());
184 return Mono.just(new ServiceResult(new ResultHeader(updateResponse.getStatus_code(), "Application update failed")));
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")));