Dama12 commited on
Commit
6399eda
·
1 Parent(s): 4ba1096

debug: add deep logs and index-based extraction fallback for K2 engine

Browse files
Files changed (1) hide show
  1. app/services/k2_think_engine.py +23 -16
app/services/k2_think_engine.py CHANGED
@@ -137,41 +137,48 @@ RESEARCHER PROFILE:
137
  import re
138
  import json
139
 
 
 
140
  # Supprimer les réflexions
141
  content = re.sub(r'<think.*?>.*?</think.*?>', '', raw_content, flags=re.DOTALL).strip()
142
 
143
- # Tenter de trouver le bloc JSON entre [RESULT] et [/RESULT] si l'IA a suivi la consigne
 
 
144
  tag_match = re.search(r'\[RESULT\](.*?)\[/RESULT\]', content, re.DOTALL)
145
  if tag_match:
146
  clean_json = tag_match.group(1).strip()
147
- else:
 
148
  # Sinon, on cherche le dernier bloc commençant par { et finissant par }
149
- # On utilise une recherche non-gloutonne mais on prend le dernier match
150
  all_blocks = re.findall(r'(\{.*\})', content, re.DOTALL)
151
- clean_json = all_blocks[-1] if all_blocks else content
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  # RÉPARATEUR DE JSON (Common LLM errors)
154
- # 1. Supprimer les commentaires style // ou #
155
  clean_json = re.sub(r'^\s*//.*$', '', clean_json, flags=re.MULTILINE)
156
- # 2. Remplacer les guillemets simples par des doubles (si ce ne sont pas des apostrophes)
157
- # (Approximation simple mais souvent efficace pour les clés)
158
- # clean_json = clean_json.replace("'", '"') # Trop risqué pour le texte
159
-
160
- # 3. Supprimer les virgules traînantes avant un ] ou }
161
  clean_json = re.sub(r',\s*([\]\}])', r'\1', clean_json)
162
 
163
  try:
164
  k2_analysis = json.loads(clean_json)
165
  except Exception as e:
166
  logger.error(f"JSON.LOADS failed: {e}")
167
- # Tentative désespérée : parse_json_markdown (si on peut l'émuler)
168
  try:
169
- # Remplacement manuel des backticks markdown si présents
170
- clean_json = clean_json.replace("```json", "").replace("```", "").strip()
171
- k2_analysis = json.loads(clean_json)
172
  except:
173
- logger.error("All JSON parsing attempts failed.")
174
- logger.debug(f"RAW CONTENT: {raw_content}")
175
  raise e
176
 
177
  # 6. Conversion en schémas internes
 
137
  import re
138
  import json
139
 
140
+ logger.info(f"K2 RAW RESPONSE (len={len(raw_content)}): {raw_content[:200]}...")
141
+
142
  # Supprimer les réflexions
143
  content = re.sub(r'<think.*?>.*?</think.*?>', '', raw_content, flags=re.DOTALL).strip()
144
 
145
+ clean_json = ""
146
+
147
+ # Tenter de trouver le bloc JSON entre [RESULT] et [/RESULT]
148
  tag_match = re.search(r'\[RESULT\](.*?)\[/RESULT\]', content, re.DOTALL)
149
  if tag_match:
150
  clean_json = tag_match.group(1).strip()
151
+
152
+ if not clean_json:
153
  # Sinon, on cherche le dernier bloc commençant par { et finissant par }
 
154
  all_blocks = re.findall(r'(\{.*\})', content, re.DOTALL)
155
+ if all_blocks:
156
+ clean_json = all_blocks[-1]
157
+ else:
158
+ # Recherche manuelle par index (plus robuste si regex s'emmêle)
159
+ start_idx = content.find('{')
160
+ end_idx = content.rfind('}')
161
+ if start_idx != -1 and end_idx != -1:
162
+ clean_json = content[start_idx:end_idx + 1]
163
+
164
+ if not clean_json:
165
+ logger.error(f"No JSON block found in content: {content[:100]}...")
166
+ raise ValueError("The AI model did not return a valid scientific result block. Please try again.")
167
 
168
  # RÉPARATEUR DE JSON (Common LLM errors)
 
169
  clean_json = re.sub(r'^\s*//.*$', '', clean_json, flags=re.MULTILINE)
 
 
 
 
 
170
  clean_json = re.sub(r',\s*([\]\}])', r'\1', clean_json)
171
 
172
  try:
173
  k2_analysis = json.loads(clean_json)
174
  except Exception as e:
175
  logger.error(f"JSON.LOADS failed: {e}")
176
+ # Tentative ultime : nettoyage markdown
177
  try:
178
+ clean_json_fixed = clean_json.replace("```json", "").replace("```", "").strip()
179
+ k2_analysis = json.loads(clean_json_fixed)
 
180
  except:
181
+ logger.error(f"All JSON parsing attempts failed. Cleaned JSON was: {clean_json[:200]}...")
 
182
  raise e
183
 
184
  # 6. Conversion en schémas internes