mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-06 16:27:32 +00:00
feat: 会话管理
This commit is contained in:
@@ -46,6 +46,7 @@ public class ChatCostServiceImpl implements IChatCostService {
|
||||
/**
|
||||
* 扣除用户余额
|
||||
*/
|
||||
@Override
|
||||
public void deductToken(ChatRequest chatRequest) {
|
||||
|
||||
int tokens = TikTokensUtil.tokens(chatRequest.getModel(), chatRequest.getPrompt());
|
||||
@@ -53,6 +54,7 @@ public class ChatCostServiceImpl implements IChatCostService {
|
||||
String modelName = chatRequest.getModel();
|
||||
|
||||
ChatMessageBo chatMessageBo = new ChatMessageBo();
|
||||
chatMessageBo.setSessionId(chatRequest.getSessionId());
|
||||
|
||||
Object userId = LocalCache.CACHE.get("userId");
|
||||
if(userId!=null){
|
||||
|
||||
@@ -24,9 +24,12 @@ import org.ruoyi.common.core.utils.StringUtils;
|
||||
import org.ruoyi.common.core.utils.file.FileUtils;
|
||||
import org.ruoyi.common.core.utils.file.MimeTypeUtils;
|
||||
import org.ruoyi.common.redis.utils.RedisUtils;
|
||||
import org.ruoyi.domain.ChatSession;
|
||||
import org.ruoyi.domain.bo.ChatSessionBo;
|
||||
import org.ruoyi.domain.vo.ChatModelVo;
|
||||
import org.ruoyi.service.EmbeddingService;
|
||||
import org.ruoyi.service.IChatModelService;
|
||||
import org.ruoyi.service.IChatSessionService;
|
||||
import org.ruoyi.service.VectorStoreService;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -65,6 +68,8 @@ public class SseServiceImpl implements ISseService {
|
||||
|
||||
private final OllamaServiceImpl ollamaService;
|
||||
|
||||
private final IChatSessionService chatSessionService;
|
||||
|
||||
private ChatModelVo chatModelVo;
|
||||
|
||||
|
||||
@@ -80,6 +85,15 @@ public class SseServiceImpl implements ISseService {
|
||||
}else {
|
||||
LocalCache.CACHE.put("userId", chatCostService.getUserId());
|
||||
chatRequest.setUserId(chatCostService.getUserId());
|
||||
// 保存会话信息
|
||||
if(chatRequest.getSessionId()==null){
|
||||
ChatSessionBo chatSessionBo = new ChatSessionBo();
|
||||
chatSessionBo.setUserId(chatCostService.getUserId());
|
||||
chatSessionBo.setSessionTitle(getFirst10Characters(chatRequest.getPrompt()));
|
||||
chatSessionBo.setSessionContent(chatRequest.getPrompt());
|
||||
chatSessionService.insertByBo(chatSessionBo);
|
||||
chatRequest.setSessionId(chatSessionBo.getId());
|
||||
}
|
||||
// 保存消息记录 并扣除费用
|
||||
chatCostService.deductToken(chatRequest);
|
||||
}
|
||||
@@ -92,6 +106,23 @@ public class SseServiceImpl implements ISseService {
|
||||
return sseEmitter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取对话标题
|
||||
*
|
||||
* @param str 原字符
|
||||
* @return 截取后的字符
|
||||
*/
|
||||
public static String getFirst10Characters(String str) {
|
||||
// 判断字符串长度
|
||||
if (str.length() > 10) {
|
||||
// 如果长度大于10,截取前10个字符
|
||||
return str.substring(0, 10);
|
||||
} else {
|
||||
// 如果长度不足10,返回整个字符串
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查未登录用户是否超过当日对话次数限制
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user