优化发送逻辑

This commit is contained in:
Soulter
2022-12-08 21:22:27 +08:00
parent 84f948a617
commit 2caba25652
6 changed files with 106 additions and 25 deletions
+40
View File
@@ -236,3 +236,43 @@
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] 心跳维持启动...
2022-12-08 20:31:53,159 [INFO] (client.py:159)_bot_login [botpy] 登录机器人账号中...
2022-12-08 20:31:58,306 [INFO] (client.py:178)_bot_init [botpy] 程序启动...
2022-12-08 20:31:58,307 [INFO] (connection.py:59)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1
2022-12-08 20:31:58,308 [INFO] (client.py:236)bot_connect [botpy] 会话启动中...
2022-12-08 20:31:58,308 [INFO] (gateway.py:110)ws_connect [botpy] 启动中...
2022-12-08 20:31:58,748 [INFO] (gateway.py:136)ws_identify [botpy] 鉴权中...
2022-12-08 20:31:58,924 [INFO] (gateway.py:80)on_message [botpy] 机器人「SoGPT-测试中」启动成功!
2022-12-08 20:31:58,925 [INFO] (gateway.py:217)_send_heart [botpy] 心跳维持启动...
2022-12-08 20:34:26,596 [INFO] (client.py:159)_bot_login [botpy] 登录机器人账号中...
2022-12-08 20:34:27,610 [INFO] (client.py:178)_bot_init [botpy] 程序启动...
2022-12-08 20:34:27,611 [INFO] (connection.py:59)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1
2022-12-08 20:34:27,613 [INFO] (client.py:236)bot_connect [botpy] 会话启动中...
2022-12-08 20:34:27,613 [INFO] (gateway.py:110)ws_connect [botpy] 启动中...
2022-12-08 20:34:27,919 [INFO] (gateway.py:136)ws_identify [botpy] 鉴权中...
2022-12-08 20:34:28,022 [INFO] (gateway.py:80)on_message [botpy] 机器人「SoGPT-测试中」启动成功!
2022-12-08 20:34:28,023 [INFO] (gateway.py:217)_send_heart [botpy] 心跳维持启动...
2022-12-08 20:35:47,952 [INFO] (client.py:159)_bot_login [botpy] 登录机器人账号中...
2022-12-08 20:35:48,499 [INFO] (client.py:178)_bot_init [botpy] 程序启动...
2022-12-08 20:35:48,500 [INFO] (connection.py:59)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1
2022-12-08 20:35:48,500 [INFO] (client.py:236)bot_connect [botpy] 会话启动中...
2022-12-08 20:35:48,501 [INFO] (gateway.py:110)ws_connect [botpy] 启动中...
2022-12-08 20:35:48,723 [INFO] (gateway.py:136)ws_identify [botpy] 鉴权中...
2022-12-08 20:35:48,793 [INFO] (gateway.py:80)on_message [botpy] 机器人「SoGPT-测试中」启动成功!
2022-12-08 20:35:48,794 [INFO] (gateway.py:217)_send_heart [botpy] 心跳维持启动...
2022-12-08 20:55:03,450 [ERROR] (http.py:73)_handle_response [botpy] 接口请求异常,请求连接: https://api.sgroup.qq.com/channels/7150658/messages, 错误代码: 403, 返回内容: {'code': 304003, 'message': 'url not allowed'}, trace_id:c4c4d23f9b2829d03ae9a7dca0184fe9
2022-12-08 21:05:48,678 [INFO] (gateway.py:54)on_closed [botpy] 关闭, 返回码: 4009, 返回信息: Session timed out
2022-12-08 21:05:53,700 [INFO] (connection.py:59)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1
2022-12-08 21:05:53,701 [INFO] (client.py:236)bot_connect [botpy] 会话启动中...
2022-12-08 21:05:53,702 [INFO] (gateway.py:110)ws_connect [botpy] 启动中...
2022-12-08 21:05:54,145 [INFO] (gateway.py:169)ws_resume [botpy] 重连启动...
2022-12-08 21:05:54,232 [INFO] (gateway.py:85)on_message [botpy] 机器人重连成功!
2022-12-08 21:05:54,233 [INFO] (gateway.py:217)_send_heart [botpy] 心跳维持启动...
2022-12-08 21:07:39,468 [INFO] (client.py:159)_bot_login [botpy] 登录机器人账号中...
2022-12-08 21:07:40,244 [INFO] (client.py:178)_bot_init [botpy] 程序启动...
2022-12-08 21:07:40,245 [INFO] (connection.py:59)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1
2022-12-08 21:07:40,246 [INFO] (client.py:236)bot_connect [botpy] 会话启动中...
2022-12-08 21:07:40,247 [INFO] (gateway.py:110)ws_connect [botpy] 启动中...
2022-12-08 21:07:40,553 [INFO] (gateway.py:136)ws_identify [botpy] 鉴权中...
2022-12-08 21:07:40,659 [INFO] (gateway.py:80)on_message [botpy] 机器人「SoGPT-测试中」启动成功!
2022-12-08 21:07:40,660 [INFO] (gateway.py:217)_send_heart [botpy] 心跳维持启动...
+24
View File
@@ -0,0 +1,24 @@
import pymysql
import yaml
# TODO: 数据库缓存prompt
class dbConn():
def __init__(self):
with open("./configs/config.yaml", 'r', encoding='utf-8') as ymlfile:
cfg = yaml.safe_load(ymlfile)
if cfg['database']['host'] != '' or cfg['database']['port'] or cfg['database']['user'] != '' or cfg['database']['password'] != '' or cfg['database']['db'] != '':
print("读取数据库配置成功")
self.db = pymysql.connect(
host=cfg['database']['host'],
port=cfg['database']['port'],
user=cfg['database']['user'],
password=cfg['database']['password'],
db=cfg['database']['db'],
charset='utf8mb4',
)
else:
raise BaseException("请在config中完善你的数据库配置")
def getCursor(self):
return self.db.cursor()
Binary file not shown.
+1 -1
View File
@@ -18,7 +18,7 @@ class ChatGPT:
inst = self
async def chat(self, prompt):
print("[ChatGPT] 接收到prompt: "+prompt)
print("[ChatGPT] 接收到prompt:\n"+prompt)
response = openai.Completion.create(
prompt=prompt,
**self.chatGPT_configs
Binary file not shown.
+41 -24
View File
@@ -11,30 +11,47 @@ session_dict = {}
class botClient(botpy.Client):
async def on_at_message_create(self, message: Message):
print(message.content)
qq_msg = ""
# 过滤@头
pattern = r"<@!\d+>\s+(.+)"
result = re.search(pattern, message.content)
if result:
qq_msg = result.group(1).strip()
print(qq_msg)
# 检测@头,返回对应缓存的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}")
try:
print("[QQBOT] 接收到消息:"+ message.content)
qq_msg = ""
# 过滤@头
pattern = r"<@!\d+>\s+(.+)"
result = re.search(pattern, message.content)
if result:
qq_msg = result.group(1).strip()
# 检测@头,返回对应缓存的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)
if qq_msg == "/reset":
session_id = session_id_result.group(0)
session_dict[session_id] = ""
await message.reply(content=f"{message.member.nick} [ChatGPT] 重置成功")
return
if session_id not in session_dict:
session_dict[session_id] = ""
# 添加新条目进入缓存的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"
#检测是否存在url,如果存在,则去除url 防止被qq过滤
chatgpt_res = re.sub(r"([\s]+)(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)([\s]+)", r"\1\3", chatgpt_res)
print(f'{session_id} 目前prompt: {session_dict[session_id]}' )
# 发送qq信息
await message.reply(content=f"[ChatGPT]{chatgpt_res}")
except botpy.errors.Forbidden:
print("无法发送消息,可能是因为没有给botpy发消息的权限")
def initBot(chatgpt_inst):
global chatgpt