Spaces:
Running
Running
fix: robust qdrant indexing and K2 think JSON extraction logic
Browse files
app/services/k2_think_engine.py
CHANGED
|
@@ -109,12 +109,12 @@ RESEARCHER PROFILE:
|
|
| 109 |
("user", "Analyze these documents and provide results:\n\n{context}")
|
| 110 |
])
|
| 111 |
|
| 112 |
-
# 5. Exécution de la chaîne
|
| 113 |
-
chain = prompt | llm
|
| 114 |
|
| 115 |
self._log_reasoning("K2_ANALYSIS", "Chain Execution", "Invoking LangChain LCEL with K2 Think V2")
|
| 116 |
|
| 117 |
-
|
| 118 |
"past_context": past_context or "No past context.",
|
| 119 |
"user_profile": request.user_profile or "General researcher.",
|
| 120 |
"depth": request.reasoning_depth,
|
|
@@ -124,6 +124,28 @@ RESEARCHER PROFILE:
|
|
| 124 |
"context": context
|
| 125 |
})
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
# 6. Conversion en schémas internes
|
| 128 |
comparative_analysis = self._convert_k2_to_comparative_analysis(k2_analysis, request.documents)
|
| 129 |
counter_hypotheses = self._convert_k2_to_counter_hypotheses(k2_analysis)
|
|
|
|
| 109 |
("user", "Analyze these documents and provide results:\n\n{context}")
|
| 110 |
])
|
| 111 |
|
| 112 |
+
# 5. Exécution de la chaîne (on enlève le parser ici pour nettoyer manuellement après)
|
| 113 |
+
chain = prompt | llm
|
| 114 |
|
| 115 |
self._log_reasoning("K2_ANALYSIS", "Chain Execution", "Invoking LangChain LCEL with K2 Think V2")
|
| 116 |
|
| 117 |
+
response = await chain.ainvoke({
|
| 118 |
"past_context": past_context or "No past context.",
|
| 119 |
"user_profile": request.user_profile or "General researcher.",
|
| 120 |
"depth": request.reasoning_depth,
|
|
|
|
| 124 |
"context": context
|
| 125 |
})
|
| 126 |
|
| 127 |
+
# NETTOYAGE MANUEL DU JSON (Crucial pour les modèles "Thinking")
|
| 128 |
+
raw_content = response.content
|
| 129 |
+
|
| 130 |
+
# Supprimer les balises de réflexion (<think>, <think_faster>, etc.)
|
| 131 |
+
import re
|
| 132 |
+
content_no_think = re.sub(r'<think.*?>.*?</think.*?>', '', raw_content, flags=re.DOTALL).strip()
|
| 133 |
+
|
| 134 |
+
# Extraire le bloc JSON entre les premières et dernières accolades
|
| 135 |
+
json_match = re.search(r'(\{.*\})', content_no_think, re.DOTALL)
|
| 136 |
+
if json_match:
|
| 137 |
+
clean_json = json_match.group(1)
|
| 138 |
+
else:
|
| 139 |
+
# Fallback : suppression des blocs de code markdown
|
| 140 |
+
clean_json = content_no_think.replace("```json", "").replace("```", "").strip()
|
| 141 |
+
|
| 142 |
+
try:
|
| 143 |
+
k2_analysis = parser.parse(clean_json)
|
| 144 |
+
except Exception as e:
|
| 145 |
+
logger.error(f"Failed to parse cleaned JSON: {e}")
|
| 146 |
+
logger.debug(f"Raw content was: {raw_content}")
|
| 147 |
+
raise e
|
| 148 |
+
|
| 149 |
# 6. Conversion en schémas internes
|
| 150 |
comparative_analysis = self._convert_k2_to_comparative_analysis(k2_analysis, request.documents)
|
| 151 |
counter_hypotheses = self._convert_k2_to_counter_hypotheses(k2_analysis)
|
app/services/memory_service.py
CHANGED
|
@@ -41,14 +41,19 @@ class MemoryService:
|
|
| 41 |
distance=models.Distance.COSINE
|
| 42 |
)
|
| 43 |
)
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
self.client.create_payload_index(
|
| 47 |
collection_name=self.COLLECTION_NAME,
|
| 48 |
field_name="user_id",
|
| 49 |
field_schema=models.PayloadSchemaType.KEYWORD,
|
| 50 |
)
|
| 51 |
-
logger.info(f"Payload index
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
logger.error(f"Failed to ensure Qdrant collection or index: {e}")
|
| 54 |
|
|
|
|
| 41 |
distance=models.Distance.COSINE
|
| 42 |
)
|
| 43 |
)
|
| 44 |
+
|
| 45 |
+
# Vérifier/Créer l'index user_id séparément pour être sûr
|
| 46 |
+
try:
|
| 47 |
self.client.create_payload_index(
|
| 48 |
collection_name=self.COLLECTION_NAME,
|
| 49 |
field_name="user_id",
|
| 50 |
field_schema=models.PayloadSchemaType.KEYWORD,
|
| 51 |
)
|
| 52 |
+
logger.info(f"Payload index ensured for 'user_id' in {self.COLLECTION_NAME}")
|
| 53 |
+
except Exception:
|
| 54 |
+
# L'index existe probablement déjà
|
| 55 |
+
pass
|
| 56 |
+
|
| 57 |
except Exception as e:
|
| 58 |
logger.error(f"Failed to ensure Qdrant collection or index: {e}")
|
| 59 |
|