diff --git a/bot/chatgpt/chat_gpt_bot.py b/bot/chatgpt/chat_gpt_bot.py index f883d7d..279ca80 100644 --- a/bot/chatgpt/chat_gpt_bot.py +++ b/bot/chatgpt/chat_gpt_bot.py @@ -3,10 +3,14 @@ from bot.bot import Bot from config import conf from common.log import logger +from common.expired_dict import ExpiredDict import openai import time -user_session = dict() +if conf().get('expires_in_seconds'): + user_session = ExpiredDict(conf().get('expires_in_seconds')) +else: + user_session = dict() # OpenAI对话模型API (可用) class ChatGPTBot(Bot): diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 75c3c68..66778f4 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -46,6 +46,9 @@ class WechatChannel(Channel): other_user_id = msg['User']['UserName'] # 对手方id content = msg['Text'] match_prefix = self.check_prefix(content, conf().get('single_chat_prefix')) + if "」\n- - - - - - - - - - - - - - -" in content: + logger.debug("[WX]reference query skipped") + return if from_user_id == other_user_id and match_prefix is not None: # 好友向自己发送消息 if match_prefix != '': @@ -87,7 +90,9 @@ class WechatChannel(Channel): content = context_special_list[1] elif len(content_list) == 2: content = content_list[1] - + if "」\n- - - - - - - - - - - - - - -" in content: + logger.debug("[WX]reference query skipped") + return "" config = conf() match_prefix = (msg['IsAt'] and not config.get("group_at_off", False)) or self.check_prefix(origin_content, config.get('group_chat_prefix')) \ or self.check_contain(origin_content, config.get('group_chat_keyword')) diff --git a/common/expired_dict.py b/common/expired_dict.py new file mode 100644 index 0000000..ccf16c7 --- /dev/null +++ b/common/expired_dict.py @@ -0,0 +1,23 @@ +from datetime import datetime, timedelta + +class ExpiredDict(dict): + def __init__(self, expires_in_seconds): + super().__init__() + self.expires_in_seconds = expires_in_seconds + + def __getitem__(self, key): + value, expiry_time = super().__getitem__(key) + if datetime.now() > expiry_time: + del self[key] + raise KeyError("expired {}".format(key)) + self.__setitem__(key, value) + return value + + def __setitem__(self, key, value): + expiry_time = datetime.now() + timedelta(seconds=self.expires_in_seconds) + super().__setitem__(key, (value, expiry_time)) + def get(self, key, default=None): + try: + return self[key] + except KeyError: + return default \ No newline at end of file diff --git a/config-template.json b/config-template.json index 6e99c48..f83c344 100644 --- a/config-template.json +++ b/config-template.json @@ -6,5 +6,6 @@ "group_name_white_list": ["ChatGPT测试群", "ChatGPT测试群2"], "image_create_prefix": ["画", "看", "找"], "conversation_max_tokens": 1000, - "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。" + "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。", + "expires_in_seconds": 3600 }