fix(projekt-matching): retry chat_json on empty/unparseable vLLM completion

This commit is contained in:
tlg
2026-07-09 15:33:11 +02:00
parent cf088f0bd3
commit 20796b3a68
2 changed files with 32 additions and 13 deletions

View File

@@ -7,6 +7,7 @@ from __future__ import annotations
import json
import re
import time
import requests
@@ -131,19 +132,23 @@ def _post(base, model, messages, schema):
timeout=1500)
def chat_json(base, model, messages, schema):
resp = _post(base, model, messages, schema)
if resp.status_code != 200:
raise LlmError(f"vLLM HTTP {resp.status_code}: "
f"{getattr(resp, 'text', '')[:300]}")
content = resp.json()["choices"][0]["message"]["content"] or ""
content = THINK_RE.sub("", content).strip()
if content.startswith("```"):
content = re.sub(r"^```[a-z]*\s*|\s*```$", "", content).strip()
try:
return json.loads(content)
except json.JSONDecodeError as exc:
raise LlmError(f"LLM lieferte kein JSON: {exc}: {content[:200]}")
def chat_json(base, model, messages, schema, attempts=3):
last = None
for attempt in range(attempts):
resp = _post(base, model, messages, schema)
if resp.status_code != 200:
raise LlmError(f"vLLM HTTP {resp.status_code}: "
f"{getattr(resp, 'text', '')[:300]}")
content = resp.json()["choices"][0]["message"]["content"] or ""
content = THINK_RE.sub("", content).strip()
if content.startswith("```"):
content = re.sub(r"^```[a-z]*\s*|\s*```$", "", content).strip()
try:
return json.loads(content)
except json.JSONDecodeError as exc:
last = LlmError(f"LLM lieferte kein JSON: {exc}: {content[:200]}")
time.sleep(10 * (attempt + 1))
raise last
def extract_project(base, model, page_text):