[PRISM] Robust loose JSON parsing and source retry queue
This commit is contained in:
1 parent
d37be5bea9
commit
16808373da
1 file changed
+26
-3
@@ -27,6 +27,30 @@ Domain: %s
|
||||
Known specs: %s
|
||||
"""
|
||||
|
||||
|
||||
def parse_loose(text):
|
||||
import json as _json
|
||||
if not text:
|
||||
raise ValueError("empty LLM text")
|
||||
t = text.strip()
|
||||
t = re.sub(r"^```(?:json)?", "", t, flags=re.I).strip()
|
||||
t = re.sub(r"```$", "", t).strip()
|
||||
start = t.find("{")
|
||||
if start < 0:
|
||||
raise ValueError("no JSON object")
|
||||
raw = t[start:]
|
||||
raw = raw.replace("\r", " ").replace("\n", " ")
|
||||
try:
|
||||
return _json.loads(raw, strict=False)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
obj, _ = _json.JSONDecoder(strict=False).raw_decode(raw)
|
||||
return obj
|
||||
except Exception as e:
|
||||
raise ValueError("JSON parse: %s" % e)
|
||||
|
||||
|
||||
conn = psycopg2.connect(rag.DSN)
|
||||
cur = conn.cursor()
|
||||
cur.execute("update prism.tasks set status='pending', started_at=null, worker=null where status='running'")
|
||||
@@ -75,10 +99,9 @@ while True:
|
||||
if len(txt) < 200:
|
||||
raise RuntimeError("empty text %s" % len(txt))
|
||||
chunk = R.pick_excerpt(txt, 3500)
|
||||
r = L.ask(SUPPORT_PROMPT + chunk, max_tokens=700, temperature=0.0,
|
||||
r = L.ask(SUPPORT_PROMPT + chunk, max_tokens=450, temperature=0.0,
|
||||
chain=[{"name": "qwen2.5:7b", "kind": "ollama", "timeout": 300}])
|
||||
m = re.search(r"\{.*\}", r["text"], re.S)
|
||||
d = json.loads(m.group(0)) if m else {}
|
||||
d = parse_loose(r["text"])
|
||||
facts = d.get("facts") or []
|
||||
for f in facts:
|
||||
key = re.sub(r"[^a-zA-Z0-9_.-]+", "_", str(f.get("key") or "fact")).strip("_").lower()
|
||||
|
||||
Reference in new issue
Block a user