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 property

Get the attributes of the memory item.

Returns:
  • List[str]

    The attributes.

add_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
66
67
68
69
70
71
72
def add_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)

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
46
47
48
49
50
51
52
53
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}

from_dict(data)

Convert the dictionary to a MemoryItem.

Parameters:
  • data (Dict[str, str]) –

    The dictionary.

Source code in agents/memory/memory.py
31
32
33
34
35
36
37
def from_dict(self, data: Dict[str, str]) -> None:
    """
    Convert the dictionary to a MemoryItem.
    :param data: The dictionary.
    """
    for key, value in data.items():
        self.set_value(key, value)

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
74
75
76
77
78
79
80
81
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
83
84
85
86
87
88
89
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
55
56
57
58
59
60
61
62
63
64
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)

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
29
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
39
40
41
42
43
44
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 property

Get the content of the memory.

Returns:

length property

Get the length of the memory.

Returns:
  • int

    The length of the memory.

list_content 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
131
132
133
134
135
136
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
138
139
140
141
142
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
152
153
154
155
156
157
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
123
124
125
126
127
128
129
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
115
116
117
118
119
120
121
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]

from_list_of_dicts(data)

Convert the list of dictionaries to the memory.

Parameters:
  • data (List[Dict[str, str]]) –

    The list of dictionaries.

Source code in agents/memory/memory.py
176
177
178
179
180
181
182
183
184
185
def from_list_of_dicts(self, data: List[Dict[str, str]]) -> None:
    """
    Convert the list of dictionaries to the memory.
    :param data: The list of dictionaries.
    """
    self._content = []
    for item in data:
        memory_item = MemoryItem()
        memory_item.from_dict(item)
        self._content.append(memory_item)

get_latest_item()

Get the latest memory item.

Returns:
Source code in agents/memory/memory.py
187
188
189
190
191
192
193
194
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
212
213
214
215
216
217
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
108
109
110
111
112
113
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
159
160
161
162
163
164
165
166
167
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]
    )

to_list_of_dicts()

Convert the memory to a list of dictionaries.

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

    The list of dictionaries.

Source code in agents/memory/memory.py
169
170
171
172
173
174
def to_list_of_dicts(self) -> List[Dict[str, str]]:
    """
    Convert the memory to a list of dictionaries.
    :return: The list of dictionaries.
    """
    return [item.to_dict() for item in self._content]

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.