agentscope.rpc package

Submodules

Module contents

Import all rpc related modules in the package.

class AsyncResult(host: str, port: int, task_id: int | None = None, stub: ~concurrent.futures._base.Future | None = None, retry: ~agentscope.rpc.retry_strategy.RetryBase = <agentscope.rpc.retry_strategy.RetryFixedTimes object>)[source]

Bases: object

Use this class to get the the async result from rpc server.

result() Any[source]

Get the result.

update_value() None[source]

Update the value. For compatibility with old version.

class DistConf(host: str = 'localhost', port: int | None = None, max_pool_size: int = 8192, max_expire_time: int = 7200, max_timeout_seconds: int = 5, local_mode: bool = True, lazy_launch: bool = False)[source]

Bases: dict

Distribution configuration for agents.

class RpcClient(host: str, port: int)[source]

Bases: object

A client of Rpc agent server

call_agent_func(func_name: str, agent_id: str, value: bytes | None = None, timeout: int = 300) bytes[source]

Call the specific function of an agent running on the server.

Parameters:
  • func_name (str) – The name of the function being called.

  • value (bytes, optional) – The serialized function input value.

  • None. (Defaults to)

  • timeout (int, optional) – The timeout for the RPC call in seconds.

  • 300. (Defaults to)

Returns:

serialized return data.

Return type:

bytes

create_agent(agent_configs: dict, agent_id: str | None = None) bool[source]

Create a new agent for this client.

Parameters:
  • agent_configs (dict) – Init configs of the agent, generated by RpcMeta.

  • agent_id (str) – agent_id of the created agent.

Returns:

Indicate whether the creation is successful

Return type:

bool

delete_agent(agent_id: str | None = None) bool[source]

Delete agents with the specific agent_id.

Parameters:

agent_id (str) – id of the agent to be deleted.

Returns:

Indicate whether the deletion is successful

Return type:

bool

delete_all_agent() bool[source]

Delete all agents on the server.

download_file(path: str) str[source]

Download a file from a remote server to the local machine.

Parameters:

path (str) – The path of the file to be downloaded. Note that it is the path on the remote server.

Returns:

The path of the downloaded file. Note that it is the path on the local machine.

Return type:

str

get_agent_list() Sequence[dict][source]

Get the summary of all agents on the server as a list.

Returns:

list of agent summary information.

Return type:

Sequence[str]

get_agent_memory(agent_id: str) list[Msg] | Msg[source]

Get the memory usage of the specific agent.

get_server_info() dict[source]

Get the agent server resource usage information.

is_alive() bool[source]

Check if the agent server is alive.

Returns:

Indicate whether the server is alive.

Return type:

bool

set_model_configs(model_configs: dict | list[dict]) bool[source]

Set the model configs of the server.

stop() bool[source]

Stop the agent server.

update_result(task_id: int, retry: ~agentscope.rpc.retry_strategy.RetryBase = <agentscope.rpc.retry_strategy.RetryFixedTimes object>) str[source]

Update the value of the async result.

Note

DON’T USE THIS FUNCTION IN ThreadPoolExecutor.

Parameters:
  • task_id (int) – task_id of the PlaceholderMessage.

  • retry (RetryBase) – Retry strategy. Defaults to RetryFixedTimes(10, 5).

Returns:

Serialized value.

Return type:

bytes

class RpcMeta(name: Any, bases: Any, attrs: Any)[source]

Bases: ABCMeta

The metaclass for all classes that can use to_dist and other distributed related functionality.

Note

The RpcMeta will automatically add the to_dist method and initialization parameter to its subclasses. And it will also detect all public functions and register them into the _info attribute of the class.

static get_class(cls_name: str) Any[source]

Get the class based on the specific class name.

Parameters:

cls_name (str) – the name of the class.

Raises:

ValueError – class name not exits.

Returns:

the class

Return type:

Any

static register_class(cls: type) bool[source]

Register the class into the registry.

Parameters:

cls (Type) – the class to be registered.

Returns:

whether the registration is successful.

Return type:

bool

static to_dist(self: ~typing.Any, host: str = 'localhost', port: int | None = None, max_pool_size: int = 8192, max_expire_time: int = 7200, max_timeout_seconds: int = 5, local_mode: bool = True, retry_strategy: ~agentscope.rpc.retry_strategy.RetryBase = <agentscope.rpc.retry_strategy.RetryFixedTimes object>) Any[source]

Convert current object into its distributed version.

Parameters:
  • host (str, defaults to “localhost”) – Hostname of the rpc agent server.

  • port (int, defaults to None) – Port of the rpc agent server.

  • max_pool_size (int, defaults to 8192) – 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_expire_time (int, defaults to 7200) – 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.

  • max_timeout_seconds (int, defaults to 5) – Max timeout seconds for the rpc call.

  • local_mode (bool, defaults to True) – Only takes effect when host and port are not filled in. Whether the started agent server only listens to local requests.

  • retry_strategy (RetryBase, defaults to _DEAFULT_RETRY_STRATEGY) – The retry strategy for the async rpc call.

Returns:

the wrapped agent instance with distributed functionality

Return type:

RpcObject

class RpcObject(cls: type, oid: str, host: str, port: int, connect_existing: bool = False, max_pool_size: int = 8192, max_expire_time: int = 7200, max_timeout_seconds: int = 5, local_mode: bool = True, retry_strategy: ~agentscope.rpc.retry_strategy.RetryBase | dict = <agentscope.rpc.retry_strategy.RetryFixedTimes object>, configs: dict | None = None)[source]

Bases: ABC

A proxy object which represent an object located in an Rpc server.

Note

When to_dist is called on an object or when using the to_dist parameter to create an object, the object is moved to an Rpc server, and an RpcObject instance is left behind in the local process. The RpcObject will automatically forward all public method invocations to the original object in the rpc server.

create(configs: dict) None[source]

create the object on the rpc server.

stop() None[source]

Stop the RpcAgent and the rpc server.

async_func(func: Callable) Callable[source]

A decorator for async function.

In distributed mode, async functions will return a AsyncResult immediately.

Parameters:

func (Callable) – The function to decorate.

sync_func(func: Callable) Callable[source]

A decorator for sync function.

In distributed mode, sync functions will block the current thread until the result is ready.

In most cases, you don’t need to use this decorator. RpcMeta will treat all public functions without async_func as sync_func. However, for magic methods (e.g. __str__ and __getitem__, which are started with __), you can use sync_func to mark them as sync.

Parameters:

func (Callable) – The function to decorate.