#!/usr/bin/env python3 """LLM synthesis on top of deterministic audit facts. Evidence is embedded in the prompt.""" import json import urllib.request from pathlib import Path import psycopg2 PASS = Path("/root/.pg-superuser-pass").read_text().strip() conn = psycopg2.connect(host="192.168.1.151", dbname="rag", user="postgres", password=PASS, client_encoding="UTF8") cur = conn.cursor() cur.execute("select key,title,services,maturity,automation,leverage,criticality,evidence,gaps from audit.capabilities where run_id=1 order by key") caps = [dict(key=r[0], title=r[1], services=r[2], maturity=r[3], automation=r[4], leverage=r[5], criticality=r[6], evidence=r[7], gaps=r[8]) for r in cur.fetchall()] cur.execute("select severity,area,title,evidence from audit.findings where run_id=1 order by id") findings = [dict(severity=r[0], area=r[1], title=r[2], evidence=r[3]) for r in cur.fetchall()] compact = [{"key": c["key"], "title": c["title"], "gaps": c["gaps"], "evidence": c["evidence"]} for c in caps] compact_findings = [{"severity": f["severity"], "title": f["title"]} for f in findings] prompt = """Ты — независимый аудитор Buryans Hub. Ниже ТОЛЬКО факты и известные пробелы. Не выдумывай сервисы, метрики и владельцев. Предложи 8 возможностей развития. Для каждой: title, capability (key), hypothesis, impact 1-5, effort 1-5, risk 1-5, first_step, evidence. Верни JSON: {"opportunities":[...]}. CAPABILITIES: %s FINDINGS: %s """ % (json.dumps(compact, ensure_ascii=False)[:7000], json.dumps(compact_findings, ensure_ascii=False)[:2500]) req = urllib.request.Request("http://192.168.1.229:11434/api/chat", data=json.dumps({ "model": "qwen2.5:3b-32k", "messages": [{"role": "system", "content": "Отвечай только валидным JSON."}, {"role": "user", "content": prompt}], "stream": False, "format": "json", "options": {"temperature": 0.2, "num_predict": 1800, "num_gpu": 999}, }).encode(), headers={"Content-Type": "application/json"}, method="POST") with urllib.request.urlopen(req, timeout=300) as r: d = json.load(r) data = json.loads((d.get("message") or {}).get("content") or "{}") ops = data.get("opportunities") or [] for o in ops: if not isinstance(o, dict) or not o.get("title"): continue cur.execute("""insert into audit.opportunities(run_id,title,capability,impact,effort,risk,hypothesis,first_step,evidence,source) values(1,%s,%s,%s,%s,%s,%s,%s,%s,'llm') on conflict(run_id,title) do nothing""", (str(o.get("title"))[:200], str(o.get("capability") or ""), int(o.get("impact") or 3), int(o.get("effort") or 3), int(o.get("risk") or 3), str(o.get("hypothesis") or "")[:1000], str(o.get("first_step") or "")[:1000], str(o.get("evidence") or "")[:1000])) conn.commit() print("llm opportunities", len(ops)) cur.close(); conn.close()