Files
buryan-infra/ops/scripts/prism_note_audit_worker.py
T
root fec4a2a528
lint / hygiene (push) Failing after 18s
standard / check (push) Successful in 8s
[STANDARD] Ночной пересчёт соответствия 2026-11-26
2026-11-26 04:40:02 +03:00

75 lines
3.3 KiB
Python

import json
import re
import sys
import time
import psycopg2
sys.path.insert(0, "/opt/hub-core")
import llm_fallback as L
import rag
conn = psycopg2.connect(rag.DSN)
cur = conn.cursor()
cur.execute("update prism.tasks set status='pending', started_at=null, worker=null where kind='note_audit' and status='running'")
conn.commit()
print("note audit worker start", flush=True)
while True:
cur.execute("""
update prism.tasks t set status='running', worker='229-7b', started_at=now()
where t.id = (
select id from prism.tasks where kind='note_audit' and status='pending'
order by id limit 1 for update skip locked
) returning t.id, t.model_id
""")
row = cur.fetchone()
conn.commit()
if not row:
print("no note_audit tasks", flush=True)
break
task_id, mid = row
try:
cur.execute("select vendor,name from prism.models where id=%s", (mid,))
m = cur.fetchone()
vendor, name = m
cur.execute("""select string_agg(kind || ': ' || text, E'\n' order by kind)
from prism.model_notes where model_id=%s""", (mid,))
notes = (cur.fetchone() or [""])[0] or ""
cur.execute("""select string_agg(key || '=' || coalesce(value_text,''), ', ' order by key)
from (select distinct key,value_text from prism.specs where model_id=%s limit 25) q""", (mid,))
specs = (cur.fetchone() or [""])[0] or ""
prompt = f"""Проверь, подтверждаются ли технические заметки фактами из спецификаций.
Не придумывай. Верни только JSON:
{{"ok":true/false,"issues":["..."]}}
Модель: {name}
Вендор: {vendor}
Заметки:
{notes[:2500]}
Спецификации:
{specs[:1500]}
"""
r = L.ask(prompt, max_tokens=350, temperature=0.0,
chain=[{"name": "qwen2.5:7b", "kind": "ollama", "timeout": 300}])
mm = re.search(r"\{.*\}", r["text"], re.S)
d = json.loads(mm.group(0)) if mm else {}
good = bool(d.get("ok"))
issues = d.get("issues") or []
cur.execute("""insert into prism.note_audits(model_id,ok,issues,model)
values (%s,%s,%s::jsonb,'qwen2.5:7b')""",
(mid, good, json.dumps(issues, ensure_ascii=False)))
cur.execute("""insert into prism.runs(model_id,kind,status,worker,model,stats,started_at,finished_at)
values (%s,'note_audit7b','ok','229',%s,%s,now(),now())""",
(mid, "qwen2.5:7b", json.dumps({"issues": len(issues), "ok": good})))
cur.execute("update prism.tasks set status='done', finished_at=now() where id=%s", (task_id,))
print("done", task_id, "ok", good, "issues", len(issues), flush=True)
except Exception as e:
cur.execute("update prism.tasks set status='error', error=%s, finished_at=now() where id=%s",
(str(e)[:300], task_id))
cur.execute("""insert into prism.runs(model_id,kind,status,worker,model,error,started_at,finished_at)
values (%s,'note_audit7b','error','229',%s,%s,now(),now())""",
(mid, "qwen2.5:7b", str(e)[:300]))
print("error", task_id, str(e)[:150], flush=True)
conn.commit()
time.sleep(1)
conn.close()