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

[Enhance] Support pruning tool call messages in `ChatAgent` to save token budget (#2883)

Hesam Sheikh4f9bf4213fbe
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 0fca1aa4c..cd01a855b 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -375,6 +375,11 @@ class ChatAgent(BaseAgent):
pause_event (Optional[asyncio.Event]): Event to signal pause of the
agent's operation. When clear, the agent will pause its execution.
(default: :obj:`None`)
+ prune_tool_calls_from_memory (bool): Whether to clean tool
+ call messages from memory after response generation to save token
+ usage. When enabled, removes FUNCTION/TOOL role messages and
+ ASSISTANT messages with tool_calls after each step.
+ (default: :obj:`False`)
"""
def __init__(
@@ -411,6 +416,7 @@ class ChatAgent(BaseAgent):
tool_execution_timeout: Optional[float] = None,
mask_tool_output: bool = False,
pause_event: Optional[asyncio.Event] = None,
+ prune_tool_calls_from_memory: bool = False,
) -> None:
if isinstance(model, ModelManager):
self.model_backend = model
@@ -492,6 +498,7 @@ class ChatAgent(BaseAgent):
self._image_retry_count: Dict[str, int] = {}
# Store images to attach to next user message
self.pause_event = pause_event
+ self.prune_tool_calls_from_memory = prune_tool_calls_from_memory
def reset(self):
r"""Resets the :obj:`ChatAgent` to its initial state."""
@@ -1472,6 +1479,10 @@ class ChatAgent(BaseAgent):
self._record_final_output(response.output_messages)
+ # Clean tool call messages from memory after response generation
+ if self.prune_tool_calls_from_memory and tool_call_records:
+ self.memory.clean_tool_calls()
+
return self._convert_to_chatagent_response(
response,
tool_call_records,
@@ -1730,6 +1741,10 @@ class ChatAgent(BaseAgent):
self._record_final_output(response.output_messages)
+ # Clean tool call messages from memory after response generation
+ if self.prune_tool_calls_from_memory and tool_call_records:
+ self.memory.clean_tool_calls()
+
return self._convert_to_chatagent_response(
response,
tool_call_records,
@@ -3270,11 +3285,20 @@ class ChatAgent(BaseAgent):
return
# Start async streaming response
+ last_response = None
async for response in self._astream_response(
openai_messages, num_tokens, response_format
):
+ last_response = response
yield response
+ # Clean tool call messages from memory after response generation
+ if self.prune_tool_calls_from_memory and last_response:
+ # Extract tool_calls from the last response info
+ tool_calls = last_response.info.get("tool_calls", [])
+ if tool_calls:
+ self.memory.clean_tool_calls()
+
async def _astream_response(
self,
openai_messages: List[OpenAIMessage],
@@ -3799,6 +3823,7 @@ class ChatAgent(BaseAgent):
stop_event=self.stop_event,
tool_execution_timeout=self.tool_execution_timeout,
pause_event=self.pause_event,
+ prune_tool_calls_from_memory=self.prune_tool_calls_from_memory,
)
# Copy memory if requested
diff --git a/camel/memories/agent_memories.py b/camel/memories/agent_memories.py
index d379e4343..5c46cddc2 100644
--- a/camel/memories/agent_memories.py
+++ b/camel/memories/agent_memories.py
@@ -88,6 +88,44 @@ class ChatHistoryMemory(AgentMemory):
def clear(self) -> None:
self._chat_history_block.clear()
+ def clean_tool_calls(self) -> None:
+ r"""Removes tool call messages from memory.
+ This method removes all FUNCTION/TOOL role messages and any ASSISTANT
+ messages that contain tool_calls in their meta_dict to save token
+ usage.
+ """
+ from camel.types import OpenAIBackendRole
+
+ # Get all messages from storage
+ record_dicts = self._chat_history_block.storage.load()
+ if not record_dicts:
+ return
+
+ # Filter out tool-related messages
+ cleaned_records = []
+ for record in record_dicts:
+ role = record.get('role_at_backend')
+
+ # Skip FUNCTION messages
+ if role == OpenAIBackendRole.FUNCTION.value:
+ continue
+
+ # Skip TOOL messages
+ if role == OpenAIBackendRole.TOOL.value:
+ continue
+
+ # Skip ASSISTANT messages with tool_calls
+ if role == OpenAIBackendRole.ASSISTANT.value:
+ meta_dict = record.get('meta_dict', {})
+ if meta_dict and 'tool_calls' in meta_dict:
+ continue
+
+ # Keep all other messages
+ cleaned_records.append(record)
+
+ # Save the cleaned records back to storage
+ self._chat_history_block.storage.save(cleaned_records)
+
class VectorDBMemory(AgentMemory):
r"""An agent memory wrapper of :obj:`VectorDBBlock`. This memory queries
diff --git a/camel/memories/base.py b/camel/memories/base.py
index 140a72009..f9d4a0ad8 100644
--- a/camel/memories/base.py
+++ b/camel/memories/base.py
@@ -149,6 +149,14 @@ class AgentMemory(MemoryBlock, ABC):
"""
return self.get_context_creator().create_context(self.retrieve())
+ def clean_tool_calls(self) -> None:
+ r"""Removes tool call messages from memory.
+ This is an optional method that can be overridden by subclasses
+ to implement cleaning of tool-related messages. By default, it
+ does nothing, maintaining backward compatibility.
+ """
+ pass
+
def __repr__(self) -> str:
r"""Returns a string representation of the AgentMemory.
diff --git a/camel/storages/vectordb_storages/surreal.py b/camel/storages/vectordb_storages/surreal.py
index 905acbfd2..cd0d7f82a 100644
--- a/camel/storages/vectordb_storages/surreal.py
+++ b/camel/storages/vectordb_storages/surreal.py
@@ -157,8 +157,7 @@ class SurrealStorage(BaseVectorStorage):
return {"dim": dim, "count": count}
def _create_table(self):
- r"""Define and create the vector storage table with HNSW index.
- """
+ r"""Define and create the vector storage table with HNSW index."""
from surrealdb import Surreal # type: ignore[import-not-found]
with Surreal(self.url) as db: