v1.0.166: classify prompt v2 (LLM-suggested) + safe JSON parse with auto-repair
This commit is contained in:
@@ -50,13 +50,7 @@ def _call_llm_classify(header_text):
|
||||
data = resp.json()
|
||||
|
||||
raw = data.get("choices", [{}])[0].get("message", {}).get("content", "")
|
||||
# LLM может обернуть JSON в markdown-блок ```json ... ```
|
||||
json_text = raw
|
||||
if "```json" in json_text:
|
||||
json_text = json_text.split("```json")[1].split("```")[0]
|
||||
elif "```" in json_text:
|
||||
json_text = json_text.split("```")[1].split("```")[0]
|
||||
return json.loads(json_text.strip())
|
||||
return _safe_json_parse(raw)
|
||||
|
||||
|
||||
def classify_batch(batch_id):
|
||||
@@ -103,7 +97,34 @@ def classify_batch(batch_id):
|
||||
return {"ok": True, "total": total, "done": done, "failed": failed}
|
||||
|
||||
|
||||
def _smart_extract(elements_json):
|
||||
def _safe_json_parse(raw):
|
||||
"""Parse LLM response, fixing common JSON errors (multiline, markdown, unescaped chars)."""
|
||||
if not raw:
|
||||
raise ValueError("empty LLM response")
|
||||
|
||||
# Strip markdown wrappers
|
||||
text = raw.strip()
|
||||
if "```json" in text:
|
||||
text = text.split("```json")[1].split("```")[0].strip()
|
||||
elif "```" in text:
|
||||
text = text.split("```")[1].split("```")[0].strip()
|
||||
|
||||
# Try strict parse first
|
||||
try:
|
||||
return json.loads(text)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
# Fix common issues: unescaped newlines inside strings, unterminated strings
|
||||
# Replace literal newlines inside JSON values
|
||||
import re as _re
|
||||
# Collapse multiline to single line
|
||||
text = _re.sub(r'\n\s*', ' ', text)
|
||||
# Remove trailing commas before closing braces
|
||||
text = _re.sub(r',\s*}', '}', text)
|
||||
text = _re.sub(r',\s*]', ']', text)
|
||||
|
||||
return json.loads(text)
|
||||
"""
|
||||
Умная выжимка текста для классификации (решение Q3 от Opus).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user