agentscope.module

The module in agentscope.

class StateModule[source]

Bases: object

The state module class in agentscope to support nested state serialization and deserialization.

__init__()[source]

Initialize the state module.

Return type:

None

state_dict()[source]

Get the state dictionary of the module, including the nested state modules and registered attributes.

Returns:

A dictionary that keys are attribute names and values are the state of the attribute.

Return type:

dict

load_state_dict(state_dict, strict=True)[source]

Load the state dictionary into the module.

Parameters:
  • state_dict (dict) – The state dictionary to load.

  • strict (bool, defaults to True) – If True, raises an error if any key in the module is not found in the state_dict. If False, skips missing keys.

Return type:

None

register_state(attr_name, custom_to_json=None, custom_from_json=None)[source]

Register an attribute to be tracked as a state variable.

Parameters:
  • attr_name (str) – The name of the attribute to register.

  • custom_to_json (Callable[[Any], JSONSerializableObject] | None, optional) – A custom function to convert the attribute to a JSON-serializable format. If not provided, json.dumps will be used.

  • custom_from_json (Callable[[JSONSerializableObject], Any] | None , defaults to None) – A custom function to convert the JSON dictionary back to the original attribute format.

Return type:

None