Modify the return value of the interactive interface 62/140262/1 master
authorkaixiliu <liukaixi@chinamobile.com>
Fri, 21 Feb 2025 02:08:10 +0000 (10:08 +0800)
committerkaixiliu <liukaixi@chinamobile.com>
Fri, 21 Feb 2025 02:08:31 +0000 (10:08 +0800)
Issue-ID: USECASEUI-844
Change-Id: Ia8142587fb958b85a0d7a32aee77a6727c3441bf
Signed-off-by: kaixiliu <liukaixi@chinamobile.com>
llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/bean/ChatResponse.java [new file with mode: 0644]
llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/config/WebClientConfig.java
llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/controller/ApplicationController.java
llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/service/ApplicationService.java
llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/service/BiShengApplicationService.java
llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/service/FastGptApplicationService.java
llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/service/impl/ApplicationServiceImpl.java
llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/service/impl/BiShengApplicationServiceImpl.java
llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/service/impl/FastGptApplicationServiceImpl.java
llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/util/CommonUtil.java [new file with mode: 0644]

diff --git a/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/bean/ChatResponse.java b/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/bean/ChatResponse.java
new file mode 100644 (file)
index 0000000..c7cdc2e
--- /dev/null
@@ -0,0 +1,14 @@
+package org.onap.usecaseui.llmadaptation.bean;
+
+import lombok.Data;
+
+@Data
+public class ChatResponse {
+    private ResultHeader result_header;
+
+    private String finished;
+
+    private String answer;
+
+    private String reference;
+}
index 8434b6b..234157f 100644 (file)
@@ -31,8 +31,8 @@ public class WebClientConfig {
     public WebClient getWebClient() {
         HttpClient httpClient = HttpClient.create()
                 .tcpConfiguration(tcpClient -> tcpClient
-                        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 50000))
-                .responseTimeout(Duration.ofSeconds(50));
+                        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 60000))
+                .responseTimeout(Duration.ofSeconds(60));
         return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
     }
 }
index a6493de..796e59f 100644 (file)
@@ -29,7 +29,7 @@ public class ApplicationController {
     }
 
     @PostMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
-    public Flux<String> streamData(@RequestBody JSONObject question) {
+    public Flux<ChatResponse> streamData(@RequestBody JSONObject question) {
         return applicationService.chat(question);
     }
 
index 636832e..bb61e19 100644 (file)
@@ -2,6 +2,7 @@ package org.onap.usecaseui.llmadaptation.service;
 
 import com.alibaba.fastjson2.JSONObject;
 import org.onap.usecaseui.llmadaptation.bean.Application;
+import org.onap.usecaseui.llmadaptation.bean.ChatResponse;
 import org.onap.usecaseui.llmadaptation.bean.ServiceResult;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
@@ -9,7 +10,7 @@ import reactor.core.publisher.Mono;
 public interface ApplicationService {
     Mono<ServiceResult> createApplication(Application application);
 
-    Flux<String> chat(JSONObject question);
+    Flux<ChatResponse> chat(JSONObject question);
 
     Mono<ServiceResult> removeApplication(String applicationId);
 
index 391a146..fddaf15 100644 (file)
@@ -2,6 +2,7 @@ package org.onap.usecaseui.llmadaptation.service;
 
 import com.alibaba.fastjson2.JSONObject;
 import org.onap.usecaseui.llmadaptation.bean.Application;
+import org.onap.usecaseui.llmadaptation.bean.ChatResponse;
 import org.onap.usecaseui.llmadaptation.bean.ServiceResult;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
@@ -9,7 +10,7 @@ import reactor.core.publisher.Mono;
 public interface BiShengApplicationService {
     Mono<ServiceResult> createApplication(Application application,  String serverIp);
 
-    Flux<String> chat(JSONObject question, String serverIp);
+    Flux<ChatResponse> chat(JSONObject question, String serverIp);
 
     Mono<ServiceResult> removeApplication(String applicationId, String serverIp);
 
index 2d02778..89323f6 100644 (file)
@@ -2,6 +2,7 @@ package org.onap.usecaseui.llmadaptation.service;
 
 import com.alibaba.fastjson2.JSONObject;
 import org.onap.usecaseui.llmadaptation.bean.Application;
+import org.onap.usecaseui.llmadaptation.bean.ChatResponse;
 import org.onap.usecaseui.llmadaptation.bean.ServiceResult;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
@@ -9,7 +10,7 @@ import reactor.core.publisher.Mono;
 public interface FastGptApplicationService {
     Mono<ServiceResult> createApplication(Application application, String serverIp);
 
-    Flux<String> chat(JSONObject question,  String serverIp);
+    Flux<ChatResponse> chat(JSONObject question, String serverIp);
 
     Mono<ServiceResult> removeApplication(String applicationId,  String serverIp);
 
index 90de2c7..071edea 100644 (file)
@@ -66,7 +66,7 @@ public class ApplicationServiceImpl implements ApplicationService {
     }
 
     @Override
-    public Flux<String> chat(JSONObject question) {
+    public Flux<ChatResponse> chat(JSONObject question) {
         String applicationId = question.getString("applicationId");
         MaaSPlatform maaSPlatform = getMaaSPlatFormByAppId(applicationId);
         if (FastGptConstant.FAST_GPT.equals(maaSPlatform.getMaaSType())) {
index 1486a99..8a138ba 100644 (file)
@@ -5,6 +5,7 @@ import com.alibaba.fastjson2.JSONObject;
 import io.netty.util.internal.StringUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.usecaseui.llmadaptation.bean.Application;
+import org.onap.usecaseui.llmadaptation.bean.ChatResponse;
 import org.onap.usecaseui.llmadaptation.bean.ResultHeader;
 import org.onap.usecaseui.llmadaptation.bean.ServiceResult;
 import org.onap.usecaseui.llmadaptation.bean.bisheng.BiShengCreateDatasetResponse;
@@ -12,6 +13,7 @@ import org.onap.usecaseui.llmadaptation.constant.BiShengConstant;
 import org.onap.usecaseui.llmadaptation.constant.CommonConstant;
 import org.onap.usecaseui.llmadaptation.mapper.ApplicationMapper;
 import org.onap.usecaseui.llmadaptation.service.BiShengApplicationService;
+import org.onap.usecaseui.llmadaptation.util.CommonUtil;
 import org.onap.usecaseui.llmadaptation.util.TimeUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -86,7 +88,7 @@ public class BiShengApplicationServiceImpl implements BiShengApplicationService
     }
 
     @Override
-    public Flux<String> chat(JSONObject question, String serverIp) {
+    public Flux<ChatResponse> chat(JSONObject question, String serverIp) {
         JSONObject param = new JSONObject();
         param.put("model", question.getString("applicationId"));
         param.put("temperature", 0);
@@ -103,18 +105,25 @@ public class BiShengApplicationServiceImpl implements BiShengApplicationService
                 .retrieve()
                 .bodyToFlux(String.class)
                 .flatMap(response -> {
+                    ChatResponse result = new ChatResponse();
+                    result.setReference("");
+                    result.setFinished("stop");
+                    ResultHeader resultHeader = new ResultHeader(200,"success");
+                    result.setResult_header(resultHeader);
                     if ("[DONE]".equals(response)) {
-                        return Flux.just(response);
+                        result.setAnswer("[DONE]");
+                        return Flux.just(result);
                     }
                     JSONArray choices = JSONObject.parseObject(response).getJSONArray("choices");
                     String jsonString = JSONObject.toJSONString(choices.get(0));
                     JSONObject jsonObject = JSONObject.parseObject(jsonString);
                     String string = jsonObject.getJSONObject("delta").getString("content");
-                    return Flux.just(string);
+                    result.setAnswer(string);
+                    return Flux.just(result);
                 })
                 .onErrorResume(e -> {
                     log.error("An error occurred {}", e.getMessage());
-                    return Flux.just("Network Error");
+                    return CommonUtil.chatFailed();
                 });
     }
 
index 22a54a6..a488226 100644 (file)
@@ -5,6 +5,7 @@ import com.alibaba.fastjson2.JSONObject;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.usecaseui.llmadaptation.bean.Application;
+import org.onap.usecaseui.llmadaptation.bean.ChatResponse;
 import org.onap.usecaseui.llmadaptation.bean.ResultHeader;
 import org.onap.usecaseui.llmadaptation.bean.ServiceResult;
 import org.onap.usecaseui.llmadaptation.bean.fastgpt.dataset.CreateDataSetResponse;
@@ -13,6 +14,7 @@ import org.onap.usecaseui.llmadaptation.constant.CommonConstant;
 import org.onap.usecaseui.llmadaptation.constant.FastGptConstant;
 import org.onap.usecaseui.llmadaptation.mapper.ApplicationMapper;
 import org.onap.usecaseui.llmadaptation.service.FastGptApplicationService;
+import org.onap.usecaseui.llmadaptation.util.CommonUtil;
 import org.onap.usecaseui.llmadaptation.util.TimeUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ResourceLoader;
@@ -164,8 +166,7 @@ public class FastGptApplicationServiceImpl implements FastGptApplicationService
         });
     }
 
-    @Override
-    public Flux<String> chat(JSONObject question,  String serverIp) {
+    public Flux<ChatResponse> chat(JSONObject question, String serverIp) {
         ChatParam chatParam = new ChatParam();
         chatParam.setAppId(question.getString("applicationId"));
         chatParam.setStream(true);
@@ -192,11 +193,11 @@ public class FastGptApplicationServiceImpl implements FastGptApplicationService
                 .bodyToFlux(String.class).flatMap(response -> parseAndTransform(response, isDone))
                 .onErrorResume(throwable -> {
                     log.error("An error occurred {}", throwable.getMessage());
-                    return Flux.just("Network Error");
+                    return CommonUtil.chatFailed();
                 });
     }
 
-    private Flux<String> parseAndTransform(String param, AtomicBoolean isDone) {
+    private Flux<ChatResponse> parseAndTransform(String param, AtomicBoolean isDone) {
         if (isDone.get()) {
             return Flux.empty();
         }
@@ -206,14 +207,21 @@ public class FastGptApplicationServiceImpl implements FastGptApplicationService
         }
         JSONArray choices = jsonObject.getJSONArray("choices");
         JSONObject choice = choices.getJSONObject(0);
+        ChatResponse response = new ChatResponse();
+        response.setReference("");
+        response.setFinished("stop");
+        ResultHeader resultHeader = new ResultHeader(200,"success");
+        response.setResult_header(resultHeader);
         if ("stop".equals(choice.getString("finish_reason"))) {
             isDone.set(true);
-            return Flux.just("[DONE]");
+            response.setAnswer("[DONE]");
+            return Flux.just(response);
         }
         String string = choice.getJSONObject("delta").getString("content");
         isDone.set(false);
         string = string.replace(" ", "__SPACE__");
-        return Flux.just(string);
+        response.setAnswer(string);
+        return Flux.just(response);
     }
 
     @Override
diff --git a/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/util/CommonUtil.java b/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/util/CommonUtil.java
new file mode 100644 (file)
index 0000000..1392ab5
--- /dev/null
@@ -0,0 +1,18 @@
+package org.onap.usecaseui.llmadaptation.util;
+
+import org.onap.usecaseui.llmadaptation.bean.ChatResponse;
+import org.onap.usecaseui.llmadaptation.bean.ResultHeader;
+import reactor.core.publisher.Flux;
+
+public class CommonUtil {
+
+    public static Flux<ChatResponse> chatFailed() {
+        ChatResponse result = new ChatResponse();
+        result.setReference("");
+        result.setFinished("error");
+        ResultHeader resultHeader = new ResultHeader(500, "failure");
+        result.setResult_header(resultHeader);
+        result.setAnswer("Network Error");
+        return Flux.just(result);
+    }
+}