cognitive-engine 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. cognitive_engine-0.2.0/PKG-INFO +91 -0
  2. cognitive_engine-0.2.0/README.md +73 -0
  3. cognitive_engine-0.2.0/cognitive_engine/__init__.py +7 -0
  4. cognitive_engine-0.2.0/cognitive_engine/adapters/__init__.py +2 -0
  5. cognitive_engine-0.2.0/cognitive_engine/adapters/plastic_numeric_adapter.py +141 -0
  6. cognitive_engine-0.2.0/cognitive_engine/api/__init__.py +2 -0
  7. cognitive_engine-0.2.0/cognitive_engine/api/service.py +41 -0
  8. cognitive_engine-0.2.0/cognitive_engine/compression/__init__.py +2 -0
  9. cognitive_engine-0.2.0/cognitive_engine/compression/knowledge_compressor.py +53 -0
  10. cognitive_engine-0.2.0/cognitive_engine/compression/semantic_compressor_v2.py +115 -0
  11. cognitive_engine-0.2.0/cognitive_engine/config/__init__.py +2 -0
  12. cognitive_engine-0.2.0/cognitive_engine/config/loader.py +44 -0
  13. cognitive_engine-0.2.0/cognitive_engine/config/schema.py +45 -0
  14. cognitive_engine-0.2.0/cognitive_engine/consolidation/__init__.py +2 -0
  15. cognitive_engine-0.2.0/cognitive_engine/consolidation/engine.py +25 -0
  16. cognitive_engine-0.2.0/cognitive_engine/consolidation/engine_v2.py +39 -0
  17. cognitive_engine-0.2.0/cognitive_engine/context/__init__.py +2 -0
  18. cognitive_engine-0.2.0/cognitive_engine/context/long_context.py +64 -0
  19. cognitive_engine-0.2.0/cognitive_engine/core/__init__.py +2 -0
  20. cognitive_engine-0.2.0/cognitive_engine/core/builder.py +154 -0
  21. cognitive_engine-0.2.0/cognitive_engine/core/engine.py +174 -0
  22. cognitive_engine-0.2.0/cognitive_engine/core/engine_v2.py +280 -0
  23. cognitive_engine-0.2.0/cognitive_engine/core/registry.py +29 -0
  24. cognitive_engine-0.2.0/cognitive_engine/core/types.py +346 -0
  25. cognitive_engine-0.2.0/cognitive_engine/interfaces/__init__.py +2 -0
  26. cognitive_engine-0.2.0/cognitive_engine/interfaces/base.py +181 -0
  27. cognitive_engine-0.2.0/cognitive_engine/memory/__init__.py +2 -0
  28. cognitive_engine-0.2.0/cognitive_engine/memory/graph_memory.py +165 -0
  29. cognitive_engine-0.2.0/cognitive_engine/memory/hybrid_memory.py +110 -0
  30. cognitive_engine-0.2.0/cognitive_engine/memory/project_memory.py +80 -0
  31. cognitive_engine-0.2.0/cognitive_engine/memory/stores.py +177 -0
  32. cognitive_engine-0.2.0/cognitive_engine/memory/vector_store.py +28 -0
  33. cognitive_engine-0.2.0/cognitive_engine/models/__init__.py +2 -0
  34. cognitive_engine-0.2.0/cognitive_engine/models/stable_core.py +79 -0
  35. cognitive_engine-0.2.0/cognitive_engine/modules/__init__.py +2 -0
  36. cognitive_engine-0.2.0/cognitive_engine/modules/importance_evaluator.py +96 -0
  37. cognitive_engine-0.2.0/cognitive_engine/modules/input_processing.py +78 -0
  38. cognitive_engine-0.2.0/cognitive_engine/modules/semantic_understanding.py +130 -0
  39. cognitive_engine-0.2.0/cognitive_engine/nlp/__init__.py +16 -0
  40. cognitive_engine-0.2.0/cognitive_engine/nlp/models.py +116 -0
  41. cognitive_engine-0.2.0/cognitive_engine/nlp/trainer.py +95 -0
  42. cognitive_engine-0.2.0/cognitive_engine/replay/__init__.py +2 -0
  43. cognitive_engine-0.2.0/cognitive_engine/replay/buffer.py +40 -0
  44. cognitive_engine-0.2.0/cognitive_engine/routing/__init__.py +2 -0
  45. cognitive_engine-0.2.0/cognitive_engine/routing/dynamic_router.py +45 -0
  46. cognitive_engine-0.2.0/cognitive_engine/routing/learned_router.py +165 -0
  47. cognitive_engine-0.2.0/cognitive_engine/specialists/__init__.py +2 -0
  48. cognitive_engine-0.2.0/cognitive_engine/specialists/runtime.py +97 -0
  49. cognitive_engine-0.2.0/cognitive_engine/stability/__init__.py +2 -0
  50. cognitive_engine-0.2.0/cognitive_engine/stability/governor.py +38 -0
  51. cognitive_engine-0.2.0/cognitive_engine/training/__init__.py +2 -0
  52. cognitive_engine-0.2.0/cognitive_engine/training/online_trainer.py +87 -0
  53. cognitive_engine-0.2.0/cognitive_engine/utils/__init__.py +2 -0
  54. cognitive_engine-0.2.0/cognitive_engine/utils/numeric.py +67 -0
  55. cognitive_engine-0.2.0/cognitive_engine/utils/seeding.py +13 -0
  56. cognitive_engine-0.2.0/cognitive_engine/utils/telemetry.py +39 -0
  57. cognitive_engine-0.2.0/cognitive_engine/utils/text.py +104 -0
  58. cognitive_engine-0.2.0/cognitive_engine/utils/visualization.py +87 -0
  59. cognitive_engine-0.2.0/cognitive_engine.egg-info/PKG-INFO +91 -0
  60. cognitive_engine-0.2.0/cognitive_engine.egg-info/SOURCES.txt +67 -0
  61. cognitive_engine-0.2.0/cognitive_engine.egg-info/dependency_links.txt +1 -0
  62. cognitive_engine-0.2.0/cognitive_engine.egg-info/requires.txt +10 -0
  63. cognitive_engine-0.2.0/cognitive_engine.egg-info/top_level.txt +1 -0
  64. cognitive_engine-0.2.0/pyproject.toml +29 -0
  65. cognitive_engine-0.2.0/setup.cfg +4 -0
  66. cognitive_engine-0.2.0/tests/test_engine_smoke.py +14 -0
  67. cognitive_engine-0.2.0/tests/test_engine_v2.py +30 -0
  68. cognitive_engine-0.2.0/tests/test_learned_router_v2.py +38 -0
  69. cognitive_engine-0.2.0/tests/test_numeric_training.py +30 -0
@@ -0,0 +1,91 @@
1
+ Metadata-Version: 2.4
2
+ Name: cognitive-engine
3
+ Version: 0.2.0
4
+ Summary: Librería NLP basada en Arquitectura Cognitiva V2 (Aprendizaje Continuo y Memoria).
5
+ Author-email: bueormnew <dalusx64@gmail.com>
6
+ Requires-Python: >=3.12
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: matplotlib>=3.10
9
+ Requires-Dist: networkx>=3.6
10
+ Requires-Dist: numpy>=2.4
11
+ Requires-Dist: python-docx>=1.2
12
+ Requires-Dist: pyyaml>=6.0
13
+ Requires-Dist: pytest>=9.0
14
+ Requires-Dist: torch>=2.12
15
+ Requires-Dist: tqdm>=4.67
16
+ Requires-Dist: transformers>=4.30.0
17
+ Requires-Dist: datasets>=2.14.0
18
+
19
+ # Cognitive Engine NLP Library
20
+
21
+ Una librería de Python de alto nivel para construir, entrenar y desplegar modelos de Inteligencia Artificial basados en la Arquitectura Cognitiva Modular V2.
22
+
23
+ Esta librería facilita la adopción de las ventajas de la Arquitectura V2 (Aprendizaje Continuo Selectivo, Memoria Jerárquica, Enrutamiento Dinámico) en tareas clásicas de NLP como:
24
+ - Modelos Text-to-Text (e.g. Traducción, Resumen).
25
+ - Encoders para representación densa (e.g. tipo BERT).
26
+
27
+ ## Instalación
28
+
29
+ Puedes instalar la librería y sus dependencias (incluyendo soporte para Hugging Face) directamente desde el código fuente o mediante `pip` si está publicada:
30
+
31
+ ```bash
32
+ pip install cognitive-engine
33
+ # o desde el código fuente:
34
+ pip install .
35
+ ```
36
+
37
+ ## Dependencias
38
+
39
+ - `torch>=2.12`
40
+ - `transformers>=4.30.0`
41
+ - `datasets>=2.14.0`
42
+ - Y dependencias internas como `networkx`, `numpy`, etc.
43
+
44
+ ## Estructura de la API de Alto Nivel
45
+
46
+ La API se encuentra en `cognitive_engine.nlp` y expone un flujo de trabajo muy similar a las librerías tradicionales de Deep Learning, pero operando sobre la Memoria y el Routing de V2.
47
+
48
+ - **`CognitiveTranslator`**: Modelo pre-configurado para tareas generativas (traducción).
49
+ - **`CognitiveEncoder`**: Modelo pre-configurado para extraer representaciones contextuales.
50
+ - **`CognitiveTrainer`**: Motor de entrenamiento que mapea `epochs` a ciclos de Inyección de Memoria y Consolidación Plástica.
51
+
52
+ ## Ejemplo de Uso: Traducción Inglés a Español
53
+
54
+ ```python
55
+ from cognitive_engine.nlp import CognitiveTranslator, CognitiveTrainer
56
+ from datasets import load_dataset
57
+
58
+ # 1. Cargar Dataset (ejemplo desde HuggingFace)
59
+ dataset = load_dataset("opus_books", "en-es", split="train[:100]")
60
+
61
+ # 2. Inicializar Modelo de Traducción
62
+ modelo = CognitiveTranslator(source_lang="en", target_lang="es")
63
+
64
+ # 3. Entrenar el Modelo (Aprendizaje Continuo V2)
65
+ trainer = CognitiveTrainer(
66
+ model=modelo,
67
+ train_dataset=dataset["translation"],
68
+ args={"num_train_epochs": 3, "consolidation_steps": 50}
69
+ )
70
+ trainer.train()
71
+
72
+ # 4. Inferir
73
+ respuesta = modelo.translate("The architecture is very robust.", allow_learning=False)
74
+ print("Traducción:", respuesta)
75
+
76
+ # 5. Guardar el Modelo (incluye su Memoria)
77
+ modelo.save_pretrained("./mi_modelo_traductor")
78
+
79
+ # 6. Cargar el Modelo desde el disco
80
+ modelo_cargado = CognitiveTranslator.from_pretrained("./mi_modelo_traductor")
81
+ ```
82
+
83
+ ## Arquitectura Interna
84
+
85
+ La librería envuelve:
86
+ - **`SemanticBackboneV2`**: Como encoder principal.
87
+ - **`StableCoreV2`**: Como generador o reasoner.
88
+ - **`MemorySystem`**: Donde se guarda el conocimiento aprendido de los datasets.
89
+ - **`ConsolidationEngine`**: Ejecutado periódicamente por el `CognitiveTrainer` para fusionar patrones y abstraer conocimiento.
90
+
91
+ Para un análisis detallado del engine V2, revisa los documentos en `docs/`.
@@ -0,0 +1,73 @@
1
+ # Cognitive Engine NLP Library
2
+
3
+ Una librería de Python de alto nivel para construir, entrenar y desplegar modelos de Inteligencia Artificial basados en la Arquitectura Cognitiva Modular V2.
4
+
5
+ Esta librería facilita la adopción de las ventajas de la Arquitectura V2 (Aprendizaje Continuo Selectivo, Memoria Jerárquica, Enrutamiento Dinámico) en tareas clásicas de NLP como:
6
+ - Modelos Text-to-Text (e.g. Traducción, Resumen).
7
+ - Encoders para representación densa (e.g. tipo BERT).
8
+
9
+ ## Instalación
10
+
11
+ Puedes instalar la librería y sus dependencias (incluyendo soporte para Hugging Face) directamente desde el código fuente o mediante `pip` si está publicada:
12
+
13
+ ```bash
14
+ pip install cognitive-engine
15
+ # o desde el código fuente:
16
+ pip install .
17
+ ```
18
+
19
+ ## Dependencias
20
+
21
+ - `torch>=2.12`
22
+ - `transformers>=4.30.0`
23
+ - `datasets>=2.14.0`
24
+ - Y dependencias internas como `networkx`, `numpy`, etc.
25
+
26
+ ## Estructura de la API de Alto Nivel
27
+
28
+ La API se encuentra en `cognitive_engine.nlp` y expone un flujo de trabajo muy similar a las librerías tradicionales de Deep Learning, pero operando sobre la Memoria y el Routing de V2.
29
+
30
+ - **`CognitiveTranslator`**: Modelo pre-configurado para tareas generativas (traducción).
31
+ - **`CognitiveEncoder`**: Modelo pre-configurado para extraer representaciones contextuales.
32
+ - **`CognitiveTrainer`**: Motor de entrenamiento que mapea `epochs` a ciclos de Inyección de Memoria y Consolidación Plástica.
33
+
34
+ ## Ejemplo de Uso: Traducción Inglés a Español
35
+
36
+ ```python
37
+ from cognitive_engine.nlp import CognitiveTranslator, CognitiveTrainer
38
+ from datasets import load_dataset
39
+
40
+ # 1. Cargar Dataset (ejemplo desde HuggingFace)
41
+ dataset = load_dataset("opus_books", "en-es", split="train[:100]")
42
+
43
+ # 2. Inicializar Modelo de Traducción
44
+ modelo = CognitiveTranslator(source_lang="en", target_lang="es")
45
+
46
+ # 3. Entrenar el Modelo (Aprendizaje Continuo V2)
47
+ trainer = CognitiveTrainer(
48
+ model=modelo,
49
+ train_dataset=dataset["translation"],
50
+ args={"num_train_epochs": 3, "consolidation_steps": 50}
51
+ )
52
+ trainer.train()
53
+
54
+ # 4. Inferir
55
+ respuesta = modelo.translate("The architecture is very robust.", allow_learning=False)
56
+ print("Traducción:", respuesta)
57
+
58
+ # 5. Guardar el Modelo (incluye su Memoria)
59
+ modelo.save_pretrained("./mi_modelo_traductor")
60
+
61
+ # 6. Cargar el Modelo desde el disco
62
+ modelo_cargado = CognitiveTranslator.from_pretrained("./mi_modelo_traductor")
63
+ ```
64
+
65
+ ## Arquitectura Interna
66
+
67
+ La librería envuelve:
68
+ - **`SemanticBackboneV2`**: Como encoder principal.
69
+ - **`StableCoreV2`**: Como generador o reasoner.
70
+ - **`MemorySystem`**: Donde se guarda el conocimiento aprendido de los datasets.
71
+ - **`ConsolidationEngine`**: Ejecutado periódicamente por el `CognitiveTrainer` para fusionar patrones y abstraer conocimiento.
72
+
73
+ Para un análisis detallado del engine V2, revisa los documentos en `docs/`.
@@ -0,0 +1,7 @@
1
+ """Hybrid cognitive architecture package."""
2
+
3
+ from cognitive_engine.core.builder import EngineBuilder
4
+ from cognitive_engine.core.engine import CognitiveEngine
5
+ from cognitive_engine.core.engine_v2 import CognitiveEngineV2
6
+
7
+ __all__ = ["CognitiveEngine", "CognitiveEngineV2", "EngineBuilder"]
@@ -0,0 +1,2 @@
1
+ """Plastic adapters and localized learners."""
2
+
@@ -0,0 +1,141 @@
1
+ from __future__ import annotations
2
+
3
+ from copy import deepcopy
4
+ from typing import Any, Dict, Optional
5
+
6
+ import torch
7
+ from torch import nn
8
+ from torch.nn import functional as F
9
+
10
+ from cognitive_engine.core.types import CoreInference, NumericBatch, SemanticState
11
+ from cognitive_engine.interfaces.base import PlasticLearner
12
+
13
+
14
+ class LowRankResidual(nn.Module):
15
+ def __init__(self, input_dim: int, hidden_dim: int, rank: int = 4, alpha: float = 8.0) -> None:
16
+ super().__init__()
17
+ self.frozen = nn.Linear(input_dim, hidden_dim)
18
+ self.adapter_a = nn.Linear(input_dim, rank, bias=False)
19
+ self.adapter_b = nn.Linear(rank, hidden_dim, bias=False)
20
+ self.scale = alpha / rank
21
+ for parameter in self.frozen.parameters():
22
+ parameter.requires_grad = False
23
+
24
+ def forward(self, inputs: torch.Tensor) -> torch.Tensor:
25
+ return self.frozen(inputs) + self.adapter_b(self.adapter_a(inputs)) * self.scale
26
+
27
+
28
+ class PlasticArithmeticModule(nn.Module, PlasticLearner):
29
+ name = "plastic_arithmetic_module"
30
+
31
+ def __init__(
32
+ self,
33
+ input_dim: int = 7,
34
+ hidden_dim: int = 48,
35
+ operations: int = 3,
36
+ output_scale: float = 144.0,
37
+ device: str = "cpu",
38
+ ) -> None:
39
+ super().__init__()
40
+ self.device = device
41
+ self.operations = operations
42
+ self.output_scale = output_scale
43
+ self.adapter = LowRankResidual(input_dim + operations, hidden_dim)
44
+ self.hidden = nn.Sequential(nn.Tanh(), nn.Linear(hidden_dim, hidden_dim), nn.Tanh())
45
+ self.direct_head = nn.Linear(input_dim + operations, operations)
46
+ self.residual_head = nn.Linear(hidden_dim, operations)
47
+ self.confidence_head = nn.Linear(hidden_dim, 1)
48
+ self.loss_fn = nn.SmoothL1Loss()
49
+ self.to(self.device)
50
+ self.optimizer = torch.optim.AdamW(self.trainable_parameters(), lr=2e-3, weight_decay=1e-4)
51
+ self._initial_trainable_state = deepcopy(self._trainable_state_dict())
52
+
53
+ def describe(self) -> Dict[str, Any]:
54
+ return {"name": self.name, "operations": self.operations, "trainable_parameters": sum(p.numel() for p in self.trainable_parameters())}
55
+
56
+ def predict(self, semantic_state: SemanticState) -> CoreInference:
57
+ batch = self._batch_from_semantic_state(semantic_state)
58
+ predictions, confidence = self._forward(batch.features, batch.operation_ids)
59
+ value = float(predictions.squeeze(0).detach().cpu().item())
60
+ confidence_score = float(confidence.squeeze(0).detach().cpu().item())
61
+ return CoreInference(
62
+ prediction=value,
63
+ confidence=max(0.01, min(0.999, confidence_score)),
64
+ explanation=f"Plastic arithmetic pathway predicted {value:.3f} for operation {semantic_state.metadata['operation']}.",
65
+ artifacts={"raw_prediction": value, "confidence": confidence_score},
66
+ )
67
+
68
+ def train_step(self, batch: NumericBatch, replay_batch: Optional[NumericBatch] = None) -> Dict[str, float]:
69
+ self.train()
70
+ features = batch.features
71
+ operation_ids = batch.operation_ids
72
+ targets = batch.targets
73
+ replay_ratio = 0.0
74
+ if replay_batch is not None and replay_batch.targets is not None:
75
+ replay_ratio = len(replay_batch.targets) / max(len(targets), 1)
76
+ features = torch.cat([features, replay_batch.features], dim=0)
77
+ operation_ids = torch.cat([operation_ids, replay_batch.operation_ids], dim=0)
78
+ targets = torch.cat([targets, replay_batch.targets], dim=0)
79
+
80
+ predictions, confidence = self._forward(features, operation_ids)
81
+ normalized_targets = targets / self.output_scale
82
+ main_loss = self.loss_fn(predictions / self.output_scale, normalized_targets)
83
+ confidence_loss = F.mse_loss(confidence, torch.exp(-torch.abs(predictions - targets) / self.output_scale))
84
+ stability_penalty = 5e-4 * self.parameter_drift()
85
+ loss = main_loss + 0.15 * confidence_loss + stability_penalty
86
+
87
+ self.optimizer.zero_grad()
88
+ loss.backward()
89
+ nn.utils.clip_grad_norm_(list(self.trainable_parameters()), max_norm=1.0)
90
+ self.optimizer.step()
91
+
92
+ mae = torch.mean(torch.abs(predictions - targets)).item()
93
+ accuracy = torch.mean((torch.abs(predictions - targets) < 0.5).float()).item()
94
+ return {
95
+ "loss": float(loss.item()),
96
+ "mae": float(mae),
97
+ "accuracy": float(accuracy),
98
+ "replay_ratio": float(replay_ratio),
99
+ "plastic_norm": float(self.parameter_drift()),
100
+ "stable_drift": 0.0,
101
+ }
102
+
103
+ def parameter_drift(self) -> float:
104
+ initial = self._initial_trainable_state
105
+ total = 0.0
106
+ for name, current in self._trainable_state_dict().items():
107
+ total += torch.norm(current - initial[name]).item()
108
+ return total
109
+
110
+ def trainable_parameters(self):
111
+ return (parameter for parameter in self.parameters() if parameter.requires_grad)
112
+
113
+ def _forward(self, features: torch.Tensor, operation_ids: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
114
+ op_one_hot = F.one_hot(operation_ids, num_classes=self.operations).float()
115
+ merged = torch.cat([features, op_one_hot], dim=-1)
116
+ hidden = self.hidden(self.adapter(merged))
117
+ outputs = self.direct_head(merged) + 0.35 * self.residual_head(hidden)
118
+ gathered = outputs.gather(1, operation_ids.unsqueeze(1)).squeeze(1) * self.output_scale
119
+ confidence = torch.sigmoid(self.confidence_head(hidden)).squeeze(1)
120
+ return gathered, confidence
121
+
122
+ def _batch_from_semantic_state(self, semantic_state: SemanticState) -> NumericBatch:
123
+ features = semantic_state.metadata["numeric_features"].float().to(self.device).unsqueeze(0)
124
+ operation_id = torch.tensor([semantic_state.metadata["operation_id"]], dtype=torch.long, device=self.device)
125
+ target = semantic_state.metadata.get("target")
126
+ targets = None
127
+ if target is not None:
128
+ targets = torch.tensor([float(target)], dtype=torch.float32, device=self.device)
129
+ return NumericBatch(features=features, operation_ids=operation_id, targets=targets)
130
+
131
+ def _trainable_state_dict(self) -> Dict[str, torch.Tensor]:
132
+ return {
133
+ name: tensor.detach().clone()
134
+ for name, tensor in self.state_dict().items()
135
+ if self.state_dict()[name].dtype.is_floating_point and self._is_trainable_name(name)
136
+ }
137
+
138
+ def _is_trainable_name(self, name: str) -> bool:
139
+ if name.startswith("adapter.frozen"):
140
+ return False
141
+ return True
@@ -0,0 +1,2 @@
1
+ """Service layer interfaces."""
2
+
@@ -0,0 +1,41 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Dict
4
+
5
+ from cognitive_engine.core.engine import CognitiveEngine
6
+
7
+
8
+ class CognitiveAPIService:
9
+ def __init__(self, engine: CognitiveEngine) -> None:
10
+ self.engine = engine
11
+
12
+ def ingest_text(self, text: str) -> Dict[str, Any]:
13
+ response = self.engine.process(text, allow_learning=True)
14
+ return self._serialize(response)
15
+
16
+ def ask_text(self, text: str) -> Dict[str, Any]:
17
+ response = self.engine.process(text, allow_learning=False)
18
+ return self._serialize(response)
19
+
20
+ def train_numeric(self, a: float, b: float, operation: str, target: float) -> Dict[str, Any]:
21
+ response = self.engine.process(
22
+ {"modality": "numeric", "a": a, "b": b, "operation": operation, "target": target},
23
+ allow_learning=True,
24
+ )
25
+ return self._serialize(response)
26
+
27
+ def infer_numeric(self, a: float, b: float, operation: str) -> Dict[str, Any]:
28
+ response = self.engine.process({"modality": "numeric", "a": a, "b": b, "operation": operation}, allow_learning=False)
29
+ return self._serialize(response)
30
+
31
+ def _serialize(self, response: Any) -> Dict[str, Any]:
32
+ return {
33
+ "text": response.text,
34
+ "importance_action": response.importance.action,
35
+ "importance_score": response.importance.importance_score,
36
+ "confidence_score": response.inference.confidence,
37
+ "learning_applied": response.learning_applied,
38
+ "trace_count": len(response.traces),
39
+ "memory_snapshot": self.engine.snapshot(),
40
+ }
41
+
@@ -0,0 +1,2 @@
1
+ """Knowledge compression modules."""
2
+
@@ -0,0 +1,53 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Dict, List, Tuple
4
+ from uuid import uuid4
5
+
6
+ from cognitive_engine.core.types import CompressedKnowledge, ImportanceAssessment, SemanticState, tensor_to_numpy
7
+ from cognitive_engine.interfaces.base import KnowledgeCompressor
8
+
9
+
10
+ class SemanticKnowledgeCompressor(KnowledgeCompressor):
11
+ name = "semantic_knowledge_compressor"
12
+
13
+ def describe(self) -> Dict[str, Any]:
14
+ return {"name": self.name}
15
+
16
+ def compress(self, semantic_state: SemanticState, importance: ImportanceAssessment) -> CompressedKnowledge:
17
+ concepts = [concept.label for concept in semantic_state.concepts]
18
+ relations = self._build_relations(semantic_state, concepts)
19
+ summary = self._rewrite_summary(semantic_state, concepts, importance.action)
20
+ return CompressedKnowledge(
21
+ record_id=str(uuid4()),
22
+ source_type=semantic_state.modality,
23
+ summary=summary,
24
+ concepts=concepts,
25
+ relations=relations,
26
+ embedding=tensor_to_numpy(semantic_state.pooled_embedding.float()),
27
+ importance=importance.importance_score,
28
+ confidence=importance.confidence_score,
29
+ provenance={"intent": semantic_state.intent, "compressed_context": semantic_state.compressed_context},
30
+ metadata={"action": importance.action, "raw_modality": semantic_state.modality},
31
+ )
32
+
33
+ def _build_relations(self, semantic_state: SemanticState, concepts: List[str]) -> List[Tuple[str, str, str]]:
34
+ if len(concepts) < 2:
35
+ return [(concepts[0], semantic_state.intent, semantic_state.compressed_context)] if concepts else []
36
+ root = concepts[0]
37
+ return [(root, semantic_state.intent, concept) for concept in concepts[1:4]]
38
+
39
+ def _rewrite_summary(self, semantic_state: SemanticState, concepts: List[str], action: str) -> str:
40
+ if semantic_state.modality == "numeric":
41
+ a = semantic_state.metadata["a"]
42
+ b = semantic_state.metadata["b"]
43
+ op = semantic_state.metadata["operation"]
44
+ return f"Arithmetic pattern observed: {a:g} {op} {b:g} with action={action}"
45
+ concept_text = ", ".join(concepts[:4]) if concepts else semantic_state.compressed_context
46
+ if semantic_state.intent == "preference":
47
+ return f"User preference extracted: {concept_text}"
48
+ if semantic_state.intent == "correction":
49
+ return f"Correction compressed: {concept_text}"
50
+ if semantic_state.intent == "knowledge_share":
51
+ return f"Knowledge compressed: {concept_text}"
52
+ return f"Contextual memory: {semantic_state.compressed_context}"
53
+
@@ -0,0 +1,115 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Dict, List
4
+ from uuid import uuid4
5
+
6
+ from cognitive_engine.compression.knowledge_compressor import SemanticKnowledgeCompressor
7
+ from cognitive_engine.core.types import (
8
+ CompressionBundle,
9
+ GraphEdge,
10
+ GraphNode,
11
+ GraphPatch,
12
+ ImportanceAssessment,
13
+ KnowledgeTriple,
14
+ PreferenceRecord,
15
+ ProcedureMemory,
16
+ SemanticState,
17
+ )
18
+
19
+
20
+ class SemanticCompressorV2(SemanticKnowledgeCompressor):
21
+ name = "semantic_compressor_v2"
22
+
23
+ def describe(self) -> Dict[str, Any]:
24
+ return {"name": self.name, "base": "semantic_knowledge_compressor"}
25
+
26
+ def compress_v2(self, semantic_state: SemanticState, importance: ImportanceAssessment, project_id: str = "global") -> CompressionBundle:
27
+ knowledge = self.compress(semantic_state, importance)
28
+ concepts = [concept.label for concept in semantic_state.concepts]
29
+ triples = self._triples(semantic_state, concepts)
30
+ graph_patch = self._graph_patch(semantic_state, concepts, triples, project_id)
31
+ preferences = self._preferences(semantic_state, concepts)
32
+ procedures = self._procedures(semantic_state, concepts)
33
+ return CompressionBundle(
34
+ compressed_knowledge=[knowledge],
35
+ triples=triples,
36
+ graph_patch=graph_patch,
37
+ preferences=preferences,
38
+ procedures=procedures,
39
+ validation_requests=[] if importance.confidence_score > 0.65 else [f"Validate low confidence memory: {knowledge.summary}"],
40
+ )
41
+
42
+ def _triples(self, semantic_state: SemanticState, concepts: List[str]) -> List[KnowledgeTriple]:
43
+ triples: List[KnowledgeTriple] = []
44
+ if not concepts:
45
+ return triples
46
+ subject = concepts[0]
47
+ for concept in concepts[1:5]:
48
+ triples.append(KnowledgeTriple(subject=subject, relation=semantic_state.intent, object=concept, confidence=0.75))
49
+ if semantic_state.intent == "preference":
50
+ triples.append(KnowledgeTriple(subject="user", relation="prefers", object=", ".join(concepts[:4]), confidence=0.82))
51
+ return triples
52
+
53
+ def _graph_patch(self, semantic_state: SemanticState, concepts: List[str], triples: List[KnowledgeTriple], project_id: str) -> GraphPatch:
54
+ nodes: Dict[str, GraphNode] = {}
55
+ edges: List[GraphEdge] = []
56
+
57
+ def node(label: str, node_type: str) -> GraphNode:
58
+ key = f"{node_type}:{label}".lower()
59
+ if key not in nodes:
60
+ nodes[key] = GraphNode(node_id=str(uuid4()), node_type=node_type, label=label, project_id=project_id, confidence=0.75)
61
+ return nodes[key]
62
+
63
+ intent_node = node(semantic_state.intent, "Concept")
64
+ for concept in concepts:
65
+ concept_node = node(concept, "Concept")
66
+ edges.append(
67
+ GraphEdge(
68
+ edge_id=str(uuid4()),
69
+ source_id=intent_node.node_id,
70
+ target_id=concept_node.node_id,
71
+ relation="mentions",
72
+ confidence=0.7,
73
+ )
74
+ )
75
+ for triple in triples:
76
+ source = node(triple.subject, "Concept")
77
+ target = node(triple.object, "Concept")
78
+ edges.append(
79
+ GraphEdge(
80
+ edge_id=str(uuid4()),
81
+ source_id=source.node_id,
82
+ target_id=target.node_id,
83
+ relation=triple.relation,
84
+ confidence=triple.confidence,
85
+ )
86
+ )
87
+ return GraphPatch(nodes=list(nodes.values()), edges=edges, source="semantic_compressor_v2")
88
+
89
+ def _preferences(self, semantic_state: SemanticState, concepts: List[str]) -> List[PreferenceRecord]:
90
+ if semantic_state.intent != "preference":
91
+ return []
92
+ value = ", ".join(concepts[:5]) if concepts else semantic_state.compressed_context
93
+ return [PreferenceRecord(key="user.preference", value=value, confidence=0.82, source="semantic_compressor_v2")]
94
+
95
+ def _procedures(self, semantic_state: SemanticState, concepts: List[str]) -> List[ProcedureMemory]:
96
+ lowered = semantic_state.compressed_context.lower()
97
+ if not any(marker in lowered for marker in ["fix", "debug", "test", "error", "bug", "correccion", "corrige"]):
98
+ return []
99
+ title = f"Procedure from {semantic_state.intent}: {', '.join(concepts[:3])}"
100
+ steps = [
101
+ "Inspect the relevant project graph nodes.",
102
+ "Reproduce the observed issue or requirement.",
103
+ "Apply the smallest compatible change.",
104
+ "Run focused validation before broad validation.",
105
+ ]
106
+ return [
107
+ ProcedureMemory(
108
+ procedure_id=str(uuid4()),
109
+ title=title,
110
+ steps=steps,
111
+ domains=concepts[:5],
112
+ confidence=0.68,
113
+ )
114
+ ]
115
+
@@ -0,0 +1,2 @@
1
+ """Configuration schemas and loaders."""
2
+
@@ -0,0 +1,44 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import asdict
4
+ from pathlib import Path
5
+ from typing import Any, Dict, Type, TypeVar
6
+
7
+ import yaml
8
+
9
+ from cognitive_engine.config.schema import EngineConfig, MemoryConfig, NumericDemoConfig, ThresholdConfig
10
+
11
+
12
+ T = TypeVar("T")
13
+
14
+
15
+ def _merge_dicts(base: Dict[str, Any], override: Dict[str, Any]) -> Dict[str, Any]:
16
+ merged = dict(base)
17
+ for key, value in override.items():
18
+ if isinstance(value, dict) and isinstance(merged.get(key), dict):
19
+ merged[key] = _merge_dicts(merged[key], value)
20
+ else:
21
+ merged[key] = value
22
+ return merged
23
+
24
+
25
+ def _construct(dataclass_type: Type[T], data: Dict[str, Any]) -> T:
26
+ if dataclass_type is EngineConfig:
27
+ thresholds = _construct(ThresholdConfig, data.get("thresholds", {}))
28
+ memory = _construct(MemoryConfig, data.get("memory", {}))
29
+ numeric_demo = _construct(NumericDemoConfig, data.get("numeric_demo", {}))
30
+ payload = {**data, "thresholds": thresholds, "memory": memory, "numeric_demo": numeric_demo}
31
+ return dataclass_type(**payload)
32
+ return dataclass_type(**data)
33
+
34
+
35
+ def load_engine_config(path: str | Path | None = None) -> EngineConfig:
36
+ defaults = asdict(EngineConfig())
37
+ if path is None:
38
+ return EngineConfig()
39
+
40
+ config_path = Path(path)
41
+ override = yaml.safe_load(config_path.read_text(encoding="utf-8")) or {}
42
+ merged = _merge_dicts(defaults, override)
43
+ return _construct(EngineConfig, merged)
44
+
@@ -0,0 +1,45 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass, field
4
+ from typing import Dict, List
5
+
6
+
7
+ @dataclass
8
+ class ThresholdConfig:
9
+ learn: float = 0.62
10
+ reinforce: float = 0.75
11
+ consolidate: float = 0.82
12
+ uncertainty: float = 0.45
13
+ contradiction: float = 0.55
14
+
15
+
16
+ @dataclass
17
+ class MemoryConfig:
18
+ short_term_capacity: int = 24
19
+ episodic_capacity: int = 128
20
+ semantic_capacity: int = 512
21
+ replay_capacity: int = 512
22
+ retrieval_top_k: int = 5
23
+ consolidation_interval: int = 10
24
+
25
+
26
+ @dataclass
27
+ class NumericDemoConfig:
28
+ operations: List[str] = field(default_factory=lambda: ["add", "sub", "mul"])
29
+ max_operand: int = 12
30
+ train_size: int = 2048
31
+ val_size: int = 512
32
+ batch_size: int = 64
33
+ epochs: int = 25
34
+ replay_batch_size: int = 32
35
+
36
+
37
+ @dataclass
38
+ class EngineConfig:
39
+ seed: int = 13
40
+ device: str = "cpu"
41
+ thresholds: ThresholdConfig = field(default_factory=ThresholdConfig)
42
+ memory: MemoryConfig = field(default_factory=MemoryConfig)
43
+ numeric_demo: NumericDemoConfig = field(default_factory=NumericDemoConfig)
44
+ language_hints: Dict[str, str] = field(default_factory=lambda: {"default": "multilingual-lite"})
45
+
@@ -0,0 +1,2 @@
1
+ """Consolidation services."""
2
+
@@ -0,0 +1,25 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Dict
4
+
5
+ from cognitive_engine.core.types import ConsolidationReport
6
+ from cognitive_engine.interfaces.base import Consolidator
7
+
8
+
9
+ class BackgroundConsolidationEngine(Consolidator):
10
+ name = "background_consolidation_engine"
11
+
12
+ def __init__(self, memory_system: Any, replay_buffer: Any) -> None:
13
+ self.memory_system = memory_system
14
+ self.replay_buffer = replay_buffer
15
+
16
+ def describe(self) -> Dict[str, Any]:
17
+ return {"name": self.name}
18
+
19
+ def run(self) -> ConsolidationReport:
20
+ report = self.memory_system.consolidate()
21
+ if hasattr(self.replay_buffer, "rescale"):
22
+ report.replay_reweighted = self.replay_buffer.rescale()
23
+ report.notes += " Replay priorities were decayed to maintain balanced rehearsal."
24
+ return report
25
+