186 lines
4.5 KiB
Python
186 lines
4.5 KiB
Python
from appPublic.jsonConfig import getConfig
|
|
from appPublic.oauth_client import OAuthClient
|
|
from appPublic.uniqueID import getID
|
|
from sqlor.dbpools import DBPools
|
|
|
|
from ahserver.globalEnv import get_definition
|
|
from .api import api_desc
|
|
|
|
class MinimaxAdm:
|
|
def __init__(self, dbname, url_base='https://api.minimax.chat'):
|
|
self.api_desc = api_desc
|
|
self.name = api_desc.name
|
|
self.dbname = dbname
|
|
self.url_base = url_base
|
|
self.api = OAuthClient(DictObject(**self.api_desc))
|
|
|
|
async def write_usage(self, userid, model, usage):
|
|
ns = {
|
|
'id':getID(),
|
|
'userid':userid,
|
|
'apiname':self.name,
|
|
'model':model,
|
|
'u_date':curdate(),
|
|
'u_time':curtime(),
|
|
'input_usage':usage.input_token,
|
|
'output_usage':usage.output_token,
|
|
'total_usage':usage.total_token
|
|
}
|
|
db = DBPools()
|
|
async with db.sqlorContext(self.dbname) as sor:
|
|
await sor.C('token_usage', ns)
|
|
|
|
async def get_account(self, userid):
|
|
db = DBPools()
|
|
async with db.sqlorContext(self.dbname) as sor:
|
|
ns = {'userid':userid}
|
|
r = await sor.sqlExe('select * from minimax_acc where userid=${userid}$', ns)
|
|
if len(r) > 0:
|
|
return r[0]
|
|
return None
|
|
asyncdef get_username(self, userid):
|
|
db = DBPools()
|
|
async with db.sqlorContext(self.dbname) as sor:
|
|
ns = {'userid':userid}
|
|
r = await sor.sqlExe('select * from users where userid=${userid}$', ns)
|
|
if len(r) > 0:
|
|
return r[0].username
|
|
return None
|
|
|
|
async def new_account(self, ns):
|
|
db = DBPools()
|
|
async with db.sqlContext(self.dbname) as sor:
|
|
await sor.C('minimax_acc', ns)
|
|
return True
|
|
return False
|
|
|
|
async def open_accoount(self, userid):
|
|
acc = await self.get_account(userid)
|
|
if acc:
|
|
return acc
|
|
username = self.get_username(userid)
|
|
if not username:
|
|
return False
|
|
resp = await self.api(self.url_base,
|
|
'sub_account',
|
|
{'account_name':username})
|
|
if resp.status != 'ok':
|
|
print(resp)
|
|
return False
|
|
d = resp.data
|
|
ns = {
|
|
'id':getID(),
|
|
'userid':userid,
|
|
'acc_id:d.subject_id,
|
|
'acc_name':d.account,
|
|
'password':d.password,
|
|
'apikey':d.token,
|
|
'groupid':d.group_id,
|
|
'acc_status':0
|
|
}
|
|
r = await self.new_account(self, ns)
|
|
return r
|
|
|
|
async def delete_account(self, ns):
|
|
db = DBPools()
|
|
async with db.sqlContext(self.dbname) as sor:
|
|
await sor.D('minimax_acc', ns)
|
|
return True
|
|
return False
|
|
|
|
async def update_acc(self, ns):
|
|
db = DBPools()
|
|
async with db.sqlorContext(self.dbname) as sor:
|
|
r = await sor.U('minimax_acc', ns)
|
|
return r
|
|
return False
|
|
|
|
async def delete_acc(self, userid):
|
|
acc = await self.get_account(userid)
|
|
if acc is None:
|
|
return False
|
|
if acc.acc_status == 0:
|
|
print(f'{acc_name} minimax acc is using, please disable it first')
|
|
return False
|
|
|
|
resp = await self.api(self.url_base,
|
|
'sub_delete',
|
|
{'sub_account_id':acc.acc_id})
|
|
if resp.status == 'ok':
|
|
r = await self.delete_account(acc)
|
|
return r
|
|
return False
|
|
|
|
async def acc_enable(self, userid):
|
|
return await self._acc_change_status(self, user, 0)
|
|
|
|
async def acc_disable(self, userid):
|
|
return await self._acc_change_status(self, user, 2)
|
|
|
|
async def _acc_change_status(self, userid, status):
|
|
acc = await self.get_account(userid)
|
|
if acc is None:
|
|
return False
|
|
if acc.acc_status == status:
|
|
return True
|
|
resp = await self.api(self.url_base, 'ststus', {
|
|
'sub_account_id':acc.acc_id,
|
|
'status':status
|
|
})
|
|
if resp.status != 'ok':
|
|
return False
|
|
ns = {
|
|
'id':acc.id,
|
|
'status':status
|
|
}
|
|
return await self.update_acc(ns):
|
|
|
|
async def reset_apikey(self, userid):
|
|
acc = await self.get_account(userid)
|
|
if acc is None:
|
|
return False
|
|
resp = await self.api(self.url_base, 'token', {
|
|
'sub_account_id':acc.acc_id
|
|
})
|
|
if resp.status != 'ok':
|
|
return False
|
|
ns = {
|
|
'id':acc.id,
|
|
'apikey':resp.apikey
|
|
}
|
|
return await self.update_acc(ns):
|
|
|
|
async def get_daily_bill(self, dat):
|
|
acc = await self.get_account(userid)
|
|
if acc is None:
|
|
return False
|
|
d = ''.join(dat.split('-'))
|
|
resp = await self.api(self.url_base, 'bill', {
|
|
'start_date':d,
|
|
'end_date':d
|
|
})
|
|
if resp.status != 'ok':
|
|
return await self.append_bill(resp.data.bills, dat)
|
|
|
|
async def append_bill(bills, dat):
|
|
db = DBPools()
|
|
for b in bills:
|
|
ns = {
|
|
'id':getID(),
|
|
'acc_id':b.creator_id,
|
|
'acc_name':b.creator_name,
|
|
'model':b.model,
|
|
'consume_time':b.consume_time,
|
|
'consume_token':b.consume_token,
|
|
'consume_amt':b.consume_cash,
|
|
'create_at':b.create_at,
|
|
'consume_date':dat
|
|
}
|
|
r = False
|
|
async with db.sqlorContext(self.dbname) as sor:
|
|
sor.C('minimax_bill', ns)
|
|
r = True
|
|
if not r:
|
|
print(f'{ns} write to db error')
|
|
return True
|