Agent Memory

The Memory manages the memory of the agent and stores the information required for the agent to interact with the user and applications at every step. Parts of elements in the Memory will be visible to the agent for decision-making.

MemoryItem

A MemoryItem is a dataclass that represents a single step in the agent's memory. The fields of a MemoryItem is flexible and can be customized based on the requirements of the agent. The MemoryItem class is defined as follows:

This data class represents a memory item of an agent at one step.

attributes: List[str] property

Get the attributes of the memory item.

Returns:
  • List[str]

    The attributes.

filter(keys=[])

Fetch the memory item.

Parameters:
  • keys (List[str], default: [] ) –

    The keys to fetch.

Returns:
  • None

    The filtered memory item.

Source code in agents/memory/memory.py
37
38
39
40
41
42
43
44
def filter(self, keys: List[str] = []) -> None:
    """
    Fetch the memory item.
    :param keys: The keys to fetch.
    :return: The filtered memory item.
    """

    return {key: value for key, value in self.to_dict().items() if key in keys}

get_value(key)

Get the value of the field.

Parameters:
  • key (str) –

    The key of the field.

Returns:
  • Optional[str]

    The value of the field.

Source code in agents/memory/memory.py
65
66
67
68
69
70
71
72
def get_value(self, key: str) -> Optional[str]:
    """
    Get the value of the field.
    :param key: The key of the field.
    :return: The value of the field.
    """

    return getattr(self, key, None)

get_values(keys)

Get the values of the fields.

Parameters:
  • keys (List[str]) –

    The keys of the fields.

Returns:
  • dict

    The values of the fields.

Source code in agents/memory/memory.py
74
75
76
77
78
79
80
def get_values(self, keys: List[str]) -> dict:
    """
    Get the values of the fields.
    :param keys: The keys of the fields.
    :return: The values of the fields.
    """
    return {key: self.get_value(key) for key in keys}

set_value(key, value)

Add a field to the memory item.

Parameters:
  • key (str) –

    The key of the field.

  • value (str) –

    The value of the field.

Source code in agents/memory/memory.py
46
47
48
49
50
51
52
53
54
55
def set_value(self, key: str, value: str) -> None:
    """
    Add a field to the memory item.
    :param key: The key of the field.
    :param value: The value of the field.
    """
    setattr(self, key, value)

    if key not in self._memory_attributes:
        self._memory_attributes.append(key)

set_values_from_dict(values)

Add fields to the memory item.

Parameters:
  • values (Dict[str, Any]) –

    The values of the fields.

Source code in agents/memory/memory.py
57
58
59
60
61
62
63
def set_values_from_dict(self, values: Dict[str, Any]) -> None:
    """
    Add fields to the memory item.
    :param values: The values of the fields.
    """
    for key, value in values.items():
        self.set_value(key, value)

to_dict()

Convert the MemoryItem to a dictionary.

Returns:
  • Dict[str, str]

    The dictionary.

Source code in agents/memory/memory.py
19
20
21
22
23
24
25
26
27
28
def to_dict(self) -> Dict[str, str]:
    """
    Convert the MemoryItem to a dictionary.
    :return: The dictionary.
    """
    return {
        key: value
        for key, value in self.__dict__.items()
        if key in self._memory_attributes
    }

to_json()

Convert the memory item to a JSON string.

Returns:
  • str

    The JSON string.

Source code in agents/memory/memory.py
30
31
32
33
34
35
def to_json(self) -> str:
    """
    Convert the memory item to a JSON string.
    :return: The JSON string.
    """
    return json.dumps(self.to_dict())

Info

At each step, an instance of MemoryItem is created and stored in the Memory to record the information of the agent's interaction with the user and applications.

Memory

The Memory class is responsible for managing the memory of the agent. It stores a list of MemoryItem instances that represent the agent's memory at each step. The Memory class is defined as follows:

This data class represents a memory of an agent.

content: List[MemoryItem] property

Get the content of the memory.

Returns:

length: int property

Get the length of the memory.

Returns:
  • int

    The length of the memory.

list_content: List[Dict[str, str]] property

List the content of the memory.

Returns:
  • List[Dict[str, str]]

    The content of the memory.

add_memory_item(memory_item)

Add a memory item to the memory.

Parameters:
  • memory_item (MemoryItem) –

    The memory item to add.

Source code in agents/memory/memory.py
122
123
124
125
126
127
def add_memory_item(self, memory_item: MemoryItem) -> None:
    """
    Add a memory item to the memory.
    :param memory_item: The memory item to add.
    """
    self._content.append(memory_item)

clear()

Clear the memory.

Source code in agents/memory/memory.py
129
130
131
132
133
def clear(self) -> None:
    """
    Clear the memory.
    """
    self._content = []

delete_memory_item(step)

Delete a memory item from the memory.

Parameters:
  • step (int) –

    The step of the memory item to delete.

Source code in agents/memory/memory.py
143
144
145
146
147
148
def delete_memory_item(self, step: int) -> None:
    """
    Delete a memory item from the memory.
    :param step: The step of the memory item to delete.
    """
    self._content = [item for item in self._content if item.step != step]

filter_memory_from_keys(keys)

Filter the memory from the keys. If an item does not have the key, the key will be ignored.

Parameters:
  • keys (List[str]) –

    The keys to filter.

Returns:
  • List[Dict[str, str]]

    The filtered memory.

Source code in agents/memory/memory.py
114
115
116
117
118
119
120
def filter_memory_from_keys(self, keys: List[str]) -> List[Dict[str, str]]:
    """
    Filter the memory from the keys. If an item does not have the key, the key will be ignored.
    :param keys: The keys to filter.
    :return: The filtered memory.
    """
    return [item.filter(keys) for item in self._content]

filter_memory_from_steps(steps)

Filter the memory from the steps.

Parameters:
  • steps (List[int]) –

    The steps to filter.

Returns:
  • List[Dict[str, str]]

    The filtered memory.

Source code in agents/memory/memory.py
106
107
108
109
110
111
112
def filter_memory_from_steps(self, steps: List[int]) -> List[Dict[str, str]]:
    """
    Filter the memory from the steps.
    :param steps: The steps to filter.
    :return: The filtered memory.
    """
    return [item.to_dict() for item in self._content if item.step in steps]

get_latest_item()

Get the latest memory item.

Returns:
Source code in agents/memory/memory.py
160
161
162
163
164
165
166
167
def get_latest_item(self) -> MemoryItem:
    """
    Get the latest memory item.
    :return: The latest memory item.
    """
    if self.length == 0:
        return None
    return self._content[-1]

is_empty()

Check if the memory is empty.

Returns:
  • bool

    The boolean value indicating if the memory is empty.

Source code in agents/memory/memory.py
185
186
187
188
189
190
def is_empty(self) -> bool:
    """
    Check if the memory is empty.
    :return: The boolean value indicating if the memory is empty.
    """
    return self.length == 0

load(content)

Load the data from the memory.

Parameters:
  • content (List[MemoryItem]) –

    The content to load.

Source code in agents/memory/memory.py
 99
100
101
102
103
104
def load(self, content: List[MemoryItem]) -> None:
    """
    Load the data from the memory.
    :param content: The content to load.
    """
    self._content = content

to_json()

Convert the memory to a JSON string.

Returns:
  • str

    The JSON string.

Source code in agents/memory/memory.py
150
151
152
153
154
155
156
157
158
def to_json(self) -> str:
    """
    Convert the memory to a JSON string.
    :return: The JSON string.
    """

    return json.dumps(
        [item.to_dict() for item in self._content if item is not None]
    )

Info

Each agent has its own Memory instance to store their information.

Info

Not all information in the Memory are provided to the agent for decision-making. The agent can access parts of the memory based on the requirements of the agent's logic.