Ver código fonte

添加每次增量输出内容延迟

zhaojinyu 1 mês atrás
pai
commit
846181ffe2

+ 20 - 1
service-ai/service-ai-biz/src/main/java/com/usky/ai/controller/web/AiChatController.java

@@ -53,6 +53,9 @@ public class AiChatController {
     @Value("${ai.historyLimit}")
     private int Limit;
 
+    @Value("${ai.millis}")
+    private int millis;
+
     @Resource
     private Generation generation;
 
@@ -169,7 +172,7 @@ public class AiChatController {
                 .contentType(MediaType.TEXT_EVENT_STREAM)
                 .body(outputStream -> {
                     try {
-                        // 调用流式接口
+// 调用流式接口
                         generation.streamCall(param).blockingForEach(chunk -> {
                             try {
                                 // 获取每次生成的内容
@@ -195,6 +198,14 @@ public class AiChatController {
                                 // 累加到完整回答内容中
                                 completeAnswer.append(partialAnswer);
 
+                                // 添加延迟
+                                try {
+                                    Thread.sleep(millis); // 延迟*毫秒
+                                } catch (InterruptedException e) {
+                                    Thread.currentThread().interrupt();
+                                    log.error("Thread interrupted", e);
+                                }
+
                             } catch (Exception e) {
                                 log.error("Error processing chunk", e);
                                 outputStream.write(("data: Error processing chunk\n\n").getBytes(StandardCharsets.UTF_8));
@@ -352,6 +363,14 @@ public class AiChatController {
                                 // 累加到完整回答内容中
                                 completeAnswer.append(partialAnswer);
 
+                                // 添加延迟
+                                try {
+                                    Thread.sleep(millis); // 延迟*毫秒
+                                } catch (InterruptedException e) {
+                                    Thread.currentThread().interrupt();
+                                    log.error("Thread interrupted", e);
+                                }
+
                             } catch (Exception e) {
                                 log.error("Error processing chunk", e);
                                 outputStream.write(("data: Error processing chunk\n\n").getBytes(StandardCharsets.UTF_8));