Merge branch '11111' into develop

develop
CrazyIter_Bin 4 months ago
commit fa2bad9f86

@ -0,0 +1,35 @@
package cn.teammodel.controller.admin.controller;
import cn.teammodel.common.R;
import cn.teammodel.controller.admin.service.ArtService;
import cn.teammodel.controller.admin.service.CommonService;
import cn.teammodel.model.dto.admin.art.ArtFindDto;
import cn.teammodel.model.dto.admin.common.GCDto;
import cn.teammodel.model.vo.admin.ArtElementsVo;
import cn.teammodel.model.vo.admin.GradeAndClassVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("admin/common")
@Api(tags = "管理员端-公共组件")
public class CommonController {
@Resource
private CommonService commonService ;
@PostMapping("getGradeAndClass")
@ApiOperation("获取当前学校当前学段年级班级信息")
public R<List<GradeAndClassVo>> findRecords(@Valid @RequestBody GCDto gcDto) {
List<GradeAndClassVo> res = commonService.getGradeAndClass(gcDto);
return R.success(res);
}
}

@ -0,0 +1,10 @@
package cn.teammodel.controller.admin.service;
import cn.teammodel.model.dto.admin.common.GCDto;
import cn.teammodel.model.vo.admin.GradeAndClassVo;
import org.springframework.stereotype.Service;
import java.util.List;
public interface CommonService {
List<GradeAndClassVo> getGradeAndClass(GCDto gcDto);
}

@ -0,0 +1,83 @@
package cn.teammodel.controller.admin.service.impl;
import cn.teammodel.controller.admin.service.CommonService;
import cn.teammodel.model.dto.admin.common.GCDto;
import cn.teammodel.model.entity.school.ClassInfo;
import cn.teammodel.model.entity.school.School;
import cn.teammodel.model.vo.admin.GradeAndClassVo;
import cn.teammodel.repository.ClassRepository;
import cn.teammodel.repository.SchoolRepository;
import cn.teammodel.utils.MonthToNumberConverter;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import static java.time.LocalDate.now;
@Service
public class CommonServiceImpl implements CommonService {
@Resource
private SchoolRepository schoolRepository;
@Resource
private ClassRepository classRepository;
@Override
public List<GradeAndClassVo> getGradeAndClass(GCDto gcDto) {
List<GradeAndClassVo> gradeAndClassVos = new ArrayList<>();
try {
//获取当前学校该学段下详细信息
List<School.Period> period = schoolRepository.findPeriodById(gcDto.getSchoolId(), gcDto.getPeriodId());
List<ClassInfo> classes = classRepository.findClassBySchoolIdAndPeriodId(gcDto.getSchoolId(), gcDto.getPeriodId());
int year = now().getYear();
Month month = now().getMonth();
int mon = MonthToNumberConverter.convertMonthToNumber(month.name());
int day = now().getDayOfMonth();
//处理年级ID
for (ClassInfo classInfo : classes) {
if(period.get(0).getId().equalsIgnoreCase(classInfo.getPeriodId())) {
for (School.Semester semester : period.get(0).getSemesters()) {
int time = 0;
if(semester.getStart() == 1) {
if (mon == semester.getMonth())
{
time = day >= semester.getDay() ? 0 : 1;
}
else
{
time = mon > semester.getMonth() ? 0 : 1;
}
int eYear = year - time;
classInfo.setGrade(eYear- classInfo.getYear());
}
}
}
}
List<String> grades = period.get(0).getGrades();
int index = 0;
for (String grade : grades) {
GradeAndClassVo gradeAndClassVo = new GradeAndClassVo();
gradeAndClassVo.setGradeId(index);
gradeAndClassVo.setGradeName(grade);
classes.stream().filter(classInfo -> classInfo.getGrade() == gradeAndClassVo.getGradeId()).forEach(classInfo -> {
GradeAndClassVo.CI ci = new GradeAndClassVo.CI();
ci.setClassId(classInfo.getId());
ci.setClassName(classInfo.getName());
gradeAndClassVo.AddClass(ci);
});
index ++;
gradeAndClassVos.add(gradeAndClassVo);
}
return gradeAndClassVos;
}catch (Exception e) {
throw new RuntimeException("获取当前学校当前学段年级班级信息失败");
}
}
}

@ -10,9 +10,11 @@ import cn.teammodel.security.utils.SecurityUtil;
import cn.teammodel.service.ChatAppService;
import cn.teammodel.service.ChatMessageService;
import cn.teammodel.service.ChatSessionService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@ -147,9 +149,22 @@ public class AiController {
return R.success("删除应用成功");
}
@PostMapping("chat/comments")
@ApiOperation("设置评语")
public SseEmitter chatComments(@RequestBody @Valid ChatCompletionReqDto chatCompletionReqDto) {
/*
Authentication user0 = SecurityUtil.getAuthentication();
Object user01 = SecurityUtil.getAuthentication().getPrincipal();
TmdUserDetail user02 = (TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal();
Claims user03 = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims();
String user04 = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
*/
//String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
// 获取getClaims时为空
String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getUser().getId();
String userName = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getUser().getName();
return chatMessageService.chatComments(chatCompletionReqDto, userId, userName);
}
}

@ -0,0 +1,9 @@
package cn.teammodel.model.dto.admin.common;
import lombok.Data;
@Data
public class GCDto {
private String schoolId;
private String periodId;
}

@ -19,6 +19,7 @@ public class ClassInfo extends BaseItem {
private String room;
private String school;
private Integer graduate;
private Integer grade;
private String pk;
private Integer ttl;

@ -0,0 +1,23 @@
package cn.teammodel.model.vo.admin;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class GradeAndClassVo {
private int gradeId;
private String gradeName;
private List<CI> classes = new ArrayList<>();
public void AddClass(CI ci) {
this.classes.add(ci);
}
@Data
public static class CI {
private String classId;
private String className;
}
}

@ -22,4 +22,7 @@ public interface ChatSessionRepository extends CosmosRepository<ChatSession, Str
@Query("SELECT value ARRAY_SLICE(c.history, -3) FROM c where c.id = @sessionId and c.code = 'ChatSession'")
List<ChatSession.Message> findLatestMessage(String sessionId);
@Query("select c.id, c.code, c.title, c.userId, c.createTime from c where c.code = 'ChatSession' and c.id = @userId")
List<ChatSession> findCommentsById(String userId);
}

@ -18,4 +18,6 @@ public interface ClassRepository extends CosmosRepository<ClassInfo, String> {
@Query("select c.id, c.name from School as c where c.code = @code and c.id in (@ids)")
List<ClassInfo> findAllByCodeAndIdIn(String code, Collection<String> ids);
@Query("select c.id, c.name,c.year,c.periodId from School as c where c.school = @school and c.periodId = @periodId and c.pk = 'Class'")
List<ClassInfo> findClassBySchoolIdAndPeriodId(String school, String periodId);
}

@ -12,4 +12,11 @@ public interface ChatMessageService {
* AI
*/
SseEmitter chatCompletion(ChatCompletionReqDto chatCompletionReqDto, String userId);
/**
* AI
* @param chatCompletionReqDto
* @return
*/
SseEmitter chatComments(ChatCompletionReqDto chatCompletionReqDto, String userId,String userName);
}

@ -1,5 +1,6 @@
package cn.teammodel.service.impl;
import cn.hutool.core.lang.UUID;
import cn.teammodel.ai.SparkGptClient;
import cn.teammodel.ai.SseHelper;
import cn.teammodel.ai.cache.HistoryCache;
@ -27,6 +28,7 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@ -57,6 +59,25 @@ public class ChatMessageServiceImpl implements ChatMessageService {
return sseEmitter;
}
/**
*
* @param chatCompletionReqDto
* @param userId
* @return
*/
@Override
public SseEmitter chatComments(ChatCompletionReqDto chatCompletionReqDto, String userId,String userName) {
// 目前仅使用讯飞星火大模型
String appId = chatCompletionReqDto.getAppId();
SseEmitter sseEmitter;
if (StringUtils.isEmpty(appId)) {
sseEmitter = commentsBySession(chatCompletionReqDto, userId,userName);
} else {
sseEmitter = completionByApp(chatCompletionReqDto, false);
}
return sseEmitter;
}
/**
* ()
*/
@ -158,6 +179,73 @@ public class ChatMessageServiceImpl implements ChatMessageService {
return sseEmitter;
}
/**
*
*/
private SseEmitter commentsBySession(ChatCompletionReqDto chatCompletionReqDto, String userId,String userName) {
String userPrompt = chatCompletionReqDto.getText();
String sessionId = chatCompletionReqDto.getSessionId();
ChatSession session = null;
List<ChatSession> sessions = chatSessionRepository.findCommentsById(userId);
if (sessions.size() == 0) {
// 初始化欢迎语
ChatSession.Message message = ChatSession.Message.of("", "你好" + userName + " ,我是你的私人 AI 助手小豆,你可以问我任何包括但不仅限于教育的问题,我会尽力为您解答!");
List<ChatSession.Message> history = Collections.singletonList(message);
session = new ChatSession();
session.setId(userId);
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(userId), "该会话不存在");
}
/*
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(sessionId, userPrompt);
SparkChatRequestParam requestParam = SparkChatRequestParam
.builder()
.uid(userId)
.chatId(sessionId)
.messageList(messageList)
.build();
sparkGptClient.streamChatCompletion(requestParam, listener);
return sseEmitter;
}
List<SparkChatRequestParam.Message> fetchContext(String sessionId, String prompt) {
List<ChatSession.Message> context = HistoryCache.getContext(sessionId);
List<SparkChatRequestParam.Message> paramMessages = new ArrayList<>();

@ -0,0 +1,36 @@
package cn.teammodel.utils;
import java.util.HashMap;
import java.util.Map;
public class MonthToNumberConverter {
private static final Map<String, Integer> monthMap = createMonthMap();
private static Map<String, Integer> createMonthMap() {
Map<String, Integer> map = new HashMap<>();
map.put("JANUARY", 1);
map.put("FEBRUARY", 2);
map.put("MARCH", 3);
map.put("APRIL", 4);
map.put("MAY", 5);
map.put("JUNE", 6);
map.put("JULY", 7);
map.put("AUGUST", 8);
map.put("SEPTEMBER", 9);
map.put("OCTOBER", 10);
map.put("NOVEMBER", 11);
map.put("DECEMBER", 12);
return map;
}
public static int convertMonthToNumber(String monthName) {
return monthMap.getOrDefault(monthName, -1); // 如果找不到对应月份,则返回 -1
}
/* public static void main(String[] args) {
String month = "August";
int monthNumber = convertMonthToNumber(month);
System.out.println("The number for " + month + " is: " + monthNumber);
}*/
}
Loading…
Cancel
Save