From 89400630c09ad99deeae92dc14aa1c368f794bba Mon Sep 17 00:00:00 2001 From: zhayujie Date: Fri, 25 Aug 2023 16:55:32 +0800 Subject: [PATCH] fix: xunfei client bug --- bot/xunfei/xunfei_spark_bot.py | 16 ++++++++++------ requirements-optional.txt | 5 ++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/bot/xunfei/xunfei_spark_bot.py b/bot/xunfei/xunfei_spark_bot.py index c1564b2..ddda15e 100644 --- a/bot/xunfei/xunfei_spark_bot.py +++ b/bot/xunfei/xunfei_spark_bot.py @@ -30,6 +30,9 @@ import random # 消息队列 map queue_map = dict() +# 响应队列 map +reply_map = dict() + class XunFeiBot(Bot): def __init__(self): @@ -43,7 +46,6 @@ class XunFeiBot(Bot): self.spark_url = "ws://spark-api.xf-yun.com/v2.1/chat" self.host = urlparse(self.spark_url).netloc self.path = urlparse(self.spark_url).path - self.answer = "" # 和wenxin使用相同的session机制 self.sessions = SessionManager(BaiduWenxinSession, model=const.XUNFEI) @@ -52,6 +54,7 @@ class XunFeiBot(Bot): logger.info("[XunFei] query={}".format(query)) session_id = context["session_id"] request_id = self.gen_request_id(session_id) + reply_map[request_id] = "" session = self.sessions.session_query(query, session_id) threading.Thread(target=self.create_web_socket, args=(session.messages, request_id)).start() depth = 0 @@ -70,19 +73,20 @@ class XunFeiBot(Bot): # 请求结束 del queue_map[request_id] if data_item.reply: - self.answer += data_item.reply + reply_map[request_id] += data_item.reply usage = data_item.usage break - self.answer += data_item.reply + reply_map[request_id] += data_item.reply depth += 1 except Exception as e: depth += 1 continue t2 = time.time() - logger.info(f"[XunFei-API] response={self.answer}, time={t2 - t1}s, usage={usage}") - self.sessions.session_reply(self.answer, session_id, usage.get("total_tokens")) - reply = Reply(ReplyType.TEXT, self.answer) + logger.info(f"[XunFei-API] response={reply_map[request_id]}, time={t2 - t1}s, usage={usage}") + self.sessions.session_reply(reply_map[request_id], session_id, usage.get("total_tokens")) + reply = Reply(ReplyType.TEXT, reply_map[request_id]) + del reply_map[request_id] return reply else: reply = Reply(ReplyType.ERROR, "Bot不支持处理{}类型的消息".format(context.type)) diff --git a/requirements-optional.txt b/requirements-optional.txt index 92477f3..0acc377 100644 --- a/requirements-optional.txt +++ b/requirements-optional.txt @@ -23,6 +23,9 @@ web.py wechatpy # chatgpt-tool-hub plugin - --extra-index-url https://pypi.python.org/simple chatgpt_tool_hub==0.4.6 + +# xunfei spark +websocket==0.2.1 +websocket-client==0.57.0