Commit 18044d65 by zhangxingmin

push

parent 9574bb8d
...@@ -28,12 +28,17 @@ import java.util.Arrays; ...@@ -28,12 +28,17 @@ import java.util.Arrays;
@Service @Service
public class ApiAiStreamServiceImpl implements ApiAiStreamService { public class ApiAiStreamServiceImpl implements ApiAiStreamService {
public static final String RESET = "\u001B[0m";
public static final String CYAN = "\u001B[36m";
public static final String GREEN = "\u001B[32m";
@Autowired @Autowired
private ApiSensitiveWordDetailService apiSensitiveWordDetailService; private ApiSensitiveWordDetailService apiSensitiveWordDetailService;
@Autowired @Autowired
private ApiNotificationTaskFeignClient apiNotificationTaskFeignClient; private ApiNotificationTaskFeignClient apiNotificationTaskFeignClient;
/** /**
* 流式对话(SSE),保留敏感词检测逻辑 * 流式对话(SSE),保留敏感词检测逻辑
*/ */
...@@ -98,6 +103,9 @@ public class ApiAiStreamServiceImpl implements ApiAiStreamService { ...@@ -98,6 +103,9 @@ public class ApiAiStreamServiceImpl implements ApiAiStreamService {
flowable.subscribe( flowable.subscribe(
result -> { result -> {
String delta = result.getOutput().getChoices().get(0).getMessage().getContent(); String delta = result.getOutput().getChoices().get(0).getMessage().getContent();
// 实时着色并打印
printWithStyle(delta);
System.out.flush();
sink.next(delta); sink.next(delta);
}, },
error -> { error -> {
...@@ -108,4 +116,14 @@ public class ApiAiStreamServiceImpl implements ApiAiStreamService { ...@@ -108,4 +116,14 @@ public class ApiAiStreamServiceImpl implements ApiAiStreamService {
); );
}); });
} }
private static void printWithStyle(String text) {
for (char ch : text.toCharArray()) {
if (ch == '|' || ch == '-' || ch == '=') {
System.out.print(CYAN + ch + RESET);
} else {
System.out.print(GREEN + ch + RESET);
}
}
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment