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