Moya Documentation

Tutorials

Implementing Dynamic Agent Creation

In this tutorial, you'll learn how to implement dynamic agent creation in a multi-agent system. This system supports adding new agents at runtime and enables them to participate in interactions.

Step 1: Set Up the Project Structure

Create a new Python file called dynamic_agent_creation.py and import all necessary packages:

dynamic_agent_creation.py
"""
A multi-agent system with dynamic agent creation.
"""

from moya.agents.openai_agent import OpenAIAgent, OpenAIAgentConfig
from moya.classifiers.llm_classifier import LLMClassifier
from moya.orchestrators.multi_agent_orchestrator import MultiAgentOrchestrator
from moya.registry.agent_registry import AgentRegistry
from moya.tools.memory_tool import MemoryTool
from moya.memory.in_memory_repository import InMemoryRepository
from moya.tools.tool_registry import ToolRegistry

Step 2: Set Up the classifier and helper functions

Next, we'll set up the classifier and define memory tool

dynamic_agent_creation.py (continued)
def setup_memory_components():
    """Set up shared memory components."""
    memory_repo = InMemoryRepository()
    memory_tool = MemoryTool(memory_repository=memory_repo)
    tool_registry = ToolRegistry()
    tool_registry.register_tool(memory_tool)
    return tool_registry

def format_conversation_context(messages):
    """Format conversation history for context."""
    context = "\nPrevious conversation:\n"
    for msg in messages:
        sender = "User" if msg.sender == "user" else "Assistant"
        context += f"{sender}: {msg.content}\n"
    return context

def create_initial_classifier() -> OpenAIAgent:
    """Create the initial classifier with basic routing."""
    classifier_config = OpenAIAgentConfig(
        system_prompt="""You are a classifier that routes messages to appropriate agents.
        Given a user message and list of available specialized agents, select the most appropriate agent.
        Currently available agents:
        - english_agent: Default agent that responds in English
        
        If the message starts with 'Create new agent', return 'CREATOR' as this requires agent creation.
        Otherwise, return the most appropriate agent name from the available agents list.
        Return only the agent name, nothing else.""",
        model_name="gpt-4o",
        temperature=0.7
    )
    agent = OpenAIAgent(
        agent_name="classifier",
        description="Dynamic message router",
        agent_config=classifier_config
    )
    agent.setup()
    return agent

Step 3: Create Functions for Dynamic Agent Creation

Now, let's create functions to dynamically create new agents and update the classifier:

dynamic_agent_creation.py (continued)
def create_new_agent(tool_registry, agents_info: Dict[str, Dict[str, Any]]) -> OpenAIAgent:
    """Create a new agent based on user input."""
    print("\nCreating new agent...")
    agent_name = input("Enter agent name: ").strip()
    description = input("Enter agent description: ").strip()
    system_prompt = input("Enter system prompt: ").strip()

    agent_config = OpenAIAgentConfig(system_prompt=system_prompt)
    agent = OpenAIAgent(
        agent_name=agent_name,
        description=description,
        agent_config=agent_config,
        tool_registry=tool_registry
    )
    agent.setup()

    # Store agent info for classifier updates
    agents_info[agent_name] = {
        "description": description,
        "system_prompt": system_prompt
    }
    return agent

def update_classifier_prompt(classifier: OpenAIAgent, agents: Dict[str, Dict[str, Any]]):
    """Update classifier's system prompt with current agent list."""
    base_prompt = """You are a classifier that routes messages to appropriate agents.
    Given a user message and list of available specialized agents, select the most appropriate agent.
    Currently available agents:"""
    for name, details in agents.items():
        base_prompt += f"\n- {name}: {details['description']}"
    base_prompt += "\n\nIf the message starts with 'Create new agent', return 'CREATOR' as this requires agent creation."
    base_prompt += "\nOtherwise, return the most appropriate agent name from the available agents list."
    base_prompt += "\nReturn only the agent name, nothing else."
    classifier.system_prompt = base_prompt

Step 4: Implement the Main Function

Now, let's create the main function for our interactive chat:

dynamic_agent_creation.py (continued)
def main():
    # Set up initial components
    tool_registry = setup_memory_components()
    registry = AgentRegistry()

    # Create and register initial English agent
    english_config = OpenAIAgentConfig(
        system_prompt="You are a helpful assistant that responds in English."
    )
    english_agent = OpenAIAgent(
        agent_name="english_agent",
        description="Default English language agent",
        agent_config=english_config,
        tool_registry=tool_registry
    )
    english_agent.setup()
    registry.register_agent(english_agent)

    # Store agent information for classifier updates
    agents_info = {
        "english_agent": {
            "description": "Default English language agent",
            "system_prompt": "You are a helpful assistant that responds in English."
        }
    }

    # Set up classifier and orchestrator
    classifier_agent = create_initial_classifier()
    classifier = LLMClassifier(classifier_agent, default_agent="english_agent")
    orchestrator = MultiAgentOrchestrator(
        agent_registry=registry,
        classifier=classifier
    )

    # Interactive loop
    thread_id = "dynamic_agents_chat"
    print("Starting dynamic multi-agent chat (type 'exit' to quit)")
    print("Type 'Create new agent' to add a new agent to the system")
    print("-" * 50)

    def stream_callback(chunk):
        print(chunk, end="", flush=True)

    while True:
        user_message = input("\nYou: ").strip()
        if user_message.lower() == 'exit':
            print("\nGoodbye!")
            break

        if user_message.lower().startswith('create new agent'):
            new_agent = create_new_agent(tool_registry, agents_info)
            registry.register_agent(new_agent)
            update_classifier_prompt(classifier_agent, agents_info)
            print(f"\nAgent '{new_agent.agent_name}' created and registered!")
            continue

        print("\nAssistant: ", end="", flush=True)
        response = orchestrator.orchestrate(
            thread_id=thread_id,
            user_message=user_message,
            stream_callback=stream_callback
        )
        print()

if __name__ == "__main__":
    main()

Step 5: Run the System

Make sure your OpenAI API key is set in your environment, then run the script:

Terminal
export OPENAI_API_KEY=your_api_key_here
python dynamic_agent_creation.py

You should now be able to dynamically create new agents and interact with them in the chat system. Try creating a new agent, and the classifier should route messages to the appropriate agent based on the conversation context.

Note: This example is similar to the dynamic_agents.py example included in the Moya repository. You can run that example directly with python examples/dynamic_agents.py.