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ligente
|
| 5 |
+
- An谩lisis de intenci贸n avanzado
|
| 6 |
+
"""
|
| 7 |
+
import time
|
| 8 |
+
import hashlib
|
| 9 |
+
import re
|
| 10 |
+
from collections import defaultdict, deque
|
| 11 |
+
from typing import Dict, List, Tuple, Any
|
| 12 |
+
from dataclasses import dataclass
|
| 13 |
+
from enum import Enum
|
| 14 |
+
|
| 15 |
+
# ===================== ENUMERACIONES =====================
|
| 16 |
+
class IntentType(Enum):
|
| 17 |
+
IMAGE = "IMAGE"
|
| 18 |
+
CODE = "CODE"
|
| 19 |
+
REASONING = "REASONING"
|
| 20 |
+
ARCHITECTURE = "ARCHITECTURE"
|
| 21 |
+
DEVOPS = "DEVOPS"
|
| 22 |
+
QA = "QA"
|
| 23 |
+
VISUAL = "VISUAL"
|
| 24 |
+
GENERAL = "GENERAL"
|
| 25 |
+
|
| 26 |
+
# ===================== MODELOS DE DATOS =====================
|
| 27 |
+
@dataclass
|
| 28 |
+
class MemoryNode:
|
| 29 |
+
prompt: str
|
| 30 |
+
intent: IntentType
|
| 31 |
+
model: str
|
| 32 |
+
timestamp: float
|
| 33 |
+
success_score: float = 1.0
|
| 34 |
+
context_hash: str = ""
|
| 35 |
+
|
| 36 |
+
# ===================== REGISTRO DE LATENCIA MEJORADO =====================
|
| 37 |
+
class AdaptiveLatencyRegistry:
|
| 38 |
+
def __init__(self, window_size: int = 50):
|
| 39 |
+
self.data = defaultdict(lambda: deque(maxlen=window_size))
|
| 40 |
+
self.success_rates = defaultdict(lambda: deque(maxlen=window_size))
|
| 41 |
+
self.response_times = defaultdict(lambda: deque(maxlen=window_size))
|
| 42 |
+
|
| 43 |
+
def record(self, model: str, latency: float, success: bool = True):
|
| 44 |
+
self.data[model].append(latency)
|
| 45 |
+
self.success_rates[model].append(1.0 if success else 0.0)
|
| 46 |
+
self.response_times[model].append(time.time())
|
| 47 |
+
|
| 48 |
+
def get_composite_score(self, model: str) -> float:
|
| 49 |
+
"""Puntaje compuesto: latencia, tasa de 茅xito y frescura"""
|
| 50 |
+
if not self.data[model]:
|
| 51 |
+
return float("inf")
|
| 52 |
+
|
| 53 |
+
# Puntaje de latencia (menor es mejor)
|
| 54 |
+
latency_score = sum(self.data[model]) / len(self.data[model])
|
| 55 |
+
|
| 56 |
+
# Puntaje de 茅xito
|
| 57 |
+
success_rate = sum(self.success_rates[model]) / len(self.success_rates[model]) if self.success_rates[model] else 0.5
|
| 58 |
+
|
| 59 |
+
# Puntaje de frescura (preferir modelos usados recientemente)
|
| 60 |
+
freshness = 0.0
|
| 61 |
+
if self.response_times[model]:
|
| 62 |
+
latest = max(self.response_times[model])
|
| 63 |
+
freshness = min(1.0, (time.time() - latest) / 3600) # Normalizado por hora
|
| 64 |
+
|
| 65 |
+
return latency_score * (1.1 - success_rate) * (1.0 + freshness * 0.1)
|
| 66 |
+
|
| 67 |
+
# ===================== GRAFO DE MEMORIA AVANZADO =====================
|
| 68 |
+
class ContextMemoryGraph:
|
| 69 |
+
def __init__(self, max_nodes: int = 5000, embedding_dim: int = 384):
|
| 70 |
+
self.nodes: Dict[str, MemoryNode] = {}
|
| 71 |
+
self.context_order = deque(maxlen=max_nodes)
|
| 72 |
+
self.intent_clusters = defaultdict(list)
|
| 73 |
+
self.embedding_cache = {}
|
| 74 |
+
|
| 75 |
+
def _generate_hash(self, text: str, context: str = "") -> str:
|
| 76 |
+
"""Hash contextual para mejor recuperaci贸n"""
|
| 77 |
+
combined = f"{text}::{context}"
|
| 78 |
+
return hashlib.sha256(combined.encode()).hexdigest()[:24]
|
| 79 |
+
|
| 80 |
+
def _extract_keywords(self, text: str) -> List[str]:
|
| 81 |
+
"""Extrae palabras clave para clustering"""
|
| 82 |
+
words = re.findall(r'\b[a-z]{4,}\b', text.lower())
|
| 83 |
+
return [w for w in words if len(w) > 3][:10]
|
| 84 |
+
|
| 85 |
+
def store(self, prompt: str, intent: IntentType, model: str,
|
| 86 |
+
context: str = "", success_score: float = 1.0):
|
| 87 |
+
"""Almacena memoria con contexto"""
|
| 88 |
+
h = self._generate_hash(prompt, context)
|
| 89 |
+
|
| 90 |
+
if h not in self.nodes:
|
| 91 |
+
node = MemoryNode(
|
| 92 |
+
prompt=prompt,
|
| 93 |
+
intent=intent,
|
| 94 |
+
model=model,
|
| 95 |
+
timestamp=time.time(),
|
| 96 |
+
success_score=success_score,
|
| 97 |
+
context_hash=hashlib.md5(context.encode()).hexdigest()[:12]
|
| 98 |
+
)
|
| 99 |
+
self.nodes[h] = node
|
| 100 |
+
self.context_order.append(h)
|
| 101 |
+
|
| 102 |
+
# Clustering por intenci贸n
|
| 103 |
+
self.intent_clusters[intent.value].append(h)
|
| 104 |
+
|
| 105 |
+
def recall(self, intent: IntentType, current_context: str = "",
|
| 106 |
+
limit: int = 5) -> List[MemoryNode]:
|
| 107 |
+
"""Recupera memorias relevantes con matching contextual"""
|
| 108 |
+
relevant = []
|
| 109 |
+
|
| 110 |
+
for h in reversed(self.context_order):
|
| 111 |
+
node = self.nodes[h]
|
| 112 |
+
if node.intent == intent:
|
| 113 |
+
# Puntaje de relevancia basado en 茅xito y contexto
|
| 114 |
+
context_match = 1.0 if node.context_hash == hashlib.md5(
|
| 115 |
+
current_context.encode()).hexdigest()[:12] else 0.8
|
| 116 |
+
relevance = node.success_score * context_match
|
| 117 |
+
relevant.append((relevance, node))
|
| 118 |
+
|
| 119 |
+
# Ordenar por relevancia
|
| 120 |
+
relevant.sort(key=lambda x: x[0], reverse=True)
|
| 121 |
+
return [node for _, node in relevant[:limit]]
|
| 122 |
+
|
| 123 |
+
def find_similar(self, prompt: str, intent: IntentType = None) -> List[MemoryNode]:
|
| 124 |
+
"""Encuentra prompts similares usando keywords"""
|
| 125 |
+
keywords = set(self._extract_keywords(prompt))
|
| 126 |
+
similar = []
|
| 127 |
+
|
| 128 |
+
for h, node in self.nodes.items():
|
| 129 |
+
if intent and node.intent != intent:
|
| 130 |
+
continue
|
| 131 |
+
|
| 132 |
+
node_keywords = set(self._extract_keywords(node.prompt))
|
| 133 |
+
overlap = len(keywords.intersection(node_keywords))
|
| 134 |
+
|
| 135 |
+
if overlap >= 2: # Al menos 2 palabras clave coincidentes
|
| 136 |
+
similarity = overlap / max(len(keywords), len(node_keywords))
|
| 137 |
+
similar.append((similarity, node))
|
| 138 |
+
|
| 139 |
+
similar.sort(key=lambda x: x[0], reverse=True)
|
| 140 |
+
return [node for _, node in similar[:3]]
|
| 141 |
+
|
| 142 |
+
# ===================== SISTEMA DE ORQUESTACI脫N =====================
|
| 143 |
+
class CognitiveOrchestrator:
|
| 144 |
+
def __init__(self):
|
| 145 |
+
self.latency_registry = AdaptiveLatencyRegistry()
|
| 146 |
+
self.memory_graph = ContextMemoryGraph()
|
| 147 |
+
self.intent_history = deque(maxlen=100)
|
| 148 |
+
|
| 149 |
+
def analyze_intent(self, text: str, context: List[str] = None) -> IntentType:
|
| 150 |
+
"""An谩lisis de intenci贸n con contexto hist贸rico"""
|
| 151 |
+
text_lower = text.lower()
|
| 152 |
+
|
| 153 |
+
# Patrones espec铆ficos
|
| 154 |
+
patterns = {
|
| 155 |
+
IntentType.IMAGE: [
|
| 156 |
+
r'(imagen|ilustraci贸n|arte|render|dise帽o|visual|foto|retrato|cinematogr谩fico)',
|
| 157 |
+
r'genera.*imagen|crea.*visual|dise帽a.*arte'
|
| 158 |
+
],
|
| 159 |
+
IntentType.CODE: [
|
| 160 |
+
r'(c贸digo|programa|bug|error|python|api|endpoint|clase|funci贸n)',
|
| 161 |
+
r'implementa.*c贸digo|escribe.*programa|resuelve.*bug'
|
| 162 |
+
],
|
| 163 |
+
IntentType.REASONING: [
|
| 164 |
+
r'(analiza|razona|piensa|estrat茅gia|l贸gica|proceso|explica)',
|
| 165 |
+
r'por qu茅|c贸mo funciona|qu茅 significa|analiza.*situaci贸n'
|
| 166 |
+
],
|
| 167 |
+
IntentType.ARCHITECTURE: [
|
| 168 |
+
r'(arquitectura|microservicios|sistema|escala|dise帽o|patr贸n)',
|
| 169 |
+
r'dise帽a.*sistema|arquitectura.*para|esquema.*tecnol贸gico'
|
| 170 |
+
],
|
| 171 |
+
IntentType.DEVOPS: [
|
| 172 |
+
r'(docker|kubernetes|ci/cd|terraform|aws|gcp|azure|infraestructura)',
|
| 173 |
+
r'deploy|implementa.*infraestructura|configura.*servidor'
|
| 174 |
+
],
|
| 175 |
+
IntentType.QA: [
|
| 176 |
+
r'(test|prueba|pytest|unitario|integraci贸n|cobertura|qa)',
|
| 177 |
+
r'escribe.*test|prueba.*c贸digo|cobertura.*tests'
|
| 178 |
+
],
|
| 179 |
+
IntentType.VISUAL: [
|
| 180 |
+
r'(prompt.*visual|an谩lisis.*imagen|describe.*foto|interpreta.*visual)',
|
| 181 |
+
r'qu茅 hay.*imagen|describe.*escena'
|
| 182 |
+
]
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
# Ponderaci贸n por historial
|
| 186 |
+
intent_scores = defaultdict(float)
|
| 187 |
+
|
| 188 |
+
# An谩lisis por patrones
|
| 189 |
+
for intent, pattern_list in patterns.items():
|
| 190 |
+
for pattern in pattern_list:
|
| 191 |
+
if re.search(pattern, text_lower, re.IGNORECASE):
|
| 192 |
+
intent_scores[intent] += 2.0
|
| 193 |
+
|
| 194 |
+
# Contexto hist贸rico
|
| 195 |
+
if context:
|
| 196 |
+
recent_context = " ".join(context[-3:]).lower()
|
| 197 |
+
for intent, pattern_list in patterns.items():
|
| 198 |
+
for pattern in pattern_list:
|
| 199 |
+
if re.search(pattern, recent_context, re.IGNORECASE):
|
| 200 |
+
intent_scores[intent] += 1.0
|
| 201 |
+
|
| 202 |
+
if intent_scores:
|
| 203 |
+
# Seleccionar intenci贸n con mayor puntaje
|
| 204 |
+
selected = max(intent_scores.items(), key=lambda x: x[1])[0]
|
| 205 |
+
self.intent_history.append(selected)
|
| 206 |
+
return selected
|
| 207 |
+
|
| 208 |
+
# Fallback: detectar por contenido
|
| 209 |
+
if any(word in text_lower for word in ['?', 'c贸mo', 'por qu茅', 'qu茅']):
|
| 210 |
+
return IntentType.REASONING
|
| 211 |
+
|
| 212 |
+
return IntentType.GENERAL
|
| 213 |
+
|
| 214 |
+
def select_optimal_model(self, intent: IntentType, candidates: Dict[str, str],
|
| 215 |
+
context: str = "") -> Tuple[str, float]:
|
| 216 |
+
"""Selecci贸n de modelo con memoria contextual"""
|
| 217 |
+
# Revisar memoria para decisiones anteriores exitosas
|
| 218 |
+
memories = self.memory_graph.recall(intent, context)
|
| 219 |
+
|
| 220 |
+
for memory in memories:
|
| 221 |
+
if memory.model in candidates.values():
|
| 222 |
+
# Modelo previamente exitoso para este contexto
|
| 223 |
+
confidence = memory.success_score * 0.7
|
| 224 |
+
return memory.model, confidence
|
| 225 |
+
|
| 226 |
+
# Selecci贸n basada en latencia adaptativa
|
| 227 |
+
scored_models = []
|
| 228 |
+
for name, model_id in candidates.items():
|
| 229 |
+
score = self.latency_registry.get_composite_score(model_id)
|
| 230 |
+
scored_models.append((score, model_id, name))
|
| 231 |
+
|
| 232 |
+
scored_models.sort(key=lambda x: x[0])
|
| 233 |
+
|
| 234 |
+
if scored_models:
|
| 235 |
+
best_model = scored_models[0][1]
|
| 236 |
+
confidence = 1.0 / (1.0 + scored_models[0][0])
|
| 237 |
+
return best_model, min(confidence, 0.95)
|
| 238 |
+
|
| 239 |
+
# Fallback al primer modelo
|
| 240 |
+
fallback = next(iter(candidates.values()))
|
| 241 |
+
return fallback, 0.5
|
| 242 |
+
|
| 243 |
+
def record_interaction(self, prompt: str, intent: IntentType,
|
| 244 |
+
model: str, latency: float, success: bool = True,
|
| 245 |
+
context: str = ""):
|
| 246 |
+
"""Registra interacci贸n completa"""
|
| 247 |
+
success_score = 1.0 if success else 0.3
|
| 248 |
+
self.latency_registry.record(model, latency, success)
|
| 249 |
+
self.memory_graph.store(prompt, intent, model, context, success_score)
|
| 250 |
+
|
| 251 |
+
# ===================== INSTANCIAS GLOBALES =====================
|
| 252 |
+
LATENCY = AdaptiveLatencyRegistry()
|
| 253 |
+
MEMORY = ContextMemoryGraph()
|
| 254 |
+
ORCHESTRATOR = CognitiveOrchestrator()
|
| 255 |
+
|
| 256 |
+
# ===================== FUNCIONES P脷BLICAS =====================
|
| 257 |
+
def analyze_intent_detailed(text: str, history: List[Tuple] = None) -> Tuple[IntentType, Dict]:
|
| 258 |
+
"""Analiza intenci贸n con metadatos detallados"""
|
| 259 |
+
context = []
|
| 260 |
+
if history:
|
| 261 |
+
context = [msg for pair in history[-3:] for msg in pair if msg]
|
| 262 |
+
|
| 263 |
+
intent = ORCHESTRATOR.analyze_intent(text, context)
|
| 264 |
+
|
| 265 |
+
# Metadatos adicionales
|
| 266 |
+
metadata = {
|
| 267 |
+
"confidence": 0.85,
|
| 268 |
+
"keywords": MEMORY._extract_keywords(text),
|
| 269 |
+
"context_used": len(context) > 0,
|
| 270 |
+
"similar_prompts": [node.prompt[:50] + "..."
|
| 271 |
+
for node in MEMORY.find_similar(text, intent)],
|
| 272 |
+
"timestamp": time.time()
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
return intent, metadata
|
| 276 |
+
|
| 277 |
+
def select_model_with_context(intent: IntentType, candidates: Dict[str, str],
|
| 278 |
+
context: str = "") -> Tuple[str, float, Dict]:
|
| 279 |
+
"""Selecci贸n de modelo con contexto y explicaci贸n"""
|
| 280 |
+
model, confidence = ORCHESTRATOR.select_optimal_model(intent, candidates, context)
|
| 281 |
+
|
| 282 |
+
explanation = {
|
| 283 |
+
"selection_method": "composite_scoring",
|
| 284 |
+
"candidates_evaluated": len(candidates),
|
| 285 |
+
"confidence_score": confidence,
|
| 286 |
+
"historical_matches": len(MEMORY.recall(intent, context)),
|
| 287 |
+
"recommendation_reason": "Optimal balance of latency, success rate, and contextual relevance"
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
return model, confidence, explanation
|
| 291 |
+
|
| 292 |
+
def record_complete_interaction(prompt: str, intent: IntentType, model: str,
|
| 293 |
+
latency: float, success: bool = True,
|
| 294 |
+
response_quality: float = 1.0,
|
| 295 |
+
user_context: str = ""):
|
| 296 |
+
"""Registro completo de interacci贸n con calidad de respuesta"""
|
| 297 |
+
adjusted_success = success and (response_quality > 0.6)
|
| 298 |
+
ORCHESTRATOR.record_interaction(
|
| 299 |
+
prompt, intent, model, latency, adjusted_success, user_context
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
# Registrar tambi茅n en memoria global
|
| 303 |
+
MEMORY.store(prompt, intent, model, user_context,
|
| 304 |
+
response_quality if success else 0.1)
|
| 305 |
+
|
| 306 |
+
return {
|
| 307 |
+
"recorded": True,
|
| 308 |
+
"success": adjusted_success,
|
| 309 |
+
"quality_score": response_quality,
|
| 310 |
+
"timestamp": time.time()
|
| 311 |
+
}
|