|
|
@@ -2,6 +2,8 @@ package com.doc.chatglm3.controller;
|
|
|
|
|
|
import com.doc.chatglm3.util.SseEmitterUtf8;
|
|
|
import com.doc.common.core.controller.BaseController;
|
|
|
+import com.doc.common.core.domain.AjaxResult;
|
|
|
+import com.doc.common.core.redis.RedisCache;
|
|
|
import com.plexpt.chatgpt.ChatGPTStream;
|
|
|
import com.plexpt.chatgpt.entity.chat.ChatCompletion;
|
|
|
import com.plexpt.chatgpt.entity.chat.Message;
|
|
|
@@ -15,6 +17,7 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* 文件上传
|
|
|
@@ -29,6 +32,8 @@ import java.util.List;
|
|
|
public class ChatGlm3Controller extends BaseController {
|
|
|
@Resource
|
|
|
private Environment environment;
|
|
|
+ @Resource
|
|
|
+ private RedisCache redisCache;
|
|
|
|
|
|
@ApiOperation("是否开启")
|
|
|
@GetMapping()
|
|
|
@@ -37,11 +42,15 @@ public class ChatGlm3Controller extends BaseController {
|
|
|
}
|
|
|
|
|
|
@ApiOperation("开始聊天")
|
|
|
- @PostMapping()
|
|
|
- public SseEmitter chat(@RequestBody List<Message> messages) {
|
|
|
+ @GetMapping("/chat")
|
|
|
+ public SseEmitter chat(String uuid) {
|
|
|
+ SseEmitterUtf8 sseEmitter = new SseEmitterUtf8(-1L);
|
|
|
+ List<Message> messages = redisCache.getCacheList(uuid);
|
|
|
+ if (messages == null || messages.size() == 0) {
|
|
|
+ sseEmitter.complete();
|
|
|
+ }
|
|
|
String openaiApiHost = environment.getProperty("chat-glm3.host");
|
|
|
Integer maxToken = environment.getProperty("chat-glm3.max-token", Integer.class);
|
|
|
- SseEmitterUtf8 sseEmitter = new SseEmitterUtf8(-1L);
|
|
|
|
|
|
SseStreamListener listener = new SseStreamListener(sseEmitter);
|
|
|
ChatCompletion chatCompletion = ChatCompletion.builder()
|
|
|
@@ -68,8 +77,17 @@ public class ChatGlm3Controller extends BaseController {
|
|
|
listener.setOnComplate(msg -> {
|
|
|
//回答完成,可以做一些事情
|
|
|
sseEmitter.complete();
|
|
|
+ redisCache.deleteObject(uuid);
|
|
|
log.info(msg);
|
|
|
});
|
|
|
return sseEmitter;
|
|
|
}
|
|
|
+
|
|
|
+ @ApiOperation("开始聊天")
|
|
|
+ @PostMapping()
|
|
|
+ public AjaxResult chat(@RequestBody List<Message> messages) {
|
|
|
+ String uuid = UUID.randomUUID().toString();
|
|
|
+ redisCache.setCacheList(uuid, messages);
|
|
|
+ return AjaxResult.success(uuid);
|
|
|
+ }
|
|
|
}
|