fix: retry write_llmusage with *1,*2 on duplicate; gen() catch+refund but yield error not raise
This commit is contained in:
parent
670325e8eb
commit
e5d27bc6bd
@ -93,6 +93,18 @@ async def uapi_request(request, llm, callerid, callerorgid, params_kw=None):
|
|||||||
llmusage.accounting_status = 'created'
|
llmusage.accounting_status = 'created'
|
||||||
await write_llmusage(llmusage)
|
await write_llmusage(llmusage)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
# Retry on duplicate key: append *1, *2, ...
|
||||||
|
retry = 0
|
||||||
|
while 'Duplicate entry' in str(e) and retry < 3:
|
||||||
|
retry += 1
|
||||||
|
llmusage.id = f'{luid}*{retry}'
|
||||||
|
try:
|
||||||
|
await write_llmusage(llmusage)
|
||||||
|
exception(f'write_llmusage retry {retry} succeeded with id={llmusage.id}')
|
||||||
|
e = None
|
||||||
|
break
|
||||||
|
except Exception as e2:
|
||||||
|
e = e2
|
||||||
# Refund balance reservation on failure
|
# Refund balance reservation on failure
|
||||||
try:
|
try:
|
||||||
from .balance import refund_balance
|
from .balance import refund_balance
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
async def gen():
|
async def gen():
|
||||||
env = request._run_ns.copy()
|
env = request._run_ns.copy()
|
||||||
f = partial(inference_generator, request, params_kw=params_kw)
|
f = partial(inference_generator, request, params_kw=params_kw)
|
||||||
|
try:
|
||||||
if params_kw.stream:
|
if params_kw.stream:
|
||||||
async for l in f():
|
async for l in f():
|
||||||
yield f'data: {l}\\n'
|
yield f'data: {l}\\n'
|
||||||
@ -8,6 +9,16 @@ async def gen():
|
|||||||
else:
|
else:
|
||||||
async for l in f():
|
async for l in f():
|
||||||
yield l
|
yield l
|
||||||
|
except Exception as e:
|
||||||
|
luid = params_kw.get('_luid')
|
||||||
|
if luid:
|
||||||
|
try:
|
||||||
|
await env.refund_balance(luid)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
import json as _json
|
||||||
|
err = _json.dumps({"error": str(e), "status": "FAILED"}, ensure_ascii=False)
|
||||||
|
yield f'data: {err}\\n' if params_kw.stream else err
|
||||||
|
|
||||||
debug_params('params_kw', params_kw)
|
debug_params('params_kw', params_kw)
|
||||||
catelogid = params_kw.catelogid or 't2t'
|
catelogid = params_kw.catelogid or 't2t'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user