This commit is contained in:
yumoqing 2024-04-17 18:31:29 +08:00
parent 20805f8a53
commit e883fbce20

View File

@ -1,5 +1,6 @@
import re import re
import json import json
from traceback import print_exc
from aiohttp import web from aiohttp import web
from appPublic.dictObject import DictObject from appPublic.dictObject import DictObject
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
@ -17,17 +18,34 @@ class LlmProxy:
self.ac = ArgsConvert('${', '}') self.ac = ArgsConvert('${', '}')
def line_chunk_match(self, l): def line_chunk_match(self, l):
if self.chunk_match: if self.api.chunk_match:
match = re.search(self.chunk_match, l) match = re.search(self.api.chunk_match, l)
if match: if match:
return match.group(1) return match.group(1)
return l return l
async def write_chunk(self, l): async def write_chunk(self, ll):
def eq(a, b):
return a == b
def ne(a, b):
return a != b
opfuncs = {
'==':eq,
'!=':ne
}
try: try:
print('write_chunk(),l=', l) print('write_chunk(),l=', ll)
l = self.line_chunk_match(l) l = self.line_chunk_match(ll)
d = DictObject(** json.loads(l)) d = DictObject(** json.loads(l))
if self.api.chunk_filter:
v = d.get_data_by_keys(self.api.chunk_filter.name)
v1 = self.api.chunk_filter.value
op = self.api.chunk_filter.op
f = opfuncs.get(op)
if f and f(v,v1):
print('filtered', v, op, v1, f)
return
j = {} j = {}
for r in self.api.resp or []: for r in self.api.resp or []:
j[r.name] = d.get_data_by_keys(r.value); j[r.name] = d.get_data_by_keys(r.value);
@ -37,7 +55,8 @@ class LlmProxy:
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'Write_chunk("{l}") errpr:{e=}') print(f'Error:Write_chunk(),{l=} error:{e=}')
print_exc()
async def stream_handle(self, chunk): async def stream_handle(self, chunk):
chunk = chunk.decode('utf-8') chunk = chunk.decode('utf-8')