Skip to content

BufferStore基类

BaseBufferStore

Bases: TFBaseModel, ABC

Usage Documentation

Base Buffer Store

Buffer store 的基类。

Design principles: 1. 作为对话消息的基础存储单元,提供标准化的消息存取接口 2. 支持消息的增删改查等基本操作,保证消息的有序性 3. 提供灵活的消息过滤机制,支持按类型、角色、元数据等多维度筛选 4. 实现迭代器接口,支持正向和反向遍历,方便消息处理

Capability boundaries: 1. 仅负责单个对话上下文的消息管理,不处理多对话场景 Only responsible for message management of a single conversation context, does not handle multi-conversation scenarios 2. 专注于内存级别的消息存储,不负责持久化 3. 提供基础的消息操作接口,不包含消息的具体业务处理逻辑 4. 消息的格式限定为 UserAndAssMsg 类型,确保消息结构的统一性

Attributes:

Name Type Description
name Optional[str]

Name of the buffer store | 缓存存储器的名称,用于标识区分

description Optional[str]

Description of the buffer store | 缓存存储器的详细描述

length abstractmethod property

length: int

Get the length of the buffer store.

从缓存存储器中获取消息的数量

Returns:

Name Type Description
int int

The length of the buffer store.

长度值

add_msg abstractmethod

add_msg(msg: UserAndAssMsg) -> None

Add a message to buffer store.

将消息添加到缓存存储器中

Parameters:

Name Type Description Default
msg UserAndAssMsg

The message to add.

要添加的消息

required

Returns:

Type Description
None

None

Source code in tfrobot/brain/memory/store/buffer_store/base.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
@abstractmethod
def add_msg(self, msg: UserAndAssMsg) -> None:
    """
    Add a message to buffer store.

    将消息添加到缓存存储器中

    Args:
        msg (UserAndAssMsg): The message to add.

            要添加的消息

    Returns:
        None
    """
    pass

aadd_msg async

aadd_msg(msg: UserAndAssMsg) -> None

Add a message to buffer store asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
95
96
97
async def aadd_msg(self, msg: UserAndAssMsg) -> None:
    """Add a message to buffer store asynchronously."""
    self.add_msg(msg)

get_msg abstractmethod

get_msg(count: int | Annotated[list[int], Len(2, 2)] = -1, type_include: Optional[list[str]] = None, type_exclude: Optional[list[str]] = None, role_include: Optional[list[str]] = None, role_exclude: Optional[list[str]] = None, filter_meta: Optional[Callable[[Attributes], bool]] = None) -> list[UserAndAssMsg]

Gets recent messages from the buffer store.

从缓存存储器中获取最近的消息。

Notes
  1. count支持负数,表示从后向前取数,其行为与Python list类似
  2. 如果count是一个 list[int],第一个元素表示Page,第二个元素表示Size,其中Page可以为负数,亦表示从后向前取数
  3. Page从1开始,倒序从-1开始
  4. count如果是int,正序从0开始,倒序从-1开始

Parameters:

Name Type Description Default
count int

Number of messages to get. Count could be negative, which means to get messages from the end. A binary array can be specified, the first is the page number, the second is the capacity. Page number can be negative. | 要获取的消息数量。计数可以为负,这意味着从末尾获取消息。可以指定一个二元数组,第一个为页码,第二个是容量。页码可以为负数

-1
type_include Optional[List[str]]

Message types to include. | 要包含的消息类型。

None
type_exclude Optional[List[str]]

Message types to exclude. | 要排除的消息类型。

None
role_include Optional[List[str]]

Message roles to include. | 要包含的消息角色。

None
role_exclude Optional[List[str]]

Message roles to exclude. | 要排除的消息角色。

None
filter_meta Optional[Callable[[Dict[str, Any]], bool]]

Function to filter messages by metadata. | 用于通过元数据 过滤消息的函数。

None

Returns:

Type Description
list[UserAndAssMsg]

List[Message]: List of messages.| 消息列表。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
@abstractmethod
def get_msg(
    self,
    count: int | Annotated[list[int], annotated_types.Len(2, 2)] = -1,  # type: ignore
    type_include: Optional[list[str]] = None,
    type_exclude: Optional[list[str]] = None,
    role_include: Optional[list[str]] = None,
    role_exclude: Optional[list[str]] = None,
    filter_meta: Optional[Callable[[Attributes], bool]] = None,
) -> list[UserAndAssMsg]:
    """
    Gets recent messages from the buffer store.

    从缓存存储器中获取最近的消息。

    Notes:
        1. count支持负数,表示从后向前取数,其行为与Python list类似
        2. 如果count是一个 list[int],第一个元素表示Page,第二个元素表示Size,其中Page可以为负数,亦表示从后向前取数
        3. Page从1开始,倒序从-1开始
        4. count如果是int,正序从0开始,倒序从-1开始

    Args:
        count (int): Number of messages to get. Count could be negative, which means to get messages from the end.
            A binary array can be specified, the first is the page number, the second is the capacity. Page number
            can be negative. | 要获取的消息数量。计数可以为负,这意味着从末尾获取消息。可以指定一个二元数组,第一个为页码,第二个是容量。页码可以为负数
        type_include (Optional[List[str]]): Message types to include. | 要包含的消息类型。
        type_exclude (Optional[List[str]]): Message types to exclude. | 要排除的消息类型。
        role_include (Optional[List[str]]): Message roles to include. | 要包含的消息角色。
        role_exclude (Optional[List[str]]): Message roles to exclude. | 要排除的消息角色。
        filter_meta (Optional[Callable[[Dict[str, Any]], bool]]): Function to filter messages by metadata. | 用于通过元数据
            过滤消息的函数。

    Returns:
        List[Message]: List of messages.| 消息列表。
    """
    pass

aget_msg abstractmethod async

aget_msg(count: int | Annotated[list[int], Len(2, 2)] = -1, type_include: Optional[list[str]] = None, type_exclude: Optional[list[str]] = None, role_include: Optional[list[str]] = None, role_exclude: Optional[list[str]] = None, filter_meta: Optional[Callable[[Attributes], bool]] = None) -> list[UserAndAssMsg]

Get recent messages from the buffer store asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
136
137
138
139
140
141
142
143
144
145
146
147
@abstractmethod
async def aget_msg(
    self,
    count: int | Annotated[list[int], annotated_types.Len(2, 2)] = -1,  # type: ignore
    type_include: Optional[list[str]] = None,
    type_exclude: Optional[list[str]] = None,
    role_include: Optional[list[str]] = None,
    role_exclude: Optional[list[str]] = None,
    filter_meta: Optional[Callable[[Attributes], bool]] = None,
) -> list[UserAndAssMsg]:
    """Get recent messages from the buffer store asynchronously."""
    ...

get_msg_by_cursor abstractmethod

get_msg_by_cursor(count: int = 10, cursor: Optional[str] = None, type_include: Optional[list[str]] = None, type_exclude: Optional[list[str]] = None, role_include: Optional[list[str]] = None, role_exclude: Optional[list[str]] = None, filter_meta: Optional[Callable[[Attributes], bool]] = None) -> tuple[list[UserAndAssMsg], str]

Gets recent messages from the buffer store.

从缓存存储器中获取最近的消息。

Notes
  1. count支持负数,表示从后向前取数,其行为与Python list类似
  2. 如果count是一个 list[int],第一个元素表示Page,第二个元素表示Size,其中Page可以为负数,亦表示从后向前取数
  3. Page从1开始,倒序从-1开始
  4. count如果是int,正序从0开始,倒序从-1开始

Parameters:

Name Type Description Default
count int

Number of messages to get. Count could be negative, which means to get messages from the end. | 要获取的消息数量。计数可以为负,这意味着从末尾获取消息。

10
cursor Optional[str]

The cursor of the message to start from. | 计数游标。

None
type_include Optional[List[str]]

Message types to include. | 要包含的消息类型。

None
type_exclude Optional[List[str]]

Message types to exclude. | 要排除的消息类型。

None
role_include Optional[List[str]]

Message roles to include. | 要包含的消息角色。

None
role_exclude Optional[List[str]]

Message roles to exclude. | 要排除的消息角色。

None
filter_meta Optional[Callable[[Dict[str, Any]], bool]]

Function to filter messages by metadata. | 用于通过元数据 过滤消息的函数。

None

Returns:

Type Description
tuple[list[UserAndAssMsg], str]

tuple[list[UserAndAssMsg], str]: List of messages and the cursor. | 消息列表和游标。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
@abstractmethod
def get_msg_by_cursor(
    self,
    count: int = 10,
    cursor: Optional[str] = None,
    type_include: Optional[list[str]] = None,
    type_exclude: Optional[list[str]] = None,
    role_include: Optional[list[str]] = None,
    role_exclude: Optional[list[str]] = None,
    filter_meta: Optional[Callable[[Attributes], bool]] = None,
) -> tuple[list[UserAndAssMsg], str]:
    """
    Gets recent messages from the buffer store.

    从缓存存储器中获取最近的消息。

    Notes:
        1. count支持负数,表示从后向前取数,其行为与Python list类似
        2. 如果count是一个 list[int],第一个元素表示Page,第二个元素表示Size,其中Page可以为负数,亦表示从后向前取数
        3. Page从1开始,倒序从-1开始
        4. count如果是int,正序从0开始,倒序从-1开始

    Args:
        count (int): Number of messages to get. Count could be negative, which means to get messages from the end. |
            要获取的消息数量。计数可以为负,这意味着从末尾获取消息。
        cursor (Optional[str]): The cursor of the message to start from. | 计数游标。
        type_include (Optional[List[str]]): Message types to include. | 要包含的消息类型。
        type_exclude (Optional[List[str]]): Message types to exclude. | 要排除的消息类型。
        role_include (Optional[List[str]]): Message roles to include. | 要包含的消息角色。
        role_exclude (Optional[List[str]]): Message roles to exclude. | 要排除的消息角色。
        filter_meta (Optional[Callable[[Dict[str, Any]], bool]]): Function to filter messages by metadata. | 用于通过元数据
            过滤消息的函数。

    Returns:
        tuple[list[UserAndAssMsg], str]: List of messages and the cursor. | 消息列表和游标。
    """
    pass

aget_msg_by_cursor abstractmethod async

aget_msg_by_cursor(count: int = 10, cursor: Optional[str] = None, type_include: Optional[list[str]] = None, type_exclude: Optional[list[str]] = None, role_include: Optional[list[str]] = None, role_exclude: Optional[list[str]] = None, filter_meta: Optional[Callable[[Attributes], bool]] = None) -> tuple[list[UserAndAssMsg], str]

Get recent messages from the buffer store asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
187
188
189
190
191
192
193
194
195
196
197
198
199
@abstractmethod
async def aget_msg_by_cursor(
    self,
    count: int = 10,
    cursor: Optional[str] = None,
    type_include: Optional[list[str]] = None,
    type_exclude: Optional[list[str]] = None,
    role_include: Optional[list[str]] = None,
    role_exclude: Optional[list[str]] = None,
    filter_meta: Optional[Callable[[Attributes], bool]] = None,
) -> tuple[list[UserAndAssMsg], str]:
    """Get recent messages from the buffer store asynchronously."""
    ...

get_msgs_forward abstractmethod

get_msgs_forward(msg: UserAndAssMsg) -> Generator[UserAndAssMsg, None, None]

一个查询指定消息之后的消息的生成器

Parameters:

Name Type Description Default
msg UserAndAssMsg

The message to query.

required

Returns:

Type Description
None

Generator[UserAndAssMsg, None, None]: A generator that yields messages.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
201
202
203
204
205
206
207
208
209
210
211
212
213
214
@abstractmethod
def get_msgs_forward(self, msg: UserAndAssMsg) -> Generator[UserAndAssMsg, None, None]:
    """
    一个查询指定消息之后的消息的生成器

    Args:
        msg (UserAndAssMsg): The message to query.

    Returns:
        Generator[UserAndAssMsg, None, None]: A generator that yields messages.
    """
    if False:
        yield msg
    ...

aget_msgs_forward abstractmethod async

aget_msgs_forward(msg: UserAndAssMsg) -> AsyncGenerator[UserAndAssMsg, None]

Get messages forward from the given message asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
216
217
218
219
220
221
@abstractmethod
async def aget_msgs_forward(self, msg: UserAndAssMsg) -> AsyncGenerator[UserAndAssMsg, None]:
    """Get messages forward from the given message asynchronously."""
    if False:
        yield msg
    ...

get_msgs_backward abstractmethod

get_msgs_backward(msg: UserAndAssMsg) -> Generator[UserAndAssMsg, None, None]

一个查询指定消息之前的消息的生成器

Parameters:

Name Type Description Default
msg UserAndAssMsg

The message to query.

required

Returns:

Type Description
None

Generator[UserAndAssMsg, None, None]: A generator that yields messages.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
223
224
225
226
227
228
229
230
231
232
233
234
235
236
@abstractmethod
def get_msgs_backward(self, msg: UserAndAssMsg) -> Generator[UserAndAssMsg, None, None]:
    """
    一个查询指定消息之前的消息的生成器

    Args:
        msg (UserAndAssMsg): The message to query.

    Returns:
        Generator[UserAndAssMsg, None, None]: A generator that yields messages.
    """
    if False:
        yield msg
    ...

aget_msgs_backward abstractmethod async

aget_msgs_backward(msg: UserAndAssMsg) -> AsyncGenerator[UserAndAssMsg, None]

Get messages backward from the given message asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
238
239
240
241
242
243
@abstractmethod
async def aget_msgs_backward(self, msg: UserAndAssMsg) -> AsyncGenerator[UserAndAssMsg, None]:
    """Get messages backward from the given message asynchronously."""
    if False:
        yield msg
    ...

clear abstractmethod

clear() -> None

Clear the buffer store.

清空缓存存储器

Returns:

Type Description
None

None

Source code in tfrobot/brain/memory/store/buffer_store/base.py
245
246
247
248
249
250
251
252
253
254
255
@abstractmethod
def clear(self) -> None:
    """
    Clear the buffer store.

    清空缓存存储器

    Returns:
        None
    """
    pass

aclear abstractmethod async

aclear() -> None

Clear the buffer store asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
257
258
259
260
@abstractmethod
async def aclear(self) -> None:
    """Clear the buffer store asynchronously."""
    ...

edit_msg abstractmethod

edit_msg(index: int, msg: UserAndAssMsg) -> None

Edit a message in the buffer store.

编辑缓存存储器中的消息

Parameters:

Name Type Description Default
index int

The index of the message to edit.

要编辑的消息的索引

required
msg UserAndAssMsg

The new message.

新的消息

required

Returns:

Type Description
None

None

Source code in tfrobot/brain/memory/store/buffer_store/base.py
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
@abstractmethod
def edit_msg(self, index: int, msg: UserAndAssMsg) -> None:
    """
    Edit a message in the buffer store.

    编辑缓存存储器中的消息

    Args:
        index (int): The index of the message to edit.

            要编辑的消息的索引
        msg (UserAndAssMsg): The new message.

            新的消息

    Returns:
        None
    """
    pass

aedit_msg abstractmethod async

aedit_msg(index: int, msg: UserAndAssMsg) -> None

Edit a message in the buffer store asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
282
283
284
285
@abstractmethod
async def aedit_msg(self, index: int, msg: UserAndAssMsg) -> None:
    """Edit a message in the buffer store asynchronously."""
    ...

delete_msg abstractmethod

delete_msg(index: int) -> None

Delete a message in the buffer store.

删除缓存存储器中的消息

Parameters:

Name Type Description Default
index int

The index of the message to delete.

要删除的消息的索引

required

Returns:

Type Description
None

None

Source code in tfrobot/brain/memory/store/buffer_store/base.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
@abstractmethod
def delete_msg(self, index: int) -> None:
    """
    Delete a message in the buffer store.

    删除缓存存储器中的消息

    Args:
        index (int): The index of the message to delete.

            要删除的消息的索引

    Returns:
        None
    """
    pass

adelete_msg abstractmethod async

adelete_msg(index: int) -> None

Delete a message in the buffer store asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
304
305
306
307
@abstractmethod
async def adelete_msg(self, index: int) -> None:
    """Delete a message in the buffer store asynchronously."""
    ...

BaseConversationManager

Bases: TFBaseModel, ABC

Base class for conversation manager.

get_platforms abstractmethod

get_platforms() -> Sequence[tuple[PLATFORM_ID, str]] | None

获取当前可用的Platforms列表

Returns:

Type Description
Sequence[tuple[PLATFORM_ID, str]] | None

Sequence[tuple[PLATFORM_ID, str]] | None: 一个由PlatformId与PlatformURI组成的元组列表,如果返回None,表示不支持Platform

Source code in tfrobot/brain/memory/store/buffer_store/base.py
368
369
370
371
372
373
374
375
376
@abstractmethod
def get_platforms(self) -> Sequence[tuple[PLATFORM_ID, str]] | None:
    """
    获取当前可用的Platforms列表

    Returns:
        Sequence[tuple[PLATFORM_ID, str]] | None: 一个由PlatformId与PlatformURI组成的元组列表,如果返回None,表示不支持Platform
    """
    ...

aget_platforms abstractmethod async

aget_platforms() -> Sequence[tuple[PLATFORM_ID, str]] | None

获取当前可用的Platforms列表

Returns:

Type Description
Sequence[tuple[PLATFORM_ID, str]] | None

Sequence[tuple[PLATFORM_ID, str]] | None: 一个由PlatformId与PlatformURI组成的元组列表,如果返回None,表示不支持Platform

Source code in tfrobot/brain/memory/store/buffer_store/base.py
378
379
380
381
382
383
384
385
386
@abstractmethod
async def aget_platforms(self) -> Sequence[tuple[PLATFORM_ID, str]] | None:
    """
    获取当前可用的Platforms列表

    Returns:
        Sequence[tuple[PLATFORM_ID, str]] | None: 一个由PlatformId与PlatformURI组成的元组列表,如果返回None,表示不支持Platform
    """
    ...

add_platform abstractmethod

add_platform(platform_name: str) -> PLATFORM_ID

添加Platform,返回值为PlatformID | Add platform and return platform ID

Parameters:

Name Type Description Default
platform_name str

Platform名称 | Platform name

required

Returns:

Name Type Description
PLATFORM_ID PLATFORM_ID

PlatformID

Source code in tfrobot/brain/memory/store/buffer_store/base.py
388
389
390
391
392
393
394
395
396
397
398
399
@abstractmethod
def add_platform(self, platform_name: str) -> PLATFORM_ID:
    """
    添加Platform,返回值为PlatformID | Add platform and return platform ID

    Args:
        platform_name (str): Platform名称 | Platform name

    Returns:
        PLATFORM_ID: PlatformID
    """
    ...

aadd_platform abstractmethod async

aadd_platform(platform_name: str) -> PLATFORM_ID

添加Platform,返回值为PlatformID | Add platform and return platform ID

Parameters:

Name Type Description Default
platform_name str

Platform名称 | Platform name

required

Returns:

Name Type Description
PLATFORM_ID PLATFORM_ID

PlatformID

Source code in tfrobot/brain/memory/store/buffer_store/base.py
401
402
403
404
405
406
407
408
409
410
411
412
@abstractmethod
async def aadd_platform(self, platform_name: str) -> PLATFORM_ID:
    """
    添加Platform,返回值为PlatformID | Add platform and return platform ID

    Args:
        platform_name (str): Platform名称 | Platform name

    Returns:
        PLATFORM_ID: PlatformID
    """
    ...

update_platform abstractmethod

update_platform(platform_id: PLATFORM_ID, platform_name: str) -> None

更新Platform名称 | Update platform name

Parameters:

Name Type Description Default
platform_id PLATFORM_ID

Platform的ID | Platform ID

required
platform_name str

新的Platform名称 | New platform name

required
Source code in tfrobot/brain/memory/store/buffer_store/base.py
414
415
416
417
418
419
420
421
422
423
@abstractmethod
def update_platform(self, platform_id: PLATFORM_ID, platform_name: str) -> None:
    """
    更新Platform名称 | Update platform name

    Args:
        platform_id (PLATFORM_ID): Platform的ID | Platform ID
        platform_name (str): 新的Platform名称 | New platform name
    """
    ...

aupdate_platform abstractmethod async

aupdate_platform(platform_id: PLATFORM_ID, platform_name: str) -> None

更新Platform名称 | Update platform name

Parameters:

Name Type Description Default
platform_id PLATFORM_ID

Platform的ID | Platform ID

required
platform_name str

新的Platform名称 | New platform name

required
Source code in tfrobot/brain/memory/store/buffer_store/base.py
425
426
427
428
429
430
431
432
433
434
@abstractmethod
async def aupdate_platform(self, platform_id: PLATFORM_ID, platform_name: str) -> None:
    """
    更新Platform名称 | Update platform name

    Args:
        platform_id (PLATFORM_ID): Platform的ID | Platform ID
        platform_name (str): 新的Platform名称 | New platform name
    """
    ...

delete_platform abstractmethod

delete_platform(platform_id: PLATFORM_ID) -> None

删除Platform | Delete platform

Parameters:

Name Type Description Default
platform_id PLATFORM_ID

要删除的Platform的ID | Platform ID to delete

required
Source code in tfrobot/brain/memory/store/buffer_store/base.py
436
437
438
439
440
441
442
443
444
@abstractmethod
def delete_platform(self, platform_id: PLATFORM_ID) -> None:
    """
    删除Platform | Delete platform

    Args:
        platform_id (PLATFORM_ID): 要删除的Platform的ID | Platform ID to delete
    """
    ...

adelete_platform abstractmethod async

adelete_platform(platform_id: PLATFORM_ID) -> None

删除Platform | Delete platform

Parameters:

Name Type Description Default
platform_id PLATFORM_ID

要删除的Platform的ID | Platform ID to delete

required
Source code in tfrobot/brain/memory/store/buffer_store/base.py
446
447
448
449
450
451
452
453
454
@abstractmethod
async def adelete_platform(self, platform_id: PLATFORM_ID) -> None:
    """
    删除Platform | Delete platform

    Args:
        platform_id (PLATFORM_ID): 要删除的Platform的ID | Platform ID to delete
    """
    ...

get_conversations abstractmethod

get_conversations(cursor: Optional[str] = None, count: Optional[int] = None, platform_id: Optional[PLATFORM_ID] = None) -> tuple[list[tuple[CONVERSATION_KEY, str]], str]

Get all conversations from memory.

从记忆中获取所有对话。

因为会话排序随时有可能打乱,因此每次请求的时候指定一个初始位置是很有必要的。如此一来,前台可以获取到最新并且没有重复的对话。这里的最佳实践本来应该 使用游标,比如使用 update_timestamp * 1000 + id % 1000 生成游标。但我们系统并非专业的会话管理系统,其数据压力和数据量都不会过大,因此这里 不单独维护游标字段,而是直接让前台动态使用当前最后一个对话的ID作为游标。这样可以保证前台获取到的对话是最新的,且不会重复。如果有问题,刷新页面即可解决。

count支持负值,表示从后向前取数。

Parameters:

Name Type Description Default
cursor int | str | None

The id/index of the conversation to start from. | 要从哪个对话开始。

None
count Optional[int]

The number of conversations to get. | 要获取的对话数量。

None
platform_id Optional[int]

The platform id of the conversation. | 对话的平台ID。

None

Returns:

Type Description
tuple[list[tuple[CONVERSATION_KEY, str]], str]

tuple[list[tuple[CONVERSATION_KEY, str]], str]: The conversations and the cursor. | 对话列表和游标。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
@abstractmethod
def get_conversations(
    self, cursor: Optional[str] = None, count: Optional[int] = None, platform_id: Optional[PLATFORM_ID] = None
) -> tuple[list[tuple[CONVERSATION_KEY, str]], str]:
    """
    Get all conversations from memory.

    从记忆中获取所有对话。

    因为会话排序随时有可能打乱,因此每次请求的时候指定一个初始位置是很有必要的。如此一来,前台可以获取到最新并且没有重复的对话。这里的最佳实践本来应该
    使用游标,比如使用 update_timestamp * 1000 + id % 1000 生成游标。但我们系统并非专业的会话管理系统,其数据压力和数据量都不会过大,因此这里
    不单独维护游标字段,而是直接让前台动态使用当前最后一个对话的ID作为游标。这样可以保证前台获取到的对话是最新的,且不会重复。如果有问题,刷新页面即可解决。

    count支持负值,表示从后向前取数。

    Args:
        cursor (int | str | None): The id/index of the conversation to start from. | 要从哪个对话开始。
        count (Optional[int]): The number of conversations to get. | 要获取的对话数量。
        platform_id (Optional[int]): The platform id of the conversation. | 对话的平台ID。

    Returns:
        tuple[list[tuple[CONVERSATION_KEY, str]], str]: The conversations and the cursor. | 对话列表和游标。
    """
    ...

aget_conversations abstractmethod async

aget_conversations(cursor: Optional[str] = None, count: Optional[int] = None, platform_id: Optional[PLATFORM_ID] = None) -> tuple[list[tuple[CONVERSATION_KEY, str]], str]

Get all conversations from memory asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
481
482
483
484
485
486
@abstractmethod
async def aget_conversations(
    self, cursor: Optional[str] = None, count: Optional[int] = None, platform_id: Optional[PLATFORM_ID] = None
) -> tuple[list[tuple[CONVERSATION_KEY, str]], str]:
    """Get all conversations from memory asynchronously."""
    ...

get_conversation abstractmethod

get_conversation(conversation_id: CONVERSATION_KEY) -> Optional[BaseBufferStore]

Get a conversation from memory.

从记忆中获取一段对话。

注意页码可以是负数,表示从后向前取第几页。

Parameters:

Name Type Description Default
conversation_id int | str

The id/index of the conversation. | 对话的索引。

required

Returns:

Type Description
Optional[BaseBufferStore]

Optional[BaseBufferStore]: The conversation. | 对话。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
@abstractmethod
def get_conversation(self, conversation_id: CONVERSATION_KEY) -> Optional[BaseBufferStore]:
    """
    Get a conversation from memory.

    从记忆中获取一段对话。

    注意页码可以是负数,表示从后向前取第几页。

    Args:
        conversation_id (int | str): The id/index of the conversation. | 对话的索引。

    Returns:
        Optional[BaseBufferStore]: The conversation. | 对话。
    """
    ...

aget_conversation abstractmethod async

aget_conversation(conversation_id: CONVERSATION_KEY) -> Optional[BaseBufferStore]

Get a conversation from memory asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
505
506
507
508
@abstractmethod
async def aget_conversation(self, conversation_id: CONVERSATION_KEY) -> Optional[BaseBufferStore]:
    """Get a conversation from memory asynchronously."""
    ...

get_conversation_by_msg abstractmethod

get_conversation_by_msg(msg: UserAndAssMsg) -> Optional[BaseBufferStore]

Get the conversation that contains the message.

Parameters:

Name Type Description Default
msg UserAndAssMsg

The message to search. | 要搜索的消息。

required

Returns:

Type Description
Optional[BaseBufferStore]

Optional[BaseBufferStore]: The conversation that contains the message. | 包含消息的对话。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
510
511
512
513
514
515
516
517
518
519
520
521
@abstractmethod
def get_conversation_by_msg(self, msg: UserAndAssMsg) -> Optional[BaseBufferStore]:
    """
    Get the conversation that contains the message.

    Args:
        msg (UserAndAssMsg): The message to search. | 要搜索的消息。

    Returns:
        Optional[BaseBufferStore]: The conversation that contains the message. | 包含消息的对话。
    """
    ...

aget_conversation_by_msg abstractmethod async

aget_conversation_by_msg(msg: UserAndAssMsg) -> Optional[BaseBufferStore]

Get a conversation that contains the message asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
523
524
525
526
@abstractmethod
async def aget_conversation_by_msg(self, msg: UserAndAssMsg) -> Optional[BaseBufferStore]:
    """Get a conversation that contains the message asynchronously."""
    ...

get_conversation_messages

get_conversation_messages(conversation_id: CONVERSATION_KEY, page: Annotated[int, Ge(1)], size: int, type_include: Optional[list[str]] = None, type_exclude: Optional[list[str]] = None, role_include: Optional[list[str]] = None, role_exclude: Optional[list[str]] = None, filter_meta: Optional[Callable[[Attributes], bool]] = None) -> list[UserAndAssMsg]

Get messages from a conversation.

Parameters:

Name Type Description Default
conversation_id int | str

The id/index of the conversation. | 对话的索引。

required
page int

The page number. | 页码。

required
size int

The size of the page. | 页的大小。

required
type_include Optional[list[str]]

The message types to include. | 要包含的消息类型。

None
type_exclude Optional[list[str]]

The message types to exclude. | 要排除的消息类型。

None
role_include Optional[list[str]]

The message roles to include. | 要包含的消息角色。

None
role_exclude Optional[list[str]]

The message roles to exclude. | 要排除的消息角色。

None
filter_meta Optional[Callable[[Attributes], bool]]

The function to filter messages by metadata. | 用于根据 元数据过滤消息的函数。

None

Returns:

Type Description
list[UserAndAssMsg]

list[UserAndAssMsg]: The messages from the conversation. | 对话中的消息。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
def get_conversation_messages(
    self,
    conversation_id: CONVERSATION_KEY,
    page: Annotated[int, annotated_types.Ge(1)],
    size: int,
    type_include: Optional[list[str]] = None,
    type_exclude: Optional[list[str]] = None,
    role_include: Optional[list[str]] = None,
    role_exclude: Optional[list[str]] = None,
    filter_meta: Optional[Callable[[Attributes], bool]] = None,
) -> list[UserAndAssMsg]:
    """
    Get messages from a conversation.

    Args:
        conversation_id (int | str): The id/index of the conversation. | 对话的索引。
        page (int): The page number. | 页码。
        size (int): The size of the page. | 页的大小。
        type_include (Optional[list[str]]): The message types to include. | 要包含的消息类型。
        type_exclude (Optional[list[str]]): The message types to exclude. | 要排除的消息类型。
        role_include (Optional[list[str]]): The message roles to include. | 要包含的消息角色。
        role_exclude (Optional[list[str]]): The message roles to exclude. | 要排除的消息角色。
        filter_meta (Optional[Callable[[Attributes], bool]]): The function to filter messages by metadata. | 用于根据
            元数据过滤消息的函数。

    Returns:
        list[UserAndAssMsg]: The messages from the conversation. | 对话中的消息。
    """
    conversation = self.get_conversation(conversation_id)
    if not conversation:
        raise ValueError(f"Conversation {conversation_id} not found.")
    return conversation.get_msg(
        count=[page, size],
        type_include=type_include,
        type_exclude=type_exclude,
        role_include=role_include,
        role_exclude=role_exclude,
        filter_meta=filter_meta,
    )

aget_conversation_messages async

aget_conversation_messages(conversation_id: CONVERSATION_KEY, page: Annotated[int, Ge(1)], size: int, type_include: Optional[list[str]] = None, type_exclude: Optional[list[str]] = None, role_include: Optional[list[str]] = None, role_exclude: Optional[list[str]] = None, filter_meta: Optional[Callable[[Attributes], bool]] = None) -> list[UserAndAssMsg]

Get messages from a conversation.

Parameters:

Name Type Description Default
conversation_id int | str

The id/index of the conversation. | 对话的索引。

required
page int

The page number. | 页码。

required
size int

The size of the page. | 页的大小。

required
type_include Optional[list[str]]

The message types to include. | 要包含的消息类型。

None
type_exclude Optional[list[str]]

The message types to exclude. | 要排除的消息类型。

None
role_include Optional[list[str]]

The message roles to include. | 要包含的消息角色。

None
role_exclude Optional[list[str]]

The message roles to exclude. | 要排除的消息角色。

None
filter_meta Optional[Callable[[Attributes], bool]]

The function to filter messages by metadata. | 用于根据 元数据过滤消息的函数。

None

Returns:

Type Description
list[UserAndAssMsg]

list[UserAndAssMsg]: The messages from the conversation. | 对话中的消息。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
async def aget_conversation_messages(
    self,
    conversation_id: CONVERSATION_KEY,
    page: Annotated[int, annotated_types.Ge(1)],
    size: int,
    type_include: Optional[list[str]] = None,
    type_exclude: Optional[list[str]] = None,
    role_include: Optional[list[str]] = None,
    role_exclude: Optional[list[str]] = None,
    filter_meta: Optional[Callable[[Attributes], bool]] = None,
) -> list[UserAndAssMsg]:
    """
    Get messages from a conversation.

    Args:
        conversation_id (int | str): The id/index of the conversation. | 对话的索引。
        page (int): The page number. | 页码。
        size (int): The size of the page. | 页的大小。
        type_include (Optional[list[str]]): The message types to include. | 要包含的消息类型。
        type_exclude (Optional[list[str]]): The message types to exclude. | 要排除的消息类型。
        role_include (Optional[list[str]]): The message roles to include. | 要包含的消息角色。
        role_exclude (Optional[list[str]]): The message roles to exclude. | 要排除的消息角色。
        filter_meta (Optional[Callable[[Attributes], bool]]): The function to filter messages by metadata. | 用于根据
            元数据过滤消息的函数。

    Returns:
        list[UserAndAssMsg]: The messages from the conversation. | 对话中的消息。
    """
    conversation = await self.aget_conversation(conversation_id)
    if not conversation:
        raise ValueError(f"Conversation {conversation_id} not found.")
    return conversation.get_msg(
        count=[page, size],
        type_include=type_include,
        type_exclude=type_exclude,
        role_include=role_include,
        role_exclude=role_exclude,
        filter_meta=filter_meta,
    )

get_latest_msg_from_conversation

get_latest_msg_from_conversation(conversation_id: CONVERSATION_KEY) -> Optional[UserAndAssMsg]

Get the latest message from a conversation.

Parameters:

Name Type Description Default
conversation_id int | str

The id/index of the conversation. | 对话的索引。

required

Returns:

Type Description
Optional[UserAndAssMsg]

Optional[UserAndAssMsg]: The latest message from the conversation. | 对话中的最新消息。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
def get_latest_msg_from_conversation(self, conversation_id: CONVERSATION_KEY) -> Optional[UserAndAssMsg]:
    """
    Get the latest message from a conversation.

    Args:
        conversation_id (int | str): The id/index of the conversation. | 对话的索引。

    Returns:
        Optional[UserAndAssMsg]: The latest message from the conversation. | 对话中的最新消息。
    """
    conversation = self.get_conversation(conversation_id)
    if not conversation:
        raise ValueError(f"Conversation {conversation_id} not found.")
    last_msgs = conversation.get_msg(count=-1)
    return last_msgs[0] if last_msgs else None

aget_latest_msg_from_conversation async

aget_latest_msg_from_conversation(conversation_id: CONVERSATION_KEY) -> Optional[UserAndAssMsg]

Get the latest message from a conversation.

Parameters:

Name Type Description Default
conversation_id int | str

The id/index of the conversation. | 对话的索引。

required

Returns:

Type Description
Optional[UserAndAssMsg]

Optional[UserAndAssMsg]: The latest message from the conversation. | 对话中的最新消息。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
async def aget_latest_msg_from_conversation(self, conversation_id: CONVERSATION_KEY) -> Optional[UserAndAssMsg]:
    """
    Get the latest message from a conversation.

    Args:
        conversation_id (int | str): The id/index of the conversation. | 对话的索引。

    Returns:
        Optional[UserAndAssMsg]: The latest message from the conversation. | 对话中的最新消息。
    """
    conversation = await self.aget_conversation(conversation_id)
    if not conversation:
        raise ValueError(f"Conversation {conversation_id} not found.")
    last_msgs = conversation.get_msg(count=-1)
    return last_msgs[0] if last_msgs else None

get_conversation_messages_by_cursor

get_conversation_messages_by_cursor(conversation_id: CONVERSATION_KEY, count: int, cursor: Optional[str] = None, type_include: Optional[list[str]] = None, type_exclude: Optional[list[str]] = None, role_include: Optional[list[str]] = None, role_exclude: Optional[list[str]] = None, filter_meta: Optional[Callable[[Attributes], bool]] = None) -> tuple[list[UserAndAssMsg], str]

Get messages from a conversation.

Parameters:

Name Type Description Default
conversation_id int | str

The id/index of the conversation. | 对话的索引。

required
count int

The number of messages to get. | 要获取的消息数量。

required
cursor Optional[str]

The cursor of the message to start from. | 计数游标。

None
type_include Optional[list[str]]

The message types to include. | 要包含的消息类型。

None
type_exclude Optional[list[str]]

The message types to exclude. | 要排除的消息类型。

None
role_include Optional[list[str]]

The message roles to include. | 要包含的消息角色。

None
role_exclude Optional[list[str]]

The message roles to exclude. | 要排除的消息角色。

None
filter_meta Optional[Callable[[Attributes], bool]]

The function to filter messages by metadata. | 用于根据 元数据过滤消息的函数。

None

Returns:

Type Description
tuple[list[UserAndAssMsg], str]

tuple[list[UserAndAssMsg], str]: The messages from the conversation and the cursor. | 对话中的消息和游标。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
def get_conversation_messages_by_cursor(
    self,
    conversation_id: CONVERSATION_KEY,
    count: int,
    cursor: Optional[str] = None,
    type_include: Optional[list[str]] = None,
    type_exclude: Optional[list[str]] = None,
    role_include: Optional[list[str]] = None,
    role_exclude: Optional[list[str]] = None,
    filter_meta: Optional[Callable[[Attributes], bool]] = None,
) -> tuple[list[UserAndAssMsg], str]:
    """
    Get messages from a conversation.

    Args:
        conversation_id (int | str): The id/index of the conversation. | 对话的索引。
        count (int): The number of messages to get. | 要获取的消息数量。
        cursor (Optional[str]): The cursor of the message to start from. | 计数游标。
        type_include (Optional[list[str]]): The message types to include. | 要包含的消息类型。
        type_exclude (Optional[list[str]]): The message types to exclude. | 要排除的消息类型。
        role_include (Optional[list[str]]): The message roles to include. | 要包含的消息角色。
        role_exclude (Optional[list[str]]): The message roles to exclude. | 要排除的消息角色。
        filter_meta (Optional[Callable[[Attributes], bool]]): The function to filter messages by metadata. | 用于根据
            元数据过滤消息的函数。

    Returns:
        tuple[list[UserAndAssMsg], str]: The messages from the conversation and the cursor. | 对话中的消息和游标。
    """
    conversation = self.get_conversation(conversation_id)
    if not conversation:
        raise ValueError(f"Conversation {conversation_id} not found.")
    return conversation.get_msg_by_cursor(
        count=count,
        cursor=cursor,
        type_include=type_include,
        type_exclude=type_exclude,
        role_include=role_include,
        role_exclude=role_exclude,
        filter_meta=filter_meta,
    )

aget_conversation_messages_by_cursor async

aget_conversation_messages_by_cursor(conversation_id: CONVERSATION_KEY, count: int, cursor: Optional[str] = None, type_include: Optional[list[str]] = None, type_exclude: Optional[list[str]] = None, role_include: Optional[list[str]] = None, role_exclude: Optional[list[str]] = None, filter_meta: Optional[Callable[[Attributes], bool]] = None) -> tuple[list[UserAndAssMsg], str]

Get messages from a conversation.

Parameters:

Name Type Description Default
conversation_id int | str

The id/index of the conversation. | 对话的索引。

required
count int

The number of messages to get. | 要获取的消息数量。

required
cursor Optional[str]

The cursor of the message to start from. | 计数游标。

None
type_include Optional[list[str]]

The message types to include. | 要包含的消息类型。

None
type_exclude Optional[list[str]]

The message types to exclude. | 要排除的消息类型。

None
role_include Optional[list[str]]

The message roles to include. | 要包含的消息角色。

None
role_exclude Optional[list[str]]

The message roles to exclude. | 要排除的消息角色。

None
filter_meta Optional[Callable[[Attributes], bool]]

The function to filter messages by metadata. | 用于根据 元数据过滤消息的函数。

None

Returns:

Type Description
tuple[list[UserAndAssMsg], str]

tuple[list[UserAndAssMsg], str]: The messages from the conversation and the cursor. | 对话中的消息和游标。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
async def aget_conversation_messages_by_cursor(
    self,
    conversation_id: CONVERSATION_KEY,
    count: int,
    cursor: Optional[str] = None,
    type_include: Optional[list[str]] = None,
    type_exclude: Optional[list[str]] = None,
    role_include: Optional[list[str]] = None,
    role_exclude: Optional[list[str]] = None,
    filter_meta: Optional[Callable[[Attributes], bool]] = None,
) -> tuple[list[UserAndAssMsg], str]:
    """
    Get messages from a conversation.

    Args:
        conversation_id (int | str): The id/index of the conversation. | 对话的索引。
        count (int): The number of messages to get. | 要获取的消息数量。
        cursor (Optional[str]): The cursor of the message to start from. | 计数游标。
        type_include (Optional[list[str]]): The message types to include. | 要包含的消息类型。
        type_exclude (Optional[list[str]]): The message types to exclude. | 要排除的消息类型。
        role_include (Optional[list[str]]): The message roles to include. | 要包含的消息角色。
        role_exclude (Optional[list[str]]): The message roles to exclude. | 要排除的消息角色。
        filter_meta (Optional[Callable[[Attributes], bool]]): The function to filter messages by metadata. | 用于根据
            元数据过滤消息的函数。

    Returns:
        tuple[list[UserAndAssMsg], str]: The messages from the conversation and the cursor. | 对话中的消息和游标。
    """
    conversation = await self.aget_conversation(conversation_id)
    if not conversation:
        raise ValueError(f"Conversation {conversation_id} not found.")
    return await conversation.aget_msg_by_cursor(
        count=count,
        cursor=cursor,
        type_include=type_include,
        type_exclude=type_exclude,
        role_include=role_include,
        role_exclude=role_exclude,
        filter_meta=filter_meta,
    )

add_conversation abstractmethod

add_conversation(name: str, platform_id: Optional[PLATFORM_ID] = None) -> CONVERSATION_KEY

Add a conversation to memory.

是否需要传递平台ID,取决于具体的实现。建议尽可能都传。

Parameters:

Name Type Description Default
name str

The name of the conversation. | 对话的名称。

required
platform_id Optional[int]

The platform id of the conversation. | 对话的平台ID。

None

Returns:

Type Description
CONVERSATION_KEY

int | str: The id of the added conversation. | 添加的对话的ID。

Source code in tfrobot/brain/memory/store/buffer_store/base.py
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
@abstractmethod
def add_conversation(self, name: str, platform_id: Optional[PLATFORM_ID] = None) -> CONVERSATION_KEY:
    """
    Add a conversation to memory.

    是否需要传递平台ID,取决于具体的实现。建议尽可能都传。

    Args:
        name (str): The name of the conversation. | 对话的名称。
        platform_id (Optional[int]): The platform id of the conversation. | 对话的平台ID。

    Returns:
        int | str: The id of the added conversation. | 添加的对话的ID。
    """
    ...

aadd_conversation abstractmethod async

aadd_conversation(name: str, platform_id: Optional[PLATFORM_ID] = None) -> CONVERSATION_KEY

Add a conversation to memory asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
738
739
740
741
@abstractmethod
async def aadd_conversation(self, name: str, platform_id: Optional[PLATFORM_ID] = None) -> CONVERSATION_KEY:
    """Add a conversation to memory asynchronously."""
    ...

update_conversation abstractmethod

update_conversation(conversation_id: CONVERSATION_KEY, name: str) -> None

Update a conversation in memory.

Parameters:

Name Type Description Default
conversation_id int | str

The id/index of the conversation. | 对话的索引。

required
name str

The name of the conversation. | 对话的名称。

required
Source code in tfrobot/brain/memory/store/buffer_store/base.py
743
744
745
746
747
748
749
750
751
752
@abstractmethod
def update_conversation(self, conversation_id: CONVERSATION_KEY, name: str) -> None:
    """
    Update a conversation in memory.

    Args:
        conversation_id (int | str): The id/index of the conversation. | 对话的索引。
        name (str): The name of the conversation. | 对话的名称。
    """
    ...

aupdate_conversation abstractmethod async

aupdate_conversation(conversation_id: CONVERSATION_KEY, name: str) -> None

Update a conversation in memory asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
754
755
756
757
@abstractmethod
async def aupdate_conversation(self, conversation_id: CONVERSATION_KEY, name: str) -> None:
    """Update a conversation in memory asynchronously."""
    ...

delete_conversation abstractmethod

delete_conversation(conversation_id: CONVERSATION_KEY) -> None

Delete a conversation from memory.

Parameters:

Name Type Description Default
conversation_id int | str

The id/index of the conversation. | 对话的索引。

required
Source code in tfrobot/brain/memory/store/buffer_store/base.py
759
760
761
762
763
764
765
766
767
@abstractmethod
def delete_conversation(self, conversation_id: CONVERSATION_KEY) -> None:
    """
    Delete a conversation from memory.

    Args:
        conversation_id (int | str): The id/index of the conversation. | 对话的索引。
    """
    ...

adelete_conversation abstractmethod async

adelete_conversation(conversation_id: CONVERSATION_KEY) -> None

Delete a conversation from memory asynchronously.

Source code in tfrobot/brain/memory/store/buffer_store/base.py
769
770
771
772
@abstractmethod
async def adelete_conversation(self, conversation_id: CONVERSATION_KEY) -> None:
    """Delete a conversation from memory asynchronously."""
    ...