-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulti_agent.py
More file actions
45 lines (36 loc) · 1.71 KB
/
multi_agent.py
File metadata and controls
45 lines (36 loc) · 1.71 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
import asyncio
from typing import List, Dict, Any
class Agent:
def __init__(self, name: str, specialty: str):
self.name = name
self.specialty = specialty
async def process_task(self, task: Dict[str, Any]) -> Dict[str, Any]:
# Simulate processing time
await asyncio.sleep(1)
return {"agent": self.name, "result": f"Processed {task['type']} task using {self.specialty} specialty"}
class MultiAgentSystem:
def __init__(self):
self.agents: List[Agent] = []
def add_agent(self, agent: Agent):
self.agents.append(agent)
async def collaborate(self, tasks: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
async def process_task(task):
suitable_agents = [agent for agent in self.agents if agent.specialty == task['required_specialty']]
if not suitable_agents:
return {"error": f"No suitable agent found for {task['type']} task"}
return await suitable_agents[0].process_task(task)
return await asyncio.gather(*(process_task(task) for task in tasks))
# Usage
multi_agent_system = MultiAgentSystem()
multi_agent_system.add_agent(Agent("Alice", "natural language processing"))
multi_agent_system.add_agent(Agent("Bob", "data analysis"))
multi_agent_system.add_agent(Agent("Charlie", "image recognition"))
async def main():
tasks = [
{"type": "text summarization", "required_specialty": "natural language processing"},
{"type": "trend analysis", "required_specialty": "data analysis"},
{"type": "object detection", "required_specialty": "image recognition"}
]
results = await multi_agent_system.collaborate(tasks)
print("Collaboration results:", results)
asyncio.run(main())