Skip to content

Commit

Permalink
refactor RpcAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-x-c committed Jan 25, 2024
1 parent a80eec5 commit 7c3100d
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 435 deletions.
8 changes: 0 additions & 8 deletions docs/sphinx_doc/source/agentscope.agents.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,3 @@ dict_dialog_agent module
:members:
:undoc-members:
:show-inheritance:

rpc_dialog_agent module
-------------------------------

.. automodule:: agentscope.agents.dict_dialog_agent
:members:
:undoc-members:
:show-inheritance:
13 changes: 8 additions & 5 deletions examples/distributed/distributed_debate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import agentscope
from agentscope.msghub import msghub
from agentscope.agents.rpc_dialog_agent import RpcDialogAgent
from agentscope.agents.dialog_agent import DialogAgent
from agentscope.agents.rpc_agent import RpcAgentServerLauncher
from agentscope.message import Msg
from agentscope.utils.logging_utils import logger
Expand Down Expand Up @@ -72,7 +72,7 @@ def setup_server(parsed_args: argparse.Namespace) -> None:
host=host,
port=port,
local_mode=False,
agent_class=RpcDialogAgent,
agent_class=DialogAgent,
**config,
)
server_launcher.launch()
Expand All @@ -84,20 +84,23 @@ def run_main_process(parsed_args: argparse.Namespace) -> None:
agentscope.init(
model_configs="configs/model_configs.json",
)
pro_agent = RpcDialogAgent(
pro_agent = DialogAgent(
name="Pro",
).to_distributed(
host=parsed_args.pro_host,
port=parsed_args.pro_port,
launch_server=False,
)
con_agent = RpcDialogAgent(
con_agent = DialogAgent(
name="Con",
).to_distributed(
host=parsed_args.con_host,
port=parsed_args.con_port,
launch_server=False,
)
judge_agent = RpcDialogAgent(
judge_agent = DialogAgent(
name="Judge",
).to_distributed(
host=parsed_args.judge_host,
port=parsed_args.judge_port,
launch_server=False,
Expand Down
23 changes: 13 additions & 10 deletions examples/distributed/distributed_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import agentscope
from agentscope.agents.user_agent import UserAgent
from agentscope.agents.rpc_dialog_agent import RpcDialogAgent
from agentscope.agents.dialog_agent import DialogAgent
from agentscope.agents.rpc_agent import RpcAgentServerLauncher


Expand Down Expand Up @@ -38,14 +38,16 @@ def setup_assistant_server(assistant_host: str, assistant_port: int) -> None:
model_configs="configs/model_configs.json",
)
assistant_server_launcher = RpcAgentServerLauncher(
name="Assitant",
agent_class=RpcDialogAgent,
host=assistant_host,
port=assistant_port,
sys_prompt="You are a helpful assistant.",
model="gpt-3.5-turbo",
use_memory=True,
local_mode=False,
agent_class=DialogAgent,
agent_kwargs={
"name": "Assitant",
"host": assistant_host,
"port": assistant_port,
"sys_prompt": "You are a helpful assistant.",
"model": "gpt-3.5-turbo",
"use_memory": True,
"local_mode": False,
},
)
assistant_server_launcher.launch()
assistant_server_launcher.wait_until_terminate()
Expand All @@ -56,8 +58,9 @@ def run_main_process(assistant_host: str, assistant_port: int) -> None:
agentscope.init(
model_configs="configs/model_configs.json",
)
assistant_agent = RpcDialogAgent(
assistant_agent = DialogAgent(
name="Assistant",
).to_distributed(
host=assistant_host,
port=assistant_port,
launch_server=False,
Expand Down
14 changes: 7 additions & 7 deletions notebook/distributed_debate.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,28 @@
"outputs": [],
"source": [
"import agentscope\n",
"from agentscope.agents.rpc_dialog_agent import RpcDialogAgent\n",
"from agentscope.agents.dialog_agent import DialogAgent\n",
"\n",
"agentscope.init(model_configs=model_configs)\n",
"\n",
"pro_agent = RpcDialogAgent(\n",
"pro_agent = DialogAgent(\n",
" name=\"Pro\",\n",
" model=\"gpt-3.5-turbo\",\n",
" use_memory=True,\n",
" sys_prompt=\"Assume the role of a debater who is arguing in favor of the proposition that AGI (Artificial General Intelligence) can be achieved using the GPT model framework. Construct a coherent and persuasive argument, including scientific, technological, and theoretical evidence, to support the statement that GPT models are a viable path to AGI. Highlight the advancements in language understanding, adaptability, and scalability of GPT models as key factors in progressing towards AGI.\",\n",
")\n",
"con_agent = RpcDialogAgent(\n",
").to_distributed()\n",
"con_agent = DialogAgent(\n",
" name=\"Con\",\n",
" model=\"gpt-3.5-turbo\",\n",
" use_memory=True,\n",
" sys_prompt=\"Assume the role of a debater who is arguing against the proposition that AGI can be achieved using the GPT model framework. Construct a coherent and persuasive argument, including scientific, technological, and theoretical evidence, to support the statement that GPT models, while impressive, are insufficient for reaching AGI. Discuss the limitations of GPT models such as lack of understanding, consciousness, ethical reasoning, and general problem-solving abilities that are essential for true AGI.\",\n",
")\n",
"judge_agent = RpcDialogAgent(\n",
").to_distributed()\n",
"judge_agent = DialogAgent(\n",
" name=\"Judge\",\n",
" model=\"gpt-3.5-turbo\",\n",
" use_memory=True,\n",
" sys_prompt=\"Assume the role of an impartial judge in a debate where the affirmative side argues that AGI can be achieved using the GPT model framework, and the negative side contests this. Listen to both sides' arguments and provide an analytical judgment on which side presented a more compelling and reasonable case. Consider the strength of the evidence, the persuasiveness of the reasoning, and the overall coherence of the arguments presented by each side.\"\n",
")"
").to_distributed()"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions notebook/distributed_dialog.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@
"source": [
"import agentscope\n",
"from agentscope.agents.user_agent import UserAgent\n",
"from agentscope.agents.rpc_dialog_agent import RpcDialogAgent\n",
"from agentscope.agents.dialog_agent import DialogAgent\n",
"\n",
"agentscope.init(\n",
" model_configs=model_configs\n",
")\n",
"\n",
"assistant_agent = RpcDialogAgent(\n",
"assistant_agent = DialogAgent(\n",
" name=\"Assistant\",\n",
" sys_prompt=\"You are a helpful assistant.\",\n",
" model=\"gpt-3.5-turbo\",\n",
" use_memory=True,\n",
")\n",
").to_distributed()\n",
"user_agent = UserAgent(\n",
" name=\"User\",\n",
")"
Expand Down Expand Up @@ -140,7 +140,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.1.0"
"version": "3.10.9"
}
},
"nbformat": 4,
Expand Down
2 changes: 0 additions & 2 deletions src/agentscope/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
""" 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
Expand All @@ -11,7 +10,6 @@
__all__ = [
"AgentBase",
"Operator",
"RpcAgentBase",
"DialogAgent",
"DictDialogAgent",
"UserAgent",
Expand Down
47 changes: 42 additions & 5 deletions src/agentscope/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@
""" Base class for Agent """

from __future__ import annotations
from abc import ABCMeta
from typing import Optional
from typing import Sequence
from typing import Union
from typing import Any
from typing import Callable

from loguru import logger

from .operator import Operator
from ..models import load_model_by_name
from ..memory import TemporaryMemory
from agentscope.agents.operator import Operator
from agentscope.models import load_model_by_name
from agentscope.memory import TemporaryMemory


class _RecordInitSettingMeta(ABCMeta):
"""A wrapper to record the init args into `init_settings` field."""

def __call__(cls, *args: tuple, **kwargs: dict) -> Any:
instance = super().__call__(*args, **kwargs)
instance.init_settings = {"args": args, "kwargs": kwargs}
return instance


class AgentBase(Operator):
class AgentBase(Operator, metaclass=_RecordInitSettingMeta):
"""Base class for all agents.
All agents should inherit from this class and implement the `reply`
Expand Down Expand Up @@ -172,3 +181,31 @@ def _broadcast_to_audience(self, x: dict) -> None:
"""Broadcast the input to all audiences."""
for agent in self._audience:
agent.observe(x)

def to_distributed(
self,
host: str = "localhost",
port: int = None,
max_pool_size: int = 100,
max_timeout_seconds: int = 1800,
launch_server: bool = True,
local_mode: bool = True,
lazy_launch: bool = True,
) -> AgentBase:
"""Convert current agent instance into a distributed version"""
from .rpc_agent import RpcAgent

if issubclass(self.__class__, RpcAgent):
return self
return RpcAgent(
agent_class=self.__class__,
agent_configs=self.init_settings,
name=self.name,
host=host,
port=port,
max_pool_size=max_pool_size,
max_timeout_seconds=max_timeout_seconds,
launch_server=launch_server,
local_mode=local_mode,
lazy_launch=lazy_launch,
)
Loading

0 comments on commit 7c3100d

Please sign in to comment.