forked from agentscope-ai/ReMe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarizer.py
More file actions
92 lines (76 loc) · 2.93 KB
/
summarizer.py
File metadata and controls
92 lines (76 loc) · 2.93 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
"""Summarizer module for memory summarization operations."""
import datetime
import logging
from agentscope.agent import ReActAgent
from agentscope.formatter import FormatterBase
from agentscope.message import Msg
from agentscope.model import ChatModelBase
from agentscope.token import HuggingFaceTokenCounter
from agentscope.tool import Toolkit
from .memory_formatter import MemoryFormatter
from .file_io import FileIO
from ...core.op import BaseOp
logger = logging.getLogger(__name__)
class Summarizer(BaseOp):
"""Summarizer class for summarizing memory messages."""
def __init__(
self,
working_dir: str,
memory_dir: str,
memory_compact_threshold: int,
chat_model: ChatModelBase,
formatter: FormatterBase,
token_counter: HuggingFaceTokenCounter,
toolkit: Toolkit | None = None,
**kwargs,
):
super().__init__(**kwargs)
self.working_dir: str = working_dir
self.memory_dir: str = memory_dir
self.memory_compact_threshold: int = memory_compact_threshold
self.chat_model: ChatModelBase = chat_model
self.formatter: FormatterBase = formatter
self.as_token_counter: HuggingFaceTokenCounter = token_counter
if toolkit is not None:
self.toolkit: Toolkit = toolkit
else:
self.toolkit = Toolkit()
file_io = FileIO(working_dir=self.working_dir)
self.toolkit.register_tool_function(file_io.read)
self.toolkit.register_tool_function(file_io.write)
self.toolkit.register_tool_function(file_io.edit)
async def execute(self):
messages: list[Msg] = self.context.get("messages", [])
if not messages:
return ""
formatter = MemoryFormatter(
token_counter=self.as_token_counter,
memory_compact_threshold=self.memory_compact_threshold,
)
history_formatted_str: str = formatter.format(messages)
if not history_formatted_str:
logger.warning(f"No history to summarize. messages={messages}")
return ""
agent = ReActAgent(
name="reme_summarizer",
model=self.chat_model,
sys_prompt="You are a helpful assistant.",
formatter=self.formatter,
toolkit=self.toolkit,
)
user_message: str = f"<conversation>\n{history_formatted_str}\n</conversation>\n" + self.prompt_format(
"user_message",
date=datetime.datetime.now().strftime("%Y-%m-%d"),
working_dir=self.working_dir,
memory_dir=self.memory_dir,
)
summary_msg: Msg = await agent.reply(
Msg(
name="reme",
role="user",
content=user_message,
),
)
history_summary: str = summary_msg.get_text_content()
logger.info(f"Summarizer Result:\n{history_summary}")
return history_summary