50 lines
2.2 KiB
Python
50 lines
2.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Мок LLM-сервера (OpenAI-совместимый /v1/chat/completions) для проверки конвейера.
|
|
Возвращает одну-две правки пунктуации, привязываясь к первому абзацу чанка.
|
|
Только для локальной проверки проводки: качество пунктуации проверяется на реальной модели"""
|
|
import json
|
|
import re
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
|
|
class H(BaseHTTPRequestHandler):
|
|
def log_message(self, *a):
|
|
pass
|
|
|
|
def do_GET(self):
|
|
self.send_response(200)
|
|
self.send_header("Content-Type", "application/json")
|
|
self.end_headers()
|
|
self.wfile.write(json.dumps({"data": [{"id": "mock"}]}).encode())
|
|
|
|
def do_POST(self):
|
|
ln = int(self.headers.get("Content-Length", 0))
|
|
req = json.loads(self.rfile.read(ln) or b"{}")
|
|
user = ""
|
|
for m in req.get("messages", []):
|
|
if m.get("role") == "user":
|
|
user = m.get("content", "")
|
|
# блоки "Абзац N: текст"
|
|
blocks = re.findall(r"Абзац (\d+): (.*?)(?:\n\n|$)", user, re.S)
|
|
out = []
|
|
for pnum, text in blocks:
|
|
m = re.search(r"\b(сообщил|отметил|доложил|\w+)\s+что\b", text)
|
|
if m:
|
|
frag = m.group(0)
|
|
pos = text.find(frag)
|
|
fixed = frag.rsplit(" что", 1)
|
|
fixed = fixed[0] + ", что" if fixed[0].endswith((", ", "")) is False else fixed[0] + " что"
|
|
out.append({"para": int(pnum),
|
|
"fragment": frag,
|
|
"fixed": frag.split(" что")[0] + ", что",
|
|
"why": "запятая перед «что»"})
|
|
body = {"choices": [{"message": {"role": "assistant",
|
|
"content": json.dumps(out, ensure_ascii=False)}}]}
|
|
self.send_response(200)
|
|
self.send_header("Content-Type", "application/json")
|
|
self.end_headers()
|
|
self.wfile.write(json.dumps(body, ensure_ascii=False).encode())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
HTTPServer(("127.0.0.1", 8082), H).serve_forever() |