Merge branch 'master' into program-level-restriction

This commit is contained in:
Rock Chin 2023-01-09 12:26:51 +08:00 committed by GitHub
commit 77bf1c7d8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 11 deletions

View File

@ -97,7 +97,7 @@ quote_origin = True
include_image_description = True include_image_description = True
# 消息处理的超时时间,单位为秒 # 消息处理的超时时间,单位为秒
process_message_timeout = 15 process_message_timeout = 30
# 机器人的配置 # 机器人的配置
# user_name: 管理员(主人)的名字 # user_name: 管理员(主人)的名字

View File

@ -25,7 +25,7 @@ class DataGatherer:
def __init__(self): def __init__(self):
self.load_from_db() self.load_from_db()
try: try:
self.version_str = pkg.utils.updater.get_commit_id_and_time_and_msg()[:50 if len(pkg.utils.updater.get_commit_id_and_time_and_msg()) > 50 else len(pkg.utils.updater.get_commit_id_and_time_and_msg())] self.version_str = pkg.utils.updater.get_commit_id_and_time_and_msg()[:40 if len(pkg.utils.updater.get_commit_id_and_time_and_msg()) > 40 else len(pkg.utils.updater.get_commit_id_and_time_and_msg())]
except: except:
pass pass
@ -34,6 +34,7 @@ class DataGatherer:
config = pkg.utils.context.get_config() config = pkg.utils.context.get_config()
if hasattr(config, "report_usage") and not config.report_usage: if hasattr(config, "report_usage") and not config.report_usage:
return return
# print("http://rockchin.top:18989/usage?service_name=qchatgpt.{}&version={}&count={}".format(subservice_name, self.version_str, count))
res = requests.get("http://rockchin.top:18989/usage?service_name=qchatgpt.{}&version={}&count={}".format(subservice_name, self.version_str, count)) res = requests.get("http://rockchin.top:18989/usage?service_name=qchatgpt.{}&version={}&count={}".format(subservice_name, self.version_str, count))
if res.status_code != 200 or res.text != "ok": if res.status_code != 200 or res.text != "ok":
logging.warning("report to server failed, status_code: {}, text: {}".format(res.status_code, res.text)) logging.warning("report to server failed, status_code: {}, text: {}".format(res.status_code, res.text))

View File

@ -172,7 +172,10 @@ class QQBotManager:
event.sender.id) event.sender.id)
break break
except FunctionTimedOut: except FunctionTimedOut:
logging.warning("person_{}: 超时".format(event.sender.id))
pkg.openai.session.get_session('person_{}'.format(event.sender.id)).release_response_lock() pkg.openai.session.get_session('person_{}'.format(event.sender.id)).release_response_lock()
if "person_{}".format(event.sender.id) in pkg.qqbot.process.processing:
pkg.qqbot.process.processing.remove('person_{}'.format(event.sender.id))
failed += 1 failed += 1
continue continue
@ -204,6 +207,7 @@ class QQBotManager:
event.sender.id) event.sender.id)
break break
except FunctionTimedOut: except FunctionTimedOut:
logging.warning("group_{}: 超时,重试中".format(event.group.id))
pkg.openai.session.get_session('group_{}'.format(event.group.id)).release_response_lock() pkg.openai.session.get_session('group_{}'.format(event.group.id)).release_response_lock()
failed += 1 failed += 1
continue continue

View File

@ -8,7 +8,7 @@ from func_timeout import func_set_timeout
import logging import logging
import openai import openai
from mirai import Image, MessageChain from mirai import Image, MessageChain, Plain
# 这里不使用动态引入config # 这里不使用动态引入config
# 因为在这里动态引入会卡死程序 # 因为在这里动态引入会卡死程序
@ -123,7 +123,7 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
try: try:
if session_name in processing: if session_name in processing:
pkg.openai.session.get_session(session_name).release_response_lock() pkg.openai.session.get_session(session_name).release_response_lock()
return ["[bot]err:正在处理中,请稍后再试"] return MessageChain([Plain("[bot]err:正在处理中,请稍后再试")])
processing.append(session_name) processing.append(session_name)
@ -317,19 +317,19 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
reply = ["[bot]err:调用API失败请重试或联系作者或等待修复"] reply = ["[bot]err:调用API失败请重试或联系作者或等待修复"]
except openai.error.RateLimitError as e: except openai.error.RateLimitError as e:
# 尝试切换api-key # 尝试切换api-key
current_tokens_amt = pkg.utils.context.get_openai_manager().key_mgr.get_fee( current_key_name = pkg.utils.context.get_openai_manager().key_mgr.get_key_name(
pkg.utils.context.get_openai_manager().key_mgr.get_using_key()) pkg.utils.context.get_openai_manager().key_mgr.using_key
)
pkg.utils.context.get_openai_manager().key_mgr.set_current_exceeded() pkg.utils.context.get_openai_manager().key_mgr.set_current_exceeded()
switched, name = pkg.utils.context.get_openai_manager().key_mgr.auto_switch() switched, name = pkg.utils.context.get_openai_manager().key_mgr.auto_switch()
if not switched: if not switched:
mgr.notify_admin( mgr.notify_admin("api-key调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format(
"API调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format( current_key_name))
current_tokens_amt))
reply = ["[bot]err:API调用额度超额请联系作者或等待修复"] reply = ["[bot]err:API调用额度超额请联系作者或等待修复"]
else: else:
openai.api_key = pkg.utils.context.get_openai_manager().key_mgr.get_using_key() openai.api_key = pkg.utils.context.get_openai_manager().key_mgr.get_using_key()
mgr.notify_admin("API调用额度超限({}),接口报错,已切换到{}".format(current_tokens_amt, name)) mgr.notify_admin("api-key调用额度超限({}),接口报错,已切换到{}".format(current_key_name, name))
reply = ["[bot]err:API调用额度超额已自动切换请重新发送消息"] reply = ["[bot]err:API调用额度超额已自动切换请重新发送消息"]
continue continue
except openai.error.InvalidRequestError as e: except openai.error.InvalidRequestError as e:

View File

@ -52,7 +52,7 @@ def get_current_version_info() -> str:
version_str += "提交编号: "+str(entry.commit.id)[2:9] + "\n" version_str += "提交编号: "+str(entry.commit.id)[2:9] + "\n"
tz = datetime.timezone(datetime.timedelta(hours=entry.commit.commit_timezone // 3600)) tz = datetime.timezone(datetime.timedelta(hours=entry.commit.commit_timezone // 3600))
dt = datetime.datetime.fromtimestamp(entry.commit.commit_time, tz) dt = datetime.datetime.fromtimestamp(entry.commit.commit_time, tz)
version_str += "时间: "+dt.strftime('%Y-%m-%d %H:%M:%S') + "\n" version_str += "时间: "+dt.strftime('%m-%d %H:%M:%S') + "\n"
version_str += "说明: "+str(entry.commit.message, encoding="utf-8").strip() + "\n" version_str += "说明: "+str(entry.commit.message, encoding="utf-8").strip() + "\n"
version_str += "提交作者: '" + str(entry.commit.author)[2:-1] + "'" version_str += "提交作者: '" + str(entry.commit.author)[2:-1] + "'"
break break