From 382d37d479af493c439d4b1c92efd399e5185517 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Fri, 4 Aug 2023 15:21:31 +0800 Subject: [PATCH 1/3] =?UTF-8?q?chore:=20=E6=B7=BB=E5=8A=A0key=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E7=AD=96=E7=95=A5=E9=85=8D=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 5 +++++ pkg/qqbot/cmds/funcs/func.py | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/config-template.py b/config-template.py index 59e83a0e..6207fe29 100644 --- a/config-template.py +++ b/config-template.py @@ -70,6 +70,11 @@ openai_config = { "reverse_proxy": None } +# api-key切换策略 +# active:每次请求时都会切换api-key +# passive:当api-key超额时才会切换api-key +switch_strategy = "active" + # [必需] 管理员QQ号,用于接收报错等通知及执行管理员级别指令 # 支持多个管理员,可以使用list形式设置,例如: # admin_qq = [12345678, 87654321] diff --git a/pkg/qqbot/cmds/funcs/func.py b/pkg/qqbot/cmds/funcs/func.py index f33efd15..93b31844 100644 --- a/pkg/qqbot/cmds/funcs/func.py +++ b/pkg/qqbot/cmds/funcs/func.py @@ -1,6 +1,8 @@ from ..aamgr import AbstractCommandNode, Context import logging +import json + @AbstractCommandNode.register( parent=None, @@ -19,6 +21,8 @@ class FuncCommand(AbstractCommandNode): reply_str = "当前已加载的内容函数:\n\n" + logging.debug("host.__callable_functions__: {}".format(json.dumps(host.__callable_functions__, indent=4))) + index = 1 for func in host.__callable_functions__: reply_str += "{}. {}{}:\n{}\n\n".format(index, ("(已禁用) " if not func['enabled'] else ""), func['name'], func['description']) From a345043c3026b584cd66d28123f5ac82547aa676 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Fri, 4 Aug 2023 07:21:52 +0000 Subject: [PATCH 2/3] Update override-all.json --- override-all.json | 1 + 1 file changed, 1 insertion(+) diff --git a/override-all.json b/override-all.json index 6fed8cff..e46e1c90 100644 --- a/override-all.json +++ b/override-all.json @@ -21,6 +21,7 @@ "http_proxy": null, "reverse_proxy": null }, + "switch_strategy": "active", "admin_qq": 0, "default_prompt": { "default": "如果我之后想获取帮助,请你说“输入!help获取帮助”" From c8275fcfbf4577f33ef81795522a72ce3649db04 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Fri, 4 Aug 2023 17:10:03 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat(openai):=20=E6=94=AF=E6=8C=81apikey?= =?UTF-8?q?=E4=B8=BB=E5=8A=A8=E5=88=87=E6=8D=A2=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 2 +- pkg/openai/api/model.py | 11 +++++++++++ pkg/openai/keymgr.py | 27 ++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/config-template.py b/config-template.py index 6207fe29..987e63da 100644 --- a/config-template.py +++ b/config-template.py @@ -72,7 +72,7 @@ openai_config = { # api-key切换策略 # active:每次请求时都会切换api-key -# passive:当api-key超额时才会切换api-key +# passive:仅当api-key超额时才会切换api-key switch_strategy = "active" # [必需] 管理员QQ号,用于接收报错等通知及执行管理员级别指令 diff --git a/pkg/openai/api/model.py b/pkg/openai/api/model.py index 2edacc9b..58f3e3ff 100644 --- a/pkg/openai/api/model.py +++ b/pkg/openai/api/model.py @@ -13,8 +13,15 @@ class RequestBase: def __init__(self, *args, **kwargs): raise NotImplementedError + def _next_key(self): + import pkg.utils.context as context + switched, name = context.get_openai_manager().key_mgr.auto_switch() + logging.debug("切换api-key: switched={}, name={}".format(switched, name)) + openai.api_key = context.get_openai_manager().key_mgr.get_using_key() + def _req(self, **kwargs): """处理代理问题""" + import config ret: dict = {} exception: Exception = None @@ -25,6 +32,10 @@ class RequestBase: try: ret = await self.req_func(**kwargs) logging.debug("接口请求返回:%s", str(ret)) + + if config.switch_strategy == 'active': + self._next_key() + return ret except Exception as e: exception = e diff --git a/pkg/openai/keymgr.py b/pkg/openai/keymgr.py index 4428b0d1..bed44330 100644 --- a/pkg/openai/keymgr.py +++ b/pkg/openai/keymgr.py @@ -54,7 +54,24 @@ class KeysManager: 是否切换成功, 切换后的api-key的别名 """ + index = 0 + for key_name in self.api_key: + if self.api_key[key_name] == self.using_key: + break + + index += 1 + + # 从当前key开始向后轮询 + start_index = index + index += 1 + if index >= len(self.api_key): + index = 0 + + while index != start_index: + + key_name = list(self.api_key.keys())[index] + if self.api_key[key_name] not in self.exceeded: self.using_key = self.api_key[key_name] @@ -69,10 +86,14 @@ class KeysManager: return True, key_name - self.using_key = list(self.api_key.values())[0] - logging.info("使用api-key:" + list(self.api_key.keys())[0]) + index += 1 + if index >= len(self.api_key): + index = 0 - return False, "" + self.using_key = list(self.api_key.values())[start_index] + logging.debug("使用api-key:" + list(self.api_key.keys())[start_index]) + + return False, list(self.api_key.keys())[start_index] def add(self, key_name, key): self.api_key[key_name] = key