perf: 完善key管理

This commit is contained in:
Rock Chin
2022-12-23 12:07:56 +08:00
parent 4d17c8362e
commit 8a8cfdf192
2 changed files with 4 additions and 3 deletions

View File

@@ -76,7 +76,7 @@ class KeysManager:
# 经测算得出的理论与实际的偏差比例
salt_rate = 0.93
self.usage[md5] += round(int((len(new_content.encode('utf-8')) - len(new_content)) / 2 + len(new_content))*salt_rate)
self.usage[md5] += ( (len(new_content.encode('utf-8')) - len(new_content)) / 2 + len(new_content) )*salt_rate
self.usage[md5] = int(self.usage[md5])

View File

@@ -235,15 +235,16 @@ class QQBotManager:
reply = "[bot]err:调用API失败请重试或联系作者或等待修复"
except openai.error.RateLimitError as e:
# 尝试切换api-key
current_tokens_amt = pkg.openai.manager.get_inst().key_mgr.get_usage(pkg.openai.manager.get_inst().key_mgr.get_using_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")
self.notify_admin("API调用额度超限({}),请向OpenAI账户充值或在config.py中更换api_key".format(current_tokens_amt))
reply = "[bot]err:API调用额度超额请联系作者或等待修复"
else:
openai.api_key = pkg.openai.manager.get_inst().key_mgr.get_using_key()
self.notify_admin("API调用额度超限,已切换到{}".format(name))
self.notify_admin("API调用额度超限({}),已切换到{}".format(current_tokens_amt, name))
reply = "[bot]err:API调用额度超额已自动切换请重新发送消息"
except openai.error.InvalidRequestError as e:
self.notify_admin("{}API调用参数错误:{}\n\n这可能是由于config.py中的prompt_submit_length参数或"