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.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;
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 private ServerConstant serverConstant;
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)
51 .bodyToMono(BiShengCreateDatasetResponse.class)
52 .flatMap(createResponse -> {
53 JSONObject data = createResponse.getData();
55 return Mono.just(new ServiceResult(new ResultHeader(createResponse.getStatus_code(), createResponse.getStatus_message())));
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)
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")));
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")));
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)
99 .bodyToFlux(String.class)
100 .flatMap(response -> {
101 if("[DONE]".equals(response)){
102 return Flux.just(response);
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);
110 .onErrorResume(e -> {
111 log.error("An error occurred {}", e.getMessage());
112 return Flux.just("Network Error");
117 public Mono<ServiceResult> removeApplication(String applicationId) {
118 String url = serverConstant.getBiShengServer() + BiShengConstant.DELETE_APPLICATION + applicationId;
119 return webClient.post()
121 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
123 .bodyToMono(BiShengCreateDatasetResponse.class)
124 .flatMap(response -> {
125 if (response.getStatus_code() == 200) {
126 return Mono.fromRunnable(() -> {
128 applicationMapper.deleteApplicationById(applicationId);
129 } catch (Exception dbException) {
130 throw new RuntimeException("Database operation failed", dbException);
132 }).then(Mono.just(new ServiceResult(new ResultHeader(200, "delete success"))));
134 return Mono.just(new ServiceResult(new ResultHeader(500, response.getStatus_message())));
137 .onErrorResume(e -> {
138 log.error("Error occurred while delete dataset: {}", e.getMessage());
139 return Mono.just(new ServiceResult(new ResultHeader(500, "delete failed")));
144 public Mono<ServiceResult> editApplication(Application application) {
145 String url = serverConstant.getBiShengServer() + BiShengConstant.GET_APPLICATION_URL + application.getApplicationId();
146 return webClient.get()
148 .header(CommonConstant.COOKIE, BiShengConstant.COOKIE_VALUE)
150 .bodyToMono(BiShengCreateDatasetResponse.class)
151 .flatMap(createResponse -> {
152 JSONObject data = createResponse.getData();
154 return Mono.just(new ServiceResult(new ResultHeader(createResponse.getStatus_code(), createResponse.getStatus_message())));
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)
168 .bodyToMono(BiShengCreateDatasetResponse.class)
169 .flatMap(updateResponse -> {
170 applicationMapper.updateApplication(application);
171 return Mono.just(new ServiceResult(new ResultHeader(200, "Application update successfully")));
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")));