Skip to content

Commit

Permalink
move defaults and typings to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-x-c committed Jan 16, 2024
1 parent 06929d7 commit 457beb5
Show file tree
Hide file tree
Showing 33 changed files with 141 additions and 131 deletions.
8 changes: 8 additions & 0 deletions docs/sphinx_doc/source/agentscope.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Module contents
===============

constants module
--------------------------------

.. automodule:: agentscope.constants
:noindex:
:members:
:undoc-members:
:show-inheritance:

file\_manager module
--------------------------------
Expand Down
8 changes: 0 additions & 8 deletions docs/sphinx_doc/source/agentscope.utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ common module
:undoc-members:
:show-inheritance:

enums module
------------------------------

.. automodule:: agentscope.utils.enums
:members:
:undoc-members:
:show-inheritance:

logging\_utils module
---------------------------------------

Expand Down
6 changes: 2 additions & 4 deletions src/agentscope/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# -*- coding: utf-8 -*-
""" Import all agent related modules in the package. """
from typing import Callable
from .agent import AgentBase
from .rpc_agent import RpcAgentBase
from .dialog_agent import DialogAgent
from .dict_dialog_agent import DictDialogAgent
from .operator import Operator

# todo: convert Operator to a common base class for AgentBase and PipelineBase
_Operator = Callable[..., dict]

__all__ = [
"AgentBase",
"_Operator",
"Operator",
"RpcAgentBase",
"DialogAgent",
"DictDialogAgent",
Expand Down
4 changes: 2 additions & 2 deletions src/agentscope/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

from loguru import logger

from .operator import _Operator
from .operator import Operator
from ..models import load_model_by_name
from ..memory import TemporaryMemory


class AgentBase(_Operator):
class AgentBase(Operator):
"""Base class for all agents.
All agents should inherit from this class and implement the `reply`
Expand Down
2 changes: 1 addition & 1 deletion src/agentscope/agents/dialog_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ..message import Msg
from .agent import AgentBase
from ..prompt import PromptEngine
from ..utils.enums import PromptType
from ..constants import PromptType


class DialogAgent(AgentBase):
Expand Down
2 changes: 1 addition & 1 deletion src/agentscope/agents/dict_dialog_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..message import Msg
from .agent import AgentBase
from ..prompt import PromptEngine
from ..utils.enums import PromptType
from ..constants import PromptType


class DictDialogAgent(AgentBase):
Expand Down
2 changes: 1 addition & 1 deletion src/agentscope/agents/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Any


class _Operator(ABC):
class Operator(ABC):
"""
Abstract base class `Operator` defines a protocol for classes that
implement callable behavior.
Expand Down
2 changes: 1 addition & 1 deletion src/agentscope/agents/rpc_dialog_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from agentscope.message import Msg
from agentscope.agents.rpc_agent import RpcAgentBase
from agentscope.prompt import PromptEngine
from agentscope.utils.enums import PromptType
from agentscope.constants import PromptType


class RpcDialogAgent(RpcAgentBase):
Expand Down
10 changes: 4 additions & 6 deletions src/agentscope/configs/model_config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# -*- coding: utf-8 -*-
"""The model config."""
from typing import Any


DEFAULT_MAX_RETRIES = 3
DEFAULT_MESSAGES_KEY = "inputs"
from ..constants import _DEFAULT_MAX_RETRIES
from ..constants import _DEFAULT_MESSAGES_KEY


class CfgBase(dict):
Expand Down Expand Up @@ -108,10 +106,10 @@ class PostApiCfg(CfgBase):
**generate_args)`, which depends on the specific requirements of the
model API."""

max_retries: int = DEFAULT_MAX_RETRIES
max_retries: int = _DEFAULT_MAX_RETRIES
"""The max retries of the request."""

messages_key: str = DEFAULT_MESSAGES_KEY
messages_key: str = _DEFAULT_MESSAGES_KEY
"""The key of the prompt messages in `requests.post()`,
e.g. `request.post(json={${messages_key}: messages, **json_args})`. For
huggingface and modelscope inference API, the key is `inputs`"""
61 changes: 61 additions & 0 deletions src/agentscope/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,65 @@
# -*- coding: utf-8 -*-
""" Some constants used in the project"""
from numbers import Number
from enum import IntEnum

PACKAGE_NAME = "agentscope"
MSG_TOKEN = f"<{PACKAGE_NAME}_msg>"


# default values

# for file manager
_DEFAULT_DIR = "./runs"
_DEFAULT_LOG_LEVEL = "INFO"
_DEFAULT_SUBDIR_CODE = "code"
_DEFAULT_SUBDIR_FILE = "file"
_DEFAULT_SUBDIR_INVOKE = "invoke"
_DEFAULT_IMAGE_NAME = "image_{}_{}.png"
# for model wrapper
_DEFAULT_MAX_RETRIES = 3
_DEFAULT_MESSAGES_KEY = "inputs"
_DEFAULT_RETRY_INTERVAL = 1
# for summarization
_DEFAULT_SUMMARIZATION_PROMPT = """
TEXT: {}
"""
_DEFAULT_SYSTEM_PROMPT = """
You are a helpful agent to summarize the text.
You need to keep all the key information of the text in the summary.
"""
_DEFAULT_TOKEN_LIMIT_PROMPT = """
Summarize the text after TEXT in less than {} tokens:
"""

# typing
Embedding = list[Number]


# enums
class ResponseFormat(IntEnum):
"""Enum for model response format."""

NONE = 0
JSON = 1


class ServiceExecStatus(IntEnum):
"""Enum for service execution status."""

SUCCESS = 1
ERROR = -1


class PromptType(IntEnum):
"""Enum for prompt types."""

STRING = 0
LIST = 1


class ShrinkPolicy(IntEnum):
"""Enum for shrink strategies when the prompt is too long."""

TRUNCATE = 0
SUMMARIZE = 1
15 changes: 7 additions & 8 deletions src/agentscope/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
from agentscope._runtime import Runtime
from agentscope.utils.tools import _download_file, _get_timestamp
from agentscope.utils.tools import _generate_random_code

# TODO: move default values into one file
_DEFAULT_DIR = "./runs"
_DEFAULT_SUBDIR_CODE = "code"
_DEFAULT_SUBDIR_FILE = "file"
_DEFAULT_SUBDIR_INVOKE = "invoke"

_DEFAULT_IMAGE_NAME = "image_{}_{}.png"
from agentscope.constants import (
_DEFAULT_DIR,
_DEFAULT_SUBDIR_CODE,
_DEFAULT_SUBDIR_FILE,
_DEFAULT_SUBDIR_INVOKE,
_DEFAULT_IMAGE_NAME,
)


class _FileManager:
Expand Down
9 changes: 4 additions & 5 deletions src/agentscope/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@

from ..file_manager import file_manager
from ..utils.tools import _get_timestamp

# TODO: move default values into a single file
DEFAULT_MAX_RETRIES = 1
from ..constants import _DEFAULT_MAX_RETRIES
from ..constants import _DEFAULT_RETRY_INTERVAL


def _response_parse_decorator(
Expand Down Expand Up @@ -102,7 +101,7 @@ def checking_wrapper(self: Any, *args: Any, **kwargs: Any) -> dict:
# Step1: Extract parse_func and fault_handler
parse_func = kwargs.pop("parse_func", None)
fault_handler = kwargs.pop("fault_handler", None)
max_retries = kwargs.pop("max_retries", None) or DEFAULT_MAX_RETRIES
max_retries = kwargs.pop("max_retries", None) or _DEFAULT_MAX_RETRIES

# Step2: Call the model and parse the response
# Return the response directly if parse_func is not provided
Expand All @@ -124,7 +123,7 @@ def checking_wrapper(self: Any, *args: Any, **kwargs: Any) -> dict:
f"{response}.\n Exception: {e}, "
f"\t Attempt {itr} / {max_retries}",
)
time.sleep(0.5 * itr)
time.sleep(_DEFAULT_RETRY_INTERVAL * itr)

if fault_handler is not None and callable(fault_handler):
return fault_handler(response)
Expand Down
20 changes: 11 additions & 9 deletions src/agentscope/models/post_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
from loguru import logger

from .model import ModelWrapperBase

# TODO: move default values into a single file
DEFAULT_MAX_RETRIES = 3
DEFAULT_MESSAGES_KEY = "inputs"
RETRY_TIME_INTERVAL = 1
from ..constants import _DEFAULT_MAX_RETRIES
from ..constants import _DEFAULT_MESSAGES_KEY
from ..constants import _DEFAULT_RETRY_INTERVAL


class PostApiModelWrapper(ModelWrapperBase):
Expand All @@ -27,8 +25,9 @@ def __init__(
timeout: int = 30,
json_args: dict = None,
post_args: dict = None,
max_retries: int = DEFAULT_MAX_RETRIES,
messages_key: str = DEFAULT_MESSAGES_KEY,
max_retries: int = _DEFAULT_MAX_RETRIES,
messages_key: str = _DEFAULT_MESSAGES_KEY,
retry_interval: int = _DEFAULT_RETRY_INTERVAL,
) -> None:
"""Initialize the model wrapper.
Expand All @@ -52,6 +51,8 @@ def __init__(
exception.
messages_key (`str`, defaults to `inputs`):
The key of the input messages in the json argument.
retry_interval (`int`, defaults to `1`):
The interval between retries when a request fails.
Note:
When an object of `PostApiModelWrapper` is called, the arguments
Expand Down Expand Up @@ -79,6 +80,7 @@ def __init__(
self.post_args = post_args or {}
self.max_retries = max_retries
self.messages_key = messages_key
self.retry_interval = retry_interval

def __call__(self, input_: str, **kwargs: Any) -> dict:
"""Calling the model with requests.post.
Expand Down Expand Up @@ -122,14 +124,14 @@ def __call__(self, input_: str, **kwargs: Any) -> dict:
if response.status_code == requests.codes.ok:
break

if i < DEFAULT_MAX_RETRIES:
if i < self.max_retries:
# av
logger.warning(
f"Failed to call the model with "
f"requests.codes == {response.status_code}, retry "
f"{i + 1}/{self.max_retries} times",
)
time.sleep(i * RETRY_TIME_INTERVAL)
time.sleep(i * self.retry_interval)

# step3: record model invocation
# record the model api invocation, which will be skipped if
Expand Down
16 changes: 8 additions & 8 deletions src/agentscope/pipelines/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Callable, Sequence, Optional
from typing import Any
from typing import Mapping
from ..agents import _Operator
from ..agents.operator import Operator


def placeholder(x: dict = None) -> dict:
Expand All @@ -17,7 +17,7 @@ def placeholder(x: dict = None) -> dict:


def sequentialpipeline(
operators: Sequence[_Operator],
operators: Sequence[Operator],
x: Optional[dict] = None,
) -> dict:
"""Functional version of SequentialPipeline.
Expand All @@ -43,8 +43,8 @@ def sequentialpipeline(
def ifelsepipeline(
x: dict,
condition_func: Callable,
if_body_operator: _Operator,
else_body_operator: _Operator = placeholder,
if_body_operator: Operator,
else_body_operator: Operator = placeholder,
) -> dict:
"""Functional version of IfElsePipeline.
Expand Down Expand Up @@ -72,8 +72,8 @@ def ifelsepipeline(
def switchpipeline(
x: dict,
condition_func: Callable[[dict], Any],
case_operators: Mapping[Any, _Operator],
default_operator: _Operator = placeholder,
case_operators: Mapping[Any, Operator],
default_operator: Operator = placeholder,
) -> dict:
"""Functional version of SwitchPipeline.
Expand Down Expand Up @@ -104,7 +104,7 @@ def switchpipeline(

def forlooppipeline(
x: dict,
loop_body_operator: _Operator,
loop_body_operator: Operator,
max_loop: int,
break_func: Callable[[dict], bool] = lambda _: False,
) -> dict:
Expand Down Expand Up @@ -136,7 +136,7 @@ def forlooppipeline(

def whilelooppipeline(
x: dict,
loop_body_operator: _Operator,
loop_body_operator: Operator,
condition_func: Callable[[int, dict], bool] = lambda _, __: False,
) -> dict:
"""Functional version of WhileLoopPipeline.
Expand Down
Loading

0 comments on commit 457beb5

Please sign in to comment.