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
28
.gitignore
vendored
Executable file
28
.gitignore
vendored
Executable file
|
|
@ -0,0 +1,28 @@
|
||||||
|
# Fichiers d'environnement (contiennent des tokens/clés sensibles)
|
||||||
|
.env
|
||||||
|
# Fichiers de configuration générés
|
||||||
|
config.json
|
||||||
|
whitelist.json
|
||||||
|
bridges.json
|
||||||
|
config.db
|
||||||
|
malware.json
|
||||||
|
|
||||||
|
# Cache Python
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
*.pyo
|
||||||
|
*.pyd
|
||||||
|
|
||||||
|
# Environnements virtuels
|
||||||
|
venv/
|
||||||
|
env/
|
||||||
|
.venv/
|
||||||
|
|
||||||
|
# IDE
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
*.Logs
|
||||||
|
*.log
|
||||||
|
.data
|
||||||
Binary file not shown.
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"resume_global": "",
|
||||||
|
"historique_recent": [
|
||||||
|
{
|
||||||
|
"timestamp": "2026-03-21T16:12:34.189353+00:00",
|
||||||
|
"channel_id": 1432453132008554567,
|
||||||
|
"user_message": "<@1108087282692522105> <@1189193599594803272> \nJe suis la pour 18h si vous voulez !",
|
||||||
|
"bot_message": ""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -113,49 +113,54 @@ class LLMManager:
|
||||||
"""Extract JSON action objects from text more robustly."""
|
"""Extract JSON action objects from text more robustly."""
|
||||||
actions = []
|
actions = []
|
||||||
|
|
||||||
# Regex plus laxiste pour trouver des blocs JSON potentiels
|
# 1. Try to extract from markdown blocks if present
|
||||||
# On cherche des blocs commençant par {"action" ou {'action'
|
markdown_blocks = re.findall(r'```(?:json)?\s*(.*?)\s*```', text, re.DOTALL)
|
||||||
matches = re.finditer(r'\{[\s\n]*[\"\']action[\"\'].*?\}', text, re.DOTALL)
|
candidates = markdown_blocks if markdown_blocks else [text]
|
||||||
|
|
||||||
for match in matches:
|
for candidate_text in candidates:
|
||||||
json_str = match.group(0)
|
# Try to parse the whole text as a list or dict first
|
||||||
try:
|
try:
|
||||||
# Tentative de parsing standard
|
parsed = json.loads(candidate_text.strip())
|
||||||
parsed = json.loads(json_str)
|
if isinstance(parsed, list):
|
||||||
if isinstance(parsed, dict) and 'action' in parsed:
|
for item in parsed:
|
||||||
actions.append(parsed)
|
if isinstance(item, dict) and 'action' in item:
|
||||||
except json.JSONDecodeError:
|
if item not in actions:
|
||||||
# Si erreur, on tente de remplacer les simples quotes par des doubles
|
actions.append(item)
|
||||||
# (Cas fréquent si le LLM mélange les styles)
|
if actions: return json.dumps(actions)
|
||||||
try:
|
elif isinstance(parsed, dict) and 'action' in parsed:
|
||||||
# Remplacement très basique (peut échouer si contenu complexe)
|
if parsed not in actions:
|
||||||
json_corrected = json_str.replace("'", '"')
|
|
||||||
parsed = json.loads(json_corrected)
|
|
||||||
if isinstance(parsed, dict) and 'action' in parsed:
|
|
||||||
actions.append(parsed)
|
actions.append(parsed)
|
||||||
continue
|
return json.dumps(actions)
|
||||||
except: pass
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
# Méthode récursive des accolades si le bloc est tronqué ou complexe
|
# Fallback: scan for { ... } using brace matching
|
||||||
start = match.start()
|
start_indexes = [i for i, c in enumerate(candidate_text) if c == '{']
|
||||||
|
for start in start_indexes:
|
||||||
brace_count = 0
|
brace_count = 0
|
||||||
for i in range(start, len(text)):
|
for i in range(start, len(candidate_text)):
|
||||||
if text[i] == '{': brace_count += 1
|
if candidate_text[i] == '{':
|
||||||
elif text[i] == '}':
|
brace_count += 1
|
||||||
|
elif candidate_text[i] == '}':
|
||||||
brace_count -= 1
|
brace_count -= 1
|
||||||
if brace_count == 0:
|
if brace_count == 0:
|
||||||
|
json_str = candidate_text[start:i+1]
|
||||||
try:
|
try:
|
||||||
candidate = text[start:i+1]
|
parsed = json.loads(json_str)
|
||||||
try:
|
|
||||||
parsed = json.loads(candidate)
|
|
||||||
except:
|
|
||||||
parsed = json.loads(candidate.replace("'", '"'))
|
|
||||||
|
|
||||||
if isinstance(parsed, dict) and 'action' in parsed:
|
if isinstance(parsed, dict) and 'action' in parsed:
|
||||||
actions.append(parsed)
|
if parsed not in actions: # avoid duplicates
|
||||||
break
|
actions.append(parsed)
|
||||||
except: pass
|
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:
|
if actions:
|
||||||
return json.dumps(actions)
|
return json.dumps(actions)
|
||||||
return None
|
return None
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue