|
|
|
package cn.teammodel.controller.frontend;
|
|
|
|
|
|
|
|
import cn.teammodel.common.IdRequest;
|
|
|
|
import cn.teammodel.common.R;
|
|
|
|
import cn.teammodel.model.dto.ai.*;
|
|
|
|
import cn.teammodel.model.entity.ai.ChatApp;
|
|
|
|
import cn.teammodel.model.entity.ai.ChatSession;
|
|
|
|
import cn.teammodel.service.ChatAppService;
|
|
|
|
import cn.teammodel.service.ChatMessageService;
|
|
|
|
import cn.teammodel.service.ChatSessionService;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import javax.validation.Valid;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/ai/api")
|
|
|
|
@Api(tags = "AI 能力")
|
|
|
|
public class AiController {
|
|
|
|
@Resource
|
|
|
|
private ChatSessionService chatSessionService;
|
|
|
|
@Resource
|
|
|
|
private ChatMessageService chatMessageService;
|
|
|
|
@Resource
|
|
|
|
private ChatAppService chatAppService;
|
|
|
|
|
|
|
|
@PostMapping("chat/completion")
|
|
|
|
@ApiOperation("与 spark 的流式对话")
|
|
|
|
public SseEmitter chatCompletion(@RequestBody @Valid ChatCompletionReqDto chatCompletionReqDto) {
|
|
|
|
return chatMessageService.chatCompletion(chatCompletionReqDto);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @PostMapping("chat/test/completion")
|
|
|
|
@ApiOperation("与 spark 的流式对话")
|
|
|
|
public SseEmitter testCompletion(@RequestBody @Valid ChatCompletionReqDto chatCompletionReqDto) throws IOException, InterruptedException {
|
|
|
|
SseEmitter sseEmitter = new SseEmitter();
|
|
|
|
CompletableFuture.runAsync(() -> {
|
|
|
|
try {
|
|
|
|
sseEmitter.send("曾经以为我们");
|
|
|
|
Thread.sleep(1000);
|
|
|
|
sseEmitter.send("开始了就会走到最后,可是当分手之后才明白我想的");
|
|
|
|
Thread.sleep(1000);
|
|
|
|
sseEmitter.send("你不一定能做到,当我开始去遗忘我们");
|
|
|
|
Thread.sleep(1500);
|
|
|
|
sseEmitter.send("的经历时,却不知道遗忘已变成另一种开始,");
|
|
|
|
Thread.sleep(800);
|
|
|
|
sseEmitter.send("回忆是淡了,可是痛却还在,还是最真实。放手的时候微笑着说无所谓");
|
|
|
|
Thread.sleep(800);
|
|
|
|
sseEmitter.send(",离开了你我还可以过的很好");
|
|
|
|
Thread.sleep(1000);
|
|
|
|
sseEmitter.send(",而你却不知道微笑只是用来掩盖疼痛的伤疤。");
|
|
|
|
Thread.sleep(1200);
|
|
|
|
sseEmitter.send("离开你之后的自己陷入一种无助的状态,空洞的");
|
|
|
|
Thread.sleep(1300);
|
|
|
|
sseEmitter.send("双眼回忆不起曾经的你,那时候以为与世隔绝或许才是维护自己的最好方式。");
|
|
|
|
Thread.sleep(1100);
|
|
|
|
sseEmitter.send("[DONE]");
|
|
|
|
sseEmitter.complete();
|
|
|
|
|
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return sseEmitter;
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("session/my")
|
|
|
|
@ApiOperation("查询我的聊天会话")
|
|
|
|
public R<List<ChatSession>> listMySession() {
|
|
|
|
List<ChatSession> sessions = chatSessionService.listMySession();
|
|
|
|
return R.success(sessions);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("chat/history/{sessionId}")
|
|
|
|
@ApiOperation("查询我的聊天记录")
|
|
|
|
public R<List<ChatSession.Message>> getHistory(@PathVariable String sessionId) {
|
|
|
|
List<ChatSession.Message> history = chatSessionService.listMyHistory(sessionId);
|
|
|
|
return R.success(history);
|
|
|
|
}
|
|
|
|
@PostMapping("session/create")
|
|
|
|
@ApiOperation("创建聊天会话")
|
|
|
|
public R<String> createSession() {
|
|
|
|
String sessionId = chatSessionService.createSession();
|
|
|
|
return R.success(sessionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("session/remove")
|
|
|
|
@ApiOperation("删除聊天会话")
|
|
|
|
public R<String> removeSession(@RequestBody @Valid IdRequest idRequest) {
|
|
|
|
chatSessionService.deleteSession(idRequest.getId());
|
|
|
|
return R.success("删除会话成功");
|
|
|
|
}
|
|
|
|
@PostMapping("session/update")
|
|
|
|
@ApiOperation("更新聊天会话")
|
|
|
|
public R<ChatSession> updateSession(@RequestBody @Valid UpdateSessionDto updateSessionDto) {
|
|
|
|
ChatSession session = chatSessionService.updateSession(updateSessionDto);
|
|
|
|
return R.success(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("app/list")
|
|
|
|
@ApiOperation("查询聊天应用列表")
|
|
|
|
public R<List<ChatApp>> listApp(@RequestBody @Valid SearchAppDto searchAppDto) {
|
|
|
|
List<ChatApp> chatApps = chatAppService.listApp(searchAppDto);
|
|
|
|
return R.success(chatApps);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("app/create")
|
|
|
|
@ApiOperation("创建聊天应用")
|
|
|
|
public R<ChatApp> createApp(@RequestBody @Valid CreateChatAppDto createChatAppDto) {
|
|
|
|
ChatApp chatApp = chatAppService.createApp(createChatAppDto);
|
|
|
|
return R.success(chatApp);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("app/update")
|
|
|
|
@ApiOperation("更新聊天应用")
|
|
|
|
public R<ChatApp> updateApp(@RequestBody @Valid UpdateChatAppDto updateChatAppDto) {
|
|
|
|
ChatApp chatApp = chatAppService.updateApp(updateChatAppDto);
|
|
|
|
return R.success(chatApp);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("app/remove")
|
|
|
|
@ApiOperation("删除聊天应用")
|
|
|
|
public R<String> updateApp(@RequestBody @Valid IdRequest idRequest) {
|
|
|
|
chatAppService.deleteApp(idRequest);
|
|
|
|
return R.success("删除应用成功");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|