Skip to content

Commit

Permalink
[DONE] Finish building manager
Browse files Browse the repository at this point in the history
  • Loading branch information
DavdGao committed Jul 30, 2024
1 parent b1a4772 commit 2de1348
Show file tree
Hide file tree
Showing 23 changed files with 498 additions and 331 deletions.
3 changes: 2 additions & 1 deletion src/agentscope/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
from .msghub import msghub
from ._version import __version__
from ._init import init

from ._init import state_dict

__all__ = [
"init",
"state_dict",
"msghub",
]
130 changes: 9 additions & 121 deletions src/agentscope/_init.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
# -*- coding: utf-8 -*-
"""The init function for the package."""
import json
import os
from typing import Optional, Union, Sequence
from agentscope import agents
from .agents import AgentBase
from ._runtime import _runtime
from .logging import LOG_LEVEL, setup_logger
from .manager import FileManager, MonitorManager
from .manager import ModelManager
from .logging import LOG_LEVEL
from .constants import _DEFAULT_SAVE_DIR
from .constants import _DEFAULT_LOG_LEVEL
from .constants import _DEFAULT_CACHE_DIR
from .studio._client import _studio_client
from .manager import ASManager

# init setting
_INIT_SETTINGS = {}

# init the singleton class by default settings to avoid reinit in subprocess
# especially in spawn mode, which will copy the object from the parent process
# to the child process rather than re-import the module (fork mode)
FileManager()
ModelManager()
MonitorManager()
ASManager()


def init(
Expand Down Expand Up @@ -85,11 +79,11 @@ def init(
studio_url (`Optional[str]`, defaults to `None`):
The url of the agentscope studio.
"""
init_process(
# Init the runtime
ASManager.get_instance().initialize(
model_configs=model_configs,
project=project,
name=name,
runtime_id=runtime_id,
disable_saving=disable_saving,
save_dir=save_dir,
save_log=save_log,
Expand All @@ -98,23 +92,10 @@ def init(
cache_dir=cache_dir,
use_monitor=use_monitor,
logger_level=logger_level,
run_id=runtime_id,
studio_url=studio_url,
)

# save init settings for subprocess
_INIT_SETTINGS["model_configs"] = model_configs
_INIT_SETTINGS["project"] = _runtime.project
_INIT_SETTINGS["name"] = _runtime.name
_INIT_SETTINGS["runtime_id"] = _runtime.runtime_id
_INIT_SETTINGS["disable_saving"] = disable_saving
_INIT_SETTINGS["save_dir"] = save_dir
_INIT_SETTINGS["save_code"] = False
_INIT_SETTINGS["save_api_invoke"] = save_api_invoke
_INIT_SETTINGS["save_log"] = save_log
_INIT_SETTINGS["logger_level"] = logger_level
_INIT_SETTINGS["use_monitor"] = use_monitor
_INIT_SETTINGS["cache_dir"] = cache_dir

# Load config and init agent by configs
if agent_configs is not None:
if isinstance(agent_configs, str):
Expand All @@ -136,99 +117,6 @@ def init(
return []


def init_process(
model_configs: Optional[Union[dict, str, list]],
project: Optional[str],
name: Optional[str],
runtime_id: Optional[str],
disable_saving: bool,
save_dir: str,
save_api_invoke: bool,
save_log: bool,
save_code: bool,
cache_dir: str,
use_monitor: bool,
logger_level: LOG_LEVEL,
studio_url: Optional[str] = None,
) -> None:
"""An entry to initialize the package in a process.
Args:
project (`Optional[str]`, defaults to `None`):
The project name, which is used to identify the project.
name (`Optional[str]`, defaults to `None`):
The name for runtime, which is used to identify this runtime.
runtime_id (`Optional[str]`, defaults to `None`):
The id for runtime, which is used to identify this runtime.
disable_saving (`bool`):
Whether to disable saving files. If `True`, this will override
the `save_log`, `save_code`, and `save_api_invoke` parameters.
save_dir (`str`, defaults to `./runs`):
The directory to save logs, files, codes, and api invocations.
If `dir` is `None`, when saving logs, files, codes, and api
invocations, the default directory `./runs` will be created.
save_api_invoke (`bool`, defaults to `False`):
Whether to save api invocations locally, including model and web
search invocation.
model_configs (`Optional[Sequence]`, defaults to `None`):
A sequence of pre-init model configs.
save_log (`bool`, defaults to `False`):
Whether to save logs locally.
save_code (`bool`):
Whether to save codes locally.
cache_dir (`str`):
The directory to cache files. In Linux/Mac, the dir defaults to
`~/.cache/agentscope`. In Windows, the dir defaults to
`C:\\users\\<username>\\.cache\\agentscope`.
use_monitor (`bool`, defaults to `True`):
Whether to activate the monitor.
logger_level (`LOG_LEVEL`, defaults to `"INFO"`):
The logging level of logger.
studio_url (`Optional[str]`, defaults to `None`):
The url of the agentscope studio.
"""
# Init the runtime
if project is not None:
_runtime.project = project

if name is not None:
_runtime.name = name

if runtime_id is not None:
_runtime.runtime_id = runtime_id

# Init file manager
file_manager = FileManager.get_instance()
file_manager.initialize(
disable_saving,
save_dir,
save_log,
save_code,
save_api_invoke,
cache_dir,
)

# Init logger
setup_logger(file_manager.run_dir, logger_level)

# Init model manager
if model_configs is not None:
ModelManager.get_instance().load_model_configs(model_configs)

# Init monitor
MonitorManager.get_instance().initialize(
not disable_saving and use_monitor,
)

# Init studio client, which will push messages to web ui and fetch user
# inputs from web ui
if studio_url is not None:
_studio_client.initialize(_runtime.runtime_id, studio_url)
# Register in AgentScope Studio
_studio_client.register_running_instance(
project=_runtime.project,
name=_runtime.name,
timestamp=_runtime.timestamp,
run_dir=file_manager.run_dir,
pid=os.getpid(),
)
def state_dict() -> dict:
"""Get the status of agentscope."""
return ASManager.get_instance().serialize()
87 changes: 0 additions & 87 deletions src/agentscope/_runtime.py

This file was deleted.

2 changes: 2 additions & 0 deletions src/agentscope/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@


# default values
_RUNTIME_TIMESTAMP_FORMAT = "%Y-%m-%d %H:%M:%S"
_RUNTIME_ID_FORMAT = "run_%Y%m%d-%H%M%S_{}"

# for file manager
_DEFAULT_SAVE_DIR = "./runs"
Expand Down
2 changes: 2 additions & 0 deletions src/agentscope/manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from ._monitor import MonitorManager
from ._file import FileManager
from ._model import ModelManager
from ._manager import ASManager

__all__ = [
"FileManager",
"ModelManager",
"MonitorManager",
"ASManager",
]
Loading

0 comments on commit 2de1348

Please sign in to comment.