mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-13 21:53:41 +08:00
优化chatAI
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
// }
|
||||
|
||||
|
||||
@@ -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;
|
||||
// }
|
||||
|
||||
|
||||
@@ -36,8 +36,13 @@ public class ChatGLM2Properties {
|
||||
private String AIUserName;
|
||||
|
||||
/**
|
||||
* 每个用户每3分钟可以请求一次
|
||||
* 每个用户每?分钟可以请求一次
|
||||
*/
|
||||
private Long minute = 3L;
|
||||
|
||||
/**
|
||||
* 超时
|
||||
*/
|
||||
private Integer timeout = 60*1000;
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user