feat(openai): 支持apikey主动切换策略

This commit is contained in:
RockChinQ
2023-08-04 17:10:03 +08:00
parent a345043c30
commit c8275fcfbf
3 changed files with 36 additions and 4 deletions

View File

@@ -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号用于接收报错等通知及执行管理员级别指令

View File

@@ -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

View File

@@ -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