fix: KI-Plausi in Batches à 25 Preise (verhindert Token-Overflow)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Cursor 2026-02-25 22:47:04 +07:00
parent 4dde05ff01
commit 8c6eb7128a

View file

@ -66,61 +66,64 @@ def plausibilitaetspruefung(von="FRA", nach="KTI"):
conn.close()
return
preise_liste = "\n".join([
f" ID {p['id']}: {p['preis']:.0f} EUR — Scanner: {p['scanner']}"
f"Node: {p['node']} — Airline: {p['airline'] or 'k.A.'} — Abflug: {p['abflug']}"
for p in ungepruefte
])
# In Batches aufteilen (max 25 Preise pro KI-Call)
BATCH_SIZE = 25
batches = [ungepruefte[i:i+BATCH_SIZE] for i in range(0, len(ungepruefte), BATCH_SIZE)]
prompt = PLAUSI_PROMPT.format(preise_liste=preise_liste)
plausibel_total = 0
verdaechtig_total = 0
try:
response = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": prompt}],
max_tokens=2000,
temperature=0.1,
)
antwort = response.choices[0].message.content.strip()
for batch_nr, batch in enumerate(batches):
preise_liste = "\n".join([
f" ID {p['id']}: {p['preis']:.0f} EUR — Scanner: {p['scanner']}"
f"Node: {p['node']} — Airline: {p['airline'] or 'k.A.'} — Abflug: {p['abflug']}"
for p in batch
])
# JSON aus Antwort extrahieren (KI gibt manchmal Markdown-Wrapper)
if "```" in antwort:
antwort = antwort.split("```")[1]
if antwort.startswith("json"):
antwort = antwort[4:]
prompt = PLAUSI_PROMPT.format(preise_liste=preise_liste)
ergebnisse = json.loads(antwort)
plausibel_count = 0
verdaechtig_count = 0
for e in ergebnisse:
pid = e.get("id")
ist_plausibel = 1 if e.get("plausibel") else 0
grund = e.get("grund", "")[:200]
conn.execute(
"UPDATE prices SET plausibel=?, plausi_grund=? WHERE id=?",
(ist_plausibel, grund, pid)
try:
response = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": prompt}],
max_tokens=3000,
temperature=0.1,
)
if ist_plausibel:
plausibel_count += 1
else:
verdaechtig_count += 1
antwort = response.choices[0].message.content.strip()
conn.commit()
log(f"Plausibilitätsprüfung: {plausibel_count} plausibel, "
f"{verdaechtig_count} verdächtig von {len(ungepruefte)} Preisen")
if "```" in antwort:
antwort = antwort.split("```")[1]
if antwort.startswith("json"):
antwort = antwort[4:]
except json.JSONDecodeError as e:
log(f"KI-Plausi JSON-Fehler: {e} — Antwort: {antwort[:200]}", "ERROR")
# Fallback: regelbasiert markieren
_regelbasierte_plausi(conn, ungepruefte)
except Exception as e:
log(f"KI-Plausi Fehler: {e}", "ERROR")
_regelbasierte_plausi(conn, ungepruefte)
finally:
conn.close()
ergebnisse = json.loads(antwort)
for e in ergebnisse:
pid = e.get("id")
ist_plausibel = 1 if e.get("plausibel") else 0
grund = e.get("grund", "")[:200]
conn.execute(
"UPDATE prices SET plausibel=?, plausi_grund=? WHERE id=?",
(ist_plausibel, grund, pid)
)
if ist_plausibel:
plausibel_total += 1
else:
verdaechtig_total += 1
conn.commit()
except json.JSONDecodeError as e:
log(f"KI-Plausi Batch {batch_nr+1} JSON-Fehler: {e}", "WARN")
_regelbasierte_plausi(conn, batch)
except Exception as e:
log(f"KI-Plausi Batch {batch_nr+1} Fehler: {e}", "WARN")
_regelbasierte_plausi(conn, batch)
log(f"Plausibilitätsprüfung: {plausibel_total} plausibel, "
f"{verdaechtig_total} verdächtig von {len(ungepruefte)} Preisen")
conn.close()
def _regelbasierte_plausi(conn, preise):