mirror of
https://github.com/zhayujie/bot-on-anything.git
synced 2026-02-02 09:38:26 +08:00
Merge branch 'master' into master
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 "目前无等待回复信息,请输入对话"
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
"cookies":[]
|
||||
}
|
||||
},
|
||||
|
||||
"channel": {
|
||||
"type": "terminal",
|
||||
"single_chat_prefix": ["bot", "@bot"],
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user