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 all 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
19 changes: 19 additions & 0 deletions docs/sphinx_doc/source/agentscope.service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,22 @@ Service package
agentscope.service.text_processing
agentscope.service.web_search


service\_status module
--------------------------------

.. automodule:: agentscope.service.service_status
:noindex:
:members:
:undoc-members:
:show-inheritance:


service\_response module
--------------------------------

.. automodule:: agentscope.service.service_response
:noindex:
:members:
:undoc-members:
:show-inheritance:
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 ..prompt 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 ..prompt 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
11 changes: 7 additions & 4 deletions src/agentscope/agents/rpc_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ def setup_rcp_agent_server(
f"Stopping rpc server [{servicer_class.__name__}] at port [{port}]",
)
server.stop(0)
stop_event.set()
logger.info(
f"rpc server [{servicer_class.__name__}] at port [{port}] stopped "
"successfully",
Expand Down Expand Up @@ -541,9 +540,13 @@ def shutdown(self) -> None:
if self.server is not None:
if self.stop_event is not None:
self.stop_event.set()
self.stop_event.clear()
self.stop_event.wait()
self.stop_event = None
self.server.terminate()
self.server.join()
self.server.join(timeout=5)
if self.server.is_alive():
self.server.kill()
logger.info(
f"Rpc server [{self.agent_class.__name__}] at port"
f" [{self.port}] is killed.",
)
self.server = None
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.prompt 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`"""
50 changes: 50 additions & 0 deletions src/agentscope/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,54 @@
# -*- 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 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
Loading