Documentation Index
Fetch the complete documentation index at: https://mintlify.com/QwenLM/Qwen-Agent/llms.txt
Use this file to discover all available pages before exploring further.
Overview
MultiAgentHub is an abstract base class for managing multiple agents in Qwen-Agent. It provides a unified interface for accessing and managing a collection of agents with validation to ensure proper configuration.
Class Signature
from qwen_agent import MultiAgentHub
class MultiAgentHub(ABC):
pass
Properties
agents
@property
def agents(self) -> List[Agent]
Returns the list of agents managed by this hub.
Validation Rules:
- Must have an
_agents attribute of type List[Agent]
- The list must be non-empty (at least one agent)
- All agents must have non-empty string names
- Agent names must be unique within the hub
Raises:
AttributeError or AssertionError if validation fails
agent_names
@property
def agent_names(self) -> List[str]
Returns the names of all agents.
nonuser_agents
@property
def nonuser_agents(self) -> List[Agent]
Returns all agents except UserAgent instances.
Implementation Requirements
Classes that inherit from MultiAgentHub must:
- Define an
_agents attribute containing a list of Agent instances
- Ensure all agents have unique, non-empty names
- Maintain at least one agent in the list
Usage Example
Basic Implementation
from qwen_agent import Agent, MultiAgentHub
from qwen_agent.agents import Assistant
from typing import List
class MyMultiAgentSystem(MultiAgentHub):
def __init__(self, llm_config: dict):
# Initialize agents
self._agents = [
Assistant(
name='researcher',
description='Researches topics and provides detailed information',
llm=llm_config
),
Assistant(
name='writer',
description='Writes content based on research',
llm=llm_config
),
Assistant(
name='editor',
description='Reviews and edits content',
llm=llm_config
)
]
# Use the system
llm_config = {'model': 'qwen-max', 'api_key': 'your-api-key'}
system = MyMultiAgentSystem(llm_config)
print(f"Agents: {system.agent_names}")
# Output: Agents: ['researcher', 'writer', 'editor']
With Router
from qwen_agent.agents import Router, Assistant
# Create specialized agents
agents = [
Assistant(
name='math_expert',
description='Solves mathematical problems',
llm={'model': 'qwen-max'},
function_list=['code_interpreter']
),
Assistant(
name='search_expert',
description='Searches for information on the web',
llm={'model': 'qwen-max'},
function_list=['web_extractor']
)
]
# Router is a MultiAgentHub
router = Router(
llm={'model': 'qwen-max'},
agents=agents,
name='main_router'
)
print(f"Available agents: {router.agent_names}")
# Output: Available agents: ['math_expert', 'search_expert']
With GroupChat
from qwen_agent.agents import GroupChat, Assistant
# Create agents for group chat
agents = [
Assistant(
name='alice',
description='A creative writer',
system_message='You are Alice, a creative writer who loves storytelling.',
llm={'model': 'qwen-max'}
),
Assistant(
name='bob',
description='A technical expert',
system_message='You are Bob, a technical expert who loves coding.',
llm={'model': 'qwen-max'}
)
]
# GroupChat is a MultiAgentHub
group_chat = GroupChat(
agents=agents,
agent_selection_method='auto',
llm={'model': 'qwen-max'}
)
print(f"Chat participants: {group_chat.agent_names}")
# Output: Chat participants: ['alice', 'bob']
Validation Example
from qwen_agent import MultiAgentHub
from qwen_agent.agents import Assistant
class InvalidHub(MultiAgentHub):
def __init__(self):
# Invalid: agents without names
self._agents = [
Assistant(llm={'model': 'qwen-max'}), # No name!
Assistant(llm={'model': 'qwen-max'}) # No name!
]
try:
hub = InvalidHub()
agents = hub.agents # This will raise an error
except AssertionError as e:
print(f"Error: All agents must have a name.")
# Correct version
class ValidHub(MultiAgentHub):
def __init__(self):
self._agents = [
Assistant(name='agent1', llm={'model': 'qwen-max'}),
Assistant(name='agent2', llm={'model': 'qwen-max'})
]
hub = ValidHub()
print(hub.agent_names) # ['agent1', 'agent2']
Common Patterns
Dynamic Agent Creation
class DynamicHub(MultiAgentHub):
def __init__(self, agent_configs: List[dict], llm_config: dict):
self._agents = [
Assistant(
name=cfg['name'],
description=cfg['description'],
system_message=cfg.get('system_message'),
function_list=cfg.get('tools', []),
llm=llm_config
)
for cfg in agent_configs
]
# Use it
configs = [
{'name': 'coder', 'description': 'Writes code', 'tools': ['code_interpreter']},
{'name': 'researcher', 'description': 'Researches topics', 'tools': ['web_extractor']}
]
hub = DynamicHub(configs, {'model': 'qwen-max'})
Agent Access by Name
class NamedHub(MultiAgentHub):
def get_agent(self, name: str) -> Agent:
for agent in self.agents:
if agent.name == name:
return agent
raise ValueError(f"Agent '{name}' not found")
def __init__(self, agents: List[Agent]):
self._agents = agents
hub = NamedHub(agents)
coder = hub.get_agent('coder')
See Also
- Router - Routes queries to specialized agents
- GroupChat - Multi-agent conversation system
- Agent - Base agent class