mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-18 19:44:21 +00:00
feat: 支持多个api-key并支持设置文字量阈值对其进行自动切换
This commit is contained in:
+12
-3
@@ -2,19 +2,24 @@ import openai
|
||||
|
||||
import config
|
||||
|
||||
import pkg.openai.keymgr
|
||||
|
||||
inst = None
|
||||
|
||||
|
||||
# 为其他模块提供与OpenAI交互的接口
|
||||
class OpenAIInteract:
|
||||
api_key = ''
|
||||
api_params = {}
|
||||
|
||||
key_mgr = None
|
||||
|
||||
def __init__(self, api_key: str, api_params: dict):
|
||||
self.api_key = api_key
|
||||
# self.api_key = api_key
|
||||
self.api_params = api_params
|
||||
|
||||
openai.api_key = self.api_key
|
||||
self.key_mgr = pkg.openai.keymgr.KeysManager(api_key)
|
||||
|
||||
openai.api_key = self.key_mgr.get_using_key()
|
||||
|
||||
global inst
|
||||
inst = self
|
||||
@@ -27,6 +32,10 @@ class OpenAIInteract:
|
||||
timeout=config.process_message_timeout,
|
||||
**self.api_params
|
||||
)
|
||||
switched = self.key_mgr.report_usage(prompt + response['choices'][0]['text'])
|
||||
if switched:
|
||||
openai.api_key = self.key_mgr.get_using_key()
|
||||
|
||||
return response
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user