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

View File

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