Skip to content

Commit 1102515

Browse files
hesamsheikhHesam Sheikh HassaniWendong-Fan
authored
[feat] Markdown Agent Context Handling (#2934)
Co-authored-by: Hesam Sheikh Hassani <hesam@Hesams-MacBook-Pro.local> Co-authored-by: Wendong-Fan <133094783+Wendong-Fan@users.noreply.github.com>
1 parent bca70a2 commit 1102515

File tree

10 files changed

+1941
-13
lines changed

10 files changed

+1941
-13
lines changed

camel/agents/chat_agent.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
MemoryRecord,
6464
ScoreBasedContextCreator,
6565
)
66+
from camel.memories.blocks.chat_history_block import EmptyMemoryWarning
6667
from camel.messages import (
6768
BaseMessage,
6869
FunctionCallingMessage,
@@ -841,7 +842,12 @@ def _write_single_record(
841842
current_tokens = token_counter.count_tokens_from_messages(
842843
[message.to_openai_message(role)]
843844
)
844-
_, ctx_tokens = self.memory.get_context()
845+
import warnings
846+
847+
with warnings.catch_warnings():
848+
warnings.filterwarnings("ignore", category=EmptyMemoryWarning)
849+
_, ctx_tokens = self.memory.get_context()
850+
845851
remaining_budget = max(0, token_limit - ctx_tokens)
846852

847853
if current_tokens <= remaining_budget:
@@ -1035,6 +1041,7 @@ def clear_memory(self) -> None:
10351041
None
10361042
"""
10371043
self.memory.clear()
1044+
10381045
if self.system_message is not None:
10391046
self.update_memory(self.system_message, OpenAIBackendRole.SYSTEM)
10401047

camel/memories/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
VectorDBMemory,
1919
)
2020
from .base import AgentMemory, BaseContextCreator, MemoryBlock
21-
from .blocks.chat_history_block import ChatHistoryBlock
21+
from .blocks.chat_history_block import ChatHistoryBlock, EmptyMemoryWarning
2222
from .blocks.vectordb_block import VectorDBBlock
2323
from .context_creators.score_based import ScoreBasedContextCreator
2424
from .records import ContextRecord, MemoryRecord
@@ -35,4 +35,5 @@
3535
'ChatHistoryBlock',
3636
'VectorDBBlock',
3737
'LongtermAgentMemory',
38+
'EmptyMemoryWarning',
3839
]

camel/memories/agent_memories.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ def __init__(
5151
raise ValueError("`window_size` must be non-negative.")
5252
self._context_creator = context_creator
5353
self._window_size = window_size
54-
self._chat_history_block = ChatHistoryBlock(storage=storage)
54+
self._chat_history_block = ChatHistoryBlock(
55+
storage=storage,
56+
)
5557
self._agent_id = agent_id
5658

5759
@property

camel/memories/blocks/chat_history_block.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
2121
from camel.types import OpenAIBackendRole
2222

2323

24+
class EmptyMemoryWarning(UserWarning):
25+
"""Warning raised when attempting to access an empty memory.
26+
27+
This warning is raised when operations are performed on memory
28+
that contains no records. It can be safely caught and suppressed
29+
in contexts where empty memory is expected.
30+
"""
31+
32+
pass
33+
34+
2435
class ChatHistoryBlock(MemoryBlock):
2536
r"""An implementation of the :obj:`MemoryBlock` abstract base class for
2637
maintaining a record of chat histories.
@@ -39,7 +50,7 @@ class ChatHistoryBlock(MemoryBlock):
3950
last message is 1.0, and with each step taken backward, the score
4051
of the message is multiplied by the `keep_rate`. Higher `keep_rate`
4152
leads to high possibility to keep history messages during context
42-
creation.
53+
creation. (default: :obj:`0.9`)
4354
"""
4455

4556
def __init__(
@@ -70,7 +81,11 @@ def retrieve(
7081
"""
7182
record_dicts = self.storage.load()
7283
if len(record_dicts) == 0:
73-
warnings.warn("The `ChatHistoryMemory` is empty.")
84+
warnings.warn(
85+
"The `ChatHistoryMemory` is empty.",
86+
EmptyMemoryWarning,
87+
stacklevel=1,
88+
)
7489
return list()
7590

7691
if window_size is not None and window_size >= 0:

camel/toolkits/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
from .web_deploy_toolkit import WebDeployToolkit
8989
from .screenshot_toolkit import ScreenshotToolkit
9090
from .message_integration import ToolkitMessageIntegration
91+
from .context_summarizer_toolkit import ContextSummarizerToolkit
9192
from .notion_mcp_toolkit import NotionMCPToolkit
9293
from .vertex_ai_veo_toolkit import VertexAIVeoToolkit
9394
from .minimax_mcp_toolkit import MinimaxMCPToolkit
@@ -168,6 +169,7 @@
168169
'ScreenshotToolkit',
169170
'RegisteredAgentToolkit',
170171
'ToolkitMessageIntegration',
172+
'ContextSummarizerToolkit',
171173
'NotionMCPToolkit',
172174
'VertexAIVeoToolkit',
173175
'MinimaxMCPToolkit',

0 commit comments

Comments
 (0)