From 31228b64b3eff70bb9e08f948fab2cf3bb4f3eb0 Mon Sep 17 00:00:00 2001 From: "panxuchen.pxc" Date: Fri, 10 May 2024 16:17:12 +0800 Subject: [PATCH] update tutorial for agent server --- docs/sphinx_doc/en/source/index.rst | 1 + .../en/source/tutorial/208-distribute.md | 15 ++++++ .../zh_CN/source/tutorial/208-distribute.md | 15 ++++++ setup.py | 2 +- src/agentscope/server/__init__.py | 3 +- src/agentscope/server/launcher.py | 50 +++++++++++++------ 6 files changed, 69 insertions(+), 17 deletions(-) diff --git a/docs/sphinx_doc/en/source/index.rst b/docs/sphinx_doc/en/source/index.rst index fb81e2e64..1aad67356 100644 --- a/docs/sphinx_doc/en/source/index.rst +++ b/docs/sphinx_doc/en/source/index.rst @@ -38,6 +38,7 @@ AgentScope Documentation agentscope.pipelines agentscope.service agentscope.rpc + agentscope.server agentscope.web agentscope.prompt agentscope.utils diff --git a/docs/sphinx_doc/en/source/tutorial/208-distribute.md b/docs/sphinx_doc/en/source/tutorial/208-distribute.md index 5574276c4..24fbc3acf 100644 --- a/docs/sphinx_doc/en/source/tutorial/208-distribute.md +++ b/docs/sphinx_doc/en/source/tutorial/208-distribute.md @@ -79,6 +79,12 @@ server.launch() server.wait_until_terminate() ``` +> For similarity, you can run the following command in your terminal rather than the above code: +> +> ```shell +> as_server --host ip_a --port 12001 +> ``` + And run the following code on `Machine2`: ```python @@ -98,6 +104,12 @@ server.launch() server.wait_until_terminate() ``` +> Similarly, you can run the following command in your terminal to setup the agent server: +> +> ```shell +> as_server --host ip_b --port 12002 +> ``` + Then, you can connect to the agent servers from the main process with the following code. ```python @@ -254,6 +266,9 @@ About more detailed technical implementation solutions, please refer to our [pap In agentscope, the agent server provides a running platform for various types of agents. Multiple agents can run in the same agent server and hold independent memory and other local states but they will share the same computation resources. + +After installing the distributed version of AgentScope, you can use the `as_server` command to start the agent server, and the detailed startup arguments can be found in the documentation of the {func}`as_server` function. + As long as the code is not modified, an agent server can provide services for multiple main processes. This means that when running mutliple applications, you only need to start the agent server for the first time, and it can be reused subsequently. diff --git a/docs/sphinx_doc/zh_CN/source/tutorial/208-distribute.md b/docs/sphinx_doc/zh_CN/source/tutorial/208-distribute.md index 3506e6641..fe840aa00 100644 --- a/docs/sphinx_doc/zh_CN/source/tutorial/208-distribute.md +++ b/docs/sphinx_doc/zh_CN/source/tutorial/208-distribute.md @@ -78,6 +78,12 @@ server.launch() server.wait_until_terminate() ``` +> 为了进一步简化使用,可以在命令行中输入如下指令来代替上述代码: +> +> ```shell +> as_server --host ip_a --port 12001 +> ``` + 之后在 `Machine2` 上运行如下代码: ```python @@ -97,6 +103,12 @@ server.launch() server.wait_until_terminate() ``` +> 这里也同样可以用如下指令来代替上面的代码。 +> +> ```shell +> as_server --host ip_b --port 12002 +> ``` + 接下来,就可以使用如下代码从主进程中连接这两个智能体服务器进程。 ```python @@ -251,6 +263,9 @@ Placeholder 内部包含了该消息产生方的联络方法,可以通过网 #### Agent Server Agent Server 也就是智能体服务器。在 AgentScope 中,Agent Server 提供了一个让不同 Agent 实例运行的平台。多个不同类型的 Agent 可以运行在同一个 Agent Server 中并保持独立的记忆以及其他本地状态信息,但是他们将共享同一份计算资源。 + +在安装 AgentScope 的分布式版本后就可以通过 `as_server` 命令来启动 Agent Server,具体的启动参数在 {func}`as_server` 函数文档中可以找到。 + 只要没有对代码进行修改,一个已经启动的 Agent Server 可以为多个主流程提供服务。 这意味着在运行多个应用时,只需要在第一次运行前启动 Agent Server,后续这些 Agent Server 进程就可以持续复用。 diff --git a/setup.py b/setup.py index 955bb47d2..60e9c9ddd 100644 --- a/setup.py +++ b/setup.py @@ -121,7 +121,7 @@ "console_scripts": [ "as_studio=agentscope.web.studio.studio:run_app", "as_workflow=agentscope.web.workstation.workflow:main", - "as_server=agentscope.server.launcher:launch", + "as_server=agentscope.server.launcher:as_server", ], }, ) diff --git a/src/agentscope/server/__init__.py b/src/agentscope/server/__init__.py index 0ce9e8d3d..9dbfe3faa 100644 --- a/src/agentscope/server/__init__.py +++ b/src/agentscope/server/__init__.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- """Import all server related modules in the package.""" -from .launcher import AgentServerLauncher +from .launcher import AgentServerLauncher, as_server from .servicer import AgentPlatform __all__ = [ "AgentServerLauncher", "AgentPlatform", + "as_server", ] diff --git a/src/agentscope/server/launcher.py b/src/agentscope/server/launcher.py index f2e2be15b..648c4b2a5 100644 --- a/src/agentscope/server/launcher.py +++ b/src/agentscope/server/launcher.py @@ -44,9 +44,9 @@ def setup_agent_server( Args: host (`str`, defaults to `"localhost"`): - Hostname of the rpc agent server. + Hostname of the agent server. port (`int`): - The socket port monitored by grpc server. + The socket port monitored by the agent server. server_id (`str`): The id of the server. init_settings (`dict`, defaults to `None`): @@ -102,9 +102,9 @@ async def setup_agent_server_async( Args: host (`str`, defaults to `"localhost"`): - Hostname of the rpc agent server. + Hostname of the agent server. port (`int`): - The socket port monitored by grpc server. + The socket port monitored by the agent server. server_id (`str`): The id of the server. init_settings (`dict`, defaults to `None`): @@ -214,7 +214,7 @@ def check_port(port: Optional[int] = None) -> int: if port is None: new_port = find_available_port() logger.warning( - "gRpc server port is not provided, automatically select " + "agent server port is not provided, automatically select " f"[{new_port}] as the port number.", ) return new_port @@ -248,15 +248,15 @@ def __init__( Args: host (`str`, defaults to `"localhost"`): - Hostname of the rpc agent server. + Hostname of the agent server. port (`int`, defaults to `None`): - Port of the rpc agent server. + Socket port of the agent server. max_pool_size (`int`, defaults to `8192`): Max number of task results that the server can accommodate. max_timeout_seconds (`int`, defaults to `1800`): Timeout for task results. local_mode (`bool`, defaults to `False`): - Whether the started rpc server only listens to local + Whether the started server only listens to local requests. custom_agents (`list`, defaults to `None`): A list of custom agent classes that are not in @@ -298,7 +298,7 @@ def generate_server_id(self) -> str: return f"{self.host}:{self.port}-{_get_timestamp('%y%m%d-%H:%M:%S')}" def _launch_in_main(self) -> None: - """Launch gRPC server in main-process""" + """Launch agent server in main-process""" logger.info( f"Launching agent server at [{self.host}:{self.port}]...", ) @@ -315,7 +315,7 @@ def _launch_in_main(self) -> None: ) def _launch_in_sub(self) -> None: - """Launch gRPC server in sub-process.""" + """Launch an agent server in sub-process.""" from agentscope._init import _INIT_SETTINGS self.stop_event = Event() @@ -346,7 +346,7 @@ def _launch_in_sub(self) -> None: ) def launch(self, in_subprocess: bool = True) -> None: - """launch a rpc agent server. + """launch an agent server. Args: in_subprocess (bool, optional): launch the server in subprocess. @@ -364,7 +364,7 @@ def wait_until_terminate(self) -> None: self.server.join() def shutdown(self) -> None: - """Shutdown the rpc agent server.""" + """Shutdown the agent server.""" if self.server is not None: if self.stop_event is not None: self.stop_event.set() @@ -378,8 +378,28 @@ def shutdown(self) -> None: self.server = None -def launch() -> None: - """Launch an agent server""" +def as_server() -> None: + """Launch an agent server with terminal command. + + Note: + + The arguments of `as_server` are listed as follows: + + * `--host`: the hostname of the server. + * `--port`: the socket port of the server. + * `--max_pool_size`: max number of task results that the server can + accommodate. + * `--max_timeout_seconds`: max timeout seconds of a task. + * `--local_mode`: whether the started agent server only listens to + local requests. + + In most cases, you only need to specify the `--host` and `--port`. + + .. code-block:: shell + + as_server --host localhost --port 12345 + + """ # noqa parser = argparse.ArgumentParser() parser.add_argument( @@ -410,7 +430,7 @@ def launch() -> None: "--local_mode", type=bool, default=False, - help="whether the started rpc server only listens to local requests", + help="whether the started agent server only listens to local requests", ) args = parser.parse_args() launcher = AgentServerLauncher(