2022-12-10 16:40:05 +08:00
|
|
|
|
import asyncio
|
2022-12-11 17:17:33 +08:00
|
|
|
|
import json
|
|
|
|
|
import os
|
2022-12-10 16:40:05 +08:00
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
import openai.error
|
2022-12-07 23:37:52 +08:00
|
|
|
|
from mirai import At, GroupMessage, MessageEvent, Mirai, Plain, StrangerMessage, WebSocketAdapter, FriendMessage, Image
|
2022-12-09 16:18:25 +08:00
|
|
|
|
|
|
|
|
|
import config
|
2022-12-07 23:37:52 +08:00
|
|
|
|
import pkg.openai.session
|
2022-12-15 18:50:15 +08:00
|
|
|
|
import pkg.openai.manager
|
2022-12-07 23:37:52 +08:00
|
|
|
|
from func_timeout import func_set_timeout, FunctionTimedOut
|
2022-12-08 14:28:46 +08:00
|
|
|
|
import datetime
|
2022-12-08 21:58:02 +08:00
|
|
|
|
import logging
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-11 17:17:33 +08:00
|
|
|
|
import pkg.qqbot.filter
|
|
|
|
|
|
2022-12-10 00:33:42 +08:00
|
|
|
|
help_text = config.help_message
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
|
|
|
|
inst = None
|
|
|
|
|
|
|
|
|
|
processing = []
|
|
|
|
|
|
2022-12-08 12:06:04 +08:00
|
|
|
|
|
2022-12-12 22:04:38 +08:00
|
|
|
|
# 并行运行
|
|
|
|
|
def go(func, args=()):
|
|
|
|
|
thread = threading.Thread(target=func, args=args, daemon=True)
|
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
|
|
|
2022-12-19 17:07:31 +08:00
|
|
|
|
# 检查消息是否符合泛响应匹配机制
|
|
|
|
|
def check_response_rule(text: str) -> (bool, str):
|
2022-12-19 17:15:17 +08:00
|
|
|
|
if not hasattr(config, 'response_rules'):
|
|
|
|
|
return False, ''
|
|
|
|
|
|
2022-12-19 17:07:31 +08:00
|
|
|
|
rules = config.response_rules
|
|
|
|
|
# 检查前缀匹配
|
2022-12-19 17:15:17 +08:00
|
|
|
|
if 'prefix' in rules:
|
|
|
|
|
for rule in rules['prefix']:
|
|
|
|
|
if text.startswith(rule):
|
|
|
|
|
return True, text.replace(rule, "", 1)
|
2022-12-19 17:07:31 +08:00
|
|
|
|
|
|
|
|
|
# 检查正则表达式匹配
|
2022-12-19 17:15:17 +08:00
|
|
|
|
if 'regexp' in rules:
|
|
|
|
|
for rule in rules['regexp']:
|
|
|
|
|
import re
|
|
|
|
|
match = re.match(rule, text)
|
|
|
|
|
if match:
|
2022-12-19 17:17:49 +08:00
|
|
|
|
return True, text
|
2022-12-19 17:07:31 +08:00
|
|
|
|
|
|
|
|
|
return False, ""
|
|
|
|
|
|
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 控制QQ消息输入输出的类
|
2022-12-07 23:37:52 +08:00
|
|
|
|
class QQBotManager:
|
|
|
|
|
timeout = 60
|
|
|
|
|
retry = 3
|
|
|
|
|
|
|
|
|
|
bot = None
|
|
|
|
|
|
2022-12-11 17:17:33 +08:00
|
|
|
|
reply_filter = None
|
|
|
|
|
|
2022-12-07 23:37:52 +08:00
|
|
|
|
def __init__(self, mirai_http_api_config: dict, timeout: int = 60, retry: int = 3):
|
|
|
|
|
|
|
|
|
|
self.timeout = timeout
|
|
|
|
|
self.retry = retry
|
|
|
|
|
|
2022-12-11 17:17:33 +08:00
|
|
|
|
if os.path.exists("sensitive.json") \
|
|
|
|
|
and config.sensitive_word_filter is not None \
|
|
|
|
|
and config.sensitive_word_filter:
|
|
|
|
|
with open("sensitive.json", "r", encoding="utf-8") as f:
|
|
|
|
|
self.reply_filter = pkg.qqbot.filter.ReplyFilter(json.load(f)['words'])
|
|
|
|
|
else:
|
|
|
|
|
self.reply_filter = pkg.qqbot.filter.ReplyFilter([])
|
|
|
|
|
|
2022-12-07 23:37:52 +08:00
|
|
|
|
bot = Mirai(
|
|
|
|
|
qq=mirai_http_api_config['qq'],
|
|
|
|
|
adapter=WebSocketAdapter(
|
|
|
|
|
verify_key=mirai_http_api_config['verifyKey'],
|
|
|
|
|
host=mirai_http_api_config['host'],
|
2022-12-08 12:06:04 +08:00
|
|
|
|
port=mirai_http_api_config['port']
|
2022-12-07 23:37:52 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@bot.on(FriendMessage)
|
|
|
|
|
async def on_friend_message(event: FriendMessage):
|
2022-12-12 22:04:38 +08:00
|
|
|
|
go(self.on_person_message, (event,))
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
|
|
|
|
@bot.on(StrangerMessage)
|
|
|
|
|
async def on_stranger_message(event: StrangerMessage):
|
2022-12-12 22:04:38 +08:00
|
|
|
|
go(self.on_person_message, (event,))
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
|
|
|
|
@bot.on(GroupMessage)
|
|
|
|
|
async def on_group_message(event: GroupMessage):
|
2022-12-12 22:04:38 +08:00
|
|
|
|
go(self.on_group_message, (event,))
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
|
|
|
|
self.bot = bot
|
|
|
|
|
|
|
|
|
|
global inst
|
|
|
|
|
inst = self
|
|
|
|
|
|
|
|
|
|
# 统一的消息处理函数
|
|
|
|
|
@func_set_timeout(timeout)
|
|
|
|
|
def process_message(self, launcher_type: str, launcher_id: int, text_message: str) -> str:
|
2022-12-12 22:04:38 +08:00
|
|
|
|
global processing
|
2022-12-07 23:37:52 +08:00
|
|
|
|
reply = ''
|
|
|
|
|
session_name = "{}_{}".format(launcher_type, launcher_id)
|
|
|
|
|
|
2022-12-12 22:04:38 +08:00
|
|
|
|
pkg.openai.session.get_session(session_name).acquire_response_lock()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if session_name in processing:
|
2022-12-13 13:36:16 +08:00
|
|
|
|
pkg.openai.session.get_session(session_name).release_response_lock()
|
2022-12-12 22:04:38 +08:00
|
|
|
|
return "[bot]err:正在处理中,请稍后再试"
|
|
|
|
|
|
|
|
|
|
processing.append(session_name)
|
|
|
|
|
|
2022-12-08 16:52:41 +08:00
|
|
|
|
try:
|
2022-12-12 22:04:38 +08:00
|
|
|
|
|
|
|
|
|
if text_message.startswith('!') or text_message.startswith("!"): # 指令
|
|
|
|
|
try:
|
2022-12-13 13:36:16 +08:00
|
|
|
|
logging.info(
|
|
|
|
|
"[{}]发起指令:{}".format(session_name, text_message[:min(20, len(text_message))] + (
|
|
|
|
|
"..." if len(text_message) > 20 else "")))
|
2022-12-12 22:04:38 +08:00
|
|
|
|
|
|
|
|
|
cmd = text_message[1:].strip().split(' ')[0]
|
|
|
|
|
|
|
|
|
|
params = text_message[1:].strip().split(' ')[1:]
|
|
|
|
|
if cmd == 'help':
|
|
|
|
|
reply = "[bot]" + help_text
|
|
|
|
|
elif cmd == 'reset':
|
|
|
|
|
pkg.openai.session.get_session(session_name).reset(explicit=True)
|
|
|
|
|
reply = "[bot]会话已重置"
|
|
|
|
|
elif cmd == 'last':
|
|
|
|
|
result = pkg.openai.session.get_session(session_name).last_session()
|
|
|
|
|
if result is None:
|
|
|
|
|
reply = "[bot]没有前一次的对话"
|
|
|
|
|
else:
|
|
|
|
|
datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime(
|
|
|
|
|
'%Y-%m-%d %H:%M:%S')
|
2022-12-13 13:36:16 +08:00
|
|
|
|
reply = "[bot]已切换到前一次的对话:\n创建时间:{}\n".format(
|
|
|
|
|
datetime_str) + result.prompt[
|
|
|
|
|
:min(100,
|
|
|
|
|
len(result.prompt))] + \
|
2022-12-12 22:04:38 +08:00
|
|
|
|
("..." if len(result.prompt) > 100 else "#END#")
|
|
|
|
|
elif cmd == 'next':
|
|
|
|
|
result = pkg.openai.session.get_session(session_name).next_session()
|
|
|
|
|
if result is None:
|
|
|
|
|
reply = "[bot]没有后一次的对话"
|
|
|
|
|
else:
|
|
|
|
|
datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime(
|
|
|
|
|
'%Y-%m-%d %H:%M:%S')
|
2022-12-13 13:36:16 +08:00
|
|
|
|
reply = "[bot]已切换到后一次的对话:\n创建时间:{}\n".format(
|
|
|
|
|
datetime_str) + result.prompt[
|
|
|
|
|
:min(100,
|
|
|
|
|
len(result.prompt))] + \
|
2022-12-12 22:04:38 +08:00
|
|
|
|
("..." if len(result.prompt) > 100 else "#END#")
|
|
|
|
|
elif cmd == 'prompt':
|
|
|
|
|
reply = "[bot]当前对话所有内容:\n" + pkg.openai.session.get_session(session_name).prompt
|
|
|
|
|
elif cmd == 'list':
|
|
|
|
|
pkg.openai.session.get_session(session_name).persistence()
|
|
|
|
|
page = 0
|
|
|
|
|
|
|
|
|
|
if len(params) > 0:
|
|
|
|
|
try:
|
|
|
|
|
page = int(params[0])
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
results = pkg.openai.session.get_session(session_name).list_history(page=page)
|
|
|
|
|
if len(results) == 0:
|
|
|
|
|
reply = "[bot]第{}页没有历史会话".format(page)
|
|
|
|
|
else:
|
|
|
|
|
reply = "[bot]历史会话 第{}页:\n".format(page)
|
|
|
|
|
current = -1
|
|
|
|
|
for i in range(len(results)):
|
|
|
|
|
# 时间(使用create_timestamp转换) 序号 部分内容
|
|
|
|
|
datetime_obj = datetime.datetime.fromtimestamp(results[i]['create_timestamp'])
|
|
|
|
|
reply += "#{} 创建:{} {}\n".format(i + page * 10,
|
|
|
|
|
datetime_obj.strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
|
results[i]['prompt'][
|
|
|
|
|
:min(20, len(results[i]['prompt']))])
|
|
|
|
|
if results[i]['create_timestamp'] == pkg.openai.session.get_session(
|
|
|
|
|
session_name).create_timestamp:
|
|
|
|
|
current = i + page * 10
|
|
|
|
|
|
|
|
|
|
reply += "\n以上信息倒序排列"
|
|
|
|
|
if current != -1:
|
|
|
|
|
reply += ",当前会话是 #{}\n".format(current)
|
|
|
|
|
else:
|
|
|
|
|
reply += ",当前处于全新会话或不在此页"
|
2022-12-15 18:50:15 +08:00
|
|
|
|
elif cmd == 'usage':
|
|
|
|
|
api_keys = pkg.openai.manager.get_inst().key_mgr.api_key
|
2022-12-15 18:55:40 +08:00
|
|
|
|
reply = "[bot]api-key使用情况:(阈值:{})\n\n".format(pkg.openai.manager.get_inst().key_mgr.api_key_usage_threshold)
|
2022-12-15 18:50:15 +08:00
|
|
|
|
|
|
|
|
|
using_key_name = ""
|
|
|
|
|
for api_key in api_keys:
|
2022-12-15 18:55:40 +08:00
|
|
|
|
reply += "{}:\n - {}字 {}%\n".format(api_key,
|
2022-12-15 18:50:15 +08:00
|
|
|
|
pkg.openai.manager.get_inst().key_mgr.get_usage(api_keys[api_key]),
|
2022-12-15 18:55:40 +08:00
|
|
|
|
round(pkg.openai.manager.get_inst().key_mgr.get_usage(api_keys[api_key]) / pkg.openai.manager.get_inst().key_mgr.api_key_usage_threshold * 100, 3))
|
2022-12-15 18:50:15 +08:00
|
|
|
|
if api_keys[api_key] == pkg.openai.manager.get_inst().key_mgr.using_key:
|
|
|
|
|
using_key_name = api_key
|
2022-12-15 18:55:40 +08:00
|
|
|
|
reply += "\n当前使用:{}".format(using_key_name)
|
2022-12-12 22:04:38 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
self.notify_admin("{}指令执行失败:{}".format(session_name, e))
|
|
|
|
|
logging.exception(e)
|
|
|
|
|
reply = "[bot]err:{}".format(e)
|
|
|
|
|
else: # 消息
|
|
|
|
|
logging.info("[{}]发送消息:{}".format(session_name, text_message[:min(20, len(text_message))] + (
|
|
|
|
|
"..." if len(text_message) > 20 else "")))
|
|
|
|
|
|
|
|
|
|
session = pkg.openai.session.get_session(session_name)
|
|
|
|
|
try:
|
2022-12-14 21:49:26 +08:00
|
|
|
|
prefix = "[GPT]" if hasattr(config, "show_prefix") and config.show_prefix else ""
|
|
|
|
|
reply = prefix + session.append(text_message)
|
2022-12-12 22:04:38 +08:00
|
|
|
|
except openai.error.APIConnectionError as e:
|
|
|
|
|
self.notify_admin("{}会话调用API失败:{}".format(session_name, e))
|
2022-12-15 21:23:36 +08:00
|
|
|
|
reply = "[bot]err:调用API失败,请重试或联系作者,或等待修复"
|
2022-12-12 22:04:38 +08:00
|
|
|
|
except openai.error.RateLimitError as e:
|
2022-12-15 23:48:51 +08:00
|
|
|
|
# 尝试切换api-key
|
|
|
|
|
pkg.openai.manager.get_inst().key_mgr.set_current_exceeded()
|
|
|
|
|
switched, name = pkg.openai.manager.get_inst().key_mgr.auto_switch()
|
|
|
|
|
|
|
|
|
|
if not switched:
|
|
|
|
|
self.notify_admin("API调用额度超限,请向OpenAI账户充值或在config.py中更换api_key")
|
|
|
|
|
reply = "[bot]err:API调用额度超额,请联系作者,或等待修复"
|
|
|
|
|
else:
|
|
|
|
|
self.notify_admin("API调用额度超限,已切换到{}".format(name))
|
|
|
|
|
reply = "[bot]err:API调用额度超额,已自动切换,请重新发送消息"
|
2022-12-12 22:04:38 +08:00
|
|
|
|
except openai.error.InvalidRequestError as e:
|
|
|
|
|
self.notify_admin("{}API调用参数错误:{}\n\n这可能是由于config.py中的prompt_submit_length参数或"
|
|
|
|
|
"completion_api_params中的max_tokens参数数值过大导致的,请尝试将其降低".format(
|
|
|
|
|
session_name, e))
|
|
|
|
|
reply = "[bot]err:API调用参数错误,请联系作者,或等待修复"
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.exception(e)
|
|
|
|
|
reply = "[bot]err:{}".format(e)
|
|
|
|
|
|
|
|
|
|
logging.info(
|
2022-12-13 13:36:16 +08:00
|
|
|
|
"回复[{}]消息:{}".format(session_name,
|
|
|
|
|
reply[:min(100, len(reply))] + ("..." if len(reply) > 100 else "")))
|
2022-12-12 22:04:38 +08:00
|
|
|
|
reply = self.reply_filter.process(reply)
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
processing.remove(session_name)
|
|
|
|
|
finally:
|
|
|
|
|
pkg.openai.session.get_session(session_name).release_response_lock()
|
|
|
|
|
|
2022-12-13 13:36:16 +08:00
|
|
|
|
return reply
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-12 22:04:38 +08:00
|
|
|
|
def send(self, event, msg):
|
|
|
|
|
asyncio.run(self.bot.send(event, msg))
|
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 私聊消息处理
|
2022-12-12 22:04:38 +08:00
|
|
|
|
def on_person_message(self, event: MessageEvent):
|
2022-12-08 21:58:02 +08:00
|
|
|
|
global processing
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
|
|
|
|
reply = ''
|
|
|
|
|
|
|
|
|
|
if event.sender.id == self.bot.qq:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
if Image in event.message_chain:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
2022-12-12 22:04:38 +08:00
|
|
|
|
# 超时则重试,重试超过次数则放弃
|
|
|
|
|
failed = 0
|
|
|
|
|
for i in range(self.retry):
|
|
|
|
|
try:
|
|
|
|
|
reply = self.process_message('person', event.sender.id, str(event.message_chain))
|
|
|
|
|
break
|
|
|
|
|
except FunctionTimedOut:
|
2022-12-13 16:04:51 +08:00
|
|
|
|
pkg.openai.session.get_session('person_{}'.format(event.sender.id)).release_response_lock()
|
2022-12-12 22:04:38 +08:00
|
|
|
|
failed += 1
|
|
|
|
|
continue
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-12 22:04:38 +08:00
|
|
|
|
if failed == self.retry:
|
2022-12-13 16:04:51 +08:00
|
|
|
|
pkg.openai.session.get_session('person_{}'.format(event.sender.id)).release_response_lock()
|
2022-12-13 00:45:56 +08:00
|
|
|
|
self.notify_admin("{} 请求超时".format("person_{}".format(event.sender.id)))
|
2022-12-12 22:04:38 +08:00
|
|
|
|
reply = "[bot]err:请求超时"
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
|
|
|
|
if reply != '':
|
2022-12-12 22:04:38 +08:00
|
|
|
|
return self.send(event, reply)
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 群消息处理
|
2022-12-12 22:04:38 +08:00
|
|
|
|
def on_group_message(self, event: GroupMessage):
|
2022-12-08 21:58:02 +08:00
|
|
|
|
global processing
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
|
|
|
|
reply = ''
|
|
|
|
|
|
2022-12-19 17:07:31 +08:00
|
|
|
|
def process(text = None) -> str:
|
|
|
|
|
replys = ""
|
2022-12-08 12:06:04 +08:00
|
|
|
|
event.message_chain.remove(At(self.bot.qq))
|
|
|
|
|
|
2022-12-07 23:37:52 +08:00
|
|
|
|
processing.append("group_{}".format(event.sender.id))
|
|
|
|
|
|
2022-12-12 22:04:38 +08:00
|
|
|
|
# 超时则重试,重试超过次数则放弃
|
|
|
|
|
failed = 0
|
|
|
|
|
for i in range(self.retry):
|
|
|
|
|
try:
|
2022-12-19 17:07:31 +08:00
|
|
|
|
replys = self.process_message('group', event.group.id,
|
|
|
|
|
str(event.message_chain).strip() if text is None else text)
|
2022-12-12 22:04:38 +08:00
|
|
|
|
break
|
|
|
|
|
except FunctionTimedOut:
|
|
|
|
|
failed += 1
|
|
|
|
|
continue
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-12 22:04:38 +08:00
|
|
|
|
if failed == self.retry:
|
2022-12-13 00:45:56 +08:00
|
|
|
|
self.notify_admin("{} 请求超时".format("group_{}".format(event.sender.id)))
|
2022-12-19 17:07:31 +08:00
|
|
|
|
replys = "[bot]err:请求超时"
|
|
|
|
|
|
|
|
|
|
return replys
|
|
|
|
|
|
|
|
|
|
if Image in event.message_chain:
|
|
|
|
|
pass
|
|
|
|
|
elif At(self.bot.qq) not in event.message_chain:
|
|
|
|
|
check, result = check_response_rule(str(event.message_chain).strip())
|
|
|
|
|
|
|
|
|
|
if check:
|
|
|
|
|
reply = process(result)
|
|
|
|
|
else:
|
|
|
|
|
# 直接调用
|
|
|
|
|
reply = process()
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
|
|
|
|
if reply != '':
|
2022-12-12 22:04:38 +08:00
|
|
|
|
return self.send(event, reply)
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
2022-12-11 16:10:12 +08:00
|
|
|
|
# 通知系统管理员
|
2022-12-10 16:40:05 +08:00
|
|
|
|
def notify_admin(self, message: str):
|
2022-12-12 22:11:32 +08:00
|
|
|
|
if hasattr(config, "admin_qq") and config.admin_qq != 0:
|
2022-12-13 13:36:16 +08:00
|
|
|
|
logging.info("通知管理员:{}".format(message))
|
2022-12-10 16:40:05 +08:00
|
|
|
|
send_task = self.bot.send_friend_message(config.admin_qq, "[bot]{}".format(message))
|
|
|
|
|
threading.Thread(target=asyncio.run, args=(send_task,)).start()
|
|
|
|
|
|
2022-12-07 23:37:52 +08:00
|
|
|
|
|
|
|
|
|
def get_inst() -> QQBotManager:
|
|
|
|
|
global inst
|
|
|
|
|
return inst
|