🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: add warning (#1591)
Zoe Yan69e42812dc2b
diff --git a/camel/memories/agent_memories.py b/camel/memories/agent_memories.pyindex 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 warningsfrom typing import List, Optionalfrom 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 recordsdef 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.pyindex 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, Tuplefrom pydantic import BaseModel+from camel.logger import get_loggerfrom camel.memories.base import BaseContextCreatorfrom camel.memories.records import ContextRecordfrom camel.messages import OpenAIMessagefrom 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 scorecontext_units = sorted(context_units, key=lambda unit: unit.record.score