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>)[源代码]

基类:object

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

result() Any[源代码]

Get the result.

update_value() None[源代码]

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)[源代码]

基类:dict

Distribution configuration for agents.

class RpcClient(host: str, port: int)[源代码]

基类:object

A client of Rpc agent server

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

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

参数:
  • 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)

返回:

serialized return data.

返回类型:

bytes

create_agent(agent_configs: dict, agent_id: str | None = None) bool[源代码]

Create a new agent for this client.

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

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

返回:

Indicate whether the creation is successful

返回类型:

bool

delete_agent(agent_id: str | None = None) bool[源代码]

Delete agents with the specific agent_id.

参数:

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

返回:

Indicate whether the deletion is successful

返回类型:

bool

delete_all_agent() bool[源代码]

Delete all agents on the server.

download_file(path: str) str[源代码]

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

参数:

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

返回:

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

返回类型:

str

get_agent_list() Sequence[dict][源代码]

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

返回:

list of agent summary information.

返回类型:

Sequence[str]

get_agent_memory(agent_id: str) list[Msg] | Msg[源代码]

Get the memory usage of the specific agent.

get_server_info() dict[源代码]

Get the agent server resource usage information.

is_alive() bool[源代码]

Check if the agent server is alive.

返回:

Indicate whether the server is alive.

返回类型:

bool

set_model_configs(model_configs: dict | list[dict]) bool[源代码]

Set the model configs of the server.

stop() bool[源代码]

Stop the agent server.

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

Update the value of the async result.

备注

DON’T USE THIS FUNCTION IN ThreadPoolExecutor.

参数:
  • task_id (int) – task_id of the PlaceholderMessage.

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

返回:

Serialized value.

返回类型:

bytes

class RpcMeta(name: Any, bases: Any, attrs: Any)[源代码]

基类:ABCMeta

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

备注

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[源代码]

Get the class based on the specific class name.

参数:

cls_name (str) – the name of the class.

抛出:

ValueError – class name not exits.

返回:

the class

返回类型:

Any

static register_class(cls: type) bool[源代码]

Register the class into the registry.

参数:

cls (Type) – the class to be registered.

返回:

whether the registration is successful.

返回类型:

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[源代码]

Convert current object into its distributed version.

参数:
  • 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 _DEFAULT_RETRY_STRATEGY) – The retry strategy for the async rpc call.

返回:

the wrapped agent instance with distributed functionality

返回类型:

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)[源代码]

基类:ABC

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

备注

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[源代码]

create the object on the rpc server.

stop() None[源代码]

Stop the RpcAgent and the rpc server.

async_func(func: Callable) Callable[源代码]

A decorator for async function.

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

参数:

func (Callable) – The function to decorate.

sync_func(func: Callable) Callable[源代码]

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.

参数:

func (Callable) – The function to decorate.