File size: 24,158 Bytes
824bf31 f89ac19 824bf31 f89ac19 824bf31 f89ac19 824bf31 f89ac19 824bf31 f89ac19 824bf31 |
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 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 |
"""
Module: agents.abaporu
Codinome: Abaporu - Núcleo Central da IA
Description: Master agent that orchestrates other agents with self-reflection
Author: Anderson H. Silva
Date: 2025-01-24
License: Proprietary - All rights reserved
"""
import asyncio
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
from pydantic import BaseModel, Field as PydanticField
from src.core import AgentStatus, ReflectionType, get_logger
from src.core.exceptions import AgentExecutionError, InvestigationError
from .deodoro import (
AgentContext,
AgentMessage,
AgentResponse,
ReflectiveAgent,
)
from .parallel_processor import (
ParallelAgentProcessor,
ParallelTask,
ParallelStrategy,
parallel_processor,
)
class InvestigationPlan(BaseModel):
"""Plan for conducting an investigation."""
objective: str = PydanticField(..., description="Investigation objective")
steps: List[Dict[str, Any]] = PydanticField(..., description="Investigation steps")
required_agents: List[str] = PydanticField(..., description="Required agents")
estimated_time: int = PydanticField(..., description="Estimated time in seconds")
quality_criteria: Dict[str, Any] = PydanticField(..., description="Quality criteria")
fallback_strategies: List[str] = PydanticField(default_factory=list, description="Fallback strategies")
class InvestigationResult(BaseModel):
"""Result of an investigation."""
investigation_id: str = PydanticField(..., description="Investigation ID")
query: str = PydanticField(..., description="Original query")
findings: List[Dict[str, Any]] = PydanticField(..., description="Investigation findings")
confidence_score: float = PydanticField(..., description="Confidence in results")
sources: List[str] = PydanticField(..., description="Data sources used")
explanation: Optional[str] = PydanticField(default=None, description="Explanation of findings")
metadata: Dict[str, Any] = PydanticField(default_factory=dict, description="Additional metadata")
timestamp: datetime = PydanticField(default_factory=datetime.utcnow)
processing_time_ms: Optional[float] = PydanticField(default=None, description="Processing time")
class MasterAgent(ReflectiveAgent):
"""
Master agent that orchestrates investigations using other agents.
This agent has self-reflection capabilities and can:
- Plan investigation strategies
- Coordinate with other agents
- Monitor progress and quality
- Adapt strategies based on results
- Provide comprehensive explanations
"""
def __init__(
self,
llm_service: Any,
memory_agent: Any,
reflection_threshold: float = 0.8,
max_reflection_loops: int = 3,
**kwargs: Any
) -> None:
"""
Initialize master agent.
Args:
llm_service: LLM service instance
memory_agent: Memory agent instance
reflection_threshold: Minimum quality threshold
max_reflection_loops: Maximum reflection iterations
**kwargs: Additional arguments
"""
super().__init__(
name="MasterAgent",
description="Orchestrates investigations with self-reflection capabilities",
capabilities=[
"plan_investigation",
"coordinate_agents",
"monitor_progress",
"reflect_on_results",
"generate_explanations",
"adapt_strategies",
],
reflection_threshold=reflection_threshold,
max_reflection_loops=max_reflection_loops,
**kwargs
)
self.llm_service = llm_service
self.memory_agent = memory_agent
self.active_investigations: Dict[str, InvestigationPlan] = {}
self.agent_registry: Dict[str, Any] = {}
self.logger.info(
"abaporu_initialized",
reflection_threshold=reflection_threshold,
max_reflection_loops=max_reflection_loops,
)
async def initialize(self) -> None:
"""Initialize master agent."""
self.logger.info("abaporu_initializing")
# Initialize sub-services
if hasattr(self.llm_service, 'initialize'):
await self.llm_service.initialize()
if hasattr(self.memory_agent, 'initialize'):
await self.memory_agent.initialize()
self.status = AgentStatus.IDLE
self.logger.info("abaporu_initialized")
async def shutdown(self) -> None:
"""Shutdown master agent."""
self.logger.info("abaporu_shutting_down")
# Cleanup resources
if hasattr(self.llm_service, 'shutdown'):
await self.llm_service.shutdown()
if hasattr(self.memory_agent, 'shutdown'):
await self.memory_agent.shutdown()
self.active_investigations.clear()
self.agent_registry.clear()
self.logger.info("abaporu_shutdown_complete")
def register_agent(self, agent_name: str, agent_instance: Any) -> None:
"""
Register a sub-agent with the master agent.
Args:
agent_name: Name of the agent
agent_instance: Agent instance
"""
self.agent_registry[agent_name] = agent_instance
self.logger.info(
"agent_registered",
agent_name=agent_name,
total_agents=len(self.agent_registry),
)
async def process(
self,
message: AgentMessage,
context: AgentContext,
) -> AgentResponse:
"""
Process a message using the master agent.
Args:
message: Message to process
context: Agent context
Returns:
Agent response
"""
action = message.action
payload = message.payload
self.logger.info(
"master_agent_processing",
action=action,
investigation_id=context.investigation_id,
)
try:
if action == "investigate":
result = await self._investigate(payload, context)
elif action == "plan_investigation":
result = await self._plan_investigation(payload, context)
elif action == "monitor_progress":
result = await self._monitor_progress(payload, context)
elif action == "adapt_strategy":
result = await self._adapt_strategy(payload, context)
else:
raise AgentExecutionError(
f"Unknown action: {action}",
details={"action": action, "available_actions": self.capabilities}
)
return AgentResponse(
agent_name=self.name,
status=AgentStatus.COMPLETED,
result=result,
metadata={"action": action, "investigation_id": context.investigation_id},
)
except Exception as e:
self.logger.error(
"master_agent_processing_failed",
action=action,
error=str(e),
investigation_id=context.investigation_id,
)
return AgentResponse(
agent_name=self.name,
status=AgentStatus.ERROR,
error=str(e),
metadata={"action": action, "investigation_id": context.investigation_id},
)
async def _investigate(
self,
payload: Dict[str, Any],
context: AgentContext,
) -> InvestigationResult:
"""
Conduct a full investigation.
Args:
payload: Investigation payload with query
context: Agent context
Returns:
Investigation result
"""
query = payload.get("query", "")
if not query:
raise InvestigationError("No query provided for investigation")
investigation_id = context.investigation_id
start_time = datetime.utcnow()
self.logger.info(
"investigation_started",
investigation_id=investigation_id,
query=query,
)
# Step 1: Create investigation plan
plan = await self._plan_investigation({"query": query}, context)
self.active_investigations[investigation_id] = plan
# Step 2: Execute investigation steps in parallel when possible
findings = []
sources = []
# Group steps that can be executed in parallel
parallel_groups = self._group_parallel_steps(plan.steps)
for group_idx, step_group in enumerate(parallel_groups):
if len(step_group) > 1:
# Execute in parallel
self.logger.info(
f"Executing {len(step_group)} steps in parallel for group {group_idx}"
)
# Create parallel tasks
tasks = []
for step in step_group:
agent_type = self.agent_registry.get(step["agent"])
if agent_type:
task = ParallelTask(
agent_type=agent_type,
message=AgentMessage(
sender=self.name,
recipient=step["agent"],
action=step["action"],
payload=step.get("payload", {}),
),
timeout=30.0,
)
tasks.append(task)
# Execute parallel tasks
parallel_results = await parallel_processor.execute_parallel(
tasks,
context,
strategy=ParallelStrategy.BEST_EFFORT
)
# Aggregate results
aggregated = parallel_processor.aggregate_results(parallel_results)
findings.extend(aggregated.get("findings", []))
sources.extend(aggregated.get("sources", []))
else:
# Execute single step
step = step_group[0]
step_result = await self._execute_step(step, context)
if step_result.status == AgentStatus.COMPLETED:
findings.extend(step_result.result.get("findings", []))
sources.extend(step_result.result.get("sources", []))
else:
self.logger.warning(
"investigation_step_failed",
investigation_id=investigation_id,
step=step,
error=step_result.error,
)
# Step 3: Generate explanation
explanation = await self._generate_explanation(findings, query, context)
# Step 4: Calculate confidence score
confidence_score = self._calculate_confidence_score(findings, sources)
# Step 5: Create result
processing_time = (datetime.utcnow() - start_time).total_seconds() * 1000
result = InvestigationResult(
investigation_id=investigation_id,
query=query,
findings=findings,
confidence_score=confidence_score,
sources=list(set(sources)),
explanation=explanation,
metadata={
"plan": plan.model_dump(),
"steps_executed": len(plan.steps),
"agents_used": plan.required_agents,
},
processing_time_ms=processing_time,
)
# Store in memory
await self.memory_agent.store_investigation(result, context)
self.logger.info(
"investigation_completed",
investigation_id=investigation_id,
findings_count=len(findings),
confidence_score=confidence_score,
processing_time_ms=processing_time,
)
return result
def _group_parallel_steps(self, steps: List[Dict[str, Any]]) -> List[List[Dict[str, Any]]]:
"""
Group steps that can be executed in parallel.
Steps can be parallel if they don't depend on each other's output.
"""
groups = []
current_group = []
seen_agents = set()
for step in steps:
agent = step.get("agent", "")
depends_on = step.get("depends_on", [])
# Check if this step depends on any agent in current group
depends_on_current = any(dep in seen_agents for dep in depends_on)
if depends_on_current or agent in seen_agents:
# Start new group
if current_group:
groups.append(current_group)
current_group = [step]
seen_agents = {agent}
else:
# Add to current group
current_group.append(step)
seen_agents.add(agent)
# Add final group
if current_group:
groups.append(current_group)
return groups
async def _plan_investigation(
self,
payload: Dict[str, Any],
context: AgentContext,
) -> InvestigationPlan:
"""
Create an investigation plan.
Args:
payload: Planning payload
context: Agent context
Returns:
Investigation plan
"""
query = payload.get("query", "")
# Get relevant context from memory
memory_context = await self.memory_agent.get_relevant_context(query, context)
# Use LLM to generate plan
planning_prompt = self._create_planning_prompt(query, memory_context)
plan_response = await self.llm_service.generate(
prompt=planning_prompt,
context=context,
)
# Parse and validate plan
plan = self._parse_investigation_plan(plan_response, query)
self.logger.info(
"investigation_plan_created",
investigation_id=context.investigation_id,
steps_count=len(plan.steps),
required_agents=plan.required_agents,
)
return plan
async def _execute_step(
self,
step: Dict[str, Any],
context: AgentContext,
) -> AgentResponse:
"""
Execute a single investigation step.
Args:
step: Investigation step
context: Agent context
Returns:
Step result
"""
agent_name = step.get("agent")
action = step.get("action")
parameters = step.get("parameters", {})
if agent_name not in self.agent_registry:
raise AgentExecutionError(
f"Agent {agent_name} not registered",
details={"agent": agent_name, "available_agents": list(self.agent_registry.keys())}
)
agent = self.agent_registry[agent_name]
message = AgentMessage(
sender=self.name,
recipient=agent_name,
action=action,
payload=parameters,
context=context.to_dict(),
)
return await agent.execute(action, parameters, context)
async def _generate_explanation(
self,
findings: List[Dict[str, Any]],
query: str,
context: AgentContext,
) -> str:
"""
Generate explanation for investigation findings.
Args:
findings: Investigation findings
query: Original query
context: Agent context
Returns:
Explanation text
"""
explanation_prompt = self._create_explanation_prompt(findings, query)
explanation = await self.llm_service.generate(
prompt=explanation_prompt,
context=context,
)
return explanation
def _calculate_confidence_score(
self,
findings: List[Dict[str, Any]],
sources: List[str],
) -> float:
"""
Calculate confidence score for investigation results.
Args:
findings: Investigation findings
sources: Data sources used
Returns:
Confidence score (0.0 to 1.0)
"""
if not findings:
return 0.0
# Base confidence on number of findings and sources
findings_score = min(len(findings) / 10, 1.0) # More findings = higher confidence
sources_score = min(len(sources) / 3, 1.0) # More sources = higher confidence
# Average anomaly scores from findings
anomaly_scores = [f.get("anomaly_score", 0.0) for f in findings]
avg_anomaly_score = sum(anomaly_scores) / len(anomaly_scores) if anomaly_scores else 0.0
# Weighted average
confidence = (
findings_score * 0.3 +
sources_score * 0.2 +
avg_anomaly_score * 0.5
)
return min(confidence, 1.0)
async def reflect(
self,
result: Any,
context: AgentContext,
) -> Dict[str, Any]:
"""
Reflect on investigation results and provide quality assessment.
Args:
result: Investigation result
context: Agent context
Returns:
Reflection result
"""
if not isinstance(result, InvestigationResult):
return {
"quality_score": 0.0,
"issues": ["Invalid result type"],
"suggestions": ["Fix result format"],
}
issues = []
suggestions = []
# Check completeness
if not result.findings:
issues.append("No findings generated")
suggestions.append("Review investigation strategy")
# Check confidence
if result.confidence_score < 0.5:
issues.append("Low confidence score")
suggestions.append("Gather more data or use additional sources")
# Check explanation quality
if not result.explanation or len(result.explanation.strip()) < 50:
issues.append("Poor explanation quality")
suggestions.append("Generate more detailed explanation")
# Check source diversity
if len(result.sources) < 2:
issues.append("Limited source diversity")
suggestions.append("Include more data sources")
# Calculate quality score
quality_score = self._calculate_quality_score(result, issues)
reflection = {
"quality_score": quality_score,
"issues": issues,
"suggestions": suggestions,
"reflection_type": ReflectionType.COMPLETENESS_CHECK.value,
"metrics": {
"findings_count": len(result.findings),
"confidence_score": result.confidence_score,
"sources_count": len(result.sources),
"explanation_length": len(result.explanation) if result.explanation else 0,
},
}
self.logger.info(
"investigation_reflection",
investigation_id=result.investigation_id,
quality_score=quality_score,
issues_count=len(issues),
)
return reflection
def _calculate_quality_score(
self,
result: InvestigationResult,
issues: List[str],
) -> float:
"""Calculate quality score based on result and issues."""
base_score = 1.0
# Deduct points for issues
penalty_per_issue = 0.2
score = base_score - (len(issues) * penalty_per_issue)
# Bonus for high confidence
if result.confidence_score > 0.8:
score += 0.1
# Bonus for good explanation
if result.explanation and len(result.explanation) > 100:
score += 0.1
return max(0.0, min(1.0, score))
def _create_planning_prompt(
self,
query: str,
memory_context: Dict[str, Any],
) -> str:
"""Create prompt for investigation planning."""
return f"""
Você é um especialista em investigação de gastos públicos.
Crie um plano detalhado para investigar: "{query}"
Contexto da memória: {memory_context}
Agentes disponíveis:
- InvestigatorAgent: detecta anomalias
- AnalystAgent: analisa padrões
- ReporterAgent: gera relatórios
Forneça um plano estruturado com:
1. Objetivo da investigação
2. Passos específicos
3. Agentes necessários
4. Critérios de qualidade
"""
def _create_explanation_prompt(
self,
findings: List[Dict[str, Any]],
query: str,
) -> str:
"""Create prompt for explanation generation."""
return f"""
Explique em português claro os resultados da investigação sobre: "{query}"
Achados: {findings}
Forneça uma explicação que:
1. Resumo dos principais achados
2. Explique por que são suspeitos
3. Contextualize com dados normais
4. Sugira próximos passos
"""
def _parse_investigation_plan(
self,
plan_response: str,
query: str,
) -> InvestigationPlan:
"""Parse LLM response into investigation plan."""
# This is a simplified parser - in production, use more robust parsing
return InvestigationPlan(
objective=f"Investigar: {query}",
steps=[
{
"agent": "InvestigatorAgent",
"action": "detect_anomalies",
"parameters": {"query": query},
},
{
"agent": "AnalystAgent",
"action": "analyze_patterns",
"parameters": {"query": query},
},
],
required_agents=["InvestigatorAgent", "AnalystAgent"],
estimated_time=60,
quality_criteria={"min_confidence": 0.7, "min_findings": 1},
)
async def _monitor_progress(
self,
payload: Dict[str, Any],
context: AgentContext,
) -> Dict[str, Any]:
"""Monitor investigation progress."""
investigation_id = context.investigation_id
if investigation_id not in self.active_investigations:
return {"status": "not_found", "message": "Investigation not found"}
plan = self.active_investigations[investigation_id]
return {
"status": "active",
"plan": plan.model_dump(),
"progress": {
"total_steps": len(plan.steps),
"completed_steps": 0, # Would track actual progress
},
}
async def _adapt_strategy(
self,
payload: Dict[str, Any],
context: AgentContext,
) -> Dict[str, Any]:
"""Adapt investigation strategy based on results."""
# Implementation would analyze current results and modify strategy
return {
"status": "adapted",
"changes": ["Added additional data source", "Increased confidence threshold"],
} |