From 84f948a6175172fd397ca8113e7453c55612704c Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Thu, 8 Dec 2022 20:19:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9prompt=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- botpy.log | 21 +++++++++++++++++++++ cores/qqbot/core.py | 36 ++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/botpy.log b/botpy.log index 0d4a39756..d174dc3da 100644 --- a/botpy.log +++ b/botpy.log @@ -215,3 +215,24 @@ 2022-12-08 18:31:58,165 [INFO] (gateway.py:136)ws_identify [botpy] 鉴权中... 2022-12-08 18:31:58,269 [INFO] (gateway.py:80)on_message [botpy] 机器人「SoGPT-测试中」启动成功! 2022-12-08 18:31:58,270 [INFO] (gateway.py:217)_send_heart [botpy] 心跳维持启动... +2022-12-08 19:01:58,099 [INFO] (gateway.py:54)on_closed [botpy] 关闭, 返回码: 4009, 返回信息: Session timed out +2022-12-08 19:02:03,110 [INFO] (connection.py:59)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1 +2022-12-08 19:02:03,111 [INFO] (client.py:236)bot_connect [botpy] 会话启动中... +2022-12-08 19:02:03,111 [INFO] (gateway.py:110)ws_connect [botpy] 启动中... +2022-12-08 19:02:03,314 [INFO] (gateway.py:169)ws_resume [botpy] 重连启动... +2022-12-08 19:02:03,381 [INFO] (gateway.py:85)on_message [botpy] 机器人重连成功! +2022-12-08 19:02:03,382 [INFO] (gateway.py:217)_send_heart [botpy] 心跳维持启动... +2022-12-08 19:32:03,274 [INFO] (gateway.py:54)on_closed [botpy] 关闭, 返回码: 4009, 返回信息: Session timed out +2022-12-08 19:32:08,271 [INFO] (connection.py:59)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1 +2022-12-08 19:32:08,272 [INFO] (client.py:236)bot_connect [botpy] 会话启动中... +2022-12-08 19:32:08,272 [INFO] (gateway.py:110)ws_connect [botpy] 启动中... +2022-12-08 19:32:08,534 [INFO] (gateway.py:169)ws_resume [botpy] 重连启动... +2022-12-08 19:32:08,604 [INFO] (gateway.py:85)on_message [botpy] 机器人重连成功! +2022-12-08 19:32:08,605 [INFO] (gateway.py:217)_send_heart [botpy] 心跳维持启动... +2022-12-08 20:02:08,495 [INFO] (gateway.py:54)on_closed [botpy] 关闭, 返回码: 4009, 返回信息: Session timed out +2022-12-08 20:02:13,497 [INFO] (connection.py:59)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1 +2022-12-08 20:02:13,497 [INFO] (client.py:236)bot_connect [botpy] 会话启动中... +2022-12-08 20:02:13,498 [INFO] (gateway.py:110)ws_connect [botpy] 启动中... +2022-12-08 20:02:13,797 [INFO] (gateway.py:169)ws_resume [botpy] 重连启动... +2022-12-08 20:02:13,914 [INFO] (gateway.py:85)on_message [botpy] 机器人重连成功! +2022-12-08 20:02:13,915 [INFO] (gateway.py:217)_send_heart [botpy] 心跳维持启动... diff --git a/cores/qqbot/core.py b/cores/qqbot/core.py index 534802998..7b7e0a4d3 100644 --- a/cores/qqbot/core.py +++ b/cores/qqbot/core.py @@ -19,26 +19,22 @@ class botClient(botpy.Client): if result: qq_msg = result.group(1).strip() print(qq_msg) - # 检测@头,返回对应session - pattern0 = r"<@!\d+>" - result0 = re.search(pattern0, message.content) - if result0: - session_id = result0.group(0) - if session_id in session_dict: - print("旧会话 "+session_id) - chatgpt_res =await chatgpt.chat(session_dict[session_id]+' '+qq_msg) - - chatgpt_res = chatgpt_res.strip() - session_dict[session_id] = session_dict[session_id] + ' ' + qq_msg - await message.reply(content=f"[ChatGPT]{chatgpt_res}") - else: - print("新会话 "+session_id) - # new_session = chatgpt.newSession() - session_dict[session_id] = qq_msg - - chatgpt_res = await chatgpt.chat(qq_msg) - chatgpt_res = chatgpt_res.strip() - await message.reply(content=f"[ChatGPT]{chatgpt_res}") + # 检测@头,返回对应缓存的prompt + session_id_pattern = r"<@!\d+>" + session_id_result = re.search(session_id_pattern, message.content) + if session_id_result: + # 匹配出sessionid + session_id = session_id_result.group(0) + # 添加新条目进入缓存的prompt + session_dict[session_id] = "Human: "+ qq_msg + "\nAI: " + # 请求chatGPT获得结果 + chatgpt_res = await chatgpt.chat(session_dict[session_id]) + # 处理结果文本 + chatgpt_res = chatgpt_res.strip() + session_dict[session_id] += chatgpt_res + "\n" + print(f'{session_id} 目前prompt: {session_dict[session_id]}' ) + # 发送qq信息 + await message.reply(content=f"[ChatGPT]{chatgpt_res}") def initBot(chatgpt_inst): global chatgpt