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 json
from traceback import print_exc
from aiohttp import web
from appPublic.dictObject import DictObject
from appPublic.httpclient import HttpClient, RESPONSE_TEXT, RESPONSE_JSON, RESPONSE_BIN,RESPONSE_FILE, RESPONSE_STREAM
@ -17,17 +18,34 @@ class LlmProxy:
self.ac = ArgsConvert('${', '}')
def line_chunk_match(self, l):
if self.chunk_match:
match = re.search(self.chunk_match, l)
if self.api.chunk_match:
match = re.search(self.api.chunk_match, l)
if match:
return match.group(1)
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:
print('write_chunk(),l=', l)
l = self.line_chunk_match(l)
print('write_chunk(),l=', ll)
l = self.line_chunk_match(ll)
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 = {}
for r in self.api.resp or []:
j[r.name] = d.get_data_by_keys(r.value);
@ -37,7 +55,8 @@ class LlmProxy:
await self.resp.write(bin)
await self.resp.drain()
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):
chunk = chunk.decode('utf-8')