Pārlūkot izejas kodu

feat: use logging

develop
zhayujie pirms 1 gada
vecāks
revīzija
eef0dfc4fa
4 mainītis faili ar 30 papildinājumiem un 12 dzēšanām
  1. +6
    -7
      bot/chatgpt/chat_gpt_bot.py
  2. +0
    -1
      bridge/bridge.py
  3. +8
    -4
      channel/wechat/wechat_channel.py
  4. +16
    -0
      common/log.py

+ 6
- 7
bot/chatgpt/chat_gpt_bot.py Parādīt failu

@@ -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
@@ -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

+ 0
- 1
bridge/bridge.py Parādīt failu

@@ -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)

+ 8
- 4
channel/wechat/wechat_channel.py Parādīt failu

@@ -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):


+ 16
- 0
common/log.py Parādīt failu

@@ -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()

Notiek ielāde…
Atcelt
Saglabāt