From a1f45344b6a39ec019ef21d5387c9f75fe599731 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Sun, 11 Dec 2022 03:26:50 +0800 Subject: [PATCH] feat: wechat group reply --- .gitignore | 3 +- bot/__pycache__/bot.cpython-36.pyc | Bin 574 -> 0 bytes bot/__pycache__/bot_factory.cpython-36.pyc | Bin 491 -> 0 bytes .../__pycache__/baidu_unit_bot.cpython-36.pyc | Bin 1458 -> 0 bytes bot/bot_factory.py | 2 +- bot/chatgpt/chat_gpt_bot.py | 42 +++++++++----- bridge/__pycache__/bridge.cpython-36.pyc | Bin 616 -> 0 bytes bridge/bridge.py | 1 + channel/__pycache__/channel.cpython-36.pyc | Bin 1200 -> 0 bytes .../channel_factory.cpython-36.pyc | Bin 510 -> 0 bytes .../__pycache__/wechat_channel.cpython-36.pyc | Bin 1552 -> 0 bytes channel/wechat/wechat_channel.py | 54 +++++++++++++++--- 12 files changed, 77 insertions(+), 25 deletions(-) delete mode 100644 bot/__pycache__/bot.cpython-36.pyc delete mode 100644 bot/__pycache__/bot_factory.cpython-36.pyc delete mode 100644 bot/baidu/__pycache__/baidu_unit_bot.cpython-36.pyc delete mode 100644 bridge/__pycache__/bridge.cpython-36.pyc delete mode 100644 channel/__pycache__/channel.cpython-36.pyc delete mode 100644 channel/__pycache__/channel_factory.cpython-36.pyc delete mode 100644 channel/wechat/__pycache__/wechat_channel.cpython-36.pyc diff --git a/.gitignore b/.gitignore index f9c9c6b..aa03009 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store .idea -__pycache__ +__pycache__/ venv +*.pyc diff --git a/bot/__pycache__/bot.cpython-36.pyc b/bot/__pycache__/bot.cpython-36.pyc deleted file mode 100644 index a006b4219279b4c5140baccecf890864dda4fb8b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 574 zcmZWly-ve05O#k0LqsPS7_*@uwHeqdgbD~DmM(~;i{-=zZI#5S?I0y1@G?9MFW{92 zU`Gs`(;tvH>APHfci(q+r-MPD-#?RYLdXYVXFf&~ggQdU$&B0(?r`^&a2K2xZ{~9E zjzoTQ99&dd3?-BqtHX58bSOn4bjT8=B};XfW=yFdMVg(t7)=oB5FL=2g97fFe&(%- zjdv+@Y74E|uY+kz)>~Lf__P205|=zm`(S$Thx#?7?(a70CLH$PP~VbrRnz zkScEKW!%hJT`d<7+anFxVu<6$8;r`@bXvup-LPG|+w9>9M(yh~@=c%8g5^M|2`J44 zuQD6&Q@W~Hw$4b~-75caUHfK_XM>iy@3jBZ$TK}kxk!Vs K&2w){<9k2Bs*NN7 diff --git a/bot/__pycache__/bot_factory.cpython-36.pyc b/bot/__pycache__/bot_factory.cpython-36.pyc deleted file mode 100644 index 68002af308cf84209367eeecb85f7cad0b4d40c5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 491 zcmY*VJx{|h5Vf5&L{W>xf*2UGW@sb9Rw0CnSUXU4$zr*-QB(3!InI!X3HUSorASN+ zh>4wvv)hPr(mnfh@7_K8ax@A)-@jKAjL-)<2m<#T$m$$GpacmlNQQHg5R8N)+*g!1 zVz5Aw*PaA?&5A;1;REBkR88;&Lny-CaK=(mFN;*qO3h)14Rad(Tgd7Z;0e?pi8|Kk z1uc-q&#()Woi+3NCGTx@C&xeUIv9E2n)GnkwX_*i)6%wJQGF z|Admk%DA0JL>YJ8@V+i|n#((-N)_S6UzOU*zaixU;pZY=R#H`Q+ic=?&6;|X${0NA zF-yllj`vCT-!j=YU9fu$g_dug?*Sms#~xPZI*;7$h*D8`I aZ;c$l05m%PDlbKy$!iCy%2;*D(D?=YD1Wp7 diff --git a/bot/baidu/__pycache__/baidu_unit_bot.cpython-36.pyc b/bot/baidu/__pycache__/baidu_unit_bot.cpython-36.pyc deleted file mode 100644 index d453432f692a24cfa7981e02c1d3daa8f1ab2141..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1458 zcmZ`(UvJws5GO6kc3gMuwys%$J!Evr*-VJjvp@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6rXXNZ7DpXoCHAlVVuxgtW_nz7zkCv1twhYYTRCpI6TdIVCOcMU_D06-WYNgflzzqz_Cm2J5lHu4.UeCj_srwxF4V4x92z7lAjA" } chatbot = Chatbot(config) user_session = dict() @@ -13,28 +13,40 @@ last_session_refresh = time.time() 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)) + + 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)) + chatbot.refresh_session() + last_session_refresh = now + if from_user_id in user_session: - if time.time() - user_session[from_user_id]['last_reply_time'] < 60 * 3: + if time.time() - user_session[from_user_id]['last_reply_time'] < 60 * 5: chatbot.conversation_id = user_session[from_user_id]['conversation_id'] chatbot.parent_id = user_session[from_user_id]['parent_id'] + else: + chatbot.reset_chat() else: chatbot.reset_chat() - now = time.time() - global last_session_refresh - if now - last_session_refresh > 60 * 10: - chatbot.refresh_session() - last_session_refresh = now + print("[GPT]convId={}, parentId={}".format(chatbot.conversation_id, chatbot.parent_id)) - res = chatbot.get_chat_response(query, output="text") - user_cache = dict() - user_cache['last_reply_time'] = time.time() - user_cache['conversation_id'] = res['conversation_id'] - user_cache['parent_id'] = res['parent_id'] - user_session[from_user_id] = user_cache - print("[GPT]user={}, convId={}, content={}", from_user_id, res['conversation_id'], res['message']) - return res['message'] + try: + res = chatbot.get_chat_response(query, output="text") + print("[GPT]userId={}, res={}".format(from_user_id, res)) + user_cache = dict() + user_cache['last_reply_time'] = time.time() + user_cache['conversation_id'] = res['conversation_id'] + user_cache['parent_id'] = res['parent_id'] + user_session[from_user_id] = user_cache + return res['message'] + except Exception as e: + print(e) + return None diff --git a/bridge/__pycache__/bridge.cpython-36.pyc b/bridge/__pycache__/bridge.cpython-36.pyc deleted file mode 100644 index d25ba0cae902ab5786e64c1df1d148c19feaf39a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 616 zcmZuuJx{|h5Ix692~c2QLrAb@XoJLxP*n!DE{G+ImBcPhlqTRfLn^lR&+r@gCmC3n znAn-Pb0{pFbRW8VdiQ+3JUG}pd;3gY0pK0|*IcY7==C`U36d4CWD#R<3(}E%1ClFe z%_A<|I|#fFL_xs1{iHHt9;c?#ZA!hZIao~4>r)I0A|@enB$LhtkONsTyV66hK{uG{ zOg^ZTA(V_V`3-ta!XzM$&0c7uTdQKOscXa6MR;E;U5BeS4_Ax0ZSqWo=^{3xI2)n5 zFtOx6Y5drBz7ScN86iC4C!xSrN4U98YJT)eWj7 VSE;d+|42Ijs^5K0e`ci*`8RpPhUWkP diff --git a/bridge/bridge.py b/bridge/bridge.py index 6c164e8..2911e2f 100644 --- a/bridge/bridge.py +++ b/bridge/bridge.py @@ -7,3 +7,4 @@ 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/__pycache__/channel.cpython-36.pyc b/channel/__pycache__/channel.cpython-36.pyc deleted file mode 100644 index d38f44b04d40435678ee4a8b80d7eb709a800702..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1200 zcmZ`&J8#rL5Z<+YwqspF5m6yUE+{xbauReWgb)!zDYypFxLNJ(ZZ1CgaqO;<&Y~iz z`58$3B^NYQRP0~r_5{Hs^M7q+uBGOatrJHzi@`23!`bqR&8N=5qHmZo-f z6SR+p689-=6T)H1S1KtuJM~c%_bV+@K-N7SFPDJta9P^3e4D9U6;{c&T9+EGJ)^So zA4K~Q@bB~aN29cv*SpQUzTmrRld3rcv&TF=UX>Pgtid2pw!0=UmTOyW5s^W{#H%0l z*iU+!Xz#&<@;(k{BiyYn1r$HlN~rX+l5uXGLv?!qu z#edYjPfuDe2KstvXUv5ttLrbcvXw3t@t9|1?zYVJ9zbuxA>{2EK<^9r5L~(n1od>N z02*#I4I9@E-V_u|F3E;|p_e#(bO~ifb8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