mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-14 06:03:42 +08:00
fix:ChatAI优化
This commit is contained in:
@@ -29,13 +29,21 @@ public abstract class AbstractChatAIHandler {
|
||||
protected UserService userService;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
ChatAIHandlerFactory.register(getChatAIUserId(), getChatAIName(), this);
|
||||
protected void init() {
|
||||
if (isUse()) {
|
||||
ChatAIHandlerFactory.register(getChatAIUserId(), this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否启用
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected abstract boolean isUse();
|
||||
|
||||
// 获取机器人id
|
||||
public abstract Long getChatAIUserId();
|
||||
// 获取机器人名称
|
||||
public abstract String getChatAIName();
|
||||
|
||||
public void chat(Message message) {
|
||||
if (!supports(message)) {
|
||||
@@ -44,7 +52,7 @@ public abstract class AbstractChatAIHandler {
|
||||
threadPoolTaskExecutor.execute(() -> {
|
||||
String text = doChat(message);
|
||||
if (StringUtils.isNotBlank(text)) {
|
||||
answerMsg(text, message.getRoomId(), message.getFromUid());
|
||||
answerMsg(text, message);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -66,30 +74,34 @@ public abstract class AbstractChatAIHandler {
|
||||
protected abstract String doChat(Message message);
|
||||
|
||||
|
||||
protected void answerMsg(String text, Long roomId, Long uid) {
|
||||
UserInfoResp userInfo = userService.getUserInfo(uid);
|
||||
protected void answerMsg(String text, Message replyMessage) {
|
||||
UserInfoResp userInfo = userService.getUserInfo(replyMessage.getFromUid());
|
||||
text = "@" + userInfo.getName() + " " + text;
|
||||
if (text.length() < 450) {
|
||||
save(text, roomId, uid);
|
||||
}else {
|
||||
int maxLen = 450;
|
||||
if (text.length() < 800) {
|
||||
save(text, replyMessage);
|
||||
} else {
|
||||
int maxLen = 800;
|
||||
int len = text.length();
|
||||
int count = (len + maxLen - 1) / maxLen;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
int start = i * maxLen;
|
||||
int end = Math.min(start + maxLen, len);
|
||||
save(text.substring(start, end), roomId, uid);
|
||||
save(text.substring(start, end), replyMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void save(String text, Long roomId, Long uid) {
|
||||
private void save(String text, Message replyMessage) {
|
||||
Long roomId = replyMessage.getRoomId();
|
||||
Long uid = replyMessage.getFromUid();
|
||||
Long id = replyMessage.getId();
|
||||
ChatMessageReq answerReq = new ChatMessageReq();
|
||||
answerReq.setRoomId(roomId);
|
||||
answerReq.setMsgType(MessageTypeEnum.TEXT.getType());
|
||||
TextMsgReq textMsgReq = new TextMsgReq();
|
||||
textMsgReq.setContent(text);
|
||||
textMsgReq.setReplyMsgId(replyMessage.getId());
|
||||
textMsgReq.setAtUidList(Collections.singletonList(uid));
|
||||
answerReq.setBody(textMsgReq);
|
||||
chatService.sendMsg(answerReq, getChatAIUserId());
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.abin.mallchat.custom.chatai.handler;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -9,11 +8,9 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class ChatAIHandlerFactory {
|
||||
private static final Map<Long, AbstractChatAIHandler> CHATAI_ID_MAP = new ConcurrentHashMap<>();
|
||||
private static final Map<String, AbstractChatAIHandler> CHATAI_NAME_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
public static void register(Long aIUserId, String name, AbstractChatAIHandler chatAIHandler) {
|
||||
public static void register(Long aIUserId, AbstractChatAIHandler chatAIHandler) {
|
||||
CHATAI_ID_MAP.put(aIUserId, chatAIHandler);
|
||||
CHATAI_NAME_MAP.put(name, chatAIHandler);
|
||||
}
|
||||
|
||||
public static AbstractChatAIHandler getChatAIHandlerById(List<Long> userIds) {
|
||||
@@ -28,15 +25,4 @@ public class ChatAIHandlerFactory {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static AbstractChatAIHandler getChatAIHandlerByName(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return null;
|
||||
}
|
||||
for (Map.Entry<String, AbstractChatAIHandler> entry : CHATAI_NAME_MAP.entrySet()) {
|
||||
if (text.contains("@"+entry.getKey())) {
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.abin.mallchat.common.common.constant.RedisKey;
|
||||
import com.abin.mallchat.common.common.utils.RedisUtils;
|
||||
import com.abin.mallchat.custom.chatai.properties.ChatGLM2Properties;
|
||||
import com.abin.mallchat.custom.chatai.utils.ChatGLM2Utils;
|
||||
import com.abin.mallchat.custom.user.domain.vo.response.user.UserInfoResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -36,27 +37,42 @@ public class ChatGLM2Handler extends AbstractChatAIHandler {
|
||||
|
||||
private static final Random RANDOM = new Random();
|
||||
|
||||
private static String AI_NAME;
|
||||
|
||||
@Autowired
|
||||
private ChatGLM2Properties glm2Properties;
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
super.init();
|
||||
if (isUse()) {
|
||||
UserInfoResp userInfo = userService.getUserInfo(glm2Properties.getAIUserId());
|
||||
if (userInfo == null) {
|
||||
log.error("根据AIUserId:{} 找不到用户信息", glm2Properties.getAIUserId());
|
||||
throw new RuntimeException("根据AIUserId找不到用户信息");
|
||||
}
|
||||
if (StringUtils.isBlank(userInfo.getName())) {
|
||||
log.warn("根据AIUserId:{} 找到的用户信息没有name", glm2Properties.getAIUserId());
|
||||
throw new RuntimeException("根据AIUserId: " + glm2Properties.getAIUserId() + " 找到的用户没有名字");
|
||||
}
|
||||
AI_NAME = userInfo.getName();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isUse() {
|
||||
return glm2Properties.isUse();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getChatAIUserId() {
|
||||
return glm2Properties.getAIUserId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getChatAIName() {
|
||||
if (StringUtils.isNotBlank(glm2Properties.getAIUserName())) {
|
||||
return glm2Properties.getAIUserName();
|
||||
}
|
||||
String name = userService.getUserInfo(glm2Properties.getAIUserId()).getName();
|
||||
glm2Properties.setAIUserName(name);
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doChat(Message message) {
|
||||
String content = message.getContent().replace("@" +glm2Properties.getAIUserName(), "").trim();
|
||||
String content = message.getContent().replace("@" + AI_NAME, "").trim();
|
||||
Long uid = message.getFromUid();
|
||||
Long minute;
|
||||
String text;
|
||||
@@ -73,7 +89,7 @@ public class ChatGLM2Handler extends AbstractChatAIHandler {
|
||||
.send();
|
||||
text = ChatGLM2Utils.parseText(response);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.warn("glm2 doChat warn:", e);
|
||||
return getErrorText();
|
||||
}
|
||||
if (StringUtils.isNotBlank(text)) {
|
||||
@@ -132,7 +148,7 @@ public class ChatGLM2Handler extends AbstractChatAIHandler {
|
||||
if (StringUtils.isBlank(message.getContent())) {
|
||||
return false;
|
||||
}
|
||||
return StringUtils.contains(message.getContent(), "@" + glm2Properties.getAIUserName())
|
||||
&& StringUtils.isNotBlank(message.getContent().replace(glm2Properties.getAIUserName(), "").trim());
|
||||
return StringUtils.contains(message.getContent(), "@" + AI_NAME)
|
||||
&& StringUtils.isNotBlank(message.getContent().replace(AI_NAME, "").trim());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,41 +2,59 @@ package com.abin.mallchat.custom.chatai.handler;
|
||||
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||
import com.abin.mallchat.common.chat.domain.entity.msg.MessageExtra;
|
||||
import com.abin.mallchat.common.common.constant.RedisKey;
|
||||
import com.abin.mallchat.common.common.utils.DateUtils;
|
||||
import com.abin.mallchat.common.common.utils.RedisUtils;
|
||||
import com.abin.mallchat.custom.chatai.properties.ChatGPTProperties;
|
||||
import com.abin.mallchat.custom.chatai.utils.ChatGPTUtils;
|
||||
import com.abin.mallchat.custom.user.domain.vo.response.user.UserInfoResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class GPTChatAIHandler extends AbstractChatAIHandler {
|
||||
|
||||
@Autowired
|
||||
private ChatGPTProperties chatGPTProperties;
|
||||
|
||||
private static String AI_NAME;
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
super.init();
|
||||
UserInfoResp userInfo = userService.getUserInfo(chatGPTProperties.getAIUserId());
|
||||
if (userInfo == null) {
|
||||
log.error("根据AIUserId:{} 找不到用户信息", chatGPTProperties.getAIUserId());
|
||||
throw new RuntimeException("根据AIUserId: " + chatGPTProperties.getAIUserId() + " 找不到用户信息");
|
||||
}
|
||||
if (StringUtils.isBlank(userInfo.getName())) {
|
||||
log.warn("根据AIUserId:{} 找到的用户信息没有name", chatGPTProperties.getAIUserId());
|
||||
throw new RuntimeException("根据AIUserId: " + chatGPTProperties.getAIUserId() + " 找到的用户没有名字");
|
||||
}
|
||||
AI_NAME = userInfo.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isUse() {
|
||||
return chatGPTProperties.isUse();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getChatAIUserId() {
|
||||
return chatGPTProperties.getAIUserId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getChatAIName() {
|
||||
if (StringUtils.isNotBlank(chatGPTProperties.getAIUserName())) {
|
||||
return chatGPTProperties.getAIUserName();
|
||||
}
|
||||
String name = userService.getUserInfo(chatGPTProperties.getAIUserId()).getName();
|
||||
chatGPTProperties.setAIUserName(name);
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doChat(Message message) {
|
||||
String content = message.getContent().replace("@" + chatGPTProperties.getAIUserName(), "").trim();
|
||||
String content = message.getContent().replace("@" + AI_NAME, "").trim();
|
||||
Long uid = message.getFromUid();
|
||||
Long chatNum;
|
||||
String text;
|
||||
@@ -48,12 +66,13 @@ public class GPTChatAIHandler extends AbstractChatAIHandler {
|
||||
response = ChatGPTUtils.create(chatGPTProperties.getKey())
|
||||
.proxyUrl(chatGPTProperties.getProxyUrl())
|
||||
.model(chatGPTProperties.getModelName())
|
||||
.timeout(chatGPTProperties.getTimeout())
|
||||
.prompt(content)
|
||||
.send();
|
||||
text = ChatGPTUtils.parseText(response);
|
||||
userChatNumInrc(uid);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.warn("gpt doChat warn:", e);
|
||||
text = "我累了,明天再聊吧";
|
||||
}
|
||||
}
|
||||
@@ -78,21 +97,21 @@ public class GPTChatAIHandler extends AbstractChatAIHandler {
|
||||
}
|
||||
/* 前端传@信息后取消注释 */
|
||||
|
||||
// MessageExtra extra = message.getExtra();
|
||||
// if (extra == null) {
|
||||
// return false;
|
||||
// }
|
||||
// if (CollectionUtils.isEmpty(extra.getAtUidList())) {
|
||||
// return false;
|
||||
// }
|
||||
// if (!extra.getAtUidList().contains(chatGPTProperties.getAIUserId())) {
|
||||
// return false;
|
||||
// }
|
||||
MessageExtra extra = message.getExtra();
|
||||
if (extra == null) {
|
||||
return false;
|
||||
}
|
||||
if (CollectionUtils.isEmpty(extra.getAtUidList())) {
|
||||
return false;
|
||||
}
|
||||
if (!extra.getAtUidList().contains(chatGPTProperties.getAIUserId())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(message.getContent())) {
|
||||
return false;
|
||||
}
|
||||
return StringUtils.contains(message.getContent(), "@" + chatGPTProperties.getAIUserName())
|
||||
&& StringUtils.isNotBlank(message.getContent().replace(chatGPTProperties.getAIUserName(), "").trim());
|
||||
return StringUtils.contains(message.getContent(), "@" + AI_NAME)
|
||||
&& StringUtils.isNotBlank(message.getContent().replace(AI_NAME, "").trim());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,11 +30,6 @@ public class ChatGLM2Properties {
|
||||
*/
|
||||
private Long AIUserId;
|
||||
|
||||
/**
|
||||
* 机器人名称
|
||||
*/
|
||||
private String AIUserName;
|
||||
|
||||
/**
|
||||
* 每个用户每?分钟可以请求一次
|
||||
*/
|
||||
|
||||
@@ -18,11 +18,6 @@ public class ChatGPTProperties {
|
||||
* 机器人 id
|
||||
*/
|
||||
private Long AIUserId;
|
||||
|
||||
/**
|
||||
* 机器人名称
|
||||
*/
|
||||
private String AIUserName;
|
||||
/**
|
||||
* 模型名称
|
||||
*/
|
||||
@@ -36,6 +31,11 @@ public class ChatGPTProperties {
|
||||
*/
|
||||
private String proxyUrl;
|
||||
|
||||
/**
|
||||
* 超时
|
||||
*/
|
||||
private Integer timeout = 60*1000;
|
||||
|
||||
/**
|
||||
* 用户每天条数限制
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user