multi_agent_hub.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from abc import ABC
  2. from typing import List
  3. from qwen_agent.agent import Agent
  4. from qwen_agent.log import logger
  5. class MultiAgentHub(ABC):
  6. @property
  7. def agents(self) -> List[Agent]:
  8. try:
  9. agent_list = self._agents
  10. assert isinstance(agent_list, list)
  11. assert all(isinstance(a, Agent) for a in agent_list)
  12. assert len(agent_list) > 0
  13. assert all(a.name for a in agent_list), 'All agents must have a name.'
  14. assert len(set(a.name for a in agent_list)) == len(agent_list), 'Agents must have unique names.'
  15. except (AttributeError, AssertionError) as e:
  16. logger.error(
  17. f'Class {self.__class__.__name__} inherits from MultiAgentHub. '
  18. 'However, the following constraints are violated: '
  19. "1) A class that inherits from MultiAgentHub must have an '_agents' attribute of type 'List[Agent]'. "
  20. "2) The '_agents' must be a non-empty list containing at least one agent. "
  21. "3) All agents in '_agents' must have non-empty, non-duplicate string names.")
  22. raise e
  23. return agent_list
  24. @property
  25. def agent_names(self) -> List[str]:
  26. return [x.name for x in self.agents]
  27. @property
  28. def nonuser_agents(self):
  29. from qwen_agent.agents.user_agent import UserAgent # put here to avoid cyclic import
  30. return [a for a in self.agents if not isinstance(a, UserAgent)]