Skip to content

Commit

Permalink
feat: use logging
Browse files Browse the repository at this point in the history
  • Loading branch information
zhayujie committed Dec 11, 2022
1 parent a1f4534 commit eef0dfc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
13 changes: 6 additions & 7 deletions bot/chatgpt/chat_gpt_bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import time
from bot.bot import Bot
from revChatGPT.revChatGPT import Chatbot
from common.log import logger

config = {
"Authorization": "<Your Bearer Token Here>", # This is optional
Expand All @@ -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

Expand All @@ -33,13 +34,11 @@ def reply(self, query, context=None):
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()
Expand All @@ -48,5 +47,5 @@ def reply(self, query, context=None):
user_session[from_user_id] = user_cache
return res['message']
except Exception as e:
print(e)
logger.error(e)
return None
1 change: 0 additions & 1 deletion bridge/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ def __init__(self):

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)
12 changes: 8 additions & 4 deletions channel/wechat/wechat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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)

Expand All @@ -20,6 +21,10 @@ def handler_group_msg(msg):
WechatChannel().handle_group(msg)


group_white_list = ['学就完事了', '小宝群', '全天乱斗模式', '戒赌吧', '命苦还要快乐', '攒钱让姐妹当小三的组织',
'快乐家人', '技术沙龙', '流动性混子', '计算机学习交流', '如何评价']


class WechatChannel(Channel):
def __init__(self):
pass
Expand All @@ -32,7 +37,7 @@ def startup(self):
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']
Expand All @@ -43,8 +48,7 @@ def handle(self, msg):
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:
Expand All @@ -63,7 +67,7 @@ def handle_group(self, msg):

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):
Expand Down
16 changes: 16 additions & 0 deletions common/log.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit eef0dfc

Please sign in to comment.