diff --git a/llmage/accounting.py b/llmage/accounting.py index bcc8db9..38ce097 100644 --- a/llmage/accounting.py +++ b/llmage/accounting.py @@ -7,15 +7,14 @@ 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 llm_charging(sor, ppid, llmusage): env = ServerEnv() - prices = await env.pricing_program_charging(sor, ppid, llmusage.usages) + prices = await env.pbuffered_chargin(ppid, llmusage.usages) if prices is None: - e = Exception(f'{ppid=}, {llmusage.usage=}{llmusage.id=} env.pricing_program_charging() return None') + e = Exception(f'{ppid=}, {llmusage.usage=}{llmusage.id=} env.buffered_charging() return None') exception(f'{e}') raise e return None diff --git a/llmage/init.py b/llmage/init.py index ddd9edb..2a87caa 100644 --- a/llmage/init.py +++ b/llmage/init.py @@ -7,6 +7,7 @@ from .keling import keling_token from .jimeng import jimeng_auth_headers from .utils import ( llm_query_orders, + llm_query_price, get_llm_by_model ) @@ -15,7 +16,6 @@ from .llmclient import ( get_llm, inference_generator, inference, - llm_query_price, get_llmproviders, get_llms_sort_by_provider, get_llmcatelogs, diff --git a/llmage/llmclient.py b/llmage/llmclient.py index 908a12f..dc175c8 100644 --- a/llmage/llmclient.py +++ b/llmage/llmclient.py @@ -177,19 +177,3 @@ async def inference(request, *args, params_kw=None, **kw): f = partial(inference_generator, request, *args, params_kw=params_kw, **kw) return await env.stream_response(request, f) -async def llm_query_price(llmid, config_data): - env = ServerEnv() - async with get_sor_context(env, 'llmage') as sor: - llms = await sor.R('llm', {'id': llmid}) - if not llms: - e = Exception(f'id={llmid} llm not founnd') - exception(f'{e}') - raise e - llm = llms[0] - if llm.ppid is None: - e = Exception(f'{llm=} ppid is None') - exception(f'{e}') - raise e - prices = await env.pricing_program_charging(sor, llm.ppid, config_data) - return prices - diff --git a/llmage/utils.py b/llmage/utils.py index 435e14b..a2055c8 100644 --- a/llmage/utils.py +++ b/llmage/utils.py @@ -160,41 +160,55 @@ where a.llmcatelogid = b.id return d return [] -async def get_llm(llmid): - db = DBPools() - dbname = get_serverenv('get_module_dbname')('llmage') - async with db.sqlorContext(dbname) as sor: +class BufferedLLMs: + lls = {} + async def get_llm(self, llmid): today = curDateString() - sql = """select x.*, -z.input_fields -from ( -select a.*, e.ioid, e.callbackurl, e.stream -from llm a, upapp c, uapiset d, uapi e -where a.upappid = c.id - and c.apisetid = d.id - and e.apisetid = d.id - and a.apiname = e.name - and a.expired_date > ${today}$ - and a.enabled_date <= ${today}$ -) x left join uapiio z on x.ioid = z.id -where x.id = ${llmid}$ -""" - ns = {'llmid': llmid, 'today': today} - recs = await sor.sqlExe(sql, ns.copy()) - if len(recs) > 0: - r = recs[0] - api = await sor_get_uapi(sor, r.upappid, r.apiname) - if api is None: - e = Exception(f'{r.upappid=},{r.apiname=} uapi not found') - exception(f'{e=}\n{format_exc()}') - raise e - r.inputfields = api.input_fields - return recs[0] - else: - debug(f'{llmid=} not found, {ns=}, {sql=}') - return None - exception(f'{db.e_except}\n{format_exc()}') - return None + k = f'{llmid}.{today}' + d = BufferedLLMs.llms.get(k) + if d: + return d + env = ServerEnv() + async with get_sor_context(env, 'llmage') as sor: + sql = """select x.*, + z.input_fields + from ( + select a.*, e.ioid, e.callbackurl, e.stream, f.input_fields as inputfields + from llm a, upapp c, uapiset d, uapi e, uapiio f + where a.upappid = c.id + and c.apisetid = d.id + and e.apisetid = d.id + and e.ioid = f.id + and a.apiname = e.name + and a.expired_date > ${today}$ + and a.enabled_date <= ${today}$ + ) x left join uapiio z on x.ioid = z.id + where x.id = ${llmid}$ + """ + ns = {'llmid': llmid, 'today': today} + recs = await sor.sqlExe(sql, ns.copy()) + if len(recs) > 0: + r = recs[0] + dates = BufferedLLMs.llms.get(llmid, []) + dates.append(today) + cnt = len(dates) + if cnt > 2: + for i in range(0, cnt -2) + dat = dates[i] + del BufferedLLMs.llms[f'{llmid}.{dat}'] + dates = dates[-2:] + BufferedLLMs.llms[llmid] = dates + BufferedLLMs.llms[k] = r + return r + else: + debug(f'{llmid=} not found, {ns=}, {sql=}') + return None + exception(f'Error: format_exc()}') + return None + +async def get_llm(llmid): + bllms = BufferedLLMs() + return await bllms.get_llm(llmid) async def get_owner_userid(sor, llm): sql = '''select a.ownerid as userid from upappkey a, upapp b @@ -210,3 +224,13 @@ async def write_llmusage(llmusage): async with get_sor_context(env, 'llmage') as sor: await sor.C('llmusage', llmusage) +async def llm_query_price(llmid, config_data): + env = ServerEnv() + llm = await get_llm(llmid) + if llm.ppid is None: + e = Exception(f'{llm=} ppid is None') + exception(f'{e}') + raise e + prices = await env.buffered_charging(llm.ppid, config_data) + return prices +