up:教师评语

develop
PL 2 months ago
parent da7a35a3d7
commit 58ff2e7eb1

@ -4,6 +4,7 @@ import cn.teammodel.common.IdRequest;
import cn.teammodel.common.R;
import cn.teammodel.model.dto.ai.*;
import cn.teammodel.model.dto.ai.comment.ChatCommentsDto;
import cn.teammodel.model.dto.ai.comment.ChatTeacherCommentDto;
import cn.teammodel.model.entity.TmdUserDetail;
import cn.teammodel.model.entity.ai.ChatApp;
import cn.teammodel.model.entity.ai.ChatSession;
@ -149,7 +150,7 @@ public class AiController {
}
@PostMapping("chat/comments")
@ApiOperation("设置评语")
@ApiOperation("学生设置评语")
public SseEmitter chatComments(@RequestBody @Valid ChatCommentsDto chatCommentsDto) {
//String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
// 获取getClaims时为空
@ -167,4 +168,19 @@ public class AiController {
return chatMessageService.chatComments(chatCommentsDto, userId, userName);
}
/**
* ai
*/
@PostMapping("chat/teacherComment")
@ApiOperation("教师Ai评语")
public SseEmitter chatTeacherComment(@RequestBody @Valid ChatTeacherCommentDto chatTeacherCommentDto){
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getUser().getId();
String userName = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getUser().getName();
//获取会话id 看是否有sessionId 有则直接赋值 没有则赋值userId
String sessionId = StringUtils.isBlank(chatTeacherCommentDto.getSessionId()) ? userId: chatTeacherCommentDto.getSessionId();
chatTeacherCommentDto.setSessionId(sessionId);
return chatMessageService.chatTeacherComment(chatTeacherCommentDto,userId, userName);
}
}

@ -0,0 +1,37 @@
package cn.teammodel.model.dto.ai.comment;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
* @Author
* @Date 2020/7/29
*/
@Data
public class ChatTeacherCommentDto
{
/**
* id
*/
private String sessionId;
/**
* id
*/
private String appId;
/**
* txt
*/
private String type;
/**
*
*/
private String text;
}

@ -2,6 +2,7 @@ package cn.teammodel.service;
import cn.teammodel.model.dto.ai.comment.ChatCommentsDto;
import cn.teammodel.model.dto.ai.ChatCompletionReqDto;
import cn.teammodel.model.dto.ai.comment.ChatTeacherCommentDto;
import cn.teammodel.model.dto.ai.comment.WisdomSummaryDto;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@ -16,21 +17,15 @@ public interface ChatMessageService {
SseEmitter chatCompletion(ChatCompletionReqDto chatCompletionReqDto, String userId);
/**
* AI
* AI
* @param chatCommentsDto
* @return
*/
SseEmitter chatComments(ChatCommentsDto chatCommentsDto, String userId, String userName);
/**
* AI
* AI
* @return
*/
SseEmitter chatSummary(WisdomSummaryDto chatSummaryDto, String userId, String userName);
/**
* AI
* @return
*/
SseEmitter chatAnalysis();
SseEmitter chatTeacherComment(ChatTeacherCommentDto chatTeacherCommentDto, String userId, String userName);
}

@ -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 {
}
}
}

Loading…
Cancel
Save