Skip to content

Commit

Permalink
update tutorial for agent server
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-x-c committed May 10, 2024
1 parent a92eac2 commit 31228b6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/sphinx_doc/en/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ AgentScope Documentation
agentscope.pipelines
agentscope.service
agentscope.rpc
agentscope.server
agentscope.web
agentscope.prompt
agentscope.utils
Expand Down
15 changes: 15 additions & 0 deletions docs/sphinx_doc/en/source/tutorial/208-distribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<agentscope.server.launcher.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.

Expand Down
15 changes: 15 additions & 0 deletions docs/sphinx_doc/zh_CN/source/tutorial/208-distribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ server.launch()
server.wait_until_terminate()
```

> 为了进一步简化使用,可以在命令行中输入如下指令来代替上述代码:
>
> ```shell
> as_server --host ip_a --port 12001
> ```
之后在 `Machine2` 上运行如下代码:
```python
Expand All @@ -97,6 +103,12 @@ server.launch()
server.wait_until_terminate()
```
> 这里也同样可以用如下指令来代替上面的代码。
>
> ```shell
> as_server --host ip_b --port 12002
> ```
接下来,就可以使用如下代码从主进程中连接这两个智能体服务器进程。
```python
Expand Down Expand Up @@ -251,6 +263,9 @@ Placeholder 内部包含了该消息产生方的联络方法,可以通过网
#### Agent Server

Agent Server 也就是智能体服务器。在 AgentScope 中,Agent Server 提供了一个让不同 Agent 实例运行的平台。多个不同类型的 Agent 可以运行在同一个 Agent Server 中并保持独立的记忆以及其他本地状态信息,但是他们将共享同一份计算资源。

在安装 AgentScope 的分布式版本后就可以通过 `as_server` 命令来启动 Agent Server,具体的启动参数在 {func}`as_server<agentscope.server.launcher.as_server>` 函数文档中可以找到。

只要没有对代码进行修改,一个已经启动的 Agent Server 可以为多个主流程提供服务。
这意味着在运行多个应用时,只需要在第一次运行前启动 Agent Server,后续这些 Agent Server 进程就可以持续复用。

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
},
)
3 changes: 2 additions & 1 deletion src/agentscope/server/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
]
50 changes: 35 additions & 15 deletions src/agentscope/server/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`):
Expand Down Expand Up @@ -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`):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}]...",
)
Expand All @@ -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()
Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 31228b6

Please sign in to comment.