2025-07-18 15:50:49 +08:00

58 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python
from traceback import format_exc
import asyncio
import codecs
import json
import argparse
from appPublic.streamhttpclient import liner, StreamHttpClient
from appPublic.log import MyLogger
def user_message(prompt, fn=None):
x = ''
if fn:
x = user_file(fn)
return prompt + x
def user_file(fn):
with codecs.open(fn, 'r', 'utf-8') as f:
return f.read()
async def main():
parser = argparse.ArgumentParser(prog='devops')
parser.add_argument('-f', '--file')
parser.add_argument('-p', '--prompt')
parser.add_argument('-s', '--sys_prompt')
parser.add_argument('-m', '--model')
parser.add_argument('url')
args = parser.parse_args()
d = {
'model': args.model,
'stream': True,
'prompt': user_message(args.prompt, args.file),
'sys_prompt':args.sys_prompt
}
hc = StreamHttpClient()
headers = {
'Content-Type': 'application/json'
}
i = 0
buffer = ''
reco = hc('POST', args.url, headers=headers, data=json.dumps(d))
async for chunk in liner(reco):
chunk = chunk[6:]
if chunk != '[DONE]':
try:
f = json.loads(chunk)
except Exception as e:
print(f'****{chunk=} error {e} {format_exc()}')
continue
if not f['choices'][0]['finish_reason']:
print(f['choices'][0]['delta']['content'], end='', flush=True)
else:
pass
print('\n\n')
if __name__ == '__main__':
MyLogger('null', levelname='error', logfile='/dev/null')
asyncio.new_event_loop().run_until_complete(main())