Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move defaults and types to constants #9

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,28 @@ Welcome to join our community on [Discord](https://discord.gg/Fwg5hZ2S) or DingD
Table of Contents
=================

* [Installation](#installation)
* [From source](#from-source)
* [Using pip](#using-pip)
* [Quick Start](#quick-start)
* [Basic Usage](#basic-usage)
* [Step 1: Prepare Model Configs](#step-1-prepare-model-configs)
* [Step2: Create Agents](#step-2-create-agents)
* [Step3: Construct Conversation](#step-3-construct-conversation)
* [Advanced Usage](#advanced-usage)
* [Pipeline and MsgHub](#pipeline-and-msghub)
* [Customize Your Own Agent](#customize-your-own-agent)
* [Built-in Resources](#built-in-resources)
* [Agent Pool](#agent-pool)
* [Services](#services)
* [Examples Applications](#example-applications)
* [License](#license)
* [Contributing](#contributing)
* [References](#references)
- [AgentScope](#agentscope)
- [Table of Contents](#table-of-contents)
- [Installation](#installation)
- [From source](#from-source)
- [Using pip](#using-pip)
- [Quick Start](#quick-start)
- [Basic Usage](#basic-usage)
- [Step 1: Prepare Model Configs](#step-1-prepare-model-configs)
- [OpenAI API Config](#openai-api-config)
- [Post Request API Config](#post-request-api-config)
- [Step 2: Create Agents](#step-2-create-agents)
- [Step 3: Construct Conversation](#step-3-construct-conversation)
- [Advanced Usage](#advanced-usage)
- [**Pipeline** and **MsgHub**](#pipeline-and-msghub)
- [Customize Your Own Agent](#customize-your-own-agent)
- [Built-in Resources](#built-in-resources)
- [Agent Pool](#agent-pool)
- [Services](#services)
- [Example Applications](#example-applications)
- [License](#license)
- [Contributing](#contributing)
- [References](#references)

## Installation

Expand Down Expand Up @@ -209,7 +213,7 @@ To simplify the construction of agents communication, AgentScope provides two he
agent2.observe(x1) # The message x1 should be broadcast to other agents
agent3.observe(x1)

x2 = agent2(x1
x2 = agent2(x1)
agent1.observe(x2)
agent3.observe(x2)
```
Expand Down
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
1 change: 0 additions & 1 deletion src/agentscope/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from . import models
from . import pipelines
from . import service
from . import utils # TODO: not exposed to the user
from . import message
from . import prompt

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

# 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`"""
64 changes: 64 additions & 0 deletions src/agentscope/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,68 @@
# -*- 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 execute python
_DEFAULT_PYPI_MIRROR = "http://mirrors.aliyun.com/pypi/simple/"
_DEFAULT_TRUSTED_HOST = "mirrors.aliyun.com"
# 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):
pan-x-c marked this conversation as resolved.
Show resolved Hide resolved
"""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
Loading