This commit is contained in:
yumoqing 2024-04-11 09:57:54 +08:00
parent 5f00823b3c
commit 1b53d9e81d
3 changed files with 42 additions and 43 deletions

View File

@ -97,13 +97,19 @@ class BaseProcessor(AppLogger):
if self.retResponse is not None: if self.retResponse is not None:
self.set_response_headers(self.retResponse) self.set_response_headers(self.retResponse)
return self.retResponse return self.retResponse
elif type(self.content) == type({}) : elif isinstance(self.content, DictObject):
self.content = json.dumps(self.content, indent=4) self.content = json.dumps(self.content, indent=4)
jsonflg = True jsonflg = True
elif type(self.content) == type([]): elif isinstance(self.content, dict):
self.content = json.dumps(self.content, indent=4) self.content = json.dumps(self.content, indent=4)
jsonflg = True jsonflg = True
elif type(self.content) == type(b''): elif isinstance(self.content, list):
self.content = json.dumps(self.content, indent=4)
jsonflg = True
elif isinstance(self.content, tuple):
self.content = json.dumps(self.content, indent=4)
jsonflg = True
elif isinstance(self.content, bytes):
self.headers['Access-Control-Expose-Headers'] = 'Set-Cookie' self.headers['Access-Control-Expose-Headers'] = 'Set-Cookie'
self.headers['Content-Length'] = str(len(self.content)) self.headers['Content-Length'] = str(len(self.content))
resp = Response(body=self.content,headers=self.headers) resp = Response(body=self.content,headers=self.headers)

View File

@ -13,6 +13,7 @@ from openpyxl import Workbook
from tempfile import mktemp from tempfile import mktemp
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
from appPublic.dictObject import DictObject
from appPublic.Singleton import GlobalEnv from appPublic.Singleton import GlobalEnv
from appPublic.argsConvert import ArgsConvert from appPublic.argsConvert import ArgsConvert
from appPublic.timeUtils import str2Date,str2Datetime,curDatetime,getCurrentTimeStamp,curDateString, curTimeString from appPublic.timeUtils import str2Date,str2Datetime,curDatetime,getCurrentTimeStamp,curDateString, curTimeString
@ -144,19 +145,12 @@ def file_download(request,path,name,coding='utf8'):
def initEnv(): def initEnv():
pool = DBPools() pool = DBPools()
g = ServerEnv() g = ServerEnv()
set_builtins()
g.configValue = configValue g.configValue = configValue
g.visualcoding = visualcoding g.visualcoding = visualcoding
g.uriop = URIOp g.uriop = URIOp
g.isNone = isNone g.isNone = isNone
g.len = len
g.print = print
g.json = json g.json = json
g.Exception = Exception
g.int = int
g.str = str
g.float = float
g.complex = complex
g.type = type
g.ArgsConvert = ArgsConvert g.ArgsConvert = ArgsConvert
g.time = time g.time = time
g.curDateString = curDateString g.curDateString = curDateString
@ -191,7 +185,7 @@ def initEnv():
g.current_fileno = current_fileno g.current_fileno = current_fileno
g.get_config_value = get_config_value g.get_config_value = get_config_value
g.get_definition = get_definition g.get_definition = get_definition
set_builtins() g.DictObject = DictObject
def set_builtins(): def set_builtins():
all_builtins = [ i for i in dir(builtins) if not i.startswith('_')] all_builtins = [ i for i in dir(builtins) if not i.startswith('_')]
@ -199,4 +193,3 @@ def set_builtins():
gg = globals() gg = globals()
for l in all_builtins: for l in all_builtins:
exec(f'g["{l}"] = {l}',{'g':g}) exec(f'g["{l}"] = {l}',{'g':g})
print(f'{g.sorted=}, {sorted=}, globalEnv.py')

View File

@ -24,22 +24,18 @@ class LlmProxy:
return l return l
async def write_chunk(self, l): async def write_chunk(self, l):
try:
l = self.line_chunk_match(l) l = self.line_chunk_match(l)
d = DictObject(** json.loads(l)) d = DictObject(** json.loads(l))
content = d.get_data_by_keys(self.api.content_keys) j = {}
finished = d.get_data_by_keys(self.api.finished_keys) for r in self.api.resp or []:
tokens = {} j[r.name] = d.get_data_by_keys(r.value);
if finished:
tokens = d.get_data_by_keys(self.api.tokens_keys)
j = {
'content':content,
'finish':False,
'token':tokens
}
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)
except Exception as e:
print(f'Write_chunk("{l}") errpr:{e=}')
async def stream_handle(self, chunk): async def stream_handle(self, chunk):
chunk = chunk.decode('utf-8') chunk = chunk.decode('utf-8')
@ -53,22 +49,21 @@ class LlmProxy:
continue continue
await self.write_chunk(l) await self.write_chunk(l)
async def get_apikey(self, apiname, user): async def get_apikey(self, apiname):
f = self.processor.run_ns.get_llm_user_apikey f = self.processor.run_ns.get_llm_user_apikey
if f: if f:
# return a DictObject instance # return a DictObject instance
return await f(apiname, 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 do_auth(self, request): async def do_auth(self, request):
d = self.desc.auth d = self.desc.auth
user = await self.processor.run_ns.get_user() self.data = self.get_data(self.name)
self.data = self.get_data(self.name, user)
if self.data.authed: if self.data.authed:
return return
self.data = await self.get_apikey(self.name, user) self.data = await self.get_apikey(self.name)
if self.data is None: if self.data is None:
raise Exception(f'user({user}) do not has a apikey for {self.name}') raise Exception(f'user({self.user}) do not has a apikey for {self.name}')
method = d.get('method', 'POST') method = d.get('method', 'POST')
headers = {} headers = {}
for h in d.get('headers',{}): for h in d.get('headers',{}):
@ -93,24 +88,25 @@ class LlmProxy:
for sd in d.set_data: for sd in d.set_data:
self.data[sd.name] = resp_data.get_data_by_keys(sd.field) self.data[sd.name] = resp_data.get_data_by_keys(sd.field)
self.data.authed = True self.data.authed = True
self.set_data(self.name, user, self.data) self.set_data(self.name, self.data)
def data_key(self, apiname, user): def data_key(self, apiname):
if user is None: if self.user is None:
user = 'anoumous' self.user = 'anonymous'
return apiname + '_a_' + user return apiname + '_a_' + self.user
def set_data(self, apiname, user, data): def set_data(self, apiname, data):
request = self.processor.run_ns.request request = self.processor.run_ns.request
app = request.app app = request.app
app.set_data(self.data_key(apiname, user), data) app.set_data(self.data_key(apiname), data)
def get_data(self, apiname, user): def get_data(self, apiname):
request = self.processor.run_ns.request request = self.processor.run_ns.request
app = request.app app = request.app
return app.get_data(self.data_key(apiname, user)) return app.get_data(self.data_key(apiname))
async def __call__(self, request, params): async def __call__(self, request, params):
self.user = await self.processor.run_ns.get_user()
mapi = params.mapi mapi = params.mapi
stream = params.stream stream = params.stream
self.resp = web.StreamResponse() self.resp = web.StreamResponse()
@ -125,6 +121,9 @@ class LlmProxy:
self.chunk_match = d.chunk_match self.chunk_match = d.chunk_match
if self.api.need_auth and self.auth_api: if self.api.need_auth and self.auth_api:
await self.do_auth(request) await self.do_auth(request)
else:
self.data = await self.get_apikey(self.name)
assert d.get('url') assert d.get('url')
method = d.get('method', 'POST') method = d.get('method', 'POST')
headers = {} headers = {}
@ -137,6 +136,7 @@ class LlmProxy:
for p in d.get('params', {}): for p in d.get('params', {}):
myparams[p.get('name')] = p.get('value') myparams[p.get('name')] = p.get('value')
url = d.get('url') url = d.get('url')
print(f'show me self.data={self.data}')
_params = self.datalize(myparams, params) _params = self.datalize(myparams, params)
_headers = self.datalize(headers, params) _headers = self.datalize(headers, params)
_data = self.datalize(mydata, params) _data = self.datalize(mydata, params)