forked from i-am-bee/beeai-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_memory.py
More file actions
141 lines (116 loc) · 4.96 KB
/
token_memory.py
File metadata and controls
141 lines (116 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
135
136
137
138
139
140
141
# Copyright 2025 © BeeAI a Series of LF Projects, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from math import ceil
from typing import Any
from beeai_framework.backend.message import AnyMessage
from beeai_framework.memory.base_memory import BaseMemory
def simple_estimate(msg: AnyMessage) -> int:
return ceil(len(msg.text) / 4)
def simple_tokenize(msgs: list[AnyMessage]) -> int:
return sum(map(simple_estimate, msgs))
class TokenMemory(BaseMemory):
"""Memory implementation that respects token limits."""
def __init__(
self,
llm: Any,
max_tokens: int | None = None,
sync_threshold: float = 0.25,
capacity_threshold: float = 0.75,
handlers: dict[str, Any] | None = None,
) -> None:
self._messages: list[AnyMessage] = []
self.llm = llm
self._max_tokens = max_tokens
self._threshold = capacity_threshold
self._sync_threshold = sync_threshold
self._tokens_by_message: dict[str, Any] = {}
self._handlers = {
"tokenize": (handlers.get("tokenize", simple_tokenize) if handlers else simple_tokenize),
"estimate": (handlers.get("estimate", self._default_estimate) if handlers else self._default_estimate),
"removal_selector": (
handlers.get("removal_selector", lambda msgs: msgs[0]) if handlers else lambda msgs: msgs[0]
),
}
if not 0 <= self._threshold <= 1:
raise ValueError('"capacity_threshold" must be a number in range (0, 1)')
@staticmethod
def _default_estimate(msg: AnyMessage) -> int:
return int((len(msg.role) + len(msg.text)) / 4)
def _get_message_key(self, message: AnyMessage) -> str:
"""Generate a unique key for a message."""
return f"{message.role}:{message.text}"
@property
def messages(self) -> list[AnyMessage]:
return self._messages
@property
def handlers(self) -> dict[str, Any]:
return self._handlers
@property
def tokens_used(self) -> int:
return sum(info.get("tokens_count", 0) for info in self._tokens_by_message.values())
@property
def is_dirty(self) -> bool:
return any(info.get("dirty", True) for info in self._tokens_by_message.values())
async def sync(self) -> None:
"""Synchronize token counts with LLM."""
for msg in self._messages:
key = self._get_message_key(msg)
cache = self._tokens_by_message.get(key, {})
if cache.get("dirty", True):
try:
result = self.handlers["tokenize"]([msg])
self._tokens_by_message[key] = {
"tokens_count": result,
"dirty": False,
}
except Exception as e:
print(f"Error tokenizing message: {e!s}")
self._tokens_by_message[key] = {
"tokens_count": self.handlers["estimate"](msg),
"dirty": True,
}
async def add(self, message: AnyMessage, index: int | None = None) -> None:
index = len(self._messages) if index is None else max(0, min(index, len(self._messages)))
self._messages.insert(index, message)
key = self._get_message_key(message)
estimated_tokens = self.handlers["estimate"](message)
self._tokens_by_message[key] = {
"tokens_count": estimated_tokens,
"dirty": True,
}
dirty_count = sum(1 for info in self._tokens_by_message.values() if info.get("dirty", True))
if len(self._messages) > 0 and dirty_count / len(self._messages) >= self._sync_threshold:
await self.sync()
async def delete(self, message: AnyMessage) -> bool:
try:
key = self._get_message_key(message)
self._messages.remove(message)
self._tokens_by_message.pop(key, None)
return True
except ValueError:
return False
def reset(self) -> None:
self._messages.clear()
self._tokens_by_message.clear()
async def clone(self) -> "TokenMemory":
cloned = TokenMemory(
self.llm.clone(),
self._max_tokens,
self._sync_threshold,
self._threshold,
self._handlers.copy() if self._handlers else None,
)
cloned._messages = self._messages.copy()
cloned._tokens_by_message = self._tokens_by_message.copy()
return cloned