feat: 适配completion和chat_completions

This commit is contained in:
RockChinQ
2023-11-10 11:31:14 +08:00
parent c7a9988033
commit 2b852853f3
6 changed files with 74 additions and 85 deletions

View File

@@ -8,6 +8,8 @@ import openai
class RequestBase:
client: openai.Client
req_func: callable
def __init__(self, *args, **kwargs):
@@ -17,41 +19,17 @@ class RequestBase:
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()
self.client.api_key = context.get_openai_manager().key_mgr.get_using_key()
def _req(self, **kwargs):
"""处理代理问题"""
import config
ret: dict = {}
exception: Exception = None
ret = self.req_func(**kwargs)
logging.debug("接口请求返回:%s", str(ret))
async def awrapper(**kwargs):
nonlocal ret, exception
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
loop = asyncio.new_event_loop()
thr = threading.Thread(
target=loop.run_until_complete,
args=(awrapper(**kwargs),)
)
thr.start()
thr.join()
if exception is not None:
raise exception
if config.switch_strategy == 'active':
self._next_key()
return ret