mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-24 21:36:06 +00:00
feat: 新增使用量统计功能,与key切换脱钩
This commit is contained in:
@@ -28,6 +28,9 @@ class KeysManager:
|
||||
def get_using_key(self):
|
||||
return self.using_key
|
||||
|
||||
def get_using_key_md5(self):
|
||||
return hashlib.md5(self.using_key.encode('utf-8')).hexdigest()
|
||||
|
||||
def __init__(self, api_key):
|
||||
# if hasattr(config, 'api_key_usage_threshold'):
|
||||
# self.api_key_usage_threshold = config.api_key_usage_threshold
|
||||
|
||||
+10
-1
@@ -7,13 +7,16 @@ import config
|
||||
import pkg.openai.keymgr
|
||||
import pkg.openai.pricing as pricing
|
||||
import pkg.utils.context
|
||||
import pkg.audit.gatherer
|
||||
|
||||
|
||||
# 为其他模块提供与OpenAI交互的接口
|
||||
class OpenAIInteract:
|
||||
api_params = {}
|
||||
|
||||
key_mgr = None
|
||||
key_mgr: pkg.openai.keymgr.KeysManager = None
|
||||
|
||||
audit_mgr: pkg.audit.gatherer.DataGatherer = None
|
||||
|
||||
default_image_api_params = {
|
||||
"size": "256x256",
|
||||
@@ -23,6 +26,7 @@ class OpenAIInteract:
|
||||
# self.api_key = api_key
|
||||
|
||||
self.key_mgr = pkg.openai.keymgr.KeysManager(api_key)
|
||||
self.audit_mgr = pkg.audit.gatherer.DataGatherer()
|
||||
|
||||
openai.api_key = self.key_mgr.get_using_key()
|
||||
|
||||
@@ -37,6 +41,9 @@ class OpenAIInteract:
|
||||
**config.completion_api_params
|
||||
)
|
||||
|
||||
self.audit_mgr.report_text_model_usage(config.completion_api_params['model'],
|
||||
prompt + response['choices'][0]['text'])
|
||||
|
||||
switched = self.key_mgr.report_fee(pricing.language_base_price(config.completion_api_params['model'],
|
||||
prompt + response['choices'][0]['text']))
|
||||
if switched:
|
||||
@@ -54,6 +61,8 @@ class OpenAIInteract:
|
||||
**params
|
||||
)
|
||||
|
||||
self.audit_mgr.report_image_model_usage(params['size'])
|
||||
|
||||
switched = self.key_mgr.report_fee(pricing.image_price(params['size']))
|
||||
|
||||
if switched:
|
||||
|
||||
Reference in New Issue
Block a user