Spaces:
Runtime error
Runtime error
File size: 23,363 Bytes
1d75522 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 |
"""Analogical reasoning implementation with advanced pattern matching and transfer learning."""
import logging
from typing import Dict, Any, List, Optional, Set, Tuple, Callable
import json
from dataclasses import dataclass, field
from enum import Enum
from datetime import datetime
import numpy as np
from collections import defaultdict
from .base import ReasoningStrategy
class AnalogicalLevel(Enum):
"""Levels of analogical similarity."""
SURFACE = "surface"
STRUCTURAL = "structural"
SEMANTIC = "semantic"
FUNCTIONAL = "functional"
CAUSAL = "causal"
ABSTRACT = "abstract"
class MappingType(Enum):
"""Types of analogical mappings."""
DIRECT = "direct"
TRANSFORMED = "transformed"
COMPOSITE = "composite"
ABSTRACT = "abstract"
METAPHORICAL = "metaphorical"
HYBRID = "hybrid"
@dataclass
class AnalogicalPattern:
"""Represents a pattern for analogical matching."""
id: str
level: AnalogicalLevel
features: Dict[str, Any]
relations: List[Tuple[str, str, str]] # (entity1, relation, entity2)
constraints: List[str]
metadata: Dict[str, Any] = field(default_factory=dict)
@dataclass
class AnalogicalMapping:
"""Represents a mapping between source and target domains."""
id: str
type: MappingType
source_elements: Dict[str, Any]
target_elements: Dict[str, Any]
correspondences: List[Tuple[str, str, float]] # (source, target, strength)
transformations: List[Dict[str, Any]]
confidence: float
metadata: Dict[str, Any] = field(default_factory=dict)
@dataclass
class AnalogicalSolution:
"""Represents a solution derived through analogical reasoning."""
id: str
source_analogy: str
mapping: AnalogicalMapping
adaptation: Dict[str, Any]
inference: Dict[str, Any]
confidence: float
validation: Dict[str, Any]
metadata: Dict[str, Any] = field(default_factory=dict)
class AnalogicalReasoning(ReasoningStrategy):
"""
Advanced Analogical Reasoning implementation with:
- Multi-level pattern matching
- Sophisticated similarity metrics
- Transfer learning capabilities
- Dynamic adaptation mechanisms
- Quality assessment
- Learning from experience
"""
def __init__(self, config: Optional[Dict[str, Any]] = None):
"""Initialize analogical reasoning."""
super().__init__()
self.config = config or {}
# Standard reasoning parameters
self.min_confidence = self.config.get('min_confidence', 0.7)
self.parallel_threshold = self.config.get('parallel_threshold', 3)
self.learning_rate = self.config.get('learning_rate', 0.1)
self.strategy_weights = self.config.get('strategy_weights', {
"LOCAL_LLM": 0.8,
"CHAIN_OF_THOUGHT": 0.6,
"TREE_OF_THOUGHTS": 0.5,
"META_LEARNING": 0.4
})
# Analogical reasoning specific parameters
self.min_similarity = self.config.get('min_similarity', 0.6)
self.max_candidates = self.config.get('max_candidates', 5)
self.adaptation_threshold = self.config.get('adaptation_threshold', 0.7)
# Knowledge base
self.patterns: Dict[str, AnalogicalPattern] = {}
self.mappings: Dict[str, AnalogicalMapping] = {}
self.solutions: Dict[str, AnalogicalSolution] = {}
# Learning components
self.pattern_weights: Dict[str, float] = defaultdict(float)
self.success_history: List[Dict[str, Any]] = []
self.adaptation_history: List[Dict[str, Any]] = []
async def reason(self, query: str, context: Dict[str, Any]) -> Dict[str, Any]:
"""Main reasoning method implementing analogical reasoning."""
try:
# Extract patterns from query
patterns = await self._extract_patterns(query, context)
# Find analogical matches
matches = await self._find_matches(patterns, context)
# Create and evaluate mappings
mappings = await self._create_mappings(matches, context)
# Generate and adapt solutions
solutions = await self._generate_solutions(mappings, context)
# Select best solution
best_solution = await self._select_best_solution(solutions, context)
# Learn from experience
self._update_knowledge(patterns, mappings, best_solution)
return {
"success": True,
"answer": best_solution.inference["conclusion"],
"confidence": best_solution.confidence,
"analogy": {
"source": best_solution.source_analogy,
"mapping": self._mapping_to_dict(best_solution.mapping),
"adaptation": best_solution.adaptation
},
"reasoning_trace": best_solution.metadata.get("reasoning_trace", []),
"meta_insights": best_solution.metadata.get("meta_insights", [])
}
except Exception as e:
logging.error(f"Error in analogical reasoning: {str(e)}")
return {"success": False, "error": str(e)}
async def _extract_patterns(self, query: str, context: Dict[str, Any]) -> List[AnalogicalPattern]:
"""Extract patterns from query for analogical matching."""
prompt = f"""
Extract analogical patterns from query:
Query: {query}
Context: {json.dumps(context)}
For each pattern level:
1. Surface features
2. Structural relations
3. Semantic concepts
4. Functional roles
5. Causal relationships
6. Abstract principles
Format as:
[P1]
Level: ...
Features: ...
Relations: ...
Constraints: ...
[P2]
...
"""
response = await context["groq_api"].predict(prompt)
return self._parse_patterns(response["answer"])
async def _find_matches(self, patterns: List[AnalogicalPattern], context: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Find matching patterns in knowledge base."""
prompt = f"""
Find analogical matches:
Patterns: {json.dumps([self._pattern_to_dict(p) for p in patterns])}
Context: {json.dumps(context)}
For each match provide:
1. Source domain
2. Similarity assessment
3. Key correspondences
4. Transfer potential
Format as:
[M1]
Source: ...
Similarity: ...
Correspondences: ...
Transfer: ...
[M2]
...
"""
response = await context["groq_api"].predict(prompt)
return self._parse_matches(response["answer"])
async def _create_mappings(self, matches: List[Dict[str, Any]], context: Dict[str, Any]) -> List[AnalogicalMapping]:
"""Create mappings between source and target domains."""
prompt = f"""
Create analogical mappings:
Matches: {json.dumps(matches)}
Context: {json.dumps(context)}
For each mapping specify:
1. [Type]: {" | ".join([t.value for t in MappingType])}
2. [Elements]: Source and target elements
3. [Correspondences]: Element mappings
4. [Transformations]: Required adaptations
5. [Confidence]: Mapping strength
Format as:
[Map1]
Type: ...
Elements: ...
Correspondences: ...
Transformations: ...
Confidence: ...
"""
response = await context["groq_api"].predict(prompt)
return self._parse_mappings(response["answer"])
async def _generate_solutions(self, mappings: List[AnalogicalMapping], context: Dict[str, Any]) -> List[AnalogicalSolution]:
"""Generate solutions through analogical transfer."""
prompt = f"""
Generate analogical solutions:
Mappings: {json.dumps([self._mapping_to_dict(m) for m in mappings])}
Context: {json.dumps(context)}
For each solution provide:
1. Analogical inference
2. Required adaptations
3. Validation criteria
4. Confidence assessment
5. Reasoning trace
Format as:
[S1]
Inference: ...
Adaptation: ...
Validation: ...
Confidence: ...
Trace: ...
"""
response = await context["groq_api"].predict(prompt)
return self._parse_solutions(response["answer"], mappings)
async def _select_best_solution(self, solutions: List[AnalogicalSolution], context: Dict[str, Any]) -> AnalogicalSolution:
"""Select the best solution based on multiple criteria."""
prompt = f"""
Evaluate and select best solution:
Solutions: {json.dumps([self._solution_to_dict(s) for s in solutions])}
Context: {json.dumps(context)}
Evaluate based on:
1. Inference quality
2. Adaptation feasibility
3. Validation strength
4. Overall confidence
Format as:
[Evaluation]
Rankings: ...
Rationale: ...
Selection: ...
Confidence: ...
"""
response = await context["groq_api"].predict(prompt)
selection = self._parse_selection(response["answer"])
# Find selected solution
selected = max(solutions, key=lambda s: s.confidence)
for solution in solutions:
if solution.id == selection.get("selected_id"):
selected = solution
break
return selected
def _update_knowledge(self, patterns: List[AnalogicalPattern], mappings: List[AnalogicalMapping], solution: AnalogicalSolution):
"""Update knowledge base with new patterns and successful mappings."""
# Update patterns
for pattern in patterns:
if pattern.id not in self.patterns:
self.patterns[pattern.id] = pattern
self.pattern_weights[pattern.id] += self.learning_rate * solution.confidence
# Update mappings
if solution.mapping.id not in self.mappings:
self.mappings[solution.mapping.id] = solution.mapping
# Record solution
self.solutions[solution.id] = solution
# Update history
self.success_history.append({
"timestamp": datetime.now().isoformat(),
"solution_id": solution.id,
"confidence": solution.confidence,
"patterns": [p.id for p in patterns],
"mapping_type": solution.mapping.type.value
})
# Update adaptation history
self.adaptation_history.append({
"timestamp": datetime.now().isoformat(),
"solution_id": solution.id,
"adaptations": solution.adaptation,
"success": solution.confidence >= self.adaptation_threshold
})
def _parse_patterns(self, response: str) -> List[AnalogicalPattern]:
"""Parse patterns from response."""
patterns = []
current = None
for line in response.split('\n'):
line = line.strip()
if not line:
continue
if line.startswith('[P'):
if current:
patterns.append(current)
current = None
elif line.startswith('Level:'):
level_str = line[6:].strip().lower()
try:
level = AnalogicalLevel(level_str)
current = AnalogicalPattern(
id=f"pattern_{len(patterns)}",
level=level,
features={},
relations=[],
constraints=[],
metadata={}
)
except ValueError:
logging.warning(f"Invalid analogical level: {level_str}")
elif current:
if line.startswith('Features:'):
try:
current.features = json.loads(line[9:].strip())
except:
current.features = {"raw": line[9:].strip()}
elif line.startswith('Relations:'):
relations = [r.strip() for r in line[10:].split(',')]
current.relations = [(r.split()[0], r.split()[1], r.split()[2])
for r in relations if len(r.split()) >= 3]
elif line.startswith('Constraints:'):
current.constraints = [c.strip() for c in line[12:].split(',')]
if current:
patterns.append(current)
return patterns
def _parse_matches(self, response: str) -> List[Dict[str, Any]]:
"""Parse matches from response."""
matches = []
current = None
for line in response.split('\n'):
line = line.strip()
if not line:
continue
if line.startswith('[M'):
if current:
matches.append(current)
current = {
"source": "",
"similarity": 0.0,
"correspondences": [],
"transfer": []
}
elif current:
if line.startswith('Source:'):
current["source"] = line[7:].strip()
elif line.startswith('Similarity:'):
try:
current["similarity"] = float(line[11:].strip())
except:
pass
elif line.startswith('Correspondences:'):
current["correspondences"] = [c.strip() for c in line[16:].split(',')]
elif line.startswith('Transfer:'):
current["transfer"] = [t.strip() for t in line[9:].split(',')]
if current:
matches.append(current)
return matches
def _parse_mappings(self, response: str) -> List[AnalogicalMapping]:
"""Parse mappings from response."""
mappings = []
current = None
for line in response.split('\n'):
line = line.strip()
if not line:
continue
if line.startswith('[Map'):
if current:
mappings.append(current)
current = None
elif line.startswith('Type:'):
type_str = line[5:].strip().lower()
try:
mapping_type = MappingType(type_str)
current = AnalogicalMapping(
id=f"mapping_{len(mappings)}",
type=mapping_type,
source_elements={},
target_elements={},
correspondences=[],
transformations=[],
confidence=0.0,
metadata={}
)
except ValueError:
logging.warning(f"Invalid mapping type: {type_str}")
elif current:
if line.startswith('Elements:'):
try:
elements = json.loads(line[9:].strip())
current.source_elements = elements.get("source", {})
current.target_elements = elements.get("target", {})
except:
pass
elif line.startswith('Correspondences:'):
pairs = [c.strip() for c in line[16:].split(',')]
for pair in pairs:
parts = pair.split(':')
if len(parts) >= 2:
source = parts[0].strip()
target = parts[1].strip()
strength = float(parts[2]) if len(parts) > 2 else 1.0
current.correspondences.append((source, target, strength))
elif line.startswith('Transformations:'):
try:
current.transformations = json.loads(line[16:].strip())
except:
current.transformations = [{"raw": line[16:].strip()}]
elif line.startswith('Confidence:'):
try:
current.confidence = float(line[11:].strip())
except:
pass
if current:
mappings.append(current)
return mappings
def _parse_solutions(self, response: str, mappings: List[AnalogicalMapping]) -> List[AnalogicalSolution]:
"""Parse solutions from response."""
solutions = []
current = None
for line in response.split('\n'):
line = line.strip()
if not line:
continue
if line.startswith('[S'):
if current:
solutions.append(current)
current = None
mapping_idx = len(solutions)
if mapping_idx < len(mappings):
current = AnalogicalSolution(
id=f"solution_{len(solutions)}",
source_analogy="",
mapping=mappings[mapping_idx],
adaptation={},
inference={},
confidence=0.0,
validation={},
metadata={}
)
elif current:
if line.startswith('Inference:'):
try:
current.inference = json.loads(line[10:].strip())
except:
current.inference = {"conclusion": line[10:].strip()}
elif line.startswith('Adaptation:'):
try:
current.adaptation = json.loads(line[11:].strip())
except:
current.adaptation = {"steps": [line[11:].strip()]}
elif line.startswith('Validation:'):
try:
current.validation = json.loads(line[11:].strip())
except:
current.validation = {"criteria": [line[11:].strip()]}
elif line.startswith('Confidence:'):
try:
current.confidence = float(line[11:].strip())
except:
pass
elif line.startswith('Trace:'):
current.metadata["reasoning_trace"] = [t.strip() for t in line[6:].split(',')]
if current:
solutions.append(current)
return solutions
def _parse_selection(self, response: str) -> Dict[str, Any]:
"""Parse solution selection from response."""
selection = {
"selected_id": None,
"confidence": 0.0,
"rationale": []
}
for line in response.split('\n'):
line = line.strip()
if line.startswith('Selection:'):
selection["selected_id"] = line[10:].strip()
elif line.startswith('Confidence:'):
try:
selection["confidence"] = float(line[11:].strip())
except:
pass
elif line.startswith('Rationale:'):
selection["rationale"] = [r.strip() for r in line[10:].split(',')]
return selection
def _pattern_to_dict(self, pattern: AnalogicalPattern) -> Dict[str, Any]:
"""Convert pattern to dictionary for serialization."""
return {
"id": pattern.id,
"level": pattern.level.value,
"features": pattern.features,
"relations": pattern.relations,
"constraints": pattern.constraints,
"metadata": pattern.metadata
}
def _mapping_to_dict(self, mapping: AnalogicalMapping) -> Dict[str, Any]:
"""Convert mapping to dictionary for serialization."""
return {
"id": mapping.id,
"type": mapping.type.value,
"source_elements": mapping.source_elements,
"target_elements": mapping.target_elements,
"correspondences": mapping.correspondences,
"transformations": mapping.transformations,
"confidence": mapping.confidence,
"metadata": mapping.metadata
}
def _solution_to_dict(self, solution: AnalogicalSolution) -> Dict[str, Any]:
"""Convert solution to dictionary for serialization."""
return {
"id": solution.id,
"source_analogy": solution.source_analogy,
"mapping": self._mapping_to_dict(solution.mapping),
"adaptation": solution.adaptation,
"inference": solution.inference,
"confidence": solution.confidence,
"validation": solution.validation,
"metadata": solution.metadata
}
def get_pattern_statistics(self) -> Dict[str, Any]:
"""Get statistics about pattern usage and effectiveness."""
return {
"total_patterns": len(self.patterns),
"level_distribution": defaultdict(int, {p.level.value: 1 for p in self.patterns.values()}),
"average_constraints": sum(len(p.constraints) for p in self.patterns.values()) / len(self.patterns) if self.patterns else 0,
"pattern_weights": dict(self.pattern_weights)
}
def get_mapping_statistics(self) -> Dict[str, Any]:
"""Get statistics about mapping effectiveness."""
return {
"total_mappings": len(self.mappings),
"type_distribution": defaultdict(int, {m.type.value: 1 for m in self.mappings.values()}),
"average_confidence": sum(m.confidence for m in self.mappings.values()) / len(self.mappings) if self.mappings else 0,
"transformation_counts": defaultdict(int, {m.id: len(m.transformations) for m in self.mappings.values()})
}
def get_solution_statistics(self) -> Dict[str, Any]:
"""Get statistics about solution quality."""
return {
"total_solutions": len(self.solutions),
"average_confidence": sum(s.confidence for s in self.solutions.values()) / len(self.solutions) if self.solutions else 0,
"adaptation_success_rate": sum(1 for h in self.adaptation_history if h["success"]) / len(self.adaptation_history) if self.adaptation_history else 0
}
def clear_knowledge_base(self):
"""Clear the knowledge base."""
self.patterns.clear()
self.mappings.clear()
self.solutions.clear()
self.pattern_weights.clear()
self.success_history.clear()
self.adaptation_history.clear()
|