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

fix:clean_tool_calls() provide invalid llm api request (#3360)

Wendong-Fan1331aa80acf8
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml
index b86d0b752..dbfc89390 100644
--- a/.github/ISSUE_TEMPLATE/bug_report.yml
+++ b/.github/ISSUE_TEMPLATE/bug_report.yml
@@ -26,7 +26,7 @@ body:
attributes:
label: What version of camel are you using?
description: Run command `python3 -c 'print(__import__("camel").__version__)'` in your shell and paste the output here.
- placeholder: E.g., 0.2.79a1
+ placeholder: E.g., 0.2.79a2
validations:
required: true
diff --git a/camel/__init__.py b/camel/__init__.py
index a4f8d7723..abd073218 100644
--- a/camel/__init__.py
+++ b/camel/__init__.py
@@ -14,7 +14,7 @@
from camel.logger import disable_logging, enable_logging, set_log_level
-__version__ = '0.2.79a1'
+__version__ = '0.2.79a2'
__all__ = [
'__version__',
diff --git a/camel/memories/agent_memories.py b/camel/memories/agent_memories.py
index 1789cec46..4fff306d6 100644
--- a/camel/memories/agent_memories.py
+++ b/camel/memories/agent_memories.py
@@ -118,15 +118,23 @@ class ChatHistoryMemory(AgentMemory):
indices_to_remove.append(i)
# Mark ASSISTANT messages with tool_calls for removal
elif role == OpenAIBackendRole.ASSISTANT.value:
- meta_dict = record.get('meta_dict', {})
- if meta_dict and 'tool_calls' in meta_dict:
+ message_dict = record.get('message', {})
+ # Check for tool_calls in message
+ has_tool_calls = 'tool_calls' in message_dict
+ is_func_calling = (
+ message_dict.get('__class__') == 'FunctionCallingMessage'
+ and 'args' in message_dict
+ )
+
+ if has_tool_calls or is_func_calling:
indices_to_remove.append(i)
# Remove records in-place
for i in reversed(indices_to_remove):
del record_dicts[i]
- # Save the modified records back to storage
+ # Clear storage and save the modified records back
+ self._chat_history_block.storage.clear()
self._chat_history_block.storage.save(record_dicts)
def pop_records(self, count: int) -> List[MemoryRecord]:
diff --git a/docs/conf.py b/docs/conf.py
index d22c14f2a..be582d646 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -27,7 +27,7 @@ sys.path.insert(0, os.path.abspath('..'))
project = 'CAMEL'
copyright = '2024, CAMEL-AI.org'
author = 'CAMEL-AI.org'
-release = '0.2.79a1'
+release = '0.2.79a2'
html_favicon = (
'https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'
diff --git a/pyproject.toml b/pyproject.toml
index 8170e088d..28da857b4 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "camel-ai"
-version = "0.2.79a1"
+version = "0.2.79a2"
description = "Communicative Agents for AI Society Study"
authors = [{ name = "CAMEL-AI.org" }]
requires-python = ">=3.10,<3.15"
diff --git a/test/memories/test_agent_memories.py b/test/memories/test_agent_memories.py
index 161ddc271..db330aa6e 100644
--- a/test/memories/test_agent_memories.py
+++ b/test/memories/test_agent_memories.py
@@ -19,11 +19,13 @@ import pytest
from camel.memories import (
BaseContextCreator,
ChatHistoryBlock,
+ ChatHistoryMemory,
LongtermAgentMemory,
MemoryRecord,
VectorDBBlock,
)
-from camel.messages import BaseMessage
+from camel.messages import BaseMessage, FunctionCallingMessage
+from camel.storages.key_value_storages import InMemoryKeyValueStorage
from camel.types import OpenAIBackendRole, RoleType
@@ -121,3 +123,201 @@ class TestLongtermAgentMemory:
memory.clear()
mock_chat_history_block.clear.assert_called_once()
mock_vector_db_block.clear.assert_called_once()
+
+
+class TestChatHistoryMemoryCleanToolCalls:
+ @pytest.fixture
+ def mock_context_creator(self):
+ creator = MagicMock(spec=BaseContextCreator)
+ creator.create_context.return_value = ([], 0)
+ return creator
+
+ def test_clean_tool_calls_removes_function_messages(
+ self, mock_context_creator
+ ):
+ r"""Test that clean_tool_calls removes FUNCTION role messages."""
+ storage = InMemoryKeyValueStorage()
+ memory = ChatHistoryMemory(mock_context_creator, storage=storage)
+
+ # Create records with FUNCTION messages
+ records = [
+ MemoryRecord(
+ message=BaseMessage("user", RoleType.USER, None, "Question"),
+ role_at_backend=OpenAIBackendRole.USER,
+ ),
+ MemoryRecord(
+ message=FunctionCallingMessage(
+ "assistant",
+ RoleType.ASSISTANT,
+ None,
+ "",
+ func_name="tool",
+ result="result",
+ ),
+ role_at_backend=OpenAIBackendRole.FUNCTION,
+ ),
+ ]
+ memory.write_records(records)
+
+ memory.clean_tool_calls()
+ remaining = memory.retrieve()
+
+ assert len(remaining) == 1
+ assert (
+ remaining[0].memory_record.role_at_backend
+ == OpenAIBackendRole.USER
+ )
+
+ def test_clean_tool_calls_removes_assistant_with_tool_calls(
+ self, mock_context_creator
+ ):
+ r"""Test that clean_tool_calls removes ASSISTANT messages with tool
+ calls.
+ """
+ storage = InMemoryKeyValueStorage()
+ memory = ChatHistoryMemory(mock_context_creator, storage=storage)
+
+ records = [
+ MemoryRecord(
+ message=BaseMessage("user", RoleType.USER, None, "Question"),
+ role_at_backend=OpenAIBackendRole.USER,
+ ),
+ # Assistant with tool call (has args)
+ MemoryRecord(
+ message=FunctionCallingMessage(
+ "assistant",
+ RoleType.ASSISTANT,
+ None,
+ "",
+ func_name="add",
+ args={"a": 1, "b": 2},
+ tool_call_id="call_123",
+ ),
+ role_at_backend=OpenAIBackendRole.ASSISTANT,
+ ),
+ # Function result
+ MemoryRecord(
+ message=FunctionCallingMessage(
+ "function",
+ RoleType.ASSISTANT,
+ None,
+ "",
+ func_name="add",
+ result="3",
+ tool_call_id="call_123",
+ ),
+ role_at_backend=OpenAIBackendRole.FUNCTION,
+ ),
+ # Final assistant response (should be kept)
+ MemoryRecord(
+ message=BaseMessage(
+ "assistant", RoleType.ASSISTANT, None, "Answer"
+ ),
+ role_at_backend=OpenAIBackendRole.ASSISTANT,
+ ),
+ ]
+ memory.write_records(records)
+
+ memory.clean_tool_calls()
+ remaining = memory.retrieve()
+
+ # Should only have USER and final ASSISTANT
+ assert len(remaining) == 2
+ assert (
+ remaining[0].memory_record.role_at_backend
+ == OpenAIBackendRole.USER
+ )
+ assert (
+ remaining[1].memory_record.role_at_backend
+ == OpenAIBackendRole.ASSISTANT
+ )
+ assert remaining[1].memory_record.message.content == "Answer"
+
+ def test_clean_tool_calls_handles_multiple_tool_calls(
+ self, mock_context_creator
+ ):
+ r"""Test clean_tool_calls with multiple tool call sequences."""
+ storage = InMemoryKeyValueStorage()
+ memory = ChatHistoryMemory(mock_context_creator, storage=storage)
+
+ records = [
+ MemoryRecord(
+ message=BaseMessage("user", RoleType.USER, None, "Q1"),
+ role_at_backend=OpenAIBackendRole.USER,
+ ),
+ MemoryRecord(
+ message=FunctionCallingMessage(
+ "assistant",
+ RoleType.ASSISTANT,
+ None,
+ "",
+ func_name="tool1",
+ args={"x": 1},
+ ),
+ role_at_backend=OpenAIBackendRole.ASSISTANT,
+ ),
+ MemoryRecord(
+ message=FunctionCallingMessage(
+ "function",
+ RoleType.ASSISTANT,
+ None,
+ "",
+ func_name="tool1",
+ result="R1",
+ ),
+ role_at_backend=OpenAIBackendRole.FUNCTION,
+ ),
+ MemoryRecord(
+ message=BaseMessage(
+ "assistant", RoleType.ASSISTANT, None, "A1"
+ ),
+ role_at_backend=OpenAIBackendRole.ASSISTANT,
+ ),
+ MemoryRecord(
+ message=BaseMessage("user", RoleType.USER, None, "Q2"),
+ role_at_backend=OpenAIBackendRole.USER,
+ ),
+ MemoryRecord(
+ message=FunctionCallingMessage(
+ "assistant",
+ RoleType.ASSISTANT,
+ None,
+ "",
+ func_name="tool2",
+ args={"y": 2},
+ ),
+ role_at_backend=OpenAIBackendRole.ASSISTANT,
+ ),
+ MemoryRecord(
+ message=FunctionCallingMessage(
+ "function",
+ RoleType.ASSISTANT,
+ None,
+ "",
+ func_name="tool2",
+ result="R2",
+ ),
+ role_at_backend=OpenAIBackendRole.FUNCTION,
+ ),
+ MemoryRecord(
+ message=BaseMessage(
+ "assistant", RoleType.ASSISTANT, None, "A2"
+ ),
+ role_at_backend=OpenAIBackendRole.ASSISTANT,
+ ),
+ ]
+ memory.write_records(records)
+
+ memory.clean_tool_calls()
+ remaining = memory.retrieve()
+
+ # Should only have: USER, ASSISTANT, USER, ASSISTANT
+ assert len(remaining) == 4
+ expected_roles = [
+ OpenAIBackendRole.USER,
+ OpenAIBackendRole.ASSISTANT,
+ OpenAIBackendRole.USER,
+ OpenAIBackendRole.ASSISTANT,
+ ]
+ actual_roles = [r.memory_record.role_at_backend for r in remaining]
+ assert actual_roles == expected_roles
diff --git a/uv.lock b/uv.lock
index b2381a0ee..9c5d1fdfa 100644
--- a/uv.lock
+++ b/uv.lock
@@ -790,7 +790,7 @@ wheels = [
[[package]]
name = "camel-ai"
-version = "0.2.79a1"
+version = "0.2.79a2"
source = { editable = "." }
dependencies = [
{ name = "astor" },
@@ -2710,7 +2710,7 @@ wheels = [
[[package]]
name = "environs"
-version = "14.4.0"
+version = "14.5.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "marshmallow", version = "3.26.1", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.13'" },
@@ -2718,9 +2718,9 @@ dependencies = [
{ name = "python-dotenv" },
{ name = "typing-extensions", marker = "python_full_version < '3.11'" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/81/2f/e5b6f9a260165328edccd773e38923f71da5862bab2402630d8cb82cd29d/environs-14.4.0.tar.gz", hash = "sha256:7d9151d0710fc1b02f68bec6863d804ed10121925fb658bbdcb9fc7ddf704f62", size = 35070, upload-time = "2025-10-29T16:48:35.63Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/aa/75/06801d5beeb398ed3903167af9376bb81c4ac41c44a53d45193065ebb1a8/environs-14.5.0.tar.gz", hash = "sha256:f7b8f6fcf3301bc674bc9c03e39b5986d116126ffb96764efd34c339ed9464ee", size = 35426, upload-time = "2025-11-02T21:30:36.78Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/f0/e9/da61e470fb463bbc244ea76a26a0f4afa5e86b4aa5697d816cdd1b90708a/environs-14.4.0-py3-none-any.whl", hash = "sha256:fcd1b0259a2ac1f9ce6e2d2bdbbd51483b83533c37cea1524d5d4abde611270a", size = 17104, upload-time = "2025-10-29T16:48:34.206Z" },
+ { url = "https://files.pythonhosted.org/packages/d3/f3/6961beb9a1e77d01dee1dd48f00fb3064429c8abcfa26aa863eb7cb2b6dd/environs-14.5.0-py3-none-any.whl", hash = "sha256:1abd3e3a5721fb09797438d6c902bc2f35d4580dfaffe68b8ee588b67b504e13", size = 17202, upload-time = "2025-11-02T21:30:35.186Z" },
]
[[package]]
@@ -6784,7 +6784,7 @@ wheels = [
[[package]]
name = "posthog"
-version = "6.7.11"
+version = "6.7.13"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "backoff" },
@@ -6794,9 +6794,9 @@ dependencies = [
{ name = "six" },
{ name = "typing-extensions" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/cb/32/3668d5e0f8b852fad81770743ee17893854fd8e5f7cea897a0a9199b0370/posthog-6.7.11.tar.gz", hash = "sha256:62db3e97cbd95351fe081c1ea8805393293de6fabad6d2e9024bf940aca4ddbf", size = 120407, upload-time = "2025-10-28T13:06:18.335Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/37/ef/43a74ea78144a6ffacda600c0f2dbb2eb586a623973abcd8b066f3820bf3/posthog-6.7.13.tar.gz", hash = "sha256:fe31c3d2694683acdfeb2e2af148aaa3659a8c87cf1ccdc6c0d0c2dec4a55842", size = 120924, upload-time = "2025-11-02T18:58:29.532Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/8c/00/bf284e0aae5dec7c217c176f291867cfac2f7bfd5692c9ce041e80986fa7/posthog-6.7.11-py3-none-any.whl", hash = "sha256:31421a88437cef2ce20f60c14ee8d298b2e765a6de0617cb95d1fcef54170749", size = 138713, upload-time = "2025-10-28T13:06:17.018Z" },
+ { url = "https://files.pythonhosted.org/packages/e7/fe/623f14bd647a5a15cb451eb992291346c81524617d977d2548820e264d24/posthog-6.7.13-py3-none-any.whl", hash = "sha256:7572dba3c0516297611399ebdc4e4db268be02922c2c896cc9c7683b13b17f30", size = 139183, upload-time = "2025-11-02T18:58:27.967Z" },
]
[[package]]