Skip to content

Commit

Permalink
Add as_server command and add server_id (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-x-c authored May 17, 2024
1 parent 623ceaf commit 93adc0c
Show file tree
Hide file tree
Showing 18 changed files with 879 additions and 663 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
26 changes: 22 additions & 4 deletions docs/sphinx_doc/en/source/tutorial/208-distribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ b = AgentB(
#### Independent Process Mode

In the Independent Process Mode, we need to start the agent server process on the target machine first.
When starting the agent server process, you need to specify a model config file, which contains the models which can be used in the agent server, the IP address and port of the agent server process
For example, start two agent server processes on the two different machines with IP `ip_a` and `ip_b`(called `Machine1` and `Machine2` accrodingly).
You can run the following code on `Machine1`:
You can run the following code on `Machine1`.Before running, make sure that the machine has access to all models that used in your application, specifically, you need to put your model config file in `model_config_path_a` and set environment variables such as your model API key correctly in `Machine1`. The example model config file instances are located under `examples/model_configs_template`.

```python
# import some packages

# register models which can be used in the server
agentscope.init(
...
model_configs=model_config_path_a,
)
# Create an agent service process
server = RpcAgentServerLauncher(
Expand All @@ -79,13 +81,20 @@ server.launch()
server.wait_until_terminate()
```

And run the following code on `Machine2`:
> For similarity, you can run the following command in your terminal rather than the above code:
>
> ```shell
> as_server --host ip_a --port 12001 --model-config-path model_config_path_a
> ```
Then put your model config file accordingly in `model_config_path_b`, set environment variables, and run the following code on `Machine2`.
```python
# import some packages
# register models which can be used in the server
agentscope.init(
...
model_configs=model_config_path_b,
)
# Create an agent service process
server = RpcAgentServerLauncher(
Expand All @@ -98,6 +107,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 --model-config-path model_config_path_b
> ```
Then, you can connect to the agent servers from the main process with the following code.
```python
Expand Down Expand Up @@ -254,6 +269,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
1 change: 1 addition & 0 deletions docs/sphinx_doc/zh_CN/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ AgentScope 文档
agentscope.pipelines
agentscope.service
agentscope.rpc
agentscope.server
agentscope.web
agentscope.prompt
agentscope.utils
Expand Down
27 changes: 22 additions & 5 deletions docs/sphinx_doc/zh_CN/source/tutorial/208-distribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ b = AgentB(

#### 独立进程模式

在独立进程模式中,需要首先在目标机器上启动智能体服务器进程。
在独立进程模式中,需要首先在目标机器上启动智能体服务器进程,启动时需要提供该服务器能够使用的模型的配置信息,以及服务器的 IP 和端口号
例如想要将两个智能体服务进程部署在 IP 分别为 `ip_a``ip_b` 的机器上(假设这两台机器分别为`Machine1``Machine2`)。
你可以先在 `Machine1` 上运行如下代码
你可以在 `Machine1` 上运行如下代码。在运行之前请确保该机器能够正确访问到应用中所使用的所有模型。具体来讲,需要将用到的所有模型的配置信息放置在 `model_config_path_a` 文件中,并检查API key 等环境变量是否正确设置,模型配置文件样例可参考 `examples/model_configs_template`

```python
# import some packages

# register models which can be used in the server
agentscope.init(
...
model_configs=model_config_path_a,
)
# Create an agent service process
server = RpcAgentServerLauncher(
Expand All @@ -78,13 +79,20 @@ server.launch()
server.wait_until_terminate()
```

之后在 `Machine2` 上运行如下代码:
> 为了进一步简化使用,可以在命令行中输入如下指令来代替上述代码:
>
> ```shell
> as_server --host ip_a --port 12001 --model-config-path model_config_path_a
> ```
`Machine2` 上运行如下代码,这里同样要确保已经将模型配置文件放置在 `model_config_path_b` 位置并设置环境变量,从而确保运行在该机器上的 Agent 能够正常访问到模型。
```python
# import some packages
# register models which can be used in the server
agentscope.init(
...
model_configs=model_config_path_b,
)
# Create an agent service process
server = RpcAgentServerLauncher(
Expand All @@ -97,6 +105,12 @@ server.launch()
server.wait_until_terminate()
```
> 这里也同样可以用如下指令来代替上面的代码。
>
> ```shell
> as_server --host ip_b --port 12002 --model-config-path model_config_path_b
> ```
接下来,就可以使用如下代码从主进程中连接这两个智能体服务器进程。
```python
Expand Down Expand Up @@ -251,6 +265,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 examples/distributed_basic/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.dialog_agent import DialogAgent
from agentscope.agents.rpc_agent import RpcAgentServerLauncher
from agentscope.server import RpcAgentServerLauncher


def parse_args() -> argparse.Namespace:
Expand Down
2 changes: 1 addition & 1 deletion examples/distributed_debate/distributed_debate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import agentscope
from agentscope.agents import DialogAgent
from agentscope.msghub import msghub
from agentscope.agents.rpc_agent import RpcAgentServerLauncher
from agentscope.server import RpcAgentServerLauncher
from agentscope.message import Msg
from agentscope.utils.logging_utils import logger

Expand Down
2 changes: 1 addition & 1 deletion examples/distributed_simulation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import agentscope
from agentscope.agents import AgentBase
from agentscope.agents.rpc_agent import RpcAgentServerLauncher
from agentscope.server import RpcAgentServerLauncher
from agentscope.message import Msg


Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"console_scripts": [
"as_studio=agentscope.web.studio.studio:run_app",
"as_workflow=agentscope.web.workstation.workflow:main",
"as_server=agentscope.server.launcher:as_server",
],
},
)
3 changes: 1 addition & 2 deletions src/agentscope/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .dict_dialog_agent import DictDialogAgent
from .user_agent import UserAgent
from .text_to_image_agent import TextToImageAgent
from .rpc_agent import RpcAgent, RpcAgentServerLauncher
from .rpc_agent import RpcAgent
from .react_agent import ReActAgent


Expand All @@ -20,5 +20,4 @@
"ReActAgent",
"DistConf",
"RpcAgent",
"RpcAgentServerLauncher",
]
20 changes: 14 additions & 6 deletions src/agentscope/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class _AgentMeta(ABCMeta):
"""

def __init__(cls, name: Any, bases: Any, attrs: Any) -> None:
if not hasattr(cls, "registry"):
if not hasattr(cls, "_registry"):
cls._registry = {}
else:
if name in cls._registry:
Expand Down Expand Up @@ -245,7 +245,7 @@ def register_agent_class(cls, agent_class: Type[AgentBase]) -> None:
"""
agent_class_name = agent_class.__name__
if agent_class_name in cls._registry:
logger.warning(
logger.info(
f"Agent class with name [{agent_class_name}] already exists.",
)
else:
Expand Down Expand Up @@ -384,14 +384,22 @@ def to_dist(
port (`int`, defaults to `None`):
Port of the rpc agent server.
max_pool_size (`int`, defaults to `8192`):
Max number of task results that the server can accommodate.
Only takes effect when `host` and `port` are not filled in.
The max number of agent reply messages that the started agent
server can accommodate. Note that the oldest message will be
deleted after exceeding the pool size.
max_timeout_seconds (`int`, defaults to `1800`):
Timeout for task results.
Only takes effect when `host` and `port` are not filled in.
Maximum time for reply messages to be cached in the launched
agent server. Note that expired messages will be deleted.
local_mode (`bool`, defaults to `True`):
Whether the started rpc server only listens to local
Only takes effect when `host` and `port` are not filled in.
Whether the started agent server only listens to local
requests.
lazy_launch (`bool`, defaults to `True`):
Only launch the server when the agent is called.
Only takes effect when `host` and `port` are not filled in.
If `True`, launch the agent server when the agent is called,
otherwise, launch the agent server immediately.
launch_server(`bool`, defaults to `None`):
This field has been deprecated and will be removed in
future releases.
Expand Down
Loading

0 comments on commit 93adc0c

Please sign in to comment.