From 78f16ce69b4af8b2841633ecff331dbbdd4522a3 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 8 Apr 2026 13:34:10 +0800 Subject: [PATCH] bugfix --- llmage/accounting.py | 4 +-- llmage/asyncinference.py | 4 +-- llmage/llmclient.py | 60 +++++++++++++++++----------------------- llmage/syncinference.py | 6 ++-- llmage/utils.py | 10 ++----- 5 files changed, 36 insertions(+), 48 deletions(-) diff --git a/llmage/accounting.py b/llmage/accounting.py index 38ce097..042d275 100644 --- a/llmage/accounting.py +++ b/llmage/accounting.py @@ -10,9 +10,9 @@ from ahserver.serverenv import ServerEnv from accounting.consume import consume_accounting from accounting.getaccount import getCustomerBalance -async def llm_charging(sor, ppid, llmusage): +async def llm_charging(ppid, llmusage): env = ServerEnv() - prices = await env.pbuffered_chargin(ppid, llmusage.usages) + prices = await env.buffered_charging(ppid, llmusage.usages) if prices is None: e = Exception(f'{ppid=}, {llmusage.usage=}{llmusage.id=} env.buffered_charging() return None') exception(f'{e}') diff --git a/llmage/asyncinference.py b/llmage/asyncinference.py index 96ae567..233ddd5 100644 --- a/llmage/asyncinference.py +++ b/llmage/asyncinference.py @@ -52,7 +52,7 @@ async def get_asynctask_status(taskid): 'error': f'system error' } -async def async_uapi_request(request, llm, sor, +async def async_uapi_request(request, llm, callerid, callerorgid, params_kw=None): env = request._run_ns.copy() if not params_kw: @@ -61,7 +61,7 @@ async def async_uapi_request(request, llm, sor, # callerid = await env.get_user() # uapi = UAPI(request, sor=sor) uapi = UpAppApi() - userid = await get_owner_userid(sor, llm) + userid = await get_owner_userid(llm) b = None luid = getID() try: diff --git a/llmage/llmclient.py b/llmage/llmclient.py index dc175c8..6e3a5df 100644 --- a/llmage/llmclient.py +++ b/llmage/llmclient.py @@ -4,13 +4,11 @@ import asyncio from random import randint from functools import partial from traceback import format_exc -from sqlor.dbpools import DBPools, get_sor_context from appPublic.log import debug, exception, error from appPublic.uniqueID import getID from appPublic.dictObject import DictObject from appPublic.timeUtils import curDateString, timestampstr from appPublic.base64_to_file import base64_to_file, getFilenameFromBase64 -# from uapi.appapi import UAPI, sor_get_callerid, sor_get_uapi from ahserver.serverenv import get_serverenv, ServerEnv from ahserver.filestorage import FileStorage from .asyncinference import async_uapi_request @@ -18,15 +16,14 @@ from .syncinference import sync_uapi_request from .accounting import llm_accounting, llm_charging from .utils import * -async def uapi_request(request, llm, sor, callerid, callerorgid, params_kw=None): +async def uapi_request(request, llm, callerid, callerorgid, params_kw=None): env = request._run_ns.copy() if not params_kw: params_kw = env.params_kw # callerorgid = await env.get_userorgid() # callerid = await env.get_user() uapi = env.UpAppApi() - # uapi = UAPI(request, sor=sor) - userid = await get_owner_userid(sor, llm) + userid = await get_owner_userid(llm) outlines = [] txt = '' luid = getID() @@ -97,7 +94,7 @@ async def uapi_request(request, llm, sor, callerid, callerorgid, params_kw=None) llmusage.status = 'SUCCEEDED' if llm.ppid and callerorgid: try: - chargings = await llm_charging(sor, llm.ppid, llmusage) + chargings = await llm_charging(llm.ppid, llmusage) if chargings: llmusage.amount = chargings.amount llmusage.cost = chargings.cost @@ -125,7 +122,6 @@ async def uapi_request(request, llm, sor, callerid, callerorgid, params_kw=None) s = ''.join(s.split('\n')) outlines.append(ed) yield f'{s}\n' - # await write_llmusage(luid, llm, callerid, None, params_kw, outlines, sor) return async def inference_generator(request, *args, params_kw=None, **kw): @@ -144,33 +140,29 @@ async def _inference_generator(request, callerid, callerorgid, if not params_kw.transno: params_kw.transno = getID() llmid = params_kw.llmid - dbname = env.get_module_dbname('llmage') - db = env.DBPools() - async with db.sqlorContext(dbname) as sor: - f = None - llm = await get_llm(llmid) - if llm is None: - errmsg = f'{{"status": "FAILED", "error":"llmid:{llmid}没找到模型"}}\n' - exception(errmsg) - yield errmsg - return - if not params_kw.model: - params_kw.model = llm.model - if params_kw.stream and llm.stream == 'stream': - llm.stream = 'sync' - if llm.stream == 'async': - if llm.callbackurl: - cb_url = env.entire_url(llm.callbackurl) - params_kw.callbackurl = cb_url - f = partial(async_uapi_request, request, llm, sor, callerid, callerorgid, params_kw=params_kw) - elif not params_kw.stream: - f = partial(sync_uapi_request, request, llm, sor, callerid, callerorgid, params_kw=params_kw) - # env.update(llm) - else: - uapi = UAPI(request, sor=sor) - f = partial(uapi_request, request, llm, sor, callerid, callerorgid, params_kw=params_kw) - async for d in f(): - yield d + f = None + llm = await get_llm(llmid) + if llm is None: + errmsg = f'{{"status": "FAILED", "error":"llmid:{llmid}没找到模型"}}\n' + exception(errmsg) + yield errmsg + return + if not params_kw.model: + params_kw.model = llm.model + if params_kw.stream and llm.stream == 'stream': + llm.stream = 'sync' + if llm.stream == 'async': + if llm.callbackurl: + cb_url = env.entire_url(llm.callbackurl) + params_kw.callbackurl = cb_url + f = partial(async_uapi_request, request, llm, callerid, callerorgid, params_kw=params_kw) + elif not params_kw.stream: + f = partial(sync_uapi_request, request, llm, callerid, callerorgid, params_kw=params_kw) + # env.update(llm) + else: + f = partial(uapi_request, request, llm, callerid, callerorgid, params_kw=params_kw) + async for d in f(): + yield d async def inference(request, *args, params_kw=None, **kw): env = request._run_ns.copy() diff --git a/llmage/syncinference.py b/llmage/syncinference.py index acc9a24..023520a 100644 --- a/llmage/syncinference.py +++ b/llmage/syncinference.py @@ -16,7 +16,7 @@ from ahserver.filestorage import FileStorage from .accounting import llm_accounting, llm_charging from .utils import * -async def sync_uapi_request(request, llm, sor, callerid, callerorgid, params_kw=None): +async def sync_uapi_request(request, llm, callerid, callerorgid, params_kw=None): env = request._run_ns.copy() if not params_kw: params_kw = env.params_kw @@ -24,7 +24,7 @@ async def sync_uapi_request(request, llm, sor, callerid, callerorgid, params_kw= # callerorgid = await env.get_userorgid() # uapi = UAPI(request, sor=sor) uapi = UpAppApi() - userid = await get_owner_userid(sor, llm) + userid = await get_owner_userid(llm) outlines = [] b = None d = None @@ -61,7 +61,7 @@ async def sync_uapi_request(request, llm, sor, callerid, callerorgid, params_kw= llmusage.amount = llmusage.cost = 0.00 if llm.ppid: try: - charging = await llm_charging(sor, llm.ppid, llmusage) + charging = await llm_charging(llm.ppid, llmusage) if charging: llmusage.amount = charging.amount llmusage.cost = charging.cost diff --git a/llmage/utils.py b/llmage/utils.py index b2f44e6..2adfb19 100644 --- a/llmage/utils.py +++ b/llmage/utils.py @@ -211,13 +211,9 @@ async def get_llm(llmid): return await bllms.get_llm(llmid) async def get_owner_userid(sor, llm): - sql = '''select a.ownerid as userid from upappkey a, upapp b -where a.upappid=b.id - and a.orgid = b.ownerid - and a.orgid = ${ownerid}$''' - recs = await sor.sqlExe(sql, {'ownerid': llm.ownerid}) - i = randint(0, len(recs)-1) - return recs[i].userid + env = ServerEnv() + userid = await env.uapi_data.get_calluserid(llm.uappid, orgid=llm.ownerid) + return userid async def write_llmusage(llmusage): env = ServerEnv()