Implement Role-Based Multi-Agent System

Easy
Agents

Role-Based Multi-Agent Systems

Different agents specialize in different tasks based on their assigned roles.

Task

Build a role-based orchestrator that:

  1. Maintains a registry of agents with roles and capabilities.
  2. Routes tasks to the first capable agent.
  3. Returns None if no agent can handle the task.
  4. Lists all available roles.

Constraints

  • Task dict must have a 'type' key.
  • If multiple agents can handle a task, prefer the one registered first.
  • Agent names must be unique.

Examples

Example 1:
Input: orch = RoleBasedOrchestrator() orch.register_agent(Agent('Cody', 'coder', ['code', 'debug'])) orch.dispatch({'type': 'code', 'content': 'write hello world'})
Output: {'type': 'code', 'content': 'write hello world', 'handled_by': 'Cody', 'role': 'coder'}
Explanation: Cody can handle 'code' tasks.

Starter Code

from typing import Dict, List, Optional

class Agent:
    def __init__(self, name: str, role: str, capabilities: List[str]):
        self.name = name
        self.role = role
        self.capabilities = capabilities

    def can_handle(self, task_type: str) -> bool:
        return task_type in self.capabilities

    def handle(self, task: dict) -> dict:
        # Simulated: return task with agent attribution
        return {**task, 'handled_by': self.name, 'role': self.role}

class RoleBasedOrchestrator:
    def __init__(self):
        self.agents: Dict[str, Agent] = {}

    def register_agent(self, agent: Agent) -> None:
        pass

    def dispatch(self, task: dict) -> Optional[dict]:
        # TODO: Find capable agent and dispatch task
        pass

    def list_roles(self) -> List[str]:
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews