Files
aigc-home/e2e/reference/fake_llm.py
T

22 lines
1.5 KiB
Python

import json,time
from pathlib import Path
from http.server import BaseHTTPRequestHandler,ThreadingHTTPServer
root=Path(__file__).resolve().parents[1]/'shots/reference'
root.mkdir(parents=True,exist_ok=True)
class Handler(BaseHTTPRequestHandler):
def log_message(self,*args):pass
def do_POST(self):
payload=json.loads(self.rfile.read(int(self.headers.get('Content-Length','0'))))
with (root/'llm-requests.jsonl').open('a') as f:f.write(json.dumps(payload,ensure_ascii=False)+'\n')
content='【隔离测试模型回复】\n纸船带回了一片树叶。女孩望向海面,知道故事才刚刚开始。\n\n已收到任务与指定技能,后续可以继续修改。'
if payload.get('stream'):
self.send_response(200);self.send_header('Content-Type','text/event-stream');self.end_headers()
for chunk in [content[:16],content[16:]]:
body={'id':'reference-check','choices':[{'index':0,'delta':{'content':chunk},'finish_reason':None}]}
self.wfile.write(('data: '+json.dumps(body,ensure_ascii=False)+'\n\n').encode());self.wfile.flush();time.sleep(.05)
self.wfile.write(b'data: [DONE]\n\n')
else:
body={'id':'reference-check','choices':[{'index':0,'message':{'role':'assistant','content':content},'finish_reason':'stop'}]}
data=json.dumps(body,ensure_ascii=False).encode();self.send_response(200);self.send_header('Content-Type','application/json');self.end_headers();self.wfile.write(data)
ThreadingHTTPServer(('127.0.0.1',18189),Handler).serve_forever()