diff --git a/bot/chatgpt/chat_gpt_bot.py b/bot/chatgpt/chat_gpt_bot.py index 2928915..f103696 100644 --- a/bot/chatgpt/chat_gpt_bot.py +++ b/bot/chatgpt/chat_gpt_bot.py @@ -1,6 +1,7 @@ import time from bot.bot import Bot from revChatGPT.revChatGPT import Chatbot +from common.log import logger config = { "Authorization": "", # This is optional @@ -15,12 +16,12 @@ class ChatGPTBot(Bot): def reply(self, query, context=None): from_user_id = context['from_user_id'] - print("[GPT]query={}, user_id={}, session={}".format(query, from_user_id, user_session)) + logger.info("[GPT]query={}, user_id={}, session={}".format(query, from_user_id, user_session)) now = time.time() global last_session_refresh if now - last_session_refresh > 60 * 8: - print('[GPT]session refresh, now={}, last={}'.format(now, last_session_refresh)) + logger.info('[GPT]session refresh, now={}, last={}'.format(now, last_session_refresh)) chatbot.refresh_session() last_session_refresh = now @@ -33,13 +34,11 @@ class ChatGPTBot(Bot): else: chatbot.reset_chat() - print("[GPT]convId={}, parentId={}".format(chatbot.conversation_id, chatbot.parent_id)) - - + logger.info("[GPT]convId={}, parentId={}".format(chatbot.conversation_id, chatbot.parent_id)) try: res = chatbot.get_chat_response(query, output="text") - print("[GPT]userId={}, res={}".format(from_user_id, res)) + logger.info("[GPT]userId={}, res={}".format(from_user_id, res)) user_cache = dict() user_cache['last_reply_time'] = time.time() @@ -48,5 +47,5 @@ class ChatGPTBot(Bot): user_session[from_user_id] = user_cache return res['message'] except Exception as e: - print(e) + logger.error(e) return None diff --git a/bridge/bridge.py b/bridge/bridge.py index 2911e2f..6c164e8 100644 --- a/bridge/bridge.py +++ b/bridge/bridge.py @@ -7,4 +7,3 @@ class Bridge(object): def fetch_reply_content(self, query, context): return bot_factory.create_bot("chatGPT").reply(query, context) - # return bot_factory.create_bot("baidu").reply(query, context) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 46cb4b5..9bce166 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -6,6 +6,7 @@ import json from itchat.content import * from channel.channel import Channel from concurrent.futures import ThreadPoolExecutor +from common.log import logger thead_pool = ThreadPoolExecutor(max_workers=8) @@ -20,6 +21,10 @@ def handler_group_msg(msg): WechatChannel().handle_group(msg) +group_white_list = ['学就完事了', '小宝群', '全天乱斗模式', '戒赌吧', '命苦还要快乐', '攒钱让姐妹当小三的组织', + '快乐家人', '技术沙龙', '流动性混子', '计算机学习交流', '如何评价'] + + class WechatChannel(Channel): def __init__(self): pass @@ -32,7 +37,7 @@ class WechatChannel(Channel): itchat.run() def handle(self, msg): - print("[WX]receive msg: " + json.dumps(msg, ensure_ascii=False)) + logger.info("[WX]receive msg: " + json.dumps(msg, ensure_ascii=False)) from_user_id = msg['FromUserName'] other_user_id = msg['User']['UserName'] content = msg['Text'] @@ -43,8 +48,7 @@ class WechatChannel(Channel): thead_pool.submit(self._do_send, content, from_user_id) def handle_group(self, msg): - group_white_list = ['学就完事了', '小宝群', '全天乱斗模式', '戒赌吧', '命苦还要快乐','攒钱让姐妹当小三的组织'] - print("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False)) + logger.info("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False)) group_id = msg['User']['UserName'] group_name = msg['User'].get('NickName', None) if not group_name: @@ -63,7 +67,7 @@ class WechatChannel(Channel): def send(self, msg, receiver): # time.sleep(random.randint(1, 3)) - print('[WX] sendMsg={}, receiver={}'.format(msg, receiver)) + logger.info('[WX] sendMsg={}, receiver={}'.format(msg, receiver)) itchat.send(msg, toUserName=receiver) def _do_send(self, send_msg, reply_user_id): diff --git a/common/log.py b/common/log.py new file mode 100644 index 0000000..90a6c55 --- /dev/null +++ b/common/log.py @@ -0,0 +1,16 @@ +import logging +import sys + + +def _get_logger(): + log = logging.getLogger('log') + log.setLevel(logging.DEBUG) + console_handle = logging.StreamHandler(sys.stdout) + console_handle.setFormatter(logging.Formatter('[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s', + datefmt='%Y-%m-%d %H:%M:%S')) + log.addHandler(console_handle) + return log + + +# 日志句柄 +logger = _get_logger() \ No newline at end of file