bugfix
This commit is contained in:
parent
f74c3726e9
commit
30150a2ea8
@ -1,10 +1,12 @@
|
|||||||
import re
|
import re
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
from traceback import print_exc
|
from traceback import format_exc
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
from appPublic.dictObject import DictObject
|
from appPublic.dictObject import DictObject
|
||||||
|
from appPublic.log import debug, info, exception, error
|
||||||
from appPublic.httpclient import HttpClient, RESPONSE_TEXT, RESPONSE_JSON, RESPONSE_BIN,RESPONSE_FILE, RESPONSE_STREAM
|
from appPublic.httpclient import HttpClient, RESPONSE_TEXT, RESPONSE_JSON, RESPONSE_BIN,RESPONSE_FILE, RESPONSE_STREAM
|
||||||
|
from appPublic.registerfunction import RegisterFunction
|
||||||
from appPublic.argsConvert import ArgsConvert
|
from appPublic.argsConvert import ArgsConvert
|
||||||
|
|
||||||
def encode_imagefile(fn):
|
def encode_imagefile(fn):
|
||||||
@ -55,15 +57,14 @@ class StreamLlmProxy:
|
|||||||
f = opfuncs.get(op)
|
f = opfuncs.get(op)
|
||||||
if f and f(v,v1):
|
if f and f(v,v1):
|
||||||
j[self.api.chunk_filter.field] = ''
|
j[self.api.chunk_filter.field] = ''
|
||||||
print('filtered j=', j)
|
|
||||||
|
|
||||||
jstr = json.dumps(j) + '\n'
|
jstr = json.dumps(j) + '\n'
|
||||||
bin = jstr.encode('utf-8')
|
bin = jstr.encode('utf-8')
|
||||||
await self.resp.write(bin)
|
await self.resp.write(bin)
|
||||||
await self.resp.drain()
|
await self.resp.drain()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'Error:Write_chunk(),{l=} error:{e=}')
|
tb = format_exc()
|
||||||
print_exc()
|
exception(f'Error:Write_chunk(),{l=} error:{e=}{tb}')
|
||||||
|
|
||||||
async def stream_handle(self, chunk):
|
async def stream_handle(self, chunk):
|
||||||
print('chunk=', chunk)
|
print('chunk=', chunk)
|
||||||
@ -84,6 +85,18 @@ class StreamLlmProxy:
|
|||||||
return await f(apiname, self.user)
|
return await f(apiname, self.user)
|
||||||
raise Exception('get_llm_user_apikey() function not found in ServerEnv')
|
raise Exception('get_llm_user_apikey() function not found in ServerEnv')
|
||||||
|
|
||||||
|
async def get_apidata(self, parts, params={}):
|
||||||
|
ret = {}
|
||||||
|
for d in parts or []:
|
||||||
|
v = d['value']
|
||||||
|
if params != {}:
|
||||||
|
v = self.datalize(v, params)
|
||||||
|
if d.get('convertor'):
|
||||||
|
rf = RegisterFunction()
|
||||||
|
v = await rf.exe(d['convertor'], v)
|
||||||
|
ret[d['name']] = v
|
||||||
|
return ret
|
||||||
|
|
||||||
async def do_auth(self, request):
|
async def do_auth(self, request):
|
||||||
d = self.desc.auth
|
d = self.desc.auth
|
||||||
self.data = self.get_data(self.name)
|
self.data = self.get_data(self.name)
|
||||||
@ -92,21 +105,12 @@ class StreamLlmProxy:
|
|||||||
self.data = await self.get_apikey(self.name)
|
self.data = await self.get_apikey(self.name)
|
||||||
if self.data is None:
|
if self.data is None:
|
||||||
raise Exception(f'user({self.user}) do not has a apikey for {self.name}')
|
raise Exception(f'user({self.user}) do not has a apikey for {self.name}')
|
||||||
|
params = self.data
|
||||||
method = d.get('method', 'POST')
|
method = d.get('method', 'POST')
|
||||||
headers = {}
|
_headers = self.get_apidata(d.get('headers', []), params)
|
||||||
for h in d.get('headers',{}):
|
_data = self.get_apidata(d.get('data', []), params)
|
||||||
headers[h.get('name')] = h.get('value')
|
_params = self.get_apidata(d.get('params',[]), params)
|
||||||
mydata = {}
|
|
||||||
for p in d.data or []:
|
|
||||||
mydata[p.get('name')] = p.get('value')
|
|
||||||
myparams = {}
|
|
||||||
for p in d.params or []:
|
|
||||||
myparams[p.get('name')] = p.get('value')
|
|
||||||
url = d.get('url')
|
url = d.get('url')
|
||||||
params = {}
|
|
||||||
_params = self.datalize(myparams, params)
|
|
||||||
_headers = self.datalize(headers, params)
|
|
||||||
_data = self.datalize(mydata, params)
|
|
||||||
hc = HttpClient()
|
hc = HttpClient()
|
||||||
resp_data = await hc.request(url, method, response_type=RESPONSE_JSON,
|
resp_data = await hc.request(url, method, response_type=RESPONSE_JSON,
|
||||||
params=_params,
|
params=_params,
|
||||||
@ -153,23 +157,18 @@ class StreamLlmProxy:
|
|||||||
self.data = await self.get_apikey(self.name)
|
self.data = await self.get_apikey(self.name)
|
||||||
|
|
||||||
assert d.get('url')
|
assert d.get('url')
|
||||||
method = d.get('method', 'POST')
|
|
||||||
headers = {}
|
|
||||||
for h in d.get('headers',{}):
|
|
||||||
headers[h.get('name')] = h.get('value')
|
|
||||||
mydata = {}
|
|
||||||
for p in d.get('data', {}):
|
|
||||||
mydata[p.get('name')] = p.get('value')
|
|
||||||
myparams = {}
|
|
||||||
for p in d.get('params', {}):
|
|
||||||
myparams[p.get('name')] = p.get('value')
|
|
||||||
url = d.get('url')
|
url = d.get('url')
|
||||||
_params = self.datalize(myparams, params)
|
method = d.get('method', 'POST')
|
||||||
_headers = self.datalize(headers, params)
|
params1 = self.data
|
||||||
_data = self.datalize(mydata, params)
|
params1.update(params)
|
||||||
|
params = params1
|
||||||
|
method = d.get('method', 'POST')
|
||||||
|
_headers = self.get_apidata(d.get('headers', []), params)
|
||||||
|
_data = self.get_apidata(d.get('data', []), params)
|
||||||
|
_params = self.get_apidata(d.get('params',[]), params)
|
||||||
response_type = RESPONSE_STREAM
|
response_type = RESPONSE_STREAM
|
||||||
hc = HttpClient()
|
hc = HttpClient()
|
||||||
print(f'{url=},{method=},{_params=},{_data=},{_headers=}')
|
debug(f'{url=},{method=},{_params=},{_data=},{_headers=}')
|
||||||
resp_data = await hc.request(url, method, response_type=response_type,
|
resp_data = await hc.request(url, method, response_type=response_type,
|
||||||
params=_params,
|
params=_params,
|
||||||
data=None if _data == {} else json.dumps(_data),
|
data=None if _data == {} else json.dumps(_data),
|
||||||
@ -203,36 +202,33 @@ class SyncLlmProxy(StreamLlmProxy):
|
|||||||
|
|
||||||
assert d.get('url')
|
assert d.get('url')
|
||||||
method = d.get('method', 'POST')
|
method = d.get('method', 'POST')
|
||||||
headers = {}
|
|
||||||
for h in d.get('headers',{}):
|
|
||||||
headers[h.get('name')] = h.get('value')
|
|
||||||
mydata = {}
|
|
||||||
for p in d.get('data', {}):
|
|
||||||
mydata[p.get('name')] = p.get('value')
|
|
||||||
myparams = {}
|
|
||||||
for p in d.get('params', {}):
|
|
||||||
myparams[p.get('name')] = p.get('value')
|
|
||||||
url = d.get('url')
|
url = d.get('url')
|
||||||
_params = self.datalize(myparams, params)
|
params1 = self.data
|
||||||
_headers = self.datalize(headers, params)
|
params1.update(params)
|
||||||
_data = self.datalize(mydata, params)
|
params = params1
|
||||||
|
method = d.get('method', 'POST')
|
||||||
|
_headers = self.get_apidata(d.get('headers', []), params)
|
||||||
|
_data = self.get_apidata(d.get('data', []), params)
|
||||||
|
_params = self.get_apidata(d.get('params',[]), params)
|
||||||
response_type = RESPONSE_JSON
|
response_type = RESPONSE_JSON
|
||||||
hc = HttpClient()
|
hc = HttpClient()
|
||||||
print(f'{url=},{method=},{_params=},{_data=},{_headers=}')
|
debug(f'{url=},{method=},{_params=},{_data=},{_headers=}')
|
||||||
resp_data = await hc.request(url, method, response_type=response_type,
|
resp_data = await hc.request(url, method, response_type=response_type,
|
||||||
params=_params,
|
params=_params,
|
||||||
data=None if _data == {} else json.dumps(_data),
|
data=None if _data == {} else json.dumps(_data),
|
||||||
headers=_headers)
|
headers=_headers)
|
||||||
print(f'{resp_data=}')
|
debug(f'{resp_data=}')
|
||||||
resp_data = DictObject(resp_data)
|
|
||||||
if resp_data is None:
|
if resp_data is None:
|
||||||
return {
|
return {
|
||||||
"status":"Error",
|
"status":"Error",
|
||||||
"message":f'{mapi} not defined'
|
"message":f'{mapi} not defined'
|
||||||
}
|
}
|
||||||
|
resp_data = DictObject(resp_data)
|
||||||
return self.convert_resp(resp_data)
|
return self.convert_resp(resp_data)
|
||||||
|
|
||||||
def convert_resp(self, resp):
|
def convert_resp(self, resp):
|
||||||
|
if self.api.resp is None:
|
||||||
|
return resp
|
||||||
j = {}
|
j = {}
|
||||||
for r in self.api.resp or []:
|
for r in self.api.resp or []:
|
||||||
j[r.name] = resp.get_data_by_keys(r.value);
|
j[r.name] = resp.get_data_by_keys(r.value);
|
||||||
@ -240,8 +236,6 @@ class SyncLlmProxy(StreamLlmProxy):
|
|||||||
|
|
||||||
|
|
||||||
class AsyncLlmProxy(StreamLlmProxy):
|
class AsyncLlmProxy(StreamLlmProxy):
|
||||||
pass
|
|
||||||
class AsyncLlmProxy:
|
|
||||||
async def __call__(self, request, params):
|
async def __call__(self, request, params):
|
||||||
self.user = await self.processor.run_ns.get_user()
|
self.user = await self.processor.run_ns.get_user()
|
||||||
mapi = params.mapi
|
mapi = params.mapi
|
||||||
@ -262,23 +256,18 @@ class AsyncLlmProxy:
|
|||||||
self.data = await self.get_apikey(self.name)
|
self.data = await self.get_apikey(self.name)
|
||||||
|
|
||||||
assert d.get('url')
|
assert d.get('url')
|
||||||
method = d.get('method', 'POST')
|
|
||||||
headers = {}
|
|
||||||
for h in d.get('headers',{}):
|
|
||||||
headers[h.get('name')] = h.get('value')
|
|
||||||
mydata = {}
|
|
||||||
for p in d.get('data', {}):
|
|
||||||
mydata[p.get('name')] = p.get('value')
|
|
||||||
myparams = {}
|
|
||||||
for p in d.get('params', {}):
|
|
||||||
myparams[p.get('name')] = p.get('value')
|
|
||||||
url = d.get('url')
|
url = d.get('url')
|
||||||
_params = self.datalize(myparams, params)
|
method = d.get('method', 'POST')
|
||||||
_headers = self.datalize(headers, params)
|
params1 = self.data
|
||||||
_data = self.datalize(mydata, params)
|
params1.update(params)
|
||||||
|
params = params1
|
||||||
|
method = d.get('method', 'POST')
|
||||||
|
_headers = self.get_apidata(d.get('headers', []), params)
|
||||||
|
_data = self.get_apidata(d.get('data', []), params)
|
||||||
|
_params = self.get_apidata(d.get('params',[]), params)
|
||||||
response_type = RESPONSE_JSON
|
response_type = RESPONSE_JSON
|
||||||
hc = HttpClient()
|
hc = HttpClient()
|
||||||
print(f'{url=},{method=},{_params=},{_data=},{_headers=}')
|
debug(f'{url=},{method=},{_params=},{_data=},{_headers=}')
|
||||||
resp_data = await hc.request(url, method, response_type=response_type,
|
resp_data = await hc.request(url, method, response_type=response_type,
|
||||||
params=_params,
|
params=_params,
|
||||||
data=None if _data == {} else json.dumps(_data),
|
data=None if _data == {} else json.dumps(_data),
|
||||||
|
@ -2,7 +2,7 @@ asyncio
|
|||||||
aiofiles
|
aiofiles
|
||||||
aiodns
|
aiodns
|
||||||
# cchardet
|
# cchardet
|
||||||
aiohttp
|
aiohttp==3.10.10
|
||||||
aiohttp_session
|
aiohttp_session
|
||||||
aiohttp_auth_autz
|
aiohttp_auth_autz
|
||||||
aiohttp-cors
|
aiohttp-cors
|
||||||
|
Loading…
Reference in New Issue
Block a user