Skip to main content

Overview

Router intelligently routes user queries to the most appropriate specialized agent.

Class Signature

from qwen_agent.agents import Router

class Router(Assistant, MultiAgentHub):
    def __init__(
        self,
        function_list: Optional[List[Union[str, Dict, BaseTool]]] = None,
        llm: Optional[Union[Dict, BaseChatModel]] = None,
        files: Optional[List[str]] = None,
        name: Optional[str] = None,
        description: Optional[str] = None,
        agents: Optional[List[Agent]] = None,
        rag_cfg: Optional[Dict] = None
    )

Parameters

agents
List[Agent]
required
List of specialized agents to route to
llm
Union[Dict, BaseChatModel]
required
LLM for routing decisions

Usage Example

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 web for information',
        llm={'model': 'qwen-max'},
        function_list=['web_extractor']
    )
]

# Create router
router = Router(
    llm={'model': 'qwen-max'},
    agents=agents
)

messages = [{'role': 'user', 'content': 'What is 15 factorial?'}]

for response in router.run(messages):
    print(response[-1].content)
# Router will select math_expert and delegate

See Also

Build docs developers (and LLMs) love