diff --git a/llmage/accounting.py b/llmage/accounting.py index d29c8f5..c1e8eed 100644 --- a/llmage/accounting.py +++ b/llmage/accounting.py @@ -2,8 +2,24 @@ from appPublic.log import exception, debug from appPublic.uniqueID import getID from appPublic.dictObject import DictObject from sqlor.dbpools import get_sor_context +from ahserver.serverenv import ServerEnv from pricing.pricing import pricing_program_charging from accounting.consume import consume_accounting +from accounting.getaccount import getCustomerBalance + +async def checkCustomerBalance(llmid, userorgid): + env = ServerEnv() + async with get_sor_context(env, 'llmage') as sor: + llms = await sor.R('llm', { 'id': llmid}) + if len(llms) < 1: + e = Exception(f'llm({llmid}) not exists') + exception(f'{e}') + raise e + if llms[0].ownerid == userorgid: + return True + balance = await getCustomerBalance(sor, userorgid) + return llms[0].min_balance < balance + return False async def llm_accounting(request, llmid, usage, customerid, userid, orderid=None): diff --git a/llmage/init.py b/llmage/init.py index ceac435..a9d45eb 100644 --- a/llmage/init.py +++ b/llmage/init.py @@ -10,6 +10,7 @@ from llmage.llmclient import ( get_llmcatelogs, get_llms_by_catelog ) +from llmage.accounting import checkCustomerBalance from llmage.messages import ( BaseMessages, SessionMessages, @@ -33,6 +34,7 @@ def load_llmage(): env.SessageMessages = SessionMessages env.BaseMessages = BaseMessages env.keling_token = keling_token + env.checkCustomerBalance = checkCustomerBalance rf = RegisterFunction() rf.register('jimeng_auth_headers', jimeng_auth_headers) diff --git a/llmage/llmclient.py b/llmage/llmclient.py index 729f97c..79b4086 100644 --- a/llmage/llmclient.py +++ b/llmage/llmclient.py @@ -177,6 +177,7 @@ async def uapi_request(request, llm, sor, params_kw=None): usage['prompt_tokens'] = cnt u = await write_llmusage(luid, llm, callerid, usage, params_kw, outlines, sor) if llm.ppid and callerorgid != llm.ownerid: + debug(f'{usage=},{llm.ownerid=},{callerorgid=}') await llm_accounting(request, llm.id, usage, callerorgid, callerid) except Exception as e: exception(f'{e=},{format_exc()}') @@ -228,6 +229,7 @@ async def sync_uapi_request(request, llm, sor, params_kw=None): yield b await write_llmusage(luid, llm, callerid, usage, params_kw, outlines, sor) if llm.ppid and callerorgid != llm.ownerid: + debug(f'{usage=},{llm.ownerid=},{callerorgid=}') await llm_accounting(request, llm.id, usage, callerorgid, callerid) async def async_uapi_request(request, llm, sor, params_kw=None): @@ -301,6 +303,7 @@ async def async_uapi_request(request, llm, sor, params_kw=None): usage['finish_time'] = t3 -t1 await write_llmusage(luid, llm, callerid, usage, params_kw, outlines, sor) if llm.ppid and callerorgid != llm.ownerid: + debug(f'{usage=},{llm.ownerid=},{callerorgid=}') await llm_accounting(request, llm.id, usage, callerorgid, callerid) d = rzt diff --git a/models/llm.xlsx b/models/llm.xlsx index 1903ac2..5b849ab 100644 Binary files a/models/llm.xlsx and b/models/llm.xlsx differ diff --git a/wwwroot/show_llms.ui b/wwwroot/show_llms.ui index 864336d..5003d24 100644 --- a/wwwroot/show_llms.ui +++ b/wwwroot/show_llms.ui @@ -1,3 +1,4 @@ +{% set userorgid = get_userorgid %} { "widgettype":"VScrollPanel", "options":{ @@ -28,10 +29,14 @@ }, "subwidgets":[ {% for llm in get_llms_by_catelog(cate.id) %} - { +{% set can_use = checkCustomerBalance(llm.id, userorgid) %} + { "widgettype":"VScrollPanel", "options":{ "css":"card", +{% if not can_use %} + "disabled":true, +{% endif %} "bgcolor": "#def0f0", "cwidth":20, "cheight":12