Skip to content

Commit

Permalink
update dist doc
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-x-c committed Apr 26, 2024
1 parent 67020ba commit fd21daa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
8 changes: 4 additions & 4 deletions docs/sphinx_doc/en/source/tutorial/208-distribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ The above concepts may seem complex, but don't worry, for application developers

### Step 1: Convert your agent to its distributed version

All agents in AgentScope can automatically convert to its distributed version by calling its `to_dist` method.
But note that your agent must inherit from the `agentscope.agents.AgentBase` class, because the `to_dist` method is provided by the `AgentBase` class.
All agents in AgentScope can automatically convert to its distributed version by calling its {func}`to_dist<agentscope.agents.AgentBase.to_dist>` method.
But note that your agent must inherit from the {class}`agentscope.agents.AgentBase<agentscope.agents.AgentBase>` class, because the `to_dist` method is provided by the `AgentBase` class.

Suppose there are two agent classes `AgentA` and `AgentB`, both of which inherit from `AgentBase`.

Expand Down Expand Up @@ -122,7 +122,7 @@ And developers just need to write the application flow in a centralized way in t

#### Advanced Usage of `to_dist`

All examples described above convert initialized agents into their distributed version through the `to_dist()` method, which is equivalent to initialize the agent twice, once in the main process and once in the agent server process.
All examples described above convert initialized agents into their distributed version through the {func}`to_dist<agentscope.agents.AgentBase.to_dist>` method, which is equivalent to initialize the agent twice, once in the main process and once in the agent server process.
For agents whose initialization process is time-consuming, the `to_dist` method is inefficient. Therefore, AgentScope also provides a method to convert the Agent instance into its distributed version while initializing it, that is, passing in `to_dist` parameter to the Agent's initialization function.

In Child Process Mode, just pass `to_dist=True` to the Agent's initialization function.
Expand Down Expand Up @@ -241,7 +241,7 @@ By implementing each Agent as an Actor, an Agent will automatically wait for its

#### PlaceHolder

Meanwhile, to support centralized application orchestration, AgentScope introduces the concept of Placeholder.
Meanwhile, to support centralized application orchestration, AgentScope introduces the concept of {class}`Placeholder<agentscope.message.PlaceholderMessage>`.
A Placeholder is a special message that contains the address and port number of the agent that generated the placeholder, which is used to indicate that the output message of the Agent is not ready yet.
When calling the `reply` method of a distributed agent, a placeholder is returned immediately without blocking the main process.
The interface of placeholder is exactly the same as the message, so that the orchestration flow can be written in a centralized way.
Expand Down
8 changes: 4 additions & 4 deletions docs/sphinx_doc/zh_CN/source/tutorial/208-distribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ AgentScope中,我们将运行应用流程的进程称为**主进程 (Main Proc

### 步骤1: 转化为分布式版本

AgentScope 中所有智能体都可以通过 `to_dist` 方法转化为对应的分布式版本。
但需要注意,你的智能体必须继承自 `agentscope.agents.AgentBase` 类,因为是 `AgentBase` 提供了 `to_dist` 方法。
AgentScope 中所有智能体都可以通过 {func}`to_dist<agentscope.agents.AgentBase.to_dist>` 方法转化为对应的分布式版本。
但需要注意,你的智能体必须继承自 {class}`agentscope.agents.AgentBase<agentscope.agents.AgentBase>` 类,因为是 `AgentBase` 提供了 `to_dist` 方法。

假设有两个智能体类`AgentA``AgentB`,它们都继承自 `AgentBase`

Expand Down Expand Up @@ -121,7 +121,7 @@ b = AgentB(

#### `to_dist` 进阶用法

上面介绍的案例都是将一个已经初始化的 Agent 通过 `to_dist()` 方法转化为其分布式版本,相当于要执行两次初始化操作,一次在主进程中,一次在智能体进程中。如果 Agent 的初始化过程耗时较长,直接使用 `to_dist` 方法会严重影响运行效率。为此 AgentScope 也提供了在初始化 Agent 实例的同时将其转化为其分布式版本的方法,即在原 Agent 实例初始化时传入 `to_dist` 参数。
上面介绍的案例都是将一个已经初始化的 Agent 通过 {func}`to_dist<agentscope.agents.AgentBase.to_dist>` 方法转化为其分布式版本,相当于要执行两次初始化操作,一次在主进程中,一次在智能体进程中。如果 Agent 的初始化过程耗时较长,直接使用 `to_dist` 方法会严重影响运行效率。为此 AgentScope 也提供了在初始化 Agent 实例的同时将其转化为其分布式版本的方法,即在原 Agent 实例初始化时传入 `to_dist` 参数。

子进程模式下,只需要在 Agent 初始化函数中传入 `to_dist=True` 即可:

Expand Down Expand Up @@ -240,7 +240,7 @@ D-->F

#### PlaceHolder

同时,为了支持中心化的应用编排,AgentScope引入了Placeholder这一概念
同时,为了支持中心化的应用编排,AgentScope 引入了 {class}`Placeholder<agentscope.message.PlaceholderMessage>` 这一概念
Placeholder 可以理解为消息的指针,指向消息真正产生的位置,其对外接口与传统模式中的消息完全一致,因此可以按照传统中心化的消息使用方式编排应用。
Placeholder 内部包含了该消息产生方的联络方法,可以通过网络获取到被指向消息的真正值。
每个分布式部署的 Agent 在收到其他 Agent 发来的消息时都会立即返回一个 Placeholder,从而避免阻塞请求发起方。
Expand Down
2 changes: 1 addition & 1 deletion examples/distributed_simulation/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
""" Average """
""" A large-scale social simulation experiment """

import argparse
import time
Expand Down
29 changes: 25 additions & 4 deletions src/agentscope/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,31 @@ def __init__(
memory_config (`Optional[dict]`):
The config of memory.
to_dist (`Optional[Union[dict, bool]]`, default to `False`):
The parameter dict for `to_dist` method. Used in `_AgentMeta`,
when this parameter is provided, the agent will automatically
be converted into its distributed version. See
:doc:`Tutorial <tutorial/208-distribute>` for detail.
The parameter dict passed to :py:meth:`to_dist` method. Used in
:py:class:`_AgentMeta`, when this parameter is provided,
the agent will automatically be converted into its distributed
version. The input format is as follows:
.. code-block:: python
# run as a sub process
agent = XXXAgent(
# ... other parameters
to_dist=True,
)
# connect to an existing agent server
agent = XXXAgent(
# ... other parameters
to_dist={
"host": "ip of your server",
"port": 12345,
# other parameters of `to_dist`
},
)
See :doc:`Distribution Tutorial <tutorial/208-distribute>` for
detail.
"""
self.name = name
self.memory_config = memory_config
Expand Down

0 comments on commit fd21daa

Please sign in to comment.