mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-04-29 05:34:14 +08:00
@@ -3,10 +3,14 @@
|
|||||||
from bot.bot import Bot
|
from bot.bot import Bot
|
||||||
from config import conf
|
from config import conf
|
||||||
from common.log import logger
|
from common.log import logger
|
||||||
|
from common.expired_dict import ExpiredDict
|
||||||
import openai
|
import openai
|
||||||
import time
|
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 (可用)
|
# OpenAI对话模型API (可用)
|
||||||
class ChatGPTBot(Bot):
|
class ChatGPTBot(Bot):
|
||||||
|
|||||||
@@ -46,6 +46,9 @@ class WechatChannel(Channel):
|
|||||||
other_user_id = msg['User']['UserName'] # 对手方id
|
other_user_id = msg['User']['UserName'] # 对手方id
|
||||||
content = msg['Text']
|
content = msg['Text']
|
||||||
match_prefix = self.check_prefix(content, conf().get('single_chat_prefix'))
|
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 from_user_id == other_user_id and match_prefix is not None:
|
||||||
# 好友向自己发送消息
|
# 好友向自己发送消息
|
||||||
if match_prefix != '':
|
if match_prefix != '':
|
||||||
@@ -87,7 +90,9 @@ class WechatChannel(Channel):
|
|||||||
content = context_special_list[1]
|
content = context_special_list[1]
|
||||||
elif len(content_list) == 2:
|
elif len(content_list) == 2:
|
||||||
content = content_list[1]
|
content = content_list[1]
|
||||||
|
if "」\n- - - - - - - - - - - - - - -" in content:
|
||||||
|
logger.debug("[WX]reference query skipped")
|
||||||
|
return ""
|
||||||
config = conf()
|
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')) \
|
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'))
|
or self.check_contain(origin_content, config.get('group_chat_keyword'))
|
||||||
|
|||||||
23
common/expired_dict.py
Normal file
23
common/expired_dict.py
Normal file
@@ -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
|
||||||
@@ -6,5 +6,6 @@
|
|||||||
"group_name_white_list": ["ChatGPT测试群", "ChatGPT测试群2"],
|
"group_name_white_list": ["ChatGPT测试群", "ChatGPT测试群2"],
|
||||||
"image_create_prefix": ["画", "看", "找"],
|
"image_create_prefix": ["画", "看", "找"],
|
||||||
"conversation_max_tokens": 1000,
|
"conversation_max_tokens": 1000,
|
||||||
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。"
|
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。",
|
||||||
|
"expires_in_seconds": 3600
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user