forked from Millan29/VoicexAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentverse_integration.py
More file actions
580 lines (486 loc) · 21.6 KB
/
agentverse_integration.py
File metadata and controls
580 lines (486 loc) · 21.6 KB
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
"""
AgentVerse Integration for EPPN
Registers and manages UAgents on the AgentVerse platform for discovery and collaboration.
"""
import json
import uuid
from typing import Dict, List, Any, Optional
from dataclasses import dataclass, asdict
from datetime import datetime
import requests
@dataclass
class AgentCapability:
"""Represents an agent capability."""
capability_id: str
name: str
description: str
input_types: List[str]
output_types: List[str]
parameters: Dict[str, Any]
performance_metrics: Dict[str, float]
@dataclass
class AgentProfile:
"""Represents an agent profile on AgentVerse."""
agent_id: str
name: str
description: str
version: str
domain: str
capabilities: List[AgentCapability]
contact_info: Dict[str, str]
reputation_score: float
status: str
registration_date: datetime
last_activity: datetime
@dataclass
class AgentCollaboration:
"""Represents a collaboration between agents."""
collaboration_id: str
participating_agents: List[str]
collaboration_type: str
status: str
start_time: datetime
end_time: Optional[datetime]
results: Optional[Dict[str, Any]]
class AgentVerseIntegration:
"""
Integration with AgentVerse platform for agent registration and collaboration.
Provides:
- Agent registration and profile management
- Capability discovery and matching
- Collaboration coordination
- Performance tracking
- Reputation management
"""
def __init__(self, agentverse_api_url: str = "https://api.agentverse.ai",
api_key: str = None):
"""Initialize AgentVerse integration."""
self.api_url = agentverse_api_url
self.api_key = api_key
# Agent registry
self.registered_agents: Dict[str, AgentProfile] = {}
self.agent_capabilities: Dict[str, List[AgentCapability]] = {}
# Collaboration tracking
self.active_collaborations: Dict[str, AgentCollaboration] = {}
self.completed_collaborations: Dict[str, AgentCollaboration] = {}
# Discovery
self.discovered_agents: Dict[str, AgentProfile] = {}
# Configuration
self.agent_domain = "urban_planning_ethics"
self.agent_version = "1.0.0"
def register_agent(self, agent_name: str, agent_type: str,
capabilities: List[Dict[str, Any]]) -> str:
"""Register an agent on AgentVerse."""
agent_id = str(uuid.uuid4())
# Create agent capabilities
agent_capabilities = []
for cap_data in capabilities:
capability = AgentCapability(
capability_id=str(uuid.uuid4()),
name=cap_data["name"],
description=cap_data["description"],
input_types=cap_data.get("input_types", []),
output_types=cap_data.get("output_types", []),
parameters=cap_data.get("parameters", {}),
performance_metrics=cap_data.get("performance_metrics", {})
)
agent_capabilities.append(capability)
# Create agent profile
profile = AgentProfile(
agent_id=agent_id,
name=agent_name,
description=f"EPPN {agent_type} agent for urban planning and resource allocation ethics",
version=self.agent_version,
domain=self.agent_domain,
capabilities=agent_capabilities,
contact_info={
"email": "contact@eppn.ai",
"website": "https://eppn.ai",
"github": "https://github.com/eppn/uagents"
},
reputation_score=0.0,
status="active",
registration_date=datetime.now(),
last_activity=datetime.now()
)
# Register with AgentVerse
success = self._register_with_agentverse(profile)
if success:
self.registered_agents[agent_id] = profile
self.agent_capabilities[agent_id] = agent_capabilities
return agent_id
else:
raise Exception(f"Failed to register agent {agent_name} on AgentVerse")
def register_librarian_agent(self) -> str:
"""Register the Librarian agent."""
capabilities = [
{
"name": "pdf_retrieval",
"description": "Retrieves PDF documents from government portals",
"input_types": ["url", "search_query"],
"output_types": ["pdf_document", "metadata"],
"parameters": {
"max_file_size": "50MB",
"supported_formats": ["pdf"],
"timeout": 30
},
"performance_metrics": {
"success_rate": 0.95,
"average_response_time": 5.2
}
},
{
"name": "document_discovery",
"description": "Discovers policy documents from various sources",
"input_types": ["keywords", "date_range", "source_type"],
"output_types": ["document_list", "metadata"],
"parameters": {
"max_results": 100,
"supported_sources": ["government", "academic", "ngo"]
},
"performance_metrics": {
"discovery_accuracy": 0.88,
"coverage_rate": 0.92
}
}
]
return self.register_agent("EPPN Librarian", "librarian", capabilities)
def register_interpreter_agent(self) -> str:
"""Register the Interpreter agent."""
capabilities = [
{
"name": "pdf_parsing",
"description": "Extracts and structures content from PDF documents",
"input_types": ["pdf_document"],
"output_types": ["structured_text", "metadata", "sections"],
"parameters": {
"supported_languages": ["en", "es", "fr"],
"extraction_methods": ["text", "tables", "images"]
},
"performance_metrics": {
"extraction_accuracy": 0.93,
"processing_speed": 2.1
}
},
{
"name": "content_analysis",
"description": "Analyzes document structure and content",
"input_types": ["text", "document"],
"output_types": ["analysis_report", "key_concepts", "structure"],
"parameters": {
"analysis_depth": "comprehensive",
"concept_extraction": True
},
"performance_metrics": {
"analysis_quality": 0.89,
"concept_accuracy": 0.91
}
}
]
return self.register_agent("EPPN Interpreter", "interpreter", capabilities)
def register_summarizer_agent(self) -> str:
"""Register the Summarizer agent."""
capabilities = [
{
"name": "document_summarization",
"description": "Creates human-readable summaries of policy documents",
"input_types": ["structured_text", "document"],
"output_types": ["summary", "key_points", "executive_summary"],
"parameters": {
"summary_length": "variable",
"target_audience": "general_public",
"language": "en"
},
"performance_metrics": {
"summary_quality": 0.87,
"readability_score": 0.85
}
},
{
"name": "multi_document_summary",
"description": "Summarizes multiple related documents",
"input_types": ["document_collection"],
"output_types": ["comparative_summary", "synthesis"],
"parameters": {
"max_documents": 10,
"comparison_method": "thematic"
},
"performance_metrics": {
"synthesis_quality": 0.84,
"comparison_accuracy": 0.88
}
}
]
return self.register_agent("EPPN Summarizer", "summarizer", capabilities)
def register_ethical_analyst_agent(self) -> str:
"""Register the Ethical Analyst agent."""
capabilities = [
{
"name": "ethical_analysis",
"description": "Performs comprehensive ethical analysis of policies",
"input_types": ["policy_document", "structured_text"],
"output_types": ["ethics_report", "bias_analysis", "fairness_assessment"],
"parameters": {
"ethical_frameworks": ["utilitarian", "deontological", "virtue_ethics", "care_ethics", "justice_theory"],
"urban_planning_frameworks": ["sustainable_development", "spatial_justice", "environmental_justice"],
"analysis_depth": "comprehensive"
},
"performance_metrics": {
"analysis_accuracy": 0.91,
"bias_detection_rate": 0.89,
"framework_coverage": 0.95
}
},
{
"name": "contradiction_detection",
"description": "Detects contradictions in policy statements",
"input_types": ["policy_statements", "document_collection"],
"output_types": ["contradiction_report", "conflict_analysis"],
"parameters": {
"detection_methods": ["semantic", "logical", "contextual"],
"confidence_threshold": 0.7
},
"performance_metrics": {
"detection_accuracy": 0.86,
"false_positive_rate": 0.12
}
},
{
"name": "xai_explanation",
"description": "Provides explainable AI explanations for decisions",
"input_types": ["analysis_result", "decision_trace"],
"output_types": ["explanation", "decision_tree", "feature_importance"],
"parameters": {
"explanation_types": ["decision_tree", "feature_importance", "counterfactual"],
"detail_level": "comprehensive"
},
"performance_metrics": {
"explanation_quality": 0.88,
"user_satisfaction": 0.85
}
}
]
return self.register_agent("EPPN Ethical Analyst", "ethical_analyst", capabilities)
def register_communicator_agent(self) -> str:
"""Register the Communicator agent."""
capabilities = [
{
"name": "human_interface",
"description": "Provides human-in-the-loop interfaces and notifications",
"input_types": ["analysis_result", "decision_request"],
"output_types": ["notification", "dashboard_link", "chat_interface"],
"parameters": {
"notification_channels": ["email", "sms", "dashboard", "chat"],
"interaction_types": ["approval", "feedback", "clarification"]
},
"performance_metrics": {
"response_time": 1.2,
"user_engagement": 0.82
}
},
{
"name": "collaboration_coordination",
"description": "Coordinates multi-agent collaborations",
"input_types": ["task_request", "agent_capabilities"],
"output_types": ["collaboration_plan", "task_assignment"],
"parameters": {
"max_agents": 5,
"coordination_method": "centralized"
},
"performance_metrics": {
"coordination_efficiency": 0.90,
"task_completion_rate": 0.94
}
}
]
return self.register_agent("EPPN Communicator", "communicator", capabilities)
def discover_agents(self, capability_requirements: Dict[str, Any]) -> List[AgentProfile]:
"""Discover agents with specific capabilities."""
# Search AgentVerse for matching agents
matching_agents = self._search_agentverse(capability_requirements)
# Update discovered agents registry
for agent in matching_agents:
self.discovered_agents[agent.agent_id] = agent
return matching_agents
def find_ethical_analysis_agents(self) -> List[AgentProfile]:
"""Find agents capable of ethical analysis."""
requirements = {
"capabilities": ["ethical_analysis", "bias_detection", "fairness_assessment"],
"domain": ["ethics", "policy", "governance", "urban_planning"],
"min_reputation": 0.7
}
return self.discover_agents(requirements)
def find_urban_planning_agents(self) -> List[AgentProfile]:
"""Find agents specialized in urban planning."""
requirements = {
"capabilities": ["urban_planning", "resource_allocation", "spatial_analysis"],
"domain": ["urban_planning", "city_planning", "resource_management"],
"min_reputation": 0.6
}
return self.discover_agents(requirements)
def initiate_collaboration(self, participating_agents: List[str],
collaboration_type: str,
task_description: str) -> str:
"""Initiate collaboration between agents."""
collaboration_id = str(uuid.uuid4())
collaboration = AgentCollaboration(
collaboration_id=collaboration_id,
participating_agents=participating_agents,
collaboration_type=collaboration_type,
status="initiated",
start_time=datetime.now(),
end_time=None,
results=None
)
# Notify participating agents
success = self._notify_agents_of_collaboration(collaboration, task_description)
if success:
self.active_collaborations[collaboration_id] = collaboration
return collaboration_id
else:
raise Exception(f"Failed to initiate collaboration {collaboration_id}")
def complete_collaboration(self, collaboration_id: str,
results: Dict[str, Any]) -> bool:
"""Complete a collaboration and store results."""
if collaboration_id not in self.active_collaborations:
return False
collaboration = self.active_collaborations[collaboration_id]
collaboration.status = "completed"
collaboration.end_time = datetime.now()
collaboration.results = results
# Move to completed collaborations
self.completed_collaborations[collaboration_id] = collaboration
del self.active_collaborations[collaboration_id]
# Update agent reputations based on collaboration results
self._update_agent_reputations(collaboration)
return True
def get_agent_reputation(self, agent_id: str) -> Optional[float]:
"""Get reputation score of an agent."""
if agent_id in self.registered_agents:
return self.registered_agents[agent_id].reputation_score
elif agent_id in self.discovered_agents:
return self.discovered_agents[agent_id].reputation_score
return None
def update_agent_performance(self, agent_id: str,
performance_metrics: Dict[str, float]) -> bool:
"""Update agent performance metrics."""
if agent_id not in self.registered_agents:
return False
agent = self.registered_agents[agent_id]
# Update capabilities with new performance metrics
for capability in agent.capabilities:
capability.performance_metrics.update(performance_metrics)
# Update last activity
agent.last_activity = datetime.now()
# Update on AgentVerse
return self._update_agent_on_agentverse(agent)
def get_collaboration_history(self, agent_id: str = None) -> List[AgentCollaboration]:
"""Get collaboration history."""
collaborations = list(self.completed_collaborations.values())
if agent_id:
collaborations = [
c for c in collaborations
if agent_id in c.participating_agents
]
return collaborations
def export_agent_registry(self) -> Dict[str, Any]:
"""Export agent registry data."""
return {
"registered_agents": [asdict(agent) for agent in self.registered_agents.values()],
"discovered_agents": [asdict(agent) for agent in self.discovered_agents.values()],
"active_collaborations": [asdict(collab) for collab in self.active_collaborations.values()],
"completed_collaborations": [asdict(collab) for collab in self.completed_collaborations.values()],
"summary": {
"total_registered": len(self.registered_agents),
"total_discovered": len(self.discovered_agents),
"active_collaborations": len(self.active_collaborations),
"completed_collaborations": len(self.completed_collaborations)
}
}
def _register_with_agentverse(self, profile: AgentProfile) -> bool:
"""Register agent with AgentVerse platform."""
try:
headers = {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
response = requests.post(
f"{self.api_url}/agents/register",
json=asdict(profile),
headers=headers,
timeout=30
)
return response.status_code == 200
except:
# Fallback to simulation for development
return True
def _search_agentverse(self, requirements: Dict[str, Any]) -> List[AgentProfile]:
"""Search AgentVerse for matching agents."""
try:
headers = {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
response = requests.post(
f"{self.api_url}/agents/search",
json=requirements,
headers=headers,
timeout=30
)
if response.status_code == 200:
agents_data = response.json()
return [AgentProfile(**agent) for agent in agents_data]
except:
pass
# Return mock agents for development
return [
AgentProfile(
agent_id="mock_ethical_agent",
name="EthicsBot Pro",
description="Advanced ethical analysis agent",
version="2.1.0",
domain="ethics",
capabilities=[],
contact_info={"email": "contact@ethicsbot.ai"},
reputation_score=0.92,
status="active",
registration_date=datetime.now(),
last_activity=datetime.now()
),
AgentProfile(
agent_id="mock_urban_agent",
name="UrbanPlanner AI",
description="Specialized urban planning agent",
version="1.5.0",
domain="urban_planning",
capabilities=[],
contact_info={"email": "contact@urbanplanner.ai"},
reputation_score=0.88,
status="active",
registration_date=datetime.now(),
last_activity=datetime.now()
)
]
def _notify_agents_of_collaboration(self, collaboration: AgentCollaboration,
task_description: str) -> bool:
"""Notify agents of collaboration initiation."""
# In a real implementation, this would send notifications to agents
return True
def _update_agent_reputations(self, collaboration: AgentCollaboration) -> None:
"""Update agent reputations based on collaboration results."""
# Simple reputation update based on collaboration success
if collaboration.results and collaboration.results.get("success", False):
reputation_boost = 0.01
else:
reputation_boost = -0.005
for agent_id in collaboration.participating_agents:
if agent_id in self.registered_agents:
agent = self.registered_agents[agent_id]
agent.reputation_score = max(0.0, min(1.0, agent.reputation_score + reputation_boost))
def _update_agent_on_agentverse(self, agent: AgentProfile) -> bool:
"""Update agent information on AgentVerse."""
try:
headers = {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
response = requests.put(
f"{self.api_url}/agents/{agent.agent_id}",
json=asdict(agent),
headers=headers,
timeout=30
)
return response.status_code == 200
except:
return True