diff --git a/ahserver/baseProcessor.py b/ahserver/baseProcessor.py index a66fefe..ab64993 100755 --- a/ahserver/baseProcessor.py +++ b/ahserver/baseProcessor.py @@ -97,13 +97,19 @@ class BaseProcessor(AppLogger): if self.retResponse is not None: self.set_response_headers(self.retResponse) return self.retResponse - elif type(self.content) == type({}) : + elif isinstance(self.content, DictObject): self.content = json.dumps(self.content, indent=4) jsonflg = True - elif type(self.content) == type([]): + elif isinstance(self.content, dict): self.content = json.dumps(self.content, indent=4) 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['Content-Length'] = str(len(self.content)) resp = Response(body=self.content,headers=self.headers) diff --git a/ahserver/globalEnv.py b/ahserver/globalEnv.py index 1acbc3a..22578d2 100755 --- a/ahserver/globalEnv.py +++ b/ahserver/globalEnv.py @@ -13,6 +13,7 @@ from openpyxl import Workbook from tempfile import mktemp from appPublic.jsonConfig import getConfig +from appPublic.dictObject import DictObject from appPublic.Singleton import GlobalEnv from appPublic.argsConvert import ArgsConvert from appPublic.timeUtils import str2Date,str2Datetime,curDatetime,getCurrentTimeStamp,curDateString, curTimeString @@ -144,19 +145,12 @@ def file_download(request,path,name,coding='utf8'): def initEnv(): pool = DBPools() g = ServerEnv() + set_builtins() g.configValue = configValue g.visualcoding = visualcoding g.uriop = URIOp g.isNone = isNone - g.len = len - g.print = print 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.time = time g.curDateString = curDateString @@ -191,7 +185,7 @@ def initEnv(): g.current_fileno = current_fileno g.get_config_value = get_config_value g.get_definition = get_definition - set_builtins() + g.DictObject = DictObject def set_builtins(): all_builtins = [ i for i in dir(builtins) if not i.startswith('_')] @@ -199,4 +193,3 @@ def set_builtins(): gg = globals() for l in all_builtins: exec(f'g["{l}"] = {l}',{'g':g}) - print(f'{g.sorted=}, {sorted=}, globalEnv.py') diff --git a/ahserver/llm_client.py b/ahserver/llm_client.py index 724f0bf..b709221 100644 --- a/ahserver/llm_client.py +++ b/ahserver/llm_client.py @@ -24,22 +24,18 @@ class LlmProxy: return l async def write_chunk(self, l): - l = self.line_chunk_match(l) - d = DictObject(** json.loads(l)) - content = d.get_data_by_keys(self.api.content_keys) - finished = d.get_data_by_keys(self.api.finished_keys) - tokens = {} - if finished: - tokens = d.get_data_by_keys(self.api.tokens_keys) + try: + l = self.line_chunk_match(l) + d = DictObject(** json.loads(l)) + j = {} + for r in self.api.resp or []: + j[r.name] = d.get_data_by_keys(r.value); - j = { - 'content':content, - 'finish':False, - 'token':tokens - } - jstr = json.dumps(j) + '\n' - bin = jstr.encode('utf-8') - await self.resp.write(bin) + jstr = json.dumps(j) + '\n' + bin = jstr.encode('utf-8') + await self.resp.write(bin) + except Exception as e: + print(f'Write_chunk("{l}") errpr:{e=}') async def stream_handle(self, chunk): chunk = chunk.decode('utf-8') @@ -53,22 +49,21 @@ class LlmProxy: continue 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 if f: # 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') async def do_auth(self, request): d = self.desc.auth - user = await self.processor.run_ns.get_user() - self.data = self.get_data(self.name, user) + self.data = self.get_data(self.name) if self.data.authed: return - self.data = await self.get_apikey(self.name, user) + self.data = await self.get_apikey(self.name) 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') headers = {} for h in d.get('headers',{}): @@ -93,24 +88,25 @@ class LlmProxy: for sd in d.set_data: self.data[sd.name] = resp_data.get_data_by_keys(sd.field) 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): - if user is None: - user = 'anoumous' - return apiname + '_a_' + user + def data_key(self, apiname): + if self.user is None: + self.user = 'anonymous' + return apiname + '_a_' + self.user - def set_data(self, apiname, user, data): + def set_data(self, apiname, data): request = self.processor.run_ns.request 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 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): + self.user = await self.processor.run_ns.get_user() mapi = params.mapi stream = params.stream self.resp = web.StreamResponse() @@ -125,6 +121,9 @@ class LlmProxy: self.chunk_match = d.chunk_match if self.api.need_auth and self.auth_api: await self.do_auth(request) + else: + self.data = await self.get_apikey(self.name) + assert d.get('url') method = d.get('method', 'POST') headers = {} @@ -137,6 +136,7 @@ class LlmProxy: for p in d.get('params', {}): myparams[p.get('name')] = p.get('value') url = d.get('url') + print(f'show me self.data={self.data}') _params = self.datalize(myparams, params) _headers = self.datalize(headers, params) _data = self.datalize(mydata, params)