The Container module is a dependency injection and service container implementation that manages components and their dependencies in the OmAgent core system. It follows the IoC (Inversion of Control) pattern to handle component registration, configuration, and retrieval.
- Registers and manages different types of components (connections, memories, callbacks, etc.)
- Handles component dependencies automatically
- Provides type-safe access to registered components
Manages service connectors that components might depend on
- Automatically injects required connectors into components
- STM (Short-term Memory)
- LTM (Long-term Memory)
- Callback handlers
- Input handlers
- Can compile configurations to YAML
- Loads configurations from YAML files
- Supports environment variables and descriptions in configs
Examples of registering:
from omagent_core.utils.container import container
from omagent_core.services.handlers.redis_stream_handler import RedisStreamHandler
# Register a connector using component name
container.register_connector(RedisConnector, name="redis_client")
# Register a component using component class
container.register_component(RedisStreamHandler)
# Register STM component
container.register_stm("RedisSTM")
# Register LTM component
container.register_ltm("MilvusLTM")
# Register callback and input handlers
container.register_callback("AppCallback")
container.register_input("AppInput")
-
Compile Configuration: Container can automatically generate YAML configuration template files. You can change the values of the parameters in the template files which will take effect when loading the configuration. The
env_var
indicates the environment variable names for the parameters, don't change it because it is just for demonstration.from pathlib import Path container.compile_config(Path('./config_dir'))
-
Load Configuration: Load settings from YAML files. This will update the container with the settings in the YAML file.
container.from_config('container.yaml')
Access registered components:
# Get a connector
redis_client = container.get_connector("redis_client")
# Get STM component
stm = container.stm
# Get LTM component
ltm = container.ltm
# Get callback handler
callback = container.callback
# Get input handler
input_handler = container.input
- Early Registration: Register all components at application startup
- Configuration Files: Use YAML configuration files for better maintainability
- Compile Configuration: Prepare a separated script to compile container configuration before application startup.
- Update Container: Update the container with the settings in project entry file. Do register default Special Components (STM, LTM, Callback, Input) before update.
- Single Instance: Use the global container instance provided by the framework