Spaces:
Runtime error
Runtime error
Create Cortex/neurons/metacognition.py
Browse files
Cortex/neurons/metacognition.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from typing import Dict, Any
|
| 3 |
+
|
| 4 |
+
class MetacognitionNeuron:
|
| 5 |
+
"""
|
| 6 |
+
Neurone de métacognition - Évalue et améliore la qualité des réponses.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.logger = logging.getLogger("metacognition_neuron")
|
| 11 |
+
|
| 12 |
+
async def initialize(self):
|
| 13 |
+
self.logger.info("🧠 Initialisation du neurone de métacognition...")
|
| 14 |
+
return True
|
| 15 |
+
|
| 16 |
+
async def evaluate_response(self, response: Dict[str, Any], context: Dict[str, Any]) -> Dict[str, Any]:
|
| 17 |
+
"""
|
| 18 |
+
Évalue la réponse générée par le cortex et suggère des améliorations.
|
| 19 |
+
"""
|
| 20 |
+
# Critères d'évaluation
|
| 21 |
+
clarity = self._evaluate_clarity(response)
|
| 22 |
+
relevance = self._evaluate_relevance(response, context)
|
| 23 |
+
depth = self._evaluate_depth(response)
|
| 24 |
+
confidence = response.get('confidence', 0.5)
|
| 25 |
+
|
| 26 |
+
# Score global
|
| 27 |
+
overall_score = (clarity + relevance + depth + confidence) / 4
|
| 28 |
+
|
| 29 |
+
# Suggestions d'amélioration
|
| 30 |
+
improvements = []
|
| 31 |
+
if clarity < 0.5:
|
| 32 |
+
improvements.append("Clarifier la réponse.")
|
| 33 |
+
if relevance < 0.5:
|
| 34 |
+
improvements.append("Recentrer sur la question.")
|
| 35 |
+
if depth < 0.5:
|
| 36 |
+
improvements.append("Approfondir l'analyse.")
|
| 37 |
+
|
| 38 |
+
return {
|
| 39 |
+
'evaluation': {
|
| 40 |
+
'clarity': clarity,
|
| 41 |
+
'relevance': relevance,
|
| 42 |
+
'depth': depth,
|
| 43 |
+
'confidence': confidence,
|
| 44 |
+
'overall_score': overall_score
|
| 45 |
+
},
|
| 46 |
+
'improvements': improvements,
|
| 47 |
+
'should_improve': overall_score < 0.7
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
def _evaluate_clarity(self, response: Dict[str, Any]) -> float:
|
| 51 |
+
# Évalue la clarté de la réponse
|
| 52 |
+
text = response.get('response', '')
|
| 53 |
+
# Logique simplifiée: longueur et structure
|
| 54 |
+
if len(text) > 100 and '.' in text:
|
| 55 |
+
return 0.8
|
| 56 |
+
return 0.5
|
| 57 |
+
|
| 58 |
+
def _evaluate_relevance(self, response: Dict[str, Any], context: Dict[str, Any]) -> float:
|
| 59 |
+
# Évalue la pertinence par rapport au contexte et à la question
|
| 60 |
+
# Pour l'instant, on retourne un score fixe
|
| 61 |
+
return 0.8
|
| 62 |
+
|
| 63 |
+
def _evaluate_depth(self, response: Dict[str, Any]) -> float:
|
| 64 |
+
# Évalue la profondeur de l'analyse
|
| 65 |
+
analysis = response.get('analysis', {})
|
| 66 |
+
if analysis and 'synthesis' in analysis:
|
| 67 |
+
return 0.7
|
| 68 |
+
return 0.5
|