56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""Fix fehlende KI-Zusammenfassungen mit Claude Haiku."""
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
import sqlite3
|
||
|
|
import time
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
import httpx
|
||
|
|
|
||
|
|
DB = Path(__file__).resolve().parent.parent / "data" / "tracker_remote.db"
|
||
|
|
API_KEY = os.popen("security find-generic-password -s anthropic-api -w 2>/dev/null").read().strip()
|
||
|
|
|
||
|
|
PROMPT = """Fasse diese kommunalpolitische Vorlage aus Hagen in 2-3 Sätzen zusammen.
|
||
|
|
Fokus: Was wird gefordert/vorgeschlagen? Von wem? Was ist das Ziel?
|
||
|
|
|
||
|
|
Vorlage {az}:
|
||
|
|
{text}"""
|
||
|
|
|
||
|
|
def summarize(az, text):
|
||
|
|
resp = httpx.post("https://api.anthropic.com/v1/messages",
|
||
|
|
headers={"x-api-key": API_KEY, "anthropic-version": "2023-06-01", "content-type": "application/json"},
|
||
|
|
json={"model": "claude-haiku-4-5-20241022", "max_tokens": 300, "messages": [
|
||
|
|
{"role": "user", "content": PROMPT.format(az=az, text=text[:8000])}
|
||
|
|
]}, timeout=30)
|
||
|
|
if resp.status_code == 200:
|
||
|
|
return resp.json()["content"][0]["text"]
|
||
|
|
print(f" ERROR {resp.status_code}: {resp.text[:200]}")
|
||
|
|
return None
|
||
|
|
|
||
|
|
conn = sqlite3.connect(str(DB))
|
||
|
|
conn.row_factory = sqlite3.Row
|
||
|
|
missing = conn.execute("""
|
||
|
|
SELECT v.id, v.aktenzeichen, v.volltext_clean
|
||
|
|
FROM vorlagen v
|
||
|
|
WHERE v.volltext_clean IS NOT NULL AND v.volltext_clean != ''
|
||
|
|
AND v.id NOT IN (SELECT vorlage_id FROM ki_bewertungen WHERE typ='zusammenfassung')
|
||
|
|
""").fetchall()
|
||
|
|
|
||
|
|
print(f"Fehlende Zusammenfassungen: {len(missing)}")
|
||
|
|
for row in missing:
|
||
|
|
print(f" {row['aktenzeichen']}...", end=" ")
|
||
|
|
summary = summarize(row["aktenzeichen"], row["volltext_clean"])
|
||
|
|
if summary:
|
||
|
|
conn.execute("""INSERT INTO ki_bewertungen (vorlage_id, typ, score, begruendung, modell, prompt_version)
|
||
|
|
VALUES (?, 'zusammenfassung', 0, ?, 'claude-haiku-4-5', 'v2-fix')""",
|
||
|
|
(row["id"], summary))
|
||
|
|
conn.commit()
|
||
|
|
print(f"✓ {summary[:60]}")
|
||
|
|
else:
|
||
|
|
print("✗")
|
||
|
|
time.sleep(0.5)
|
||
|
|
|
||
|
|
print(f"\nFertig.")
|
||
|
|
conn.close()
|