forked from i-am-bee/beeai-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummarize_memory.py
85 lines (64 loc) · 2.9 KB
/
summarize_memory.py
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
# Copyright 2025 IBM Corp.
#
# 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 collections.abc import Iterable
from beeai_framework.backend import Message, SystemMessage
from beeai_framework.backend.chat import ChatModel
from beeai_framework.backend.message import UserMessage
from beeai_framework.memory.base_memory import BaseMemory
class SummarizeMemory(BaseMemory):
"""Memory implementation that summarizes conversations."""
def __init__(self, model: ChatModel) -> None:
self._messages: list[Message] = []
self.model = model
@property
def messages(self) -> list[Message]:
return self._messages
async def add(self, message: Message, index: int | None = None) -> None:
"""Add a message and trigger summarization if needed."""
messages_to_summarize = [*self._messages, message]
summary = self._summarize_messages(messages_to_summarize)
self._messages = [SystemMessage(summary)]
async def add_many(self, messages: Iterable[Message], start: int | None = None) -> None:
"""Add multiple messages and summarize."""
messages_to_summarize = self._messages + list(messages)
summary = await self._summarize_messages(messages_to_summarize)
self._messages = [SystemMessage(summary)]
async def _summarize_messages(self, messages: list[Message]) -> str:
"""Summarize a list of messages using the LLM."""
if not messages:
return ""
prompt = UserMessage(
"""Summarize the following conversation. Be concise but include all key information.
Previous messages:
{}
Summary:""".format("\n".join([f"{msg.role}: {msg.text}" for msg in messages]))
)
response = await self.model.create(messages=[prompt])
return response.messages[0].get_texts()[0].text
async def delete(self, message: Message) -> bool:
"""Delete a message from memory."""
try:
self._messages.remove(message)
return True
except ValueError:
return False
def reset(self) -> None:
"""Clear all messages from memory."""
self._messages.clear()
def create_snapshot(self) -> dict:
"""Create a serializable snapshot of current state."""
return {"messages": self._messages.copy()}
def load_snapshot(self, state: dict) -> None:
"""Restore state from a snapshot."""
self._messages = state["messages"].copy()