This commit is contained in:
yumoqing 2025-07-10 13:39:33 +08:00
parent 46d9621315
commit 371972f1bb
4 changed files with 19 additions and 9 deletions

Binary file not shown.

Binary file not shown.

View File

@ -18,10 +18,13 @@ async def main():
uapi = UAPI() uapi = UAPI()
params = { params = {
'baseurl':'https://qianfan.baidubce.com', 'baseurl':'https://qianfan.baidubce.com',
'model':'ernie-3.5-8k', 'model':'deepseek-v3',
'prompt':'who are you', 'prompt':'北京今天天气如何,适合跑步吗?',
} }
async for line in uapi('MRXYc49LwTjyTLuOVk89R', '0', params=params): upapiid = 'R47xUJay76dCCt1sLmWvE' # 百度智能搜索
# MRXYc49LwTjyTLuOVk89R 百度文生文
async for line in uapi(upapiid, '0', params=params):
print(line) print(line)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -98,14 +98,15 @@ class UAPI:
method = api.httpmethod method = api.httpmethod
headers = await self.rendertmpl(api.headers) headers = await self.rendertmpl(api.headers)
headers = json.loads(headers) headers = json.loads(headers)
body = await self.rendertmpl(api.body) body = await self.rendertmpl(api.data)
if body: if body:
bdy = json.loads(body) bdy = json.loads(body)
bdy['stream'] = True bdy['stream'] = True
body = bdy.dumps(bdy, ensure_ascii=False) body = json.dumps(bdy, ensure_ascii=False)
_params = await self.rendertmpl(api.params) _params = await self.rendertmpl(api.params)
if _params: if _params:
_params = json.loads(_params) _params = json.loads(_params)
debug(f'{headers=}, {body=}. {method=}, {url=}')
shc = StreamHttpClient() shc = StreamHttpClient()
gen = shc(method, url, gen = shc(method, url,
headers=headers, headers=headers,
@ -114,11 +115,17 @@ class UAPI:
chunk_match = api.chunk_match or '' chunk_match = api.chunk_match or ''
cmlen = len(chunk_match) cmlen = len(chunk_match)
async for line in liner(gen): async for line in liner(gen):
line = line.decode('utf-8')
if line.startswith(chunk_match): if line.startswith(chunk_match):
cvt_line = self.streamline_handle(line[cmlen:], line = line[cmlen:]
cvt_line = await self.streamline_handle(line,
api.streamresponse) api.streamresponse)
if cvt_line is not None: if cvt_line is not None:
yield cvt_line yield cvt_line
else:
debug(f'{line=} after convert is None')
else:
debug(f'{chunk_match=},{line=} not matched')
async def streamline_handle(self, line, resptmpl): async def streamline_handle(self, line, resptmpl):
try: try:
@ -127,7 +134,7 @@ class UAPI:
jstr = await self.rendertmpl(resptmpl, params=dic) jstr = await self.rendertmpl(resptmpl, params=dic)
jstr += '\n' jstr += '\n'
else: else:
jstr = json.dumps(self.resp_json, ensure_ascii=False) + '\n' jstr = json.dumps(dic, ensure_ascii=False) + '\n'
return jstr return jstr
except Exception as e: except Exception as e:
exception(f'{line=}\n{e=},{format_exc()}') exception(f'{line=}\n{e=},{format_exc()}')
@ -175,8 +182,8 @@ where a.id = b.upappid
raise e raise e
r = recs[0] r = recs[0]
return DictObject(**{ return DictObject(**{
'apikey':r.apikey, 'apikey':password_decode(r.apikey),
'secretkey':r.secretkey, 'secretkey':password_decode(r.secretkey),
'apisetid': r.apisetid, 'apisetid': r.apisetid,
'auth_apiid': r.auth_apiid, 'auth_apiid': r.auth_apiid,
'myappid': r.myappid 'myappid': r.myappid