feat: 异步重写

perf: 优化网页搜索回答规范
This commit is contained in:
Soulter
2024-03-03 18:54:50 +08:00
parent 53ef3bbf4f
commit 1cd1c8ea0d
12 changed files with 205 additions and 222 deletions
+28 -24
View File
@@ -1,17 +1,19 @@
import json
from util import general_utils as gu
import os
import requests
from model.provider.provider import Provider
import inspect
import aiohttp
import json
import util.plugin_util as putil
from util.cmd_config import CmdConfig as cc
from util.general_utils import Logger
import util.updator
from nakuru.entities.components import (
Plain,
Image
)
from util import general_utils as gu
from model.provider.provider import Provider
from util.cmd_config import CmdConfig as cc
from util.general_utils import Logger
from cores.qqbot.global_object import GlobalObject, AstrMessageEvent
from cores.qqbot.global_object import CommandResult
@@ -25,7 +27,7 @@ class Command:
self.global_object = global_object
self.logger: Logger = global_object.logger
def check_command(self,
async def check_command(self,
message,
session_id: str,
role,
@@ -51,7 +53,10 @@ class Command:
if "type" in v["info"] and v["info"]["plugin_type"] == "platform":
continue
try:
result = v["clsobj"].run(ame)
if inspect.iscoroutinefunction(v["clsobj"].run):
result = await v["clsobj"].run(ame)
else:
result = v["clsobj"].run(ame)
if isinstance(result, CommandResult):
hit = result.hit
res = result._result_tuple()
@@ -65,13 +70,16 @@ class Command:
except TypeError as e:
# 参数不匹配,尝试使用旧的参数方案
try:
hit, res = v["clsobj"].run(message, role, platform, message_obj, self.global_object.platform_qq)
if inspect.iscoroutinefunction(v["clsobj"].run):
hit, res = await v["clsobj"].run(message, role, platform, message_obj, self.global_object.platform_qq)
else:
hit, res = v["clsobj"].run(message, role, platform, message_obj, self.global_object.platform_qq)
if hit:
return True, res
except BaseException as e:
self.logger.log(f"{k}插件异常,原因: {str(e)}\n已安装插件: {cached_plugins.keys}\n如果你没有相关装插件的想法, 请直接忽略此报错, 不影响其他功能的运行。", level=gu.LEVEL_WARNING)
self.logger.log(f"{k} 插件异常,原因: {str(e)}\n如果你没有相关装插件的想法, 请直接忽略此报错, 不影响其他功能的运行。", level=gu.LEVEL_WARNING)
except BaseException as e:
self.logger.log(f"{k} 插件异常,原因: {str(e)}\n已安装插件: {cached_plugins.keys}\n如果你没有相关装插件的想法, 请直接忽略此报错, 不影响其他功能的运行。", level=gu.LEVEL_WARNING)
self.logger.log(f"{k} 插件异常,原因: {str(e)}\n如果你没有相关装插件的想法, 请直接忽略此报错, 不影响其他功能的运行。", level=gu.LEVEL_WARNING)
if self.command_start_with(message, "nick"):
return True, self.set_nick(message, platform, role)
@@ -79,18 +87,13 @@ class Command:
return True, self.plugin_oper(message, role, cached_plugins, platform)
if self.command_start_with(message, "myid") or self.command_start_with(message, "!myid"):
return True, self.get_my_id(message_obj, platform)
if self.command_start_with(message, "nconf") or self.command_start_with(message, "newconf"):
return True, self.get_new_conf(message, role)
if self.command_start_with(message, "web"): # 网页搜索
return True, self.web_search(message)
if self.command_start_with(message, "ip"):
ip = requests.get("https://myip.ipip.net", timeout=5).text
return True, f"机器人 IP 信息:{ip}", "ip"
if not self.provider and self.command_start_with(message, "help"):
return True, self.help()
return True, await self.help()
return False, None
def web_search(self, message):
l = message.split(' ')
if len(l) == 1:
@@ -202,10 +205,11 @@ class Command:
"/revgpt": "切换到网页版ChatGPT",
}
def help_messager(self, commands: dict, platform: str, cached_plugins: dict = None):
async def help_messager(self, commands: dict, platform: str, cached_plugins: dict = None):
try:
resp = requests.get("https://soulter.top/channelbot/notice.json").text
notice = json.loads(resp)["notice"]
async with aiohttp.ClientSession() as session:
async with session.get("https://soulter.top/channelbot/notice.json") as resp:
notice = (await resp.json())["notice"]
except BaseException as e:
notice = ""
msg = "# Help Center\n## 指令列表\n"
@@ -279,9 +283,9 @@ class Command:
def key(self):
return False
def help(self):
return True, self.help_messager(self.general_commands(), self.platform, self.global_object.cached_plugins), "help"
async def help(self):
ret = await self.help_messager(self.general_commands(), self.platform, self.global_object.cached_plugins)
return True, ret, "help"
def status(self):
return False
+11 -11
View File
@@ -11,7 +11,7 @@ class CommandOpenAIOfficial(Command):
self.personality_str = ""
super().__init__(provider, global_object)
def check_command(self,
async def check_command(self,
message: str,
session_id: str,
role: str,
@@ -20,7 +20,7 @@ class CommandOpenAIOfficial(Command):
self.platform = platform
# 检查基础指令
hit, res = super().check_command(
hit, res = await super().check_command(
message,
session_id,
role,
@@ -32,7 +32,7 @@ class CommandOpenAIOfficial(Command):
if hit:
return True, res
if self.command_start_with(message, "reset", "重置"):
return True, self.reset(session_id, message)
return True, await self.reset(session_id, message)
elif self.command_start_with(message, "his", "历史"):
return True, self.his(message, session_id)
elif self.command_start_with(message, "token"):
@@ -42,7 +42,7 @@ class CommandOpenAIOfficial(Command):
elif self.command_start_with(message, "status"):
return True, self.status()
elif self.command_start_with(message, "help", "帮助"):
return True, self.help()
return True, await self.help()
elif self.command_start_with(message, "unset"):
return True, self.unset(session_id)
elif self.command_start_with(message, "set"):
@@ -54,11 +54,11 @@ class CommandOpenAIOfficial(Command):
elif self.command_start_with(message, "key"):
return True, self.key(message)
elif self.command_start_with(message, "switch"):
return True, self.switch(message)
return True, await self.switch(message)
return False, None
def help(self):
async def help(self):
commands = super().general_commands()
commands[''] = '画画'
commands['key'] = '添加OpenAI key'
@@ -66,15 +66,15 @@ class CommandOpenAIOfficial(Command):
commands['gpt'] = '查看gpt配置信息'
commands['status'] = '查看key使用状态'
commands['token'] = '查看本轮会话token'
return True, super().help_messager(commands, self.platform, self.global_object.cached_plugins), "help"
return True, await super().help_messager(commands, self.platform, self.global_object.cached_plugins), "help"
def reset(self, session_id: str, message: str = "reset"):
async def reset(self, session_id: str, message: str = "reset"):
if self.provider is None:
return False, "未启用 OpenAI 官方 API", "reset"
l = message.split(" ")
if len(l) == 1:
self.provider.forget(session_id)
await self.provider.forget(session_id)
return True, "重置成功", "reset"
if len(l) == 2 and l[1] == "p":
self.provider.forget(session_id)
@@ -146,7 +146,7 @@ class CommandOpenAIOfficial(Command):
else:
return True, "该Key被验证为无效。也许是输入错误了,或者重试。", "key"
def switch(self, message: str):
async def switch(self, message: str):
'''
切换账号
'''
@@ -168,7 +168,7 @@ class CommandOpenAIOfficial(Command):
else:
try:
new_key = list(key_stat.keys())[index-1]
ret = self.provider.check_key(new_key)
ret = await self.provider.check_key(new_key)
self.provider.set_key(new_key)
except BaseException as e:
return True, "账号切换失败,原因: " + str(e), "switch"
+5 -5
View File
@@ -11,14 +11,14 @@ class CommandRevChatGPT(Command):
self.personality_str = ""
super().__init__(provider, global_object)
def check_command(self,
async def check_command(self,
message: str,
session_id: str,
role: str,
platform: str,
message_obj):
self.platform = platform
hit, res = super().check_command(
hit, res = await super().check_command(
message,
session_id,
role,
@@ -29,7 +29,7 @@ class CommandRevChatGPT(Command):
if hit:
return True, res
if self.command_start_with(message, "help", "帮助"):
return True, self.help()
return True, await self.help()
elif self.command_start_with(message, "reset"):
return True, self.reset(session_id, message)
elif self.command_start_with(message, "update"):
@@ -127,7 +127,7 @@ class CommandRevChatGPT(Command):
else:
return True, "参数过多。", "switch"
def help(self):
async def help(self):
commands = super().general_commands()
commands['set'] = '设置人格'
return True, super().help_messager(commands, self.platform, self.global_object.cached_plugins), "help"
return True, await super().help_messager(commands, self.platform, self.global_object.cached_plugins), "help"