fix: Mail-Klassifizierung Batch-Modus (alle Mails, nicht nur 50) + Token-Limit erhöht

This commit is contained in:
root 2026-03-09 15:38:45 +07:00
parent e643f356b7
commit 750bef5698
2 changed files with 19 additions and 7 deletions

View file

@ -242,16 +242,20 @@ def classify_mails(mails: list[dict], api_key: str) -> list[dict]:
{"role": "system", "content": CLASSIFY_PROMPT},
{"role": "user", "content": mail_text},
],
"max_tokens": 400,
"max_tokens": 2000,
"temperature": 0,
},
timeout=30,
timeout=60,
)
r.raise_for_status()
content = r.json()["choices"][0]["message"]["content"]
content = content.strip()
if content.startswith("```"):
content = content.split("\n", 1)[-1].rsplit("```", 1)[0]
if "```" in content:
content = content.split("```", 1)[-1]
if content.startswith("json"):
content = content[4:]
content = content.rsplit("```", 1)[0]
content = content.strip()
classifications = json.loads(content)
cat_map = {c["idx"]: c["cat"] for c in classifications}
@ -264,8 +268,11 @@ def classify_mails(mails: list[dict], api_key: str) -> list[dict]:
return mails
BATCH_SIZE = 40
def get_smart_digest(hours: int = 24, api_key: str = "") -> dict:
"""Intelligente Mail-Zusammenfassung: holt Mails, klassifiziert per LLM, gruppiert."""
"""Intelligente Mail-Zusammenfassung: holt Mails, klassifiziert per LLM in Batches, gruppiert."""
m = _connect()
if not m:
return {"error": "IMAP-Verbindung fehlgeschlagen"}
@ -276,7 +283,7 @@ def get_smart_digest(hours: int = 24, api_key: str = "") -> dict:
ids = data[0].split() if data[0] else []
mails = []
for mid in ids[-50:]:
for mid in ids:
_, msg_data = m.fetch(mid, "(BODY.PEEK[HEADER])")
parsed = _parse_mail(msg_data)
if parsed:
@ -291,7 +298,12 @@ def get_smart_digest(hours: int = 24, api_key: str = "") -> dict:
return {"total": 0, "mails": [], "summary": {}}
if api_key:
mails = classify_mails(mails, api_key)
classified = []
for i in range(0, len(mails), BATCH_SIZE):
batch = mails[i:i + BATCH_SIZE]
batch = classify_mails(batch, api_key)
classified.extend(batch)
mails = classified
summary = {}
for m_item in mails: