| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #!/usr/bin/env python3
- import json
- import sqlite3
- import subprocess
- DB = "data/zammad_analysis.sqlite3"
- MODEL = "qwen3:8b"
- SYSTEM = """Du analysierst historische Zammad-Artikel eines Kundenservice-Systems.
- Entscheide ausschließlich zwischen:
- AGENT_RESPONSE = individuell formulierte fachliche Antwort eines Mitarbeiters
- AUTOMATION = automatisch erzeugte System-/Ticketnachricht
- Eine fachliche Antwort kann auch von Zammad als System/email geführt werden.
- Antworte ausschließlich als JSON:
- {
- "role": "AGENT_RESPONSE|AUTOMATION",
- "confidence": 0.0,
- "reason": "kurze Begründung"
- }
- """
- db = sqlite3.connect(DB)
- rows = db.execute("""
- SELECT id, ticket_id, subject, clean_body
- FROM articles
- WHERE content_role = 'UNKNOWN_REVIEW'
- ORDER BY id
- """).fetchall()
- print("=" * 72)
- print("QWEN – UNKNOWN REVIEW")
- print("=" * 72)
- print(f"Fälle: {len(rows)}")
- print(f"Modell: {MODEL}")
- print()
- for article_id, ticket_id, subject, body in rows:
- prompt = f"""{SYSTEM}
- Betreff:
- {subject or ""}
- Artikel:
- {body or ""}
- """
- result = subprocess.run(
- [
- "ollama",
- "run",
- MODEL,
- prompt,
- ],
- capture_output=True,
- text=True,
- timeout=300,
- )
- output = result.stdout.strip()
- # JSON aus möglichem Markdown-Codeblock extrahieren
- if "{" in output and "}" in output:
- output = output[
- output.find("{"):
- output.rfind("}") + 1
- ]
- try:
- data = json.loads(output)
- role = data.get("role")
- confidence = float(
- data.get("confidence", 0)
- )
- reason = data.get("reason", "")
- if role not in (
- "AGENT_RESPONSE",
- "AUTOMATION",
- ):
- raise ValueError(
- f"Ungültige Rolle: {role}"
- )
- except Exception as exc:
- print(
- f"Ticket #{ticket_id}: "
- f"JSON-Fehler: {exc}"
- )
- print(output[:500])
- continue
- db.execute("""
- UPDATE articles
- SET content_role = ?,
- classification_score = ?,
- classification_reason = ?
- WHERE id = ?
- """, (
- role,
- confidence,
- "QWEN: " + reason,
- article_id,
- ))
- db.commit()
- print(
- f"Ticket #{ticket_id}: "
- f"{role:16} "
- f"{confidence:.2f} "
- f"{reason}"
- )
- db.close()
|