From 76622381be636cbaeab8028cf200629655123970 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 21 May 2026 13:19:48 +0800 Subject: [PATCH] bugfix --- llmage/accounting.py | 15 +++++-- llmage/init.py | 6 +++ llmage/utils.py | 59 ++++++++++++++++++++++++++ wwwroot/llm_dialog.ui | 3 +- wwwroot/llminference.dspy | 2 +- wwwroot/v1/chat/completions/index.dspy | 2 +- wwwroot/vidu_inference.dspy | 2 +- 7 files changed, 82 insertions(+), 7 deletions(-) diff --git a/llmage/accounting.py b/llmage/accounting.py index ec20c63..157dead 100644 --- a/llmage/accounting.py +++ b/llmage/accounting.py @@ -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) diff --git a/llmage/init.py b/llmage/init.py index 00140ef..8030b76 100644 --- a/llmage/init.py +++ b/llmage/init.py @@ -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 diff --git a/llmage/utils.py b/llmage/utils.py index 2d70d6f..940f5aa 100644 --- a/llmage/utils.py +++ b/llmage/utils.py @@ -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() diff --git a/wwwroot/llm_dialog.ui b/wwwroot/llm_dialog.ui index fc9e007..73c27ac 100644 --- a/wwwroot/llm_dialog.ui +++ b/wwwroot/llm_dialog.ui @@ -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 %} diff --git a/wwwroot/llminference.dspy b/wwwroot/llminference.dspy index 8381f58..bd32ebf 100644 --- a/wwwroot/llminference.dspy +++ b/wwwroot/llminference.dspy @@ -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 = { diff --git a/wwwroot/v1/chat/completions/index.dspy b/wwwroot/v1/chat/completions/index.dspy index d247ca0..5be6956 100644 --- a/wwwroot/v1/chat/completions/index.dspy +++ b/wwwroot/v1/chat/completions/index.dspy @@ -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() diff --git a/wwwroot/vidu_inference.dspy b/wwwroot/vidu_inference.dspy index 2d3d265..cf0f6da 100644 --- a/wwwroot/vidu_inference.dspy +++ b/wwwroot/vidu_inference.dspy @@ -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",