diff --git a/mallchat-common/src/main/resources/application.yml b/mallchat-common/src/main/resources/application.yml index e0f925e..2df7f5b 100644 --- a/mallchat-common/src/main/resources/application.yml +++ b/mallchat-common/src/main/resources/application.yml @@ -65,11 +65,11 @@ wx: aesKey: ${mallchat.wx.aesKey} # 接口配置里的EncodingAESKey值 chatai: chatgpt: - use: true - AIUserId: 10450 - key: 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 + use: false + AIUserId: 10452 + key: xxxxx chatglm2: - use: true - url: http://vastmiao.natapp1.cc + use: false + url: xxxxx minute: 3 # 每个用户每3分钟可以请求一次 AIUserId: 10451 diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatGLM2Handler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatGLM2Handler.java index ca529f3..13aecfd 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatGLM2Handler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatGLM2Handler.java @@ -28,7 +28,7 @@ public class ChatGLM2Handler extends AbstractChatAIHandler { "没给钱,矿工了。。。。", "服务器被你们玩儿坏了。。。。", "你们这群人,我都不想理你们了。。。。", - "还艾特我呢?那是另外的价钱。。。。", + "艾特我那是另外的价钱。。。。", "得加钱"); @@ -67,6 +67,7 @@ public class ChatGLM2Handler extends AbstractChatAIHandler { .create() .url(glm2Properties.getUrl()) .prompt(content) + .timeout(glm2Properties.getTimeout()) .send(); } catch (Exception e) { e.printStackTrace(); @@ -123,7 +124,7 @@ public class ChatGLM2Handler extends AbstractChatAIHandler { // if (CollectionUtils.isEmpty(extra.getAtUidList())) { // return false; // } -// if (!extra.getAtUidList().contains(ChatAIServiceImpl.AI_USER_ID)) { +// if (!extra.getAtUidList().contains(glm2Properties.getAIUserId())) { // return false; // } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java index e981fa7..f814713 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java @@ -36,28 +36,40 @@ public class GPTChatAIHandler extends AbstractChatAIHandler { @Override protected String doChat(Message message) { - String content = message.getContent().replace("@" +chatGPTProperties.getAIUserName(), "").trim(); + String content = message.getContent().replace("@" + chatGPTProperties.getAIUserName(), "").trim(); Long uid = message.getFromUid(); Long chatNum; String text; - if ((chatNum = userChatNumInrc(uid)) > chatGPTProperties.getLimit()) { + if ((chatNum = getUserChatNum(uid)) > chatGPTProperties.getLimit()) { text = "你今天已经和我聊了" + chatNum + "次了,我累了,明天再聊吧"; } else { - HttpResponse response = ChatGPTUtils.create(chatGPTProperties.getKey()) - .proxyUrl(chatGPTProperties.getProxyUrl()) - .model(chatGPTProperties.getModelName()) - .prompt(content) - .send(); - text = ChatGPTUtils.parseText(response); + HttpResponse response = null; + try { + response = ChatGPTUtils.create(chatGPTProperties.getKey()) + .proxyUrl(chatGPTProperties.getProxyUrl()) + .model(chatGPTProperties.getModelName()) + .prompt(content) + .send(); + text = ChatGPTUtils.parseText(response); + userChatNumInrc(uid); + } catch (Exception e) { + e.printStackTrace(); + text = "我累了,明天再聊吧"; + } } return text; } private Long userChatNumInrc(Long uid) { - //todo:白名单 return RedisUtils.inc(RedisKey.getKey(RedisKey.USER_CHAT_NUM, uid), DateUtils.getEndTimeByToday().intValue(), TimeUnit.MILLISECONDS); } + private Long getUserChatNum(Long uid) { + Long num = RedisUtils.get(RedisKey.getKey(RedisKey.USER_CHAT_NUM, uid), Long.class); + return num == null ? 0 : num; + + } + @Override protected boolean supports(Message message) { @@ -73,7 +85,7 @@ public class GPTChatAIHandler extends AbstractChatAIHandler { // if (CollectionUtils.isEmpty(extra.getAtUidList())) { // return false; // } -// if (!extra.getAtUidList().contains(ChatAIServiceImpl.AI_USER_ID)) { +// if (!extra.getAtUidList().contains(chatGPTProperties.getAIUserId())) { // return false; // } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java index 582ce4a..2ccc99b 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java @@ -36,8 +36,13 @@ public class ChatGLM2Properties { private String AIUserName; /** - * 每个用户每3分钟可以请求一次 + * 每个用户每?分钟可以请求一次 */ private Long minute = 3L; + /** + * 超时 + */ + private Integer timeout = 60*1000; + } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGLM2Utils.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGLM2Utils.java index 2729e0a..7ac9e1a 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGLM2Utils.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGLM2Utils.java @@ -42,8 +42,6 @@ public class ChatGLM2Utils { } - - public ChatGLM2Utils url(String url) { this.url = url; return this; @@ -59,6 +57,7 @@ public class ChatGLM2Utils { this.prompt = prompt; return this; } + public HttpResponse send() { JSONObject param = new JSONObject(); param.set("prompt", prompt); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGPTUtils.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGPTUtils.java index c846492..094cd39 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGPTUtils.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGPTUtils.java @@ -141,6 +141,8 @@ public class ChatGPTUtils { param.set("top_p", topP); param.set("frequency_penalty", frequencyPenalty); param.set("presence_penalty", presencePenalty); + log.info("headers >>> " + headers); + log.info("param >>> " + param); return HttpUtil.createPost(StringUtils.isNotBlank(proxyUrl) ? proxyUrl : URL) .addHeaders(headers) .body(param.toString())