agentscope.environment package

Submodules

Module contents

Import all environment related modules in the package.

class agentscope.environment.Event(name: str, args: dict | None = None, returns: Any | None = None)[源代码]

基类:object

A class representing the information of an event.

It contains the name of the event, the arguments of the event, and the returns of the event.

__init__(name: str, args: dict | None = None, returns: Any | None = None) None[源代码]
property name: str

Return the name of the event.

property args: dict

Return the arguments of the event.

property returns: Any

Return the returns of the event.

agentscope.environment.event_func(func: Callable) Callable[源代码]

A decorator to register an event function in Env and its subclasses.

备注

This decorator is only available in the subclasses of Env. If a function is decorated with @event_func, at the end of the function, all listeners bound to the function will be triggered automatically.

参数:

func (Callable) – The event function.

返回:

The decorated event function.

返回类型:

Callable

class agentscope.environment.Env(*args: tuple, **kwargs: dict)[源代码]

基类:ABC

The Env Interface. Env is a key concept of AgentScope, representing global data shared among agents.

Each env has its own name and value, and multiple envs can be organized into a tree structure, where each env can have multiple children envs.

Different implementations of envs may have different event functions, which are marked by @event_func. Users can bind EventListener to specific event functions, and the listener will be activated when the event function is called.

abstract property name: str

Name of the env.

返回:

The name of the env.

返回类型:

str

abstract get_children() dict[str, Env][源代码]

Get the children envs of the current env.

返回:

The children envs.

返回类型:

dict[str, Env]

abstract add_child(child: Env) bool[源代码]

Add a child env to the current env.

参数:

child (Env) – The children envs.

返回:

Whether the children were added successfully.

返回类型:

bool

abstract remove_child(children_name: str) bool[源代码]

Remove a child env from the current env.

参数:

children_name (str) – The name of the children env.

返回:

Whether the children were removed successfully.

返回类型:

bool

abstract add_listener(target_event: str, listener: EventListener) bool[源代码]

Add a listener to the env.

参数:
  • target_event (str) – The event function to listen.

  • listener (EventListener) – The listener to add.

返回:

Whether the listener was added successfully.

返回类型:

bool

abstract remove_listener(target_event: str, listener_name: str) bool[源代码]

Remove a listener from the env.

参数:
  • target_event (str) – The event function.

  • listener_name (str) – The name of the listener to remove.

返回:

Whether the listener was removed successfully.

返回类型:

bool

abstract get_listeners(target_event: str) List[EventListener][源代码]

Get the listeners of the specific event.

参数:

target_event (str) – The event name.

返回:

The listeners of the specific event.

返回类型:

List[EventListener]

abstract describe(**kwargs: Any) str[源代码]

Describe the current state of the environment.

to_dist(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 _DEAFULT_RETRY_STRATEGY) – The retry strategy for the async rpc call.

返回:

the wrapped agent instance with distributed functionality

返回类型:

RpcObject

class agentscope.environment.BasicEnv(*args: tuple, **kwargs: dict)[源代码]

基类:Env

A basic implementation of Env, which has no event function and cannot get value.

备注

BasicEnv is used as the base class to implement other envs. Application developers should not use this class.

__init__(name: str, listeners: dict[str, List[EventListener]] | None = None, children: List[Env] | None = None) None[源代码]

Init an BasicEnv instance.

参数:
  • name (str) – The name of the env.

  • listeners (dict[str, List[EventListener]], optional) – The

  • None. (envs. Defaults to)

  • children (List[Env], optional) – A list of children

  • None.

property name: str

Name of the env

get_children() dict[str, Env][源代码]

Get the children envs of the current env.

返回:

The children envs.

返回类型:

dict[str, Env]

add_child(child: Env) bool[源代码]

Add a child env to the current env.

参数:
  • child (Env) – The children

  • envs.

返回:

Whether the children were added successfully.

返回类型:

bool

remove_child(children_name: str) bool[源代码]

Remove a child env from the current env.

参数:

children_name (str) – The name of the children env.

返回:

Whether the children were removed successfully.

返回类型:

bool

add_listener(target_event: str, listener: EventListener) bool[源代码]

Add a listener to the env.

参数:
  • target_event (str) – The name of the event to listen.

  • listener (EventListener) – The listener to add.

返回:

Whether the listener was added successfully.

返回类型:

bool

remove_listener(target_event: str, listener_name: str) bool[源代码]

Remove a listener from the env.

参数:
  • target_event (str) – The event name.

  • listener_name (str) – The name of the listener to remove.

返回:

Whether the listener was removed successfully.

返回类型:

bool

get_listeners(target_event: str) List[EventListener][源代码]

Get the listeners of the specific event.

参数:

target_event (str) – The event name.

返回:

The listeners of the specific event.

返回类型:

List[EventListener]

describe(**kwargs: Any) str[源代码]

Describe the current state of the environment.

to_dist(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 _DEAFULT_RETRY_STRATEGY) – The retry strategy for the async rpc call.

返回:

the wrapped agent instance with distributed functionality

返回类型:

RpcObject

class agentscope.environment.EventListener(name: str)[源代码]

基类:ABC

A base class representing a listener for listening the event of an environment. The actions of the listener should be implemented in the __call__ method.

参数:
  • env (Env) – The environment instance who trigger the listener.

  • event (Event) – The event information, which contains the event

  • (name (function name) – str), the arguments of the event function

  • (argsdict), and the return value of the event function

  • (returnsAny).

备注

EventListener can only be bound to event functions (decorated with @event_func).

__init__(name: str) None[源代码]

Init a EventListener instance.

参数:

name (str) – The name of the listener.