bugfix
This commit is contained in:
parent
81bed1d384
commit
76622381be
@ -36,7 +36,7 @@ async def llm_charging(ppid, llmusage):
|
||||
'cost': cost
|
||||
})
|
||||
|
||||
async def checkCustomerBalance(llmid, userorgid):
|
||||
async def checkCustomerBalance(llmid, userid, userorgid):
|
||||
if llmid is None:
|
||||
debug(f'checkCustomerBalance(): llmid is None')
|
||||
return False
|
||||
@ -51,7 +51,12 @@ async def checkCustomerBalance(llmid, userorgid):
|
||||
if llm.ownerid == userorgid:
|
||||
debug(f'self orgid user')
|
||||
return True
|
||||
balance = await getCustomerBalance(sor, userorgid)
|
||||
apikey = await get_user_tpac_apikey(userid)
|
||||
balance = 0.00
|
||||
if apikey:
|
||||
balance = await get_tpac_balance(apikey, userid)
|
||||
else:
|
||||
balance = await getCustomerBalance(sor, userorgid)
|
||||
bal = 0 if balance is None else balance
|
||||
if llm.min_balance is None:
|
||||
llm.min_balance = 0.00
|
||||
@ -229,7 +234,11 @@ async def backend_accounting():
|
||||
for lu in lus:
|
||||
try:
|
||||
debug(f'backend_accounting(): {lu.id=} handleing...')
|
||||
await llm_accounting(lu)
|
||||
apikey = await get_user_tpac_apikey(lu.userid)
|
||||
if apikey:
|
||||
await tpac_accounting(apikey, lu.userid, lu.llmid, lu.amount, lu.usages)
|
||||
else:
|
||||
await llm_accounting(lu)
|
||||
except Exception as e:
|
||||
exception(f'{e}, {lu.id=}')
|
||||
await llm_accoung_failed(lu.id)
|
||||
|
||||
@ -15,6 +15,9 @@ from .utils import (
|
||||
get_llmcatelogs,
|
||||
get_llms_by_catelog_to_customer,
|
||||
get_llmproviders,
|
||||
tpac_accounting,
|
||||
get_tpac_balance,
|
||||
get_user_tpac_apikey,
|
||||
get_llm,
|
||||
BufferedLLMs
|
||||
)
|
||||
@ -75,6 +78,9 @@ def load_llmage():
|
||||
env.get_llmcatelogs = get_llmcatelogs
|
||||
env.checkCustomerBalance = checkCustomerBalance
|
||||
env.get_llmproviders = get_llmproviders
|
||||
env.get_user_tpac_apikey = get_user_tpac_apikey
|
||||
env.get_tpac_balance = get_tpac_balance
|
||||
env.tpac_accounting = tpac_accounting
|
||||
env.get_llms_sort_by_provider = get_llms_sort_by_provider
|
||||
env.keling_token = keling_token
|
||||
env.llm_query_price = llm_query_price
|
||||
|
||||
@ -13,6 +13,65 @@ from appPublic.timeUtils import curDateString, timestampstr
|
||||
from uapi.appapi import UAPI, sor_get_callerid, sor_get_uapi
|
||||
from ahserver.serverenv import get_serverenv, ServerEnv
|
||||
from ahserver.filestorage import FileStorage
|
||||
from appPublic.jsonConfig import getConfig
|
||||
from appPublic.streamhttpclient import StreamHttpClient
|
||||
|
||||
async def get_user_tpac_apikey(userid):
|
||||
env = ServerEnv()
|
||||
config = getConfig()
|
||||
if not config.tpac:
|
||||
return None
|
||||
apikey = await env.get_user_dapp_apikey(config.tpac.dappid, userid)
|
||||
if apikey is None:
|
||||
return None
|
||||
return apikey
|
||||
|
||||
async def get_tpac_balance(apikey, userid):
|
||||
config = getConfig()
|
||||
if apikey is None:
|
||||
return None
|
||||
|
||||
url = config.tpac.get_user_balance_url
|
||||
hc = StreamHttpClient()
|
||||
try:
|
||||
b = hc.request('GET', url, params={"apikey": apikey, 'userid': userid})
|
||||
if b:
|
||||
d = json.loads(b.decode('utf-8'))
|
||||
if d['status'] == 'ok':
|
||||
return d['balance']
|
||||
exception(f'{url=}, {userid=}, {apikey=}, error')
|
||||
return None
|
||||
except Exception as e:
|
||||
exception(f'{url=}, {userid=}, {apikey=}, error:{e}')
|
||||
return None
|
||||
|
||||
async def tpac_accounting(apikey, userid, llmid, amount, usage):
|
||||
if apikey is None:
|
||||
return
|
||||
config = getConfig()
|
||||
url = config.tpac.accounting_url
|
||||
d = {
|
||||
'apikey': apikey,
|
||||
'userid': userid,
|
||||
'llmid': llmid,
|
||||
'amount': amount,
|
||||
'usage': usage
|
||||
}
|
||||
url = config.thirdparty_accounting_center.get_user_balance_url
|
||||
hc = StreamHttpClient()
|
||||
try:
|
||||
b = hc.request('POST', url, data=d):
|
||||
d = json.loads(b.decode('utf-8'))
|
||||
if d['status'] == 'ok':
|
||||
env = ServerEnv()
|
||||
async with get_sor_context(env, 'llmage') as sor:
|
||||
await sor.U('llmusage', {'id': llmid, 'tpac_accounting_status': 'accounted')
|
||||
return
|
||||
exception(f'{apikey=}, {userid=}, {llmid=}, {amount=}, {usage=} tpac accounting error')
|
||||
return
|
||||
except Exception as e:
|
||||
exception(f'{apikey=}, {userid=}, {llmid=}, {amount=}, {usage=} tpac accounting error:{e}')
|
||||
return
|
||||
|
||||
async def append_new_llmoutput(webpath, output):
|
||||
fs = FileStorage()
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
{% if get_user() %}
|
||||
{% set userid = get_user() %}
|
||||
{% set userorgid = get_userorgid() %}
|
||||
{% if params_kw.id %}
|
||||
{% if checkCustomerBalance(params_kw.id, userorgid) %}
|
||||
{% if checkCustomerBalance(params_kw.id, userid, userorgid) %}
|
||||
{% set llm = get_llm(params_kw.id) %}
|
||||
{% set kdbs = get_user_kdbs(request) %}
|
||||
{% if llm %}
|
||||
|
||||
@ -10,7 +10,7 @@ userid = await get_user()
|
||||
userorgid = await get_userorgid()
|
||||
if userid is None:
|
||||
return UiError(title='llm inference', message='Please login first')
|
||||
f = await checkCustomerBalance(params_kw.llmid, userorgid)
|
||||
f = await checkCustomerBalance(params_kw.llmid, userid, userorgid)
|
||||
kdbids = params_kw.kdbids
|
||||
if kdbids:
|
||||
data = {
|
||||
|
||||
@ -44,7 +44,7 @@ where b.name = ${lctype}$
|
||||
return openai_400()
|
||||
params_kw.llmid = recs[0].id
|
||||
|
||||
f = await checkCustomerBalance(params_kw.llmid, userorgid)
|
||||
f = await checkCustomerBalance(params_kw.llmid, userid, userorgid)
|
||||
if not f:
|
||||
debug(f'{userid=} balance not enough')
|
||||
return openai_429()
|
||||
|
||||
@ -10,7 +10,7 @@ userid = await get_user()
|
||||
userorgid = await get_userorgid()
|
||||
if userid is None:
|
||||
return UiError(title='llm inference', message='Please login first')
|
||||
f = await checkCustomerBalance(params_kw.llmid, userorgid)
|
||||
f = await checkCustomerBalance(params_kw.llmid, userid, userorgid)
|
||||
if not f:
|
||||
return {
|
||||
"status": "error",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user