fix(projekt-matching): retry chat_json on empty/unparseable vLLM completion
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user