智能体内存

Memory 管理智能体的内存,并存储智能体在每个步骤中与用户和应用程序交互所需的信息。Memory 中的部分元素将对智能体可见,用于决策。

MemoryItem

MemoryItem 是一个 dataclass,表示智能体内存中的单个步骤。MemoryItem 的字段是灵活的,可以根据智能体的要求进行定制。MemoryItem 类定义如下:

此数据类表示智能体在一个步骤中的内存项。

attributes property

获取内存项的属性。

返回
  • List[str]

    属性。

add_values_from_dict(values)

向内存项添加字段。

参数
  • values (Dict[str, Any]) –

    字段的值。

源代码位于 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=[])

获取内存项。

参数
  • keys (List[str], 默认值: [] ) –

    要获取的键。

返回
  • None

    过滤后的内存项。

源代码位于 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)

将字典转换为 MemoryItem。

参数
  • data (Dict[str, str]) –

    字典。

源代码位于 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)

获取字段的值。

参数
  • key (str) –

    字段的键。

返回
  • Optional[str]

    字段的值。

源代码位于 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)

获取字段的值。

参数
  • keys (List[str]) –

    字段的键。

返回
  • dict

    字段的值。

源代码位于 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)

向内存项添加字段。

参数
  • key (str) –

    字段的键。

  • value (str) –

    字段的值。

源代码位于 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()

将 MemoryItem 转换为字典。

返回
  • 字典[字符串, 字符串]

    字典。

源代码位于 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()

将内存项转换为 JSON 字符串。

返回
  • 字符串

    JSON 字符串。

源代码位于 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())

信息

在每个步骤中,都会创建一个 MemoryItem 实例并将其存储在 Memory 中,以记录智能体与用户和应用程序交互的信息。

内存

Memory 类负责管理智能体的内存。它存储一个 MemoryItem 实例列表,这些实例表示智能体在每个步骤中的内存。Memory 类定义如下:

此数据类表示智能体的内存。

content property

获取内存内容。

返回

length property

获取内存长度。

返回
  • int

    内存长度。

list_content property

列出内存内容。

返回
  • List[Dict[str, str]]

    内存内容。

add_memory_item(memory_item)

向内存添加内存项。

参数
  • memory_item (MemoryItem) –

    要添加的内存项。

源代码位于 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()

清除内存。

源代码位于 agents/memory/memory.py
138
139
140
141
142
def clear(self) -> None:
    """
    Clear the memory.
    """
    self._content = []

delete_memory_item(step)

从内存中删除内存项。

参数
  • step (int) –

    要删除的内存项的步骤。

源代码位于 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)

根据键过滤内存。如果某个项没有该键,则该键将被忽略。

参数
  • keys (List[str]) –

    要过滤的键。

返回
  • List[Dict[str, str]]

    过滤后的内存。

源代码位于 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)

根据步骤过滤内存。

参数
  • steps (List[int]) –

    要过滤的步骤。

返回
  • List[Dict[str, str]]

    过滤后的内存。

源代码位于 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)

将字典列表转换为内存。

参数
  • data (List[Dict[str, str]]) –

    字典列表。

源代码位于 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()

获取最新的内存项。

返回
源代码位于 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()

检查内存是否为空。

返回
  • 布尔值

    指示内存是否为空的布尔值。

源代码位于 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)

从内存加载数据。

参数
  • content (List[MemoryItem]) –

    要加载的内容。

源代码位于 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()

将内存转换为 JSON 字符串。

返回
  • 字符串

    JSON 字符串。

源代码位于 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()

将内存转换为字典列表。

返回
  • List[Dict[str, str]]

    字典列表。

源代码位于 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]

信息

每个智能体都有自己的 Memory 实例来存储其信息。

信息

Memory 中的所有信息并非都提供给智能体用于决策。智能体可以根据其逻辑要求访问内存的一部分。