Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: add warning (#1591)

Zoe Yan69e42812dc2b
diff --git a/camel/memories/agent_memories.py b/camel/memories/agent_memories.py
index 3e4bf6123..788b3fb61 100644
--- a/camel/memories/agent_memories.py
+++ b/camel/memories/agent_memories.py
@@ -12,6 +12,7 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+import warnings
from typing import List, Optional
from camel.memories.base import AgentMemory, BaseContextCreator
@@ -49,7 +50,17 @@ class ChatHistoryMemory(AgentMemory):
self._chat_history_block = ChatHistoryBlock(storage=storage)
def retrieve(self) -> List[ContextRecord]:
- return self._chat_history_block.retrieve(self._window_size)
+ records = self._chat_history_block.retrieve(self._window_size)
+ if self._window_size is not None and len(records) == self._window_size:
+ warnings.warn(
+ f"Chat history window size limit ({self._window_size}) "
+ f"reached. Some earlier messages will not be included in "
+ f"the context. Consider increasing window_size if you need "
+ f"a longer context.",
+ UserWarning,
+ stacklevel=2,
+ )
+ return records
def write_records(self, records: List[MemoryRecord]) -> None:
self._chat_history_block.write_records(records)
diff --git a/camel/memories/context_creators/score_based.py b/camel/memories/context_creators/score_based.py
index 9ccd7ccb8..669f128ae 100644
--- a/camel/memories/context_creators/score_based.py
+++ b/camel/memories/context_creators/score_based.py
@@ -15,11 +15,14 @@ from typing import List, Tuple
from pydantic import BaseModel
+from camel.logger import get_logger
from camel.memories.base import BaseContextCreator
from camel.memories.records import ContextRecord
from camel.messages import OpenAIMessage
from camel.utils import BaseTokenCounter
+logger = get_logger(__name__)
+
class _ContextUnit(BaseModel):
idx: int
@@ -103,6 +106,12 @@ class ScoreBasedContextCreator(BaseContextCreator):
if total_tokens <= self.token_limit:
return self._create_output(context_units)
+ # Log warning about token limit being exceeded
+ logger.warning(
+ f"Token limit reached ({total_tokens} > {self.token_limit}). "
+ f"Some messages will be pruned from memory to meet the limit."
+ )
+
# Sort by score
context_units = sorted(
context_units, key=lambda unit: unit.record.score