From c948d2bff96fdb3e91382e865960d073732e712a Mon Sep 17 00:00:00 2001 From: Selier <42491291+selierlin@users.noreply.github.com> Date: Sun, 19 Mar 2023 16:01:01 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B715=E7=A7=92?= =?UTF-8?q?=E5=86=85=E7=9B=B4=E6=8E=A5=E5=9B=9E=E5=A4=8D=EF=BC=8C=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E9=80=9A=E8=BF=87=E7=BB=A7=E7=BB=AD=E5=9B=9E=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel/wechat/wechat_mp_channel.py | 59 ++++++++++++++++++----------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/channel/wechat/wechat_mp_channel.py b/channel/wechat/wechat_mp_channel.py index 7279311..4c41ee9 100644 --- a/channel/wechat/wechat_mp_channel.py +++ b/channel/wechat/wechat_mp_channel.py @@ -46,36 +46,49 @@ class WechatSubsribeAccount(Channel): robot.config['HOST'] = '0.0.0.0' robot.run() - def handle(self, msg, count=0): + def handle(self, msg, count=1): + if msg.content == "继续": + return self.get_un_send_content(msg.source) + context = dict() context['from_user_id'] = msg.source - key = msg.source + key = msg.content + '|' + msg.source res = cache.get(key) - if msg.content == "继续": - if not res or res.get("status") == "done": - return "目前不在等待回复状态,请输入对话" - if res.get("status") == "waiting": - return "还在处理中,请稍后再试" - elif res.get("status") == "success": - cache[key] = {"status":"done"} - return res.get("data") - else: - return "目前不在等待回复状态,请输入对话" - elif not res or res.get('status') == "done": + if not res: + cache[key] = {"status": "waiting", "req_times": 1} thread_pool.submit(self._do_send, msg.content, context) + + res = cache.get(key) + logger.info("count={}, res={}".format(count, res)) + if res.get('status') == 'success': + res['status'] = "done" + cache.pop(key) + return res.get("data") + + if cache.get(key)['req_times'] == 3 and count >= 4: + logger.info("微信超时3次") return "已开始处理,请稍等片刻后输入\"继续\"查看回复" - else: - if res.get('status') == "done": - reply = res.get("data") - thread_pool.submit(self._do_send, msg.content, context) - return reply - else: - return "上一句对话正在处理中,请稍后输入\"继续\"查看回复" + + if count <= 5: + time.sleep(1) + if count == 5: + # 第5秒不做返回,防止消息发送出去了但是微信已经中断连接 + return None + return self.handle(msg, count+1) def _do_send(self, query, context): - key = context['from_user_id'] - cache[key] = {"status": "waiting"} + key = query + '|' + context['from_user_id'] reply_text = super().build_reply_content(query, context) logger.info('[WX_Public] reply content: {}'.format(reply_text)) + cache[key]['status'] = "success" + cache[key]['data'] = reply_text - cache[key] = {"status": "success", "data": reply_text} + def get_un_send_content(self, from_user_id): + for key in cache: + if from_user_id in key: + value = cache[key] + if value.get('status') == "success": + cache.pop(key) + return value.get("data") + return "还在处理中,请稍后再试" + return "目前无等待回复信息,请输入对话" From be33d212ea94195c7830e33b98a3e7c664306a71 Mon Sep 17 00:00:00 2001 From: RA Date: Sun, 19 Mar 2023 17:17:49 +0800 Subject: [PATCH 2/4] [new bing]adds quick-reply suggested questions. --- channel/http/static/1.js | 2 +- model/bing/new_bing_model.py | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/channel/http/static/1.js b/channel/http/static/1.js index 8d9fbce..4a34289 100644 --- a/channel/http/static/1.js +++ b/channel/http/static/1.js @@ -41,7 +41,7 @@ ConvState.prototype.sendMessage = function (msg) { $.ajax({ url: "./chat", type: "POST", - timeout:120000, + timeout:180000, data: JSON.stringify({ "id": _this.id, "msg": msg diff --git a/model/bing/new_bing_model.py b/model/bing/new_bing_model.py index 35101b2..975a8d9 100644 --- a/model/bing/new_bing_model.py +++ b/model/bing/new_bing_model.py @@ -6,8 +6,10 @@ from common import log from EdgeGPT import Chatbot, ConversationStyle user_session = dict() +suggestion_session = dict() # newBing对话模型逆向网页gitAPI + class BingModel(Model): style = ConversationStyle.creative @@ -23,6 +25,15 @@ class BingModel(Model): bot = user_session.get(context['from_user_id'], None) if (bot == None): bot = self.bot + else: + if (len(query) == 1 and query.isdigit() and query != "0"): + suggestion_dict = suggestion_session[context['from_user_id']] + if (suggestion_dict != None): + query = suggestion_dict[int(query)-1] + if (query == None): + return "输入的序号不在建议列表范围中" + else: + query = "在上面的基础上,"+query log.info("[NewBing] query={}".format(query)) task = bot.ask(query, conversation_style=self.style) answer = asyncio.run(task) @@ -39,6 +50,18 @@ class BingModel(Model): if len(reference) > 0: reference = "***\n"+reference + + suggestion = "" + if "suggestedResponses" in reply: + suggestion_dict = dict() + for i, attribution in enumerate(reply["suggestedResponses"]): + suggestion_dict[i] = attribution["text"] + suggestion += f">{i+1}、{attribution['text']}\n\n" + suggestion_session[context['from_user_id']] = suggestion_dict + + if len(suggestion) > 0: + suggestion = "***\n你可以通过输入序号快速追问我以下建议问题:\n\n"+suggestion + throttling = answer["item"]["throttling"] throttling_str = "" @@ -48,7 +71,7 @@ class BingModel(Model): else: throttling_str = f"对话轮次: {throttling['numUserMessagesInConversation']}/{throttling['maxNumUserMessagesInConversation']}\n" - response = f"{reply_text}\n{reference}\n***\n{throttling_str}" + response = f"{reply_text}\n{reference}\n{suggestion}\n***\n{throttling_str}" log.info("[NewBing] reply={}", response) user_session[context['from_user_id']] = bot return response From 2ebb03b08ac838cd9ebd83890260a2baad9e24ca Mon Sep 17 00:00:00 2001 From: Chi Zhao Date: Sun, 19 Mar 2023 18:46:33 +0300 Subject: [PATCH 3/4] fix: config-template.json --- config-template.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config-template.json b/config-template.json index f1088e2..4362dcc 100644 --- a/config-template.json +++ b/config-template.json @@ -14,8 +14,8 @@ }, "bing":{ "cookies":[] + } }, - "channel": { "type": "terminal", "single_chat_prefix": ["bot", "@bot"], From 2caa18129b5d0176a66d0b8c0be0e83f75173a9c Mon Sep 17 00:00:00 2001 From: zhayujie Date: Mon, 20 Mar 2023 00:53:03 +0800 Subject: [PATCH 4/4] Update config-template.json --- config-template.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config-template.json b/config-template.json index 4362dcc..dba7214 100644 --- a/config-template.json +++ b/config-template.json @@ -14,7 +14,7 @@ }, "bing":{ "cookies":[] - } + } }, "channel": { "type": "terminal",