diff --git a/llmage/accounting.py b/llmage/accounting.py index d9a4fbc..7fb7f22 100644 --- a/llmage/accounting.py +++ b/llmage/accounting.py @@ -10,6 +10,10 @@ from accounting.getaccount import getCustomerBalance async def llm_charging(sor, ppid, llmusage): env = ServerEnv() prices = await env.pricing_program_charging(sor, ppid, llmusage.usage) + if not pricing: + d = DictObject() + d.original_amount = d.amount = d.cost = 0.00 + return d amount = 0 cost = 0 for p in prices: diff --git a/llmage/asyncinference.py b/llmage/asyncinference.py index 3895544..993101c 100644 --- a/llmage/asyncinference.py +++ b/llmage/asyncinference.py @@ -19,7 +19,7 @@ from .utils import * async def get_today_asynctask_list(userid): env = ServerEnv() async with get_sor_context(env, 'llmage') as sor: - today = getCurrentDate() + today = env.get_business_date(sor) sql = '''select * from llmusage where userid=${userid}$ and use_date = ${date}$''' @@ -121,7 +121,7 @@ async def add_new_llmusage_output(luid, rzt): r = recs[0] io = json.loads(r.ioinfo) out = io.get('output', []) - out.append(out) + out.append(rzt) io['output'] = out r.ioinfo = json.dumps({ 'input': io.get('input',{}), @@ -134,33 +134,51 @@ async def query_task_status(request, upappid, apinames, luid, userid, taskid): async with get_sor_context(env, 'llmage') as sor: uapi = UAPI(request, sor) for apiname in apinames: - try: + status = 'unknown' + while status != 'SUCCEEDED': ns = {'taskid': taskid} - b = await uapi.call(upappid, apiname, userid, params=ns) - if isinstance(b, bytes): - b = b.decode('utf-8') - d = json.loads(b) - rzt = DictObject(**d) - await add_new_llmusage_output(luid, rzt) - if rzt.status == 'FAILED': + d = None + try: + b = await uapi.call(upappid, apiname, userid, params=ns) + if isinstance(b, bytes): + b = b.decode('utf-8') + d = json.loads(b) + except Exception as e: + e = Exception(f'{e}') + exception(f'{e}') + changed = { + 'status': 'FAILED', + 'output': {'status': 'FAILED', 'error': str(e)} + } + await add_new_llmusage_output(luid, changed) return + rzt = DictObject(**d) + changed = { + 'status': rzt.status, + 'output': rzt + } if rzt.status == 'SUCCEEDED': if llm.ppid: try: charging = await llm_charging(sor, llm.ppid, llmusage) if charging: - llmusage.amount = charging.amount - llmusage.cost = charging.cost + changed.amount = charging.amount + changed.cost = charging.cost else: - llmusage.amount = cost = 0.0 + changed.amount = cost = 0.0 except Exception as e: e = Exception(f'{llm.pid} charging error{e}') exception(f'{e}') else: - llmusage.amount = 0 - llmusage.cost = 0 + changed.amount = 0 + changed.cost = 0 + await add_new_llmusage_output(luid, changed) await llm_accounting(request, llmusage) + status = rzt.status + if rzt.status == 'FAILED': + return + await asyncio.sleep(0.1) except Exception as e: exception(f'{e=},{format_exc()}') diff --git a/llmage/init.py b/llmage/init.py index 8105103..7df9db0 100644 --- a/llmage/init.py +++ b/llmage/init.py @@ -1,9 +1,9 @@ from appPublic.base64_to_file import hex2base64 from appPublic.registerfunction import RegisterFunction -from llmage.jimeng import jimeng_auth_headers -from llmage.keling import keling_token +from .jimeng import jimeng_auth_headers +from .keling import keling_token -from llmage.llmclient import ( +from .llmclient import ( b64media2url, get_llm, inference_generator, @@ -14,11 +14,17 @@ from llmage.llmclient import ( get_llmcatelogs, get_llms_by_catelog ) -from llmage.accounting import checkCustomerBalance -from ahserver.serverenv import ServerEnv +from .accounting import checkCustomerBalance +from .serverenv import ServerEnv +from asyncinference import ( + get_asynctask_status, + get_today_asynctask_list +) def load_llmage(): env = ServerEnv() + env.get_today_asynctask_list = get_today_asynctask_list + env.get_asynctask_status = get_asynctask_status env.get_llm = get_llm env.b64media2url = b64media2url env.hex2base64 = hex2base64 diff --git a/wwwroot/get_asynctask_status.dspy b/wwwroot/get_asynctask_status.dspy new file mode 100644 index 0000000..fa62eb3 --- /dev/null +++ b/wwwroot/get_asynctask_status.dspy @@ -0,0 +1,8 @@ +taskid = params_kw.taskid +s = await get_asynctask_status(taskid) +return { + 'status': 'ok', + 'data': { + 'resposne': s + } +} diff --git a/wwwroot/get_my_asynctasks.dspy b/wwwroot/get_my_asynctasks.dspy new file mode 100644 index 0000000..2c20cdc --- /dev/null +++ b/wwwroot/get_my_asynctasks.dspy @@ -0,0 +1,8 @@ +userid = await get_userid() +tasks = await get_today_asynctask_list(userrid) +return { + 'status': 'ok', + 'data': { + 'tasks': tasks + } +}