V0.1 de la récupération json via ollama.cpp
This commit is contained in:
parent
14985f6dbb
commit
bbeef58286
11 changed files with 78 additions and 34 deletions
|
|
@ -113,49 +113,54 @@ class LLMManager:
|
|||
"""Extract JSON action objects from text more robustly."""
|
||||
actions = []
|
||||
|
||||
# Regex plus laxiste pour trouver des blocs JSON potentiels
|
||||
# On cherche des blocs commençant par {"action" ou {'action'
|
||||
matches = re.finditer(r'\{[\s\n]*[\"\']action[\"\'].*?\}', text, re.DOTALL)
|
||||
# 1. Try to extract from markdown blocks if present
|
||||
markdown_blocks = re.findall(r'```(?:json)?\s*(.*?)\s*```', text, re.DOTALL)
|
||||
candidates = markdown_blocks if markdown_blocks else [text]
|
||||
|
||||
for match in matches:
|
||||
json_str = match.group(0)
|
||||
for candidate_text in candidates:
|
||||
# Try to parse the whole text as a list or dict first
|
||||
try:
|
||||
# Tentative de parsing standard
|
||||
parsed = json.loads(json_str)
|
||||
if isinstance(parsed, dict) and 'action' in parsed:
|
||||
actions.append(parsed)
|
||||
except json.JSONDecodeError:
|
||||
# Si erreur, on tente de remplacer les simples quotes par des doubles
|
||||
# (Cas fréquent si le LLM mélange les styles)
|
||||
try:
|
||||
# Remplacement très basique (peut échouer si contenu complexe)
|
||||
json_corrected = json_str.replace("'", '"')
|
||||
parsed = json.loads(json_corrected)
|
||||
if isinstance(parsed, dict) and 'action' in parsed:
|
||||
parsed = json.loads(candidate_text.strip())
|
||||
if isinstance(parsed, list):
|
||||
for item in parsed:
|
||||
if isinstance(item, dict) and 'action' in item:
|
||||
if item not in actions:
|
||||
actions.append(item)
|
||||
if actions: return json.dumps(actions)
|
||||
elif isinstance(parsed, dict) and 'action' in parsed:
|
||||
if parsed not in actions:
|
||||
actions.append(parsed)
|
||||
continue
|
||||
except: pass
|
||||
return json.dumps(actions)
|
||||
except:
|
||||
pass
|
||||
|
||||
# Méthode récursive des accolades si le bloc est tronqué ou complexe
|
||||
start = match.start()
|
||||
# Fallback: scan for { ... } using brace matching
|
||||
start_indexes = [i for i, c in enumerate(candidate_text) if c == '{']
|
||||
for start in start_indexes:
|
||||
brace_count = 0
|
||||
for i in range(start, len(text)):
|
||||
if text[i] == '{': brace_count += 1
|
||||
elif text[i] == '}':
|
||||
for i in range(start, len(candidate_text)):
|
||||
if candidate_text[i] == '{':
|
||||
brace_count += 1
|
||||
elif candidate_text[i] == '}':
|
||||
brace_count -= 1
|
||||
if brace_count == 0:
|
||||
json_str = candidate_text[start:i+1]
|
||||
try:
|
||||
candidate = text[start:i+1]
|
||||
try:
|
||||
parsed = json.loads(candidate)
|
||||
except:
|
||||
parsed = json.loads(candidate.replace("'", '"'))
|
||||
|
||||
parsed = json.loads(json_str)
|
||||
if isinstance(parsed, dict) and 'action' in parsed:
|
||||
actions.append(parsed)
|
||||
break
|
||||
except: pass
|
||||
|
||||
if parsed not in actions: # avoid duplicates
|
||||
actions.append(parsed)
|
||||
except:
|
||||
# Fallback with single quote replace
|
||||
try:
|
||||
parsed = json.loads(json_str.replace("'", '"'))
|
||||
if isinstance(parsed, dict) and 'action' in parsed:
|
||||
if parsed not in actions:
|
||||
actions.append(parsed)
|
||||
except:
|
||||
pass
|
||||
break # Move to next start brace
|
||||
|
||||
if actions:
|
||||
return json.dumps(actions)
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue