Compare commits

..

No commits in common. "78f16ce69b4af8b2841633ecff331dbbdd4522a3" and "1eced06197b2a890511d7abc4db236a123e1c706" have entirely different histories.

5 changed files with 49 additions and 37 deletions

View File

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

View File

@ -52,7 +52,7 @@ async def get_asynctask_status(taskid):
'error': f'system error'
}
async def async_uapi_request(request, llm,
async def async_uapi_request(request, llm, sor,
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,
# callerid = await env.get_user()
# uapi = UAPI(request, sor=sor)
uapi = UpAppApi()
userid = await get_owner_userid(llm)
userid = await get_owner_userid(sor, llm)
b = None
luid = getID()
try:

View File

@ -4,11 +4,13 @@ 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
@ -16,14 +18,15 @@ from .syncinference import sync_uapi_request
from .accounting import llm_accounting, llm_charging
from .utils import *
async def uapi_request(request, llm, callerid, callerorgid, params_kw=None):
async def uapi_request(request, llm, sor, 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()
userid = await get_owner_userid(llm)
# uapi = UAPI(request, sor=sor)
userid = await get_owner_userid(sor, llm)
outlines = []
txt = ''
luid = getID()
@ -94,7 +97,7 @@ async def uapi_request(request, llm, callerid, callerorgid, params_kw=None):
llmusage.status = 'SUCCEEDED'
if llm.ppid and callerorgid:
try:
chargings = await llm_charging(llm.ppid, llmusage)
chargings = await llm_charging(sor, llm.ppid, llmusage)
if chargings:
llmusage.amount = chargings.amount
llmusage.cost = chargings.cost
@ -122,6 +125,7 @@ async def uapi_request(request, llm, 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):
@ -140,6 +144,9 @@ 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:
@ -155,12 +162,13 @@ async def _inference_generator(request, callerid, callerorgid,
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)
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, callerid, callerorgid, params_kw=params_kw)
f = partial(sync_uapi_request, request, llm, sor, callerid, callerorgid, params_kw=params_kw)
# env.update(llm)
else:
f = partial(uapi_request, request, llm, callerid, callerorgid, params_kw=params_kw)
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

View File

@ -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, callerid, callerorgid, params_kw=None):
async def sync_uapi_request(request, llm, sor, 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, callerid, callerorgid, params_kw=None)
# callerorgid = await env.get_userorgid()
# uapi = UAPI(request, sor=sor)
uapi = UpAppApi()
userid = await get_owner_userid(llm)
userid = await get_owner_userid(sor, llm)
outlines = []
b = None
d = None
@ -61,7 +61,7 @@ async def sync_uapi_request(request, llm, callerid, callerorgid, params_kw=None)
llmusage.amount = llmusage.cost = 0.00
if llm.ppid:
try:
charging = await llm_charging(llm.ppid, llmusage)
charging = await llm_charging(sor, llm.ppid, llmusage)
if charging:
llmusage.amount = charging.amount
llmusage.cost = charging.cost

View File

@ -193,7 +193,7 @@ class BufferedLLMs:
dates.append(today)
cnt = len(dates)
if cnt > 2:
for i in range(0, cnt -2):
for i in range(0, cnt -2)
dat = dates[i]
del BufferedLLMs.llms[f'{llmid}.{dat}']
dates = dates[-2:]
@ -211,9 +211,13 @@ async def get_llm(llmid):
return await bllms.get_llm(llmid)
async def get_owner_userid(sor, llm):
env = ServerEnv()
userid = await env.uapi_data.get_calluserid(llm.uappid, orgid=llm.ownerid)
return userid
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
async def write_llmusage(llmusage):
env = ServerEnv()