流式
开放中
流式
@RequestMapping(value = "/test", method = RequestMethod.GET) public ResponseBodyEmitter test(HttpServletResponse response) throws JsonProcessingException { response.setContentType("text/event-stream"); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-cache");
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
// 1. 创建参数 ChatCompletionRequest chatCompletion = ChatCompletionRequest .builder() .stream(true) .messages(Collections.singletonList(Message.builder().role(Constants.Role.USER).content("java 冒泡排序").build())) .model(ChatCompletionRequest.Model.GPT_3_5_TURBO.getCode()) .maxTokens(1024) .build();
try {
EventSource eventSource = chatGPTOpenAiSession.chatCompletions(chatCompletion, new EventSourceListener() {
@Override
public void onEvent(EventSource eventSource, String id, String type, String data) {
if ("[DONE]".equalsIgnoreCase(data)) {
return;
}
log.info("测试结果 id:{} type:{} data:{}", id, type, data);
try {
emitter.send(data);
Thread.sleep(500);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (StringUtils.isNoneBlank(data) && "stop".equals(data)) {
return;
}
}
@Override
public void onClosed(@NotNull EventSource eventSource) {
emitter.complete();
}
@Override
public void onFailure(EventSource eventSource, Throwable t, Response response) {
log.error("失败 code:{} message:{}", response.code(), response.message());
}
});
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
// 2. 发起请求
String[] words = new String[]{"嗨,臭宝。\r\n", "恭喜💐 ", "你的", " Jenkins ", "部", "署", "测", "试", "成", "功", "了啦🌶!", "\r\nBy kancsd https://bugstack.cn"};
new Thread(() -> {
for (String word : words) {
try {
emitter.send(word);
Thread.sleep(250);
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
return emitter;
}
请注册或登录再回复