Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:HKUDS AI Trader Get Agent Class

From Leeroopedia


Knowledge Sources
Domains Software_Architecture, Design_Patterns
Last Updated 2026-02-09 14:00 GMT

Overview

Concrete tool for resolving agent type strings to Python classes via a registry and dynamic imports, wrapping Python's importlib.

Description

The get_agent_class function looks up the given agent_type string in the AGENT_REGISTRY dictionary, dynamically imports the target module using importlib.import_module(), and returns the class via getattr(). The registry in main.py supports 5 agent types (BaseAgent, BaseAgent_Hour, BaseAgentAStock, BaseAgentAStock_Hour, BaseAgentCrypto), while main_parrallel.py has a reduced registry with only BaseAgent and BaseAgent_Hour.

Usage

Call this function with the agent_type string from the configuration to obtain the agent class for instantiation. Used by both main.py and main_parrallel.py entry points.

Code Reference

Source Location

  • Repository: AI-Trader
  • File: main.py
  • Lines: L40-73

Signature

def get_agent_class(agent_type):
    """
    Resolve agent type string to Python class via AGENT_REGISTRY.

    Args:
        agent_type: String name of agent type (e.g., "BaseAgent", "BaseAgentCrypto")

    Returns:
        Agent class (e.g., BaseAgent, BaseAgentCrypto)

    Raises:
        ValueError: If agent_type not in AGENT_REGISTRY
        ImportError: If module cannot be imported
        AttributeError: If class not found in module
    """

Import

# Defined in main.py (not separately importable)
# The AGENT_REGISTRY is defined at module level (L16-37):
AGENT_REGISTRY = {
    "BaseAgent":           {"module": "agent.base_agent.base_agent",              "class": "BaseAgent"},
    "BaseAgent_Hour":      {"module": "agent.base_agent.base_agent_hour",         "class": "BaseAgent_Hour"},
    "BaseAgentAStock":     {"module": "agent.base_agent_astock.base_agent_astock","class": "BaseAgentAStock"},
    "BaseAgentAStock_Hour":{"module": "agent.base_agent_astock.base_agent_astock_hour", "class": "BaseAgentAStock_Hour"},
    "BaseAgentCrypto":     {"module": "agent.base_agent_crypto.base_agent_crypto","class": "BaseAgentCrypto"},
}

I/O Contract

Inputs

Name Type Required Description
agent_type str Yes Agent type name from configuration (e.g., "BaseAgent")

Outputs

Name Type Description
AgentClass type The resolved Python class ready for instantiation

Usage Examples

Resolve and Instantiate

AgentClass = get_agent_class("BaseAgent")
# Returns the BaseAgent class from agent.base_agent.base_agent

agent = AgentClass(
    signature="gpt-4o",
    basemodel="gpt-4o",
    max_steps=30,
    initial_cash=10000.0
)

Related Pages

Requires Environment

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment