agentsociety.agent.agent_base¶
Module Contents¶
Classes¶
Agent parameters |
|
A model for gather query |
|
Agent Type |
|
Agent base class |
Functions¶
Extract JSON substring from a raw string response. |
Data¶
API¶
- agentsociety.agent.agent_base.__all__¶
[‘Agent’, ‘AgentType’, ‘AgentParams’, ‘GatherQuery’]
- class agentsociety.agent.agent_base.AgentParams¶
Bases:
pydantic.BaseModelAgent parameters
- class agentsociety.agent.agent_base.GatherQuery¶
Bases:
pydantic.BaseModelA model for gather query
- class agentsociety.agent.agent_base.AgentType(*args, **kwds)¶
Bases:
enum.EnumAgent Type
Citizen, Citizen type agent
Institution, Organization or institution type agent
Initialization
- Unspecified¶
‘Unspecified’
- Citizen¶
‘Citizen’
- Institution¶
‘Institution’
- Supervisor¶
‘Supervisor’
- Individual¶
‘Individual’
- agentsociety.agent.agent_base.extract_json(output_str)¶
Extract JSON substring from a raw string response.
Args: output_str: Raw string output that may contain JSON data.
Returns: Extracted JSON string if valid, otherwise None.
Note: Searches for the first ‘{’ and last ‘}’ to isolate JSON content. Catches JSON decoding errors and logs warnings.
- class agentsociety.agent.agent_base.Agent(id: int, name: str, type: agentsociety.agent.agent_base.AgentType, toolbox: agentsociety.agent.toolbox.AgentToolbox, memory: agentsociety.memory.Memory, agent_params: Optional[Any] = None, blocks: Optional[list[agentsociety.agent.block.Block]] = None)¶
Bases:
abc.ABCAgent base class
Initialization
Initialize the
Agent.Args:
id(int): The ID of the agent.name(str): The name of the agent.type(AgentType): The type of the agent. Defaults toAgentType.Unspecified.toolbox(AgentToolbox): The toolbox of the agent.memory(Memory): The memory of the agent.
- ParamsType: type[agentsociety.agent.agent_base.AgentParams]¶
None
- Context: type[agentsociety.agent.context.AgentContext]¶
None
- BlockOutputType: type[agentsociety.agent.block.BlockOutput]¶
None
- StatusAttributes: list[agentsociety.agent.memory_config_generator.MemoryAttribute]¶
[]
- classmethod default_params()¶
- classmethod default_context()¶
- classmethod __init_subclass__(**kwargs)¶
- async init()¶
- __getstate__()¶
- property id¶
The Agent’s Simulator ID
- property toolbox¶
The Agent’s Toolbox
- property llm¶
The Agent’s LLM
- property environment¶
The Agent’s Environment
- property messager¶
The Agent’s Messager
- property database_writer¶
The Agent’s Database Writer
- property memory¶
The Agent’s Memory
- property status¶
The Agent’s Status Memory
- property stream¶
The Agent’s Stream Memory
- abstract async reset()¶
Reset the agent.
- abstract async react_to_intervention(intervention_message: str)¶
React to an intervention.
Args:
intervention_message(str): The message of the intervention.
Description:
React to an intervention.
- async send_message_to_agent(to_agent_id: int, content: str, type: str = 'social')¶
Send a social or economy message to another agent.
Args:
to_agent_id(int): The ID of the recipient agent.content(str): The content of the message to send.type(str, optional): The type of the message (“social” or “economy”). Defaults to “social”.
Raises:
RuntimeError: If the Messager is not set.
Description:
Validates the message type and logs a warning if it’s invalid.
Prepares the message payload with necessary metadata such as sender ID, timestamp, etc.
Sends the message asynchronously using
_send_message.Optionally records the message in Database if it’s a “social” type message.
- _get_gather_query_and_clear()¶
- register_gather_query(key: str, target_agent_ids: list[int], flatten: bool = True, keep_id: bool = True)¶
- async register_aoi_message(target_aoi: Union[int, list[int]], content: str)¶
Register a message to target aoi
Args:
target_aoi(Union[int, list[int]]): The ID of the target aoi.content(str): The content of the message to send.
Description:
Register a message to target aoi.
- abstract async forward() Any¶
Define the behavior logic of the agent.
Raises:
NotImplementedError: As this method must be implemented by subclasses.
Description:
This abstract method should contain the core logic for what the agent does at each step of its operation.
It is intended to be overridden by subclasses to define specific behaviors.
- async status_summary()¶
Status summary
- async close()¶
Execute when the agent is deleted or the simulation is finished.
- async before_forward()¶
Before forward
- async after_forward()¶
After forward
- async before_blocks()¶
Before blocks
- async after_blocks()¶
After blocks
- async run() Any¶
Unified entry point for executing the agent’s logic.
Description:
It calls the
forwardmethod to execute the agent’s behavior logic.Acts as the main control flow for the agent, coordinating when and how the agent performs its actions.
- _is_output_type_compatible(block_output_type) bool¶
Check if the block output type is compatible with the agent’s expected output type.
Description:
Checks compatibility between block output type and agent’s expected output type.
Supports both class inheritance and field compatibility checks.
Args:
block_output_type: The output type class of the block.
Returns:
bool: True if compatible, False otherwise.