|
|
|
@ -75,6 +75,7 @@ public class ChatMessageServiceImpl implements ChatMessageService {
|
|
|
|
|
try {
|
|
|
|
|
// 目前仅使用讯飞星火大模型
|
|
|
|
|
String appId = chatCommentsDto.getAppId();
|
|
|
|
|
|
|
|
|
|
// 获取模板文本
|
|
|
|
|
String text = commentsTemplate(chatCommentsDto);
|
|
|
|
|
if (!StringUtils.isEmpty(text)) {
|
|
|
|
@ -103,27 +104,38 @@ public class ChatMessageServiceImpl implements ChatMessageService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AI 总结 暂弃用
|
|
|
|
|
* 评语调用 聊天模型
|
|
|
|
|
* @param userId
|
|
|
|
|
* @param userName
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public SseEmitter chatSummary(WisdomSummaryDto chatSummaryDto, String userId, String userName)
|
|
|
|
|
public SseEmitter chatTeacherComment(ChatTeacherCommentDto chatTeacherCommentDto,String userId, String userName)
|
|
|
|
|
{
|
|
|
|
|
// 目前仅使用讯飞星火大模型
|
|
|
|
|
String appId = chatTeacherCommentDto.getAppId();
|
|
|
|
|
// 获取模板文本
|
|
|
|
|
//String text = commentsTemplate(chatCommentsDto);
|
|
|
|
|
return null;
|
|
|
|
|
String text = chatTeacherCommentDto.getText();
|
|
|
|
|
if (!StringUtils.isEmpty(text)) {
|
|
|
|
|
//chatCommentsDto.setText(text);
|
|
|
|
|
} else {
|
|
|
|
|
log.info("参数错误");
|
|
|
|
|
throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "参数错误");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AI 分析 暂弃用
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public SseEmitter chatAnalysis() {
|
|
|
|
|
return null;
|
|
|
|
|
SseEmitter sseEmitter;
|
|
|
|
|
if (StringUtils.isEmpty(appId)) {
|
|
|
|
|
sseEmitter = teacherCommentBySession(chatTeacherCommentDto, userId, userName);
|
|
|
|
|
} else {
|
|
|
|
|
ChatCompletionReqDto chatCompletionReqDto = new ChatCompletionReqDto();
|
|
|
|
|
chatCompletionReqDto.setAppId(chatTeacherCommentDto.getAppId());
|
|
|
|
|
chatCompletionReqDto.setSessionId(chatTeacherCommentDto.getSessionId());
|
|
|
|
|
chatCompletionReqDto.setText(chatTeacherCommentDto.getText());
|
|
|
|
|
sseEmitter = completionByApp(chatCompletionReqDto, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sseEmitter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 面具模式(暂时不存储聊天记录)
|
|
|
|
@ -231,7 +243,7 @@ public class ChatMessageServiceImpl implements ChatMessageService {
|
|
|
|
|
*/
|
|
|
|
|
private SseEmitter commentsBySession(ChatCommentsDto chatCommentsDto, String userId, String userName) {
|
|
|
|
|
String userPrompt = chatCommentsDto.getText();
|
|
|
|
|
Object data = chatCommentsDto.getData();
|
|
|
|
|
//Object data = chatCommentsDto.getData();
|
|
|
|
|
//获取会话id 看是否有sessionId 有则直接赋值 没有则赋值userId
|
|
|
|
|
String sessionId = chatCommentsDto.getSessionId();
|
|
|
|
|
ChatSession session = null;
|
|
|
|
@ -244,7 +256,7 @@ public class ChatMessageServiceImpl implements ChatMessageService {
|
|
|
|
|
session = new ChatSession();
|
|
|
|
|
session.setId(sessionId);
|
|
|
|
|
session.setCode(PK.CHAT_SESSION);
|
|
|
|
|
session.setTitle("评语");
|
|
|
|
|
session.setTitle("学生综合素质评价系统-评语");
|
|
|
|
|
session.setUserId(userId);
|
|
|
|
|
session.setCreateTime(Instant.now().toEpochMilli());
|
|
|
|
|
session.setUpdateTime(Instant.now().toEpochMilli());
|
|
|
|
@ -254,11 +266,71 @@ public class ChatMessageServiceImpl implements ChatMessageService {
|
|
|
|
|
session = RepositoryUtil.findOne(chatSessionRepository.findBySessionId(sessionId), "该会话不存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ChatSession session = RepositoryUtil.findOne(chatSessionRepository.findBySessionId(sessionId), "该会话不存在");
|
|
|
|
|
if (!session.getUserId().equals(userId)) {
|
|
|
|
|
throw new ServiceException(ErrorCode.NO_AUTH_ERROR.getCode(), "该会话不存在");
|
|
|
|
|
}*/
|
|
|
|
|
SseEmitter sseEmitter = new SseEmitter(-1L);
|
|
|
|
|
SparkGptStreamListener listener = new SparkGptStreamListener(sseEmitter);
|
|
|
|
|
// open 回调
|
|
|
|
|
listener.setOnOpen((s) -> {
|
|
|
|
|
// 敏感词检查,计费 (设计模型, reducePoints, 或者都可以在完成的回调中做?)
|
|
|
|
|
log.info("callback: ws open event emmit");
|
|
|
|
|
});
|
|
|
|
|
// 对话完成的回调
|
|
|
|
|
listener.setOnComplete((s) -> {
|
|
|
|
|
log.info("callback: ws complete event emmit");
|
|
|
|
|
SseHelper.send(sseEmitter, "[DONE]");
|
|
|
|
|
// 处理完成后的事件: 保存消息记录, 缓存更改
|
|
|
|
|
ChatSession.Message message = ChatSession.Message.of(userPrompt, s);
|
|
|
|
|
HistoryCache.updateContext(sessionId, message);
|
|
|
|
|
CosmosPatchOperations options = CosmosPatchOperations.create()
|
|
|
|
|
.replace("/updateTime", Instant.now().toEpochMilli())
|
|
|
|
|
.add("/history/-", message);
|
|
|
|
|
chatSessionRepository.save(sessionId, PK.of(PK.CHAT_SESSION), ChatSession.class, options);
|
|
|
|
|
});
|
|
|
|
|
// 错误的回调
|
|
|
|
|
listener.setOnError((s) -> {
|
|
|
|
|
log.error("callback: ws error, info: " + s);
|
|
|
|
|
// 返还积分
|
|
|
|
|
});
|
|
|
|
|
List<SparkChatRequestParam.Message> messageList = fetchContext(userId, userPrompt);
|
|
|
|
|
SparkChatRequestParam requestParam = SparkChatRequestParam
|
|
|
|
|
.builder()
|
|
|
|
|
.uid(userId)
|
|
|
|
|
.chatId(userId)
|
|
|
|
|
.messageList(messageList)
|
|
|
|
|
.build();
|
|
|
|
|
sparkGptClient.streamChatCompletion(requestParam, listener);
|
|
|
|
|
return sseEmitter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 教师评语 会话模式
|
|
|
|
|
* @param chatTeacherCommentDto
|
|
|
|
|
* @param userId
|
|
|
|
|
* @param userName
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private SseEmitter teacherCommentBySession(ChatTeacherCommentDto chatTeacherCommentDto, String userId, String userName) {
|
|
|
|
|
String userPrompt = chatTeacherCommentDto.getText();
|
|
|
|
|
//获取会话id 看是否有sessionId 有则直接赋值 没有则赋值userId
|
|
|
|
|
String sessionId = chatTeacherCommentDto.getSessionId();
|
|
|
|
|
ChatSession session = null;
|
|
|
|
|
List<ChatSession> sessions = chatSessionRepository.findCommentsById(sessionId);
|
|
|
|
|
if (sessions.size() == 0) {
|
|
|
|
|
// 初始化欢迎语
|
|
|
|
|
ChatSession.Message message = ChatSession.Message.of("", "你好" + userName + " ,我是你的私人 AI 助手小豆," +
|
|
|
|
|
"你可以问我任何包括但不仅限于教育的问题,我会尽力为您解答!");
|
|
|
|
|
List<ChatSession.Message> history = Collections.singletonList(message);
|
|
|
|
|
session = new ChatSession();
|
|
|
|
|
session.setId(sessionId);
|
|
|
|
|
session.setCode(PK.CHAT_SESSION);
|
|
|
|
|
session.setTitle("教师专业素质评价系统-评语");
|
|
|
|
|
session.setUserId(userId);
|
|
|
|
|
session.setCreateTime(Instant.now().toEpochMilli());
|
|
|
|
|
session.setUpdateTime(Instant.now().toEpochMilli());
|
|
|
|
|
session.setHistory(history);
|
|
|
|
|
chatSessionRepository.save(session);
|
|
|
|
|
} else {
|
|
|
|
|
session = RepositoryUtil.findOne(chatSessionRepository.findBySessionId(sessionId), "该会话不存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SseEmitter sseEmitter = new SseEmitter(-1L);
|
|
|
|
|
SparkGptStreamListener listener = new SparkGptStreamListener(sseEmitter);
|
|
|
|
@ -758,6 +830,4 @@ public class ChatMessageServiceImpl implements ChatMessageService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|