Spaces:
Runtime error
Runtime error
File size: 21,624 Bytes
dcb2a99 |
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 |
"""Advanced strategy coordination patterns for the unified reasoning engine."""
import logging
from typing import Dict, Any, List, Optional, Set, Union, Type, Callable
import json
from dataclasses import dataclass, field
from enum import Enum
from datetime import datetime
import asyncio
from collections import defaultdict
from .base import ReasoningStrategy
from .unified_engine import StrategyType, StrategyResult, UnifiedResult
class CoordinationPattern(Enum):
"""Types of strategy coordination patterns."""
PIPELINE = "pipeline"
PARALLEL = "parallel"
HIERARCHICAL = "hierarchical"
FEEDBACK = "feedback"
ADAPTIVE = "adaptive"
ENSEMBLE = "ensemble"
class CoordinationPhase(Enum):
"""Phases in strategy coordination."""
INITIALIZATION = "initialization"
EXECUTION = "execution"
SYNCHRONIZATION = "synchronization"
ADAPTATION = "adaptation"
COMPLETION = "completion"
@dataclass
class CoordinationState:
"""State of strategy coordination."""
pattern: CoordinationPattern
active_strategies: Dict[StrategyType, bool]
phase: CoordinationPhase
shared_context: Dict[str, Any]
synchronization_points: List[str]
adaptation_history: List[Dict[str, Any]]
metadata: Dict[str, Any] = field(default_factory=dict)
@dataclass
class StrategyInteraction:
"""Interaction between strategies."""
source: StrategyType
target: StrategyType
interaction_type: str
data: Dict[str, Any]
timestamp: datetime = field(default_factory=datetime.now)
class StrategyCoordinator:
"""
Advanced strategy coordinator that:
1. Manages strategy interactions
2. Implements coordination patterns
3. Handles state synchronization
4. Adapts coordination dynamically
5. Optimizes strategy combinations
"""
def __init__(self,
strategies: Dict[StrategyType, ReasoningStrategy],
learning_rate: float = 0.1):
self.strategies = strategies
self.learning_rate = learning_rate
# Coordination state
self.states: Dict[str, CoordinationState] = {}
self.interactions: List[StrategyInteraction] = []
# Pattern performance
self.pattern_performance: Dict[CoordinationPattern, List[float]] = defaultdict(list)
self.pattern_weights: Dict[CoordinationPattern, float] = {
pattern: 1.0 for pattern in CoordinationPattern
}
async def coordinate(self,
query: str,
context: Dict[str, Any],
pattern: Optional[CoordinationPattern] = None) -> Dict[str, Any]:
"""Coordinate strategy execution using specified pattern."""
try:
# Select pattern if not specified
if not pattern:
pattern = await self._select_pattern(query, context)
# Initialize coordination
state = await self._initialize_coordination(pattern, context)
# Execute coordination pattern
if pattern == CoordinationPattern.PIPELINE:
result = await self._coordinate_pipeline(query, context, state)
elif pattern == CoordinationPattern.PARALLEL:
result = await self._coordinate_parallel(query, context, state)
elif pattern == CoordinationPattern.HIERARCHICAL:
result = await self._coordinate_hierarchical(query, context, state)
elif pattern == CoordinationPattern.FEEDBACK:
result = await self._coordinate_feedback(query, context, state)
elif pattern == CoordinationPattern.ADAPTIVE:
result = await self._coordinate_adaptive(query, context, state)
elif pattern == CoordinationPattern.ENSEMBLE:
result = await self._coordinate_ensemble(query, context, state)
else:
raise ValueError(f"Unsupported coordination pattern: {pattern}")
# Update performance metrics
self._update_pattern_performance(pattern, result)
return result
except Exception as e:
logging.error(f"Error in strategy coordination: {str(e)}")
return {
"success": False,
"error": str(e),
"pattern": pattern.value if pattern else None
}
async def _select_pattern(self, query: str, context: Dict[str, Any]) -> CoordinationPattern:
"""Select appropriate coordination pattern."""
prompt = f"""
Select coordination pattern:
Query: {query}
Context: {json.dumps(context)}
Consider:
1. Task complexity and type
2. Strategy dependencies
3. Resource constraints
4. Performance history
5. Adaptation needs
Format as:
[Selection]
Pattern: ...
Rationale: ...
Confidence: ...
"""
response = await context["groq_api"].predict(prompt)
selection = self._parse_pattern_selection(response["answer"])
# Weight by performance history
weighted_patterns = {
pattern: self.pattern_weights[pattern] * selection.get(pattern.value, 0.0)
for pattern in CoordinationPattern
}
return max(weighted_patterns.items(), key=lambda x: x[1])[0]
async def _coordinate_pipeline(self,
query: str,
context: Dict[str, Any],
state: CoordinationState) -> Dict[str, Any]:
"""Coordinate strategies in pipeline pattern."""
results = []
current_context = context.copy()
# Determine optimal order
strategy_order = await self._determine_pipeline_order(query, context)
for strategy_type in strategy_order:
try:
# Execute strategy
strategy = self.strategies[strategy_type]
result = await strategy.reason(query, current_context)
# Update context with result
current_context.update({
"previous_result": result,
"pipeline_position": len(results)
})
results.append(StrategyResult(
strategy_type=strategy_type,
success=result.get("success", False),
answer=result.get("answer"),
confidence=result.get("confidence", 0.0),
reasoning_trace=result.get("reasoning_trace", []),
metadata=result.get("metadata", {}),
performance_metrics=result.get("performance_metrics", {})
))
# Record interaction
self._record_interaction(
source=strategy_type,
target=strategy_order[len(results)] if len(results) < len(strategy_order) else None,
interaction_type="pipeline_transfer",
data={"result": result}
)
except Exception as e:
logging.error(f"Error in pipeline strategy {strategy_type}: {str(e)}")
return {
"success": any(r.success for r in results),
"results": results,
"pattern": CoordinationPattern.PIPELINE.value,
"metrics": {
"total_steps": len(results),
"success_rate": sum(1 for r in results if r.success) / len(results) if results else 0
}
}
async def _coordinate_parallel(self,
query: str,
context: Dict[str, Any],
state: CoordinationState) -> Dict[str, Any]:
"""Coordinate strategies in parallel pattern."""
async def execute_strategy(strategy_type: StrategyType) -> StrategyResult:
try:
strategy = self.strategies[strategy_type]
result = await strategy.reason(query, context)
return StrategyResult(
strategy_type=strategy_type,
success=result.get("success", False),
answer=result.get("answer"),
confidence=result.get("confidence", 0.0),
reasoning_trace=result.get("reasoning_trace", []),
metadata=result.get("metadata", {}),
performance_metrics=result.get("performance_metrics", {})
)
except Exception as e:
logging.error(f"Error in parallel strategy {strategy_type}: {str(e)}")
return StrategyResult(
strategy_type=strategy_type,
success=False,
answer=None,
confidence=0.0,
reasoning_trace=[{"error": str(e)}],
metadata={},
performance_metrics={}
)
# Execute strategies in parallel
tasks = [execute_strategy(strategy_type)
for strategy_type in state.active_strategies
if state.active_strategies[strategy_type]]
results = await asyncio.gather(*tasks)
# Synthesize results
synthesis = await self._synthesize_parallel_results(results, context)
return {
"success": synthesis.get("success", False),
"results": results,
"synthesis": synthesis,
"pattern": CoordinationPattern.PARALLEL.value,
"metrics": {
"total_strategies": len(results),
"success_rate": sum(1 for r in results if r.success) / len(results) if results else 0
}
}
async def _coordinate_hierarchical(self,
query: str,
context: Dict[str, Any],
state: CoordinationState) -> Dict[str, Any]:
"""Coordinate strategies in hierarchical pattern."""
# Build strategy hierarchy
hierarchy = await self._build_strategy_hierarchy(query, context)
results = {}
async def execute_level(level_strategies: List[StrategyType],
level_context: Dict[str, Any]) -> List[StrategyResult]:
tasks = []
for strategy_type in level_strategies:
if strategy_type in state.active_strategies and state.active_strategies[strategy_type]:
strategy = self.strategies[strategy_type]
tasks.append(strategy.reason(query, level_context))
level_results = await asyncio.gather(*tasks)
return [
StrategyResult(
strategy_type=strategy_type,
success=result.get("success", False),
answer=result.get("answer"),
confidence=result.get("confidence", 0.0),
reasoning_trace=result.get("reasoning_trace", []),
metadata=result.get("metadata", {}),
performance_metrics=result.get("performance_metrics", {})
)
for strategy_type, result in zip(level_strategies, level_results)
]
# Execute hierarchy levels
current_context = context.copy()
for level, level_strategies in enumerate(hierarchy):
results[level] = await execute_level(level_strategies, current_context)
# Update context for next level
current_context.update({
"previous_level_results": results[level],
"hierarchy_level": level
})
return {
"success": any(any(r.success for r in level_results)
for level_results in results.values()),
"results": results,
"hierarchy": hierarchy,
"pattern": CoordinationPattern.HIERARCHICAL.value,
"metrics": {
"total_levels": len(hierarchy),
"level_success_rates": {
level: sum(1 for r in results[level] if r.success) / len(results[level])
for level in results if results[level]
}
}
}
async def _coordinate_feedback(self,
query: str,
context: Dict[str, Any],
state: CoordinationState) -> Dict[str, Any]:
"""Coordinate strategies with feedback loops."""
results = []
feedback_history = []
current_context = context.copy()
max_iterations = 5 # Prevent infinite loops
iteration = 0
while iteration < max_iterations:
iteration += 1
# Execute strategies
iteration_results = []
for strategy_type in state.active_strategies:
if state.active_strategies[strategy_type]:
try:
strategy = self.strategies[strategy_type]
result = await strategy.reason(query, current_context)
strategy_result = StrategyResult(
strategy_type=strategy_type,
success=result.get("success", False),
answer=result.get("answer"),
confidence=result.get("confidence", 0.0),
reasoning_trace=result.get("reasoning_trace", []),
metadata=result.get("metadata", {}),
performance_metrics=result.get("performance_metrics", {})
)
iteration_results.append(strategy_result)
except Exception as e:
logging.error(f"Error in feedback strategy {strategy_type}: {str(e)}")
results.append(iteration_results)
# Generate feedback
feedback = await self._generate_feedback(iteration_results, current_context)
feedback_history.append(feedback)
# Check termination condition
if self._should_terminate_feedback(feedback, iteration_results):
break
# Update context with feedback
current_context.update({
"previous_results": iteration_results,
"feedback": feedback,
"iteration": iteration
})
return {
"success": any(any(r.success for r in iteration_results)
for iteration_results in results),
"results": results,
"feedback_history": feedback_history,
"pattern": CoordinationPattern.FEEDBACK.value,
"metrics": {
"total_iterations": iteration,
"feedback_impact": self._calculate_feedback_impact(results, feedback_history)
}
}
async def _coordinate_adaptive(self,
query: str,
context: Dict[str, Any],
state: CoordinationState) -> Dict[str, Any]:
"""Coordinate strategies with adaptive selection."""
results = []
adaptations = []
current_context = context.copy()
while len(results) < len(state.active_strategies):
# Select next strategy
next_strategy = await self._select_next_strategy(
results, state.active_strategies, current_context)
if not next_strategy:
break
try:
# Execute strategy
strategy = self.strategies[next_strategy]
result = await strategy.reason(query, current_context)
strategy_result = StrategyResult(
strategy_type=next_strategy,
success=result.get("success", False),
answer=result.get("answer"),
confidence=result.get("confidence", 0.0),
reasoning_trace=result.get("reasoning_trace", []),
metadata=result.get("metadata", {}),
performance_metrics=result.get("performance_metrics", {})
)
results.append(strategy_result)
# Adapt strategy selection
adaptation = await self._adapt_strategy_selection(
strategy_result, current_context)
adaptations.append(adaptation)
# Update context
current_context.update({
"previous_results": results,
"adaptations": adaptations,
"current_strategy": next_strategy
})
except Exception as e:
logging.error(f"Error in adaptive strategy {next_strategy}: {str(e)}")
return {
"success": any(r.success for r in results),
"results": results,
"adaptations": adaptations,
"pattern": CoordinationPattern.ADAPTIVE.value,
"metrics": {
"total_strategies": len(results),
"adaptation_impact": self._calculate_adaptation_impact(results, adaptations)
}
}
async def _coordinate_ensemble(self,
query: str,
context: Dict[str, Any],
state: CoordinationState) -> Dict[str, Any]:
"""Coordinate strategies as an ensemble."""
# Execute all strategies
results = []
for strategy_type in state.active_strategies:
if state.active_strategies[strategy_type]:
try:
strategy = self.strategies[strategy_type]
result = await strategy.reason(query, context)
strategy_result = StrategyResult(
strategy_type=strategy_type,
success=result.get("success", False),
answer=result.get("answer"),
confidence=result.get("confidence", 0.0),
reasoning_trace=result.get("reasoning_trace", []),
metadata=result.get("metadata", {}),
performance_metrics=result.get("performance_metrics", {})
)
results.append(strategy_result)
except Exception as e:
logging.error(f"Error in ensemble strategy {strategy_type}: {str(e)}")
# Combine results using ensemble methods
ensemble_result = await self._combine_ensemble_results(results, context)
return {
"success": ensemble_result.get("success", False),
"results": results,
"ensemble_result": ensemble_result,
"pattern": CoordinationPattern.ENSEMBLE.value,
"metrics": {
"total_members": len(results),
"ensemble_confidence": ensemble_result.get("confidence", 0.0)
}
}
def _record_interaction(self,
source: StrategyType,
target: Optional[StrategyType],
interaction_type: str,
data: Dict[str, Any]):
"""Record strategy interaction."""
self.interactions.append(StrategyInteraction(
source=source,
target=target,
interaction_type=interaction_type,
data=data
))
def _update_pattern_performance(self, pattern: CoordinationPattern, result: Dict[str, Any]):
"""Update pattern performance metrics."""
success_rate = result["metrics"].get("success_rate", 0.0)
self.pattern_performance[pattern].append(success_rate)
# Update weights using exponential moving average
current_weight = self.pattern_weights[pattern]
self.pattern_weights[pattern] = (
(1 - self.learning_rate) * current_weight +
self.learning_rate * success_rate
)
def get_performance_metrics(self) -> Dict[str, Any]:
"""Get comprehensive performance metrics."""
return {
"pattern_weights": dict(self.pattern_weights),
"average_performance": {
pattern.value: sum(scores) / len(scores) if scores else 0
for pattern, scores in self.pattern_performance.items()
},
"interaction_counts": defaultdict(int, {
interaction.interaction_type: 1
for interaction in self.interactions
}),
"active_patterns": [
pattern.value for pattern, weight in self.pattern_weights.items()
if weight > 0.5
]
}
|