agentsociety.cityagent.blocks.cognition_block

Module Contents

Classes

CognitionBlockParams

CognitionBlock

A cognitive processing block handling daily updates of attitudes, thoughts, and emotions.

Functions

extract_json

Extract JSON substring from a raw string response.

Data

API

agentsociety.cityagent.blocks.cognition_block.__all__

[‘CognitionBlock’]

agentsociety.cityagent.blocks.cognition_block.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.cityagent.blocks.cognition_block.CognitionBlockParams

Bases: agentsociety.agent.BlockParams

top_k: int

‘Field(…)’

class agentsociety.cityagent.blocks.cognition_block.CognitionBlock(toolbox: agentsociety.agent.AgentToolbox, agent_memory: agentsociety.memory.Memory, block_params: Optional[agentsociety.cityagent.blocks.cognition_block.CognitionBlockParams] = None)

Bases: agentsociety.agent.Block

A cognitive processing block handling daily updates of attitudes, thoughts, and emotions.

Attributes: configurable_fields: List of configurable parameters (top_k). default_values: Default values for configurable parameters. fields_description: Metadata descriptions for configurable parameters. top_k: Number of most relevant memories retrieved for processing. last_check_time: Timestamp tracker for daily update cycles.

Initialization

Initialize CognitionBlock with dependencies.

Args: llm: Language Model interface for cognitive processing. environment: Environment for time-based operations. memory: Memory system to store/retrieve agent status and experiences.

ParamsType

None

name

‘CognitionBlock’

description

‘Handles daily updates of attitudes, thoughts, and emotions’

actions

None

async set_status(status)

Update multiple status fields in memory.

Args: status: Dictionary of key-value pairs to update.

async attitude_update()

Update agent’s attitudes toward specific topics based on daily experiences.

Workflow:

  1. Fetch agent’s profile and current emotional state from memory.

  2. Retrieve relevant incidents using topic-based memory search.

  3. Construct a structured prompt combining profile, incidents, and previous attitude.

  4. Query LLM to generate updated attitude scores (0-10 scale).

  5. Retry up to 10 times on LLM failures.

  6. Persist updated attitudes to memory.

Raises: Exception: If all LLM retries fail.

async thought_update()

Generate daily reflections based on experiences and emotional state.

Workflow:

  1. Build profile and emotion context from memory.

  2. Retrieve today’s incidents.

  3. Construct a reflection prompt.

  4. Query LLM to generate thought summary and emotional keyword.

  5. Retry up to 10 times on LLM failures.

  6. Update memory with new thought and log cognition.

Returns: Generated thought string.

Raises: Exception: If all LLM retries fail.

async cross_day()

Check if a new day has started in the simulation environment.

Returns: True if a new day is detected, False otherwise.

async forward()

Main daily cognitive update entry point.

Triggers: - thought_update() - attitude_update() Only executes when cross_day() detects a new day.

async emotion_update(incident)

Update emotion intensities based on a specific incident.

Args: incident: Description of the triggering event.

Returns: Natural language conclusion about emotional state.

Raises: Exception: If LLM requests fail after 10 retries.

Workflow: 1. Build emotion context from current state 2. Incorporate incident details into prompt 3. Query LLM for updated emotion scores and summary 4. Update memory with new emotional state