This commit is contained in:
yumoqing 2026-04-08 13:34:10 +08:00
parent f0c5f50e05
commit 78f16ce69b
5 changed files with 36 additions and 48 deletions

View File

@ -10,9 +10,9 @@ from ahserver.serverenv import ServerEnv
from accounting.consume import consume_accounting from accounting.consume import consume_accounting
from accounting.getaccount import getCustomerBalance from accounting.getaccount import getCustomerBalance
async def llm_charging(sor, ppid, llmusage): async def llm_charging(ppid, llmusage):
env = ServerEnv() env = ServerEnv()
prices = await env.pbuffered_chargin(ppid, llmusage.usages) prices = await env.buffered_charging(ppid, llmusage.usages)
if prices is None: if prices is None:
e = Exception(f'{ppid=}, {llmusage.usage=}{llmusage.id=} env.buffered_charging() return None') e = Exception(f'{ppid=}, {llmusage.usage=}{llmusage.id=} env.buffered_charging() return None')
exception(f'{e}') exception(f'{e}')

View File

@ -52,7 +52,7 @@ async def get_asynctask_status(taskid):
'error': f'system error' 'error': f'system error'
} }
async def async_uapi_request(request, llm, sor, async def async_uapi_request(request, llm,
callerid, callerorgid, params_kw=None): callerid, callerorgid, params_kw=None):
env = request._run_ns.copy() env = request._run_ns.copy()
if not params_kw: if not params_kw:
@ -61,7 +61,7 @@ async def async_uapi_request(request, llm, sor,
# callerid = await env.get_user() # callerid = await env.get_user()
# uapi = UAPI(request, sor=sor) # uapi = UAPI(request, sor=sor)
uapi = UpAppApi() uapi = UpAppApi()
userid = await get_owner_userid(sor, llm) userid = await get_owner_userid(llm)
b = None b = None
luid = getID() luid = getID()
try: try:

View File

@ -4,13 +4,11 @@ import asyncio
from random import randint from random import randint
from functools import partial from functools import partial
from traceback import format_exc from traceback import format_exc
from sqlor.dbpools import DBPools, get_sor_context
from appPublic.log import debug, exception, error from appPublic.log import debug, exception, error
from appPublic.uniqueID import getID from appPublic.uniqueID import getID
from appPublic.dictObject import DictObject from appPublic.dictObject import DictObject
from appPublic.timeUtils import curDateString, timestampstr from appPublic.timeUtils import curDateString, timestampstr
from appPublic.base64_to_file import base64_to_file, getFilenameFromBase64 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.serverenv import get_serverenv, ServerEnv
from ahserver.filestorage import FileStorage from ahserver.filestorage import FileStorage
from .asyncinference import async_uapi_request from .asyncinference import async_uapi_request
@ -18,15 +16,14 @@ from .syncinference import sync_uapi_request
from .accounting import llm_accounting, llm_charging from .accounting import llm_accounting, llm_charging
from .utils import * 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() env = request._run_ns.copy()
if not params_kw: if not params_kw:
params_kw = env.params_kw params_kw = env.params_kw
# callerorgid = await env.get_userorgid() # callerorgid = await env.get_userorgid()
# callerid = await env.get_user() # callerid = await env.get_user()
uapi = env.UpAppApi() uapi = env.UpAppApi()
# uapi = UAPI(request, sor=sor) userid = await get_owner_userid(llm)
userid = await get_owner_userid(sor, llm)
outlines = [] outlines = []
txt = '' txt = ''
luid = getID() luid = getID()
@ -97,7 +94,7 @@ async def uapi_request(request, llm, sor, callerid, callerorgid, params_kw=None)
llmusage.status = 'SUCCEEDED' llmusage.status = 'SUCCEEDED'
if llm.ppid and callerorgid: if llm.ppid and callerorgid:
try: try:
chargings = await llm_charging(sor, llm.ppid, llmusage) chargings = await llm_charging(llm.ppid, llmusage)
if chargings: if chargings:
llmusage.amount = chargings.amount llmusage.amount = chargings.amount
llmusage.cost = chargings.cost 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')) s = ''.join(s.split('\n'))
outlines.append(ed) outlines.append(ed)
yield f'{s}\n' yield f'{s}\n'
# await write_llmusage(luid, llm, callerid, None, params_kw, outlines, sor)
return return
async def inference_generator(request, *args, params_kw=None, **kw): async def inference_generator(request, *args, params_kw=None, **kw):
@ -144,9 +140,6 @@ async def _inference_generator(request, callerid, callerorgid,
if not params_kw.transno: if not params_kw.transno:
params_kw.transno = getID() params_kw.transno = getID()
llmid = params_kw.llmid llmid = params_kw.llmid
dbname = env.get_module_dbname('llmage')
db = env.DBPools()
async with db.sqlorContext(dbname) as sor:
f = None f = None
llm = await get_llm(llmid) llm = await get_llm(llmid)
if llm is None: if llm is None:
@ -162,13 +155,12 @@ async def _inference_generator(request, callerid, callerorgid,
if llm.callbackurl: if llm.callbackurl:
cb_url = env.entire_url(llm.callbackurl) cb_url = env.entire_url(llm.callbackurl)
params_kw.callbackurl = cb_url params_kw.callbackurl = cb_url
f = partial(async_uapi_request, request, llm, sor, callerid, callerorgid, params_kw=params_kw) f = partial(async_uapi_request, request, llm, callerid, callerorgid, params_kw=params_kw)
elif not params_kw.stream: elif not params_kw.stream:
f = partial(sync_uapi_request, request, llm, sor, callerid, callerorgid, params_kw=params_kw) f = partial(sync_uapi_request, request, llm, callerid, callerorgid, params_kw=params_kw)
# env.update(llm) # env.update(llm)
else: else:
uapi = UAPI(request, sor=sor) f = partial(uapi_request, request, llm, callerid, callerorgid, params_kw=params_kw)
f = partial(uapi_request, request, llm, sor, callerid, callerorgid, params_kw=params_kw)
async for d in f(): async for d in f():
yield d yield d

View File

@ -16,7 +16,7 @@ from ahserver.filestorage import FileStorage
from .accounting import llm_accounting, llm_charging from .accounting import llm_accounting, llm_charging
from .utils import * 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() env = request._run_ns.copy()
if not params_kw: if not params_kw:
params_kw = env.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() # callerorgid = await env.get_userorgid()
# uapi = UAPI(request, sor=sor) # uapi = UAPI(request, sor=sor)
uapi = UpAppApi() uapi = UpAppApi()
userid = await get_owner_userid(sor, llm) userid = await get_owner_userid(llm)
outlines = [] outlines = []
b = None b = None
d = 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 llmusage.amount = llmusage.cost = 0.00
if llm.ppid: if llm.ppid:
try: try:
charging = await llm_charging(sor, llm.ppid, llmusage) charging = await llm_charging(llm.ppid, llmusage)
if charging: if charging:
llmusage.amount = charging.amount llmusage.amount = charging.amount
llmusage.cost = charging.cost llmusage.cost = charging.cost

View File

@ -211,13 +211,9 @@ async def get_llm(llmid):
return await bllms.get_llm(llmid) return await bllms.get_llm(llmid)
async def get_owner_userid(sor, llm): async def get_owner_userid(sor, llm):
sql = '''select a.ownerid as userid from upappkey a, upapp b env = ServerEnv()
where a.upappid=b.id userid = await env.uapi_data.get_calluserid(llm.uappid, orgid=llm.ownerid)
and a.orgid = b.ownerid return userid
and a.orgid = ${ownerid}$'''
recs = await sor.sqlExe(sql, {'ownerid': llm.ownerid})
i = randint(0, len(recs)-1)
return recs[i].userid
async def write_llmusage(llmusage): async def write_llmusage(llmusage):
env = ServerEnv() env = ServerEnv()