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: Fix the ModelType recursive bug and improve logging (#2869)

Zecheng Zhangfdb24c277b9f
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 37d5fbd05..1e621f2c2 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -1264,7 +1264,11 @@ class ChatAgent(BaseAgent):
openai_message: OpenAIMessage = {"role": "user", "content": prompt}
# Explicitly set the tools to empty list to avoid calling tools
response = self._get_model_response(
- [openai_message], 0, response_format, []
+ openai_messages=[openai_message],
+ num_tokens=0,
+ response_format=response_format,
+ tool_schemas=[],
+ prev_num_openai_messages=0,
)
message.content = response.output_messages[0].content
if not self._try_format_message(message, response_format):
@@ -1292,7 +1296,11 @@ class ChatAgent(BaseAgent):
prompt = SIMPLE_FORMAT_PROMPT.format(content=message.content)
openai_message: OpenAIMessage = {"role": "user", "content": prompt}
response = await self._aget_model_response(
- [openai_message], 0, response_format, []
+ openai_messages=[openai_message],
+ num_tokens=0,
+ response_format=response_format,
+ tool_schemas=[],
+ prev_num_openai_messages=0,
)
message.content = response.output_messages[0].content
self._try_format_message(message, response_format)
@@ -1374,7 +1382,8 @@ class ChatAgent(BaseAgent):
# Initialize token usage tracker
step_token_usage = self._create_token_usage_tracker()
- iteration_count = 0
+ iteration_count: int = 0
+ prev_num_openai_messages: int = 0
while True:
if self.pause_event is not None and not self.pause_event.is_set():
@@ -1391,10 +1400,13 @@ class ChatAgent(BaseAgent):
# Get response from model backend
response = self._get_model_response(
openai_messages,
- accumulated_context_tokens, # Cumulative context tokens
- response_format,
- self._get_full_tool_schemas(),
+ num_tokens=num_tokens,
+ current_iteration=iteration_count,
+ response_format=response_format,
+ tool_schemas=self._get_full_tool_schemas(),
+ prev_num_openai_messages=prev_num_openai_messages,
)
+ prev_num_openai_messages = len(openai_messages)
iteration_count += 1
# Accumulate API token usage
@@ -1405,6 +1417,9 @@ class ChatAgent(BaseAgent):
# Terminate Agent if stop_event is set
if self.stop_event and self.stop_event.is_set():
# Use the _step_terminate to terminate the agent with reason
+ logger.info(
+ f"Termination triggered at iteration " f"{iteration_count}"
+ )
return self._step_terminate(
accumulated_context_tokens,
tool_call_records,
@@ -1439,6 +1454,7 @@ class ChatAgent(BaseAgent):
self.max_iteration is not None
and iteration_count >= self.max_iteration
):
+ logger.info(f"Max iteration reached: {iteration_count}")
break
# If we're still here, continue the loop
@@ -1564,7 +1580,8 @@ class ChatAgent(BaseAgent):
# Initialize token usage tracker
step_token_usage = self._create_token_usage_tracker()
- iteration_count = 0
+ iteration_count: int = 0
+ prev_num_openai_messages: int = 0
while True:
if self.pause_event is not None and not self.pause_event.is_set():
await self.pause_event.wait()
@@ -1578,10 +1595,13 @@ class ChatAgent(BaseAgent):
response = await self._aget_model_response(
openai_messages,
- accumulated_context_tokens,
- response_format,
- self._get_full_tool_schemas(),
+ num_tokens=num_tokens,
+ current_iteration=iteration_count,
+ response_format=response_format,
+ tool_schemas=self._get_full_tool_schemas(),
+ prev_num_openai_messages=prev_num_openai_messages,
)
+ prev_num_openai_messages = len(openai_messages)
iteration_count += 1
# Accumulate API token usage
@@ -1592,6 +1612,9 @@ class ChatAgent(BaseAgent):
# Terminate Agent if stop_event is set
if self.stop_event and self.stop_event.is_set():
# Use the _step_terminate to terminate the agent with reason
+ logger.info(
+ f"Termination triggered at iteration " f"{iteration_count}"
+ )
return self._step_terminate(
accumulated_context_tokens,
tool_call_records,
@@ -1900,14 +1923,32 @@ class ChatAgent(BaseAgent):
stripped_messages.append(msg)
return stripped_messages
+ @observe()
def _get_model_response(
self,
openai_messages: List[OpenAIMessage],
num_tokens: int,
+ current_iteration: int = 0,
response_format: Optional[Type[BaseModel]] = None,
tool_schemas: Optional[List[Dict[str, Any]]] = None,
+ prev_num_openai_messages: int = 0,
) -> ModelResponse:
- r"""Internal function for agent step model response."""
+ r"""Internal function for agent step model response.
+ Args:
+ openai_messages (List[OpenAIMessage]): The OpenAI
+ messages to process.
+ num_tokens (int): The number of tokens in the context.
+ current_iteration (int): The current iteration of the step.
+ response_format (Optional[Type[BaseModel]]): The response
+ format to use.
+ tool_schemas (Optional[List[Dict[str, Any]]]): The tool
+ schemas to use.
+ prev_num_openai_messages (int): The number of openai messages
+ logged in the previous iteration.
+
+ Returns:
+ ModelResponse: The model response.
+ """
response = None
try:
@@ -1920,7 +1961,8 @@ class ChatAgent(BaseAgent):
openai_messages
):
logger.warning(
- "Model appears to not support vision. Retrying without images." # noqa: E501
+ "Model appears to not support vision."
+ "Retrying without images."
)
try:
stripped_messages = self._strip_images_from_messages(
@@ -1937,6 +1979,7 @@ class ChatAgent(BaseAgent):
if not response:
logger.error(
f"An error occurred while running model "
+ f"iteration {current_iteration}, "
f"{self.model_backend.model_type}, "
f"index: {self.model_backend.current_model_index}",
exc_info=exc,
@@ -1955,11 +1998,12 @@ class ChatAgent(BaseAgent):
)
sanitized_messages = self._sanitize_messages_for_logging(
- openai_messages
+ openai_messages, prev_num_openai_messages
)
logger.info(
f"Model {self.model_backend.model_type}, "
f"index {self.model_backend.current_model_index}, "
+ f"iteration {current_iteration}, "
f"processed these messages: {sanitized_messages}"
)
if not isinstance(response, ChatCompletion):
@@ -1973,10 +2017,27 @@ class ChatAgent(BaseAgent):
self,
openai_messages: List[OpenAIMessage],
num_tokens: int,
+ current_iteration: int = 0,
response_format: Optional[Type[BaseModel]] = None,
tool_schemas: Optional[List[Dict[str, Any]]] = None,
+ prev_num_openai_messages: int = 0,
) -> ModelResponse:
- r"""Internal function for agent step model response."""
+ r"""Internal function for agent async step model response.
+ Args:
+ openai_messages (List[OpenAIMessage]): The OpenAI messages
+ to process.
+ num_tokens (int): The number of tokens in the context.
+ current_iteration (int): The current iteration of the step.
+ response_format (Optional[Type[BaseModel]]): The response
+ format to use.
+ tool_schemas (Optional[List[Dict[str, Any]]]): The tool schemas
+ to use.
+ prev_num_openai_messages (int): The number of openai messages
+ logged in the previous iteration.
+
+ Returns:
+ ModelResponse: The model response.
+ """
response = None
try:
@@ -2024,11 +2085,12 @@ class ChatAgent(BaseAgent):
)
sanitized_messages = self._sanitize_messages_for_logging(
- openai_messages
+ openai_messages, prev_num_openai_messages
)
logger.info(
f"Model {self.model_backend.model_type}, "
f"index {self.model_backend.current_model_index}, "
+ f"iteration {current_iteration}, "
f"processed these messages: {sanitized_messages}"
)
if not isinstance(response, ChatCompletion):
@@ -2038,12 +2100,16 @@ class ChatAgent(BaseAgent):
)
return self._handle_batch_response(response)
- def _sanitize_messages_for_logging(self, messages):
+ def _sanitize_messages_for_logging(
+ self, messages, prev_num_openai_messages: int
+ ):
r"""Sanitize OpenAI messages for logging by replacing base64 image
data with a simple message and a link to view the image.
Args:
messages (List[OpenAIMessage]): The OpenAI messages to sanitize.
+ prev_num_openai_messages (int): The number of openai messages
+ logged in the previous iteration.
Returns:
List[OpenAIMessage]: The sanitized OpenAI messages.
@@ -2056,7 +2122,7 @@ class ChatAgent(BaseAgent):
# Create a copy of messages for logging to avoid modifying the
# original messages
sanitized_messages = []
- for msg in messages:
+ for msg in messages[prev_num_openai_messages:]:
if isinstance(msg, dict):
sanitized_msg = msg.copy()
# Check if content is a list (multimodal content with images)
@@ -2339,6 +2405,7 @@ class ChatAgent(BaseAgent):
info=info,
)
+ @observe()
def _execute_tool(
self,
tool_call_request: ToolCallRequest,
@@ -2373,7 +2440,7 @@ class ChatAgent(BaseAgent):
error_msg = f"Error executing tool '{func_name}': {e!s}"
result = f"Tool execution failed: {error_msg}"
mask_flag = False
- logging.warning(error_msg)
+ logger.warning(f"{error_msg} with result: {result}")
# Check if result is a ToolResult with images
images_to_attach = None
@@ -2384,6 +2451,7 @@ class ChatAgent(BaseAgent):
tool_record = self._record_tool_calling(
func_name, args, result, tool_call_id, mask_output=mask_flag
)
+ logger.info(f"Tool calling record:\n{tool_record}")
# Store images for later attachment to next user message
if images_to_attach:
@@ -2594,6 +2662,9 @@ class ChatAgent(BaseAgent):
while True:
# Check termination condition
if self.stop_event and self.stop_event.is_set():
+ logger.info(
+ f"Termination triggered at iteration " f"{iteration_count}"
+ )
yield self._step_terminate(
num_tokens, tool_call_records, "termination_triggered"
)
@@ -3252,6 +3323,9 @@ class ChatAgent(BaseAgent):
while True:
# Check termination condition
if self.stop_event and self.stop_event.is_set():
+ logger.info(
+ f"Termination triggered at iteration " f"{iteration_count}"
+ )
yield self._step_terminate(
num_tokens, tool_call_records, "termination_triggered"
)
diff --git a/camel/models/openai_model.py b/camel/models/openai_model.py
index ba7d1f981..c174c7454 100644
--- a/camel/models/openai_model.py
+++ b/camel/models/openai_model.py
@@ -23,6 +23,7 @@ from openai.lib.streaming.chat import (
from pydantic import BaseModel
from camel.configs import OPENAI_API_PARAMS, ChatGPTConfig
+from camel.logger import get_logger
from camel.messages import OpenAIMessage
from camel.models import BaseModelBackend
from camel.types import (
@@ -39,6 +40,8 @@ from camel.utils import (
update_langfuse_trace,
)
+logger = get_logger(__name__)
+
if os.environ.get("LANGFUSE_ENABLED", "False").lower() == "true":
try:
from langfuse.decorators import observe
@@ -273,17 +276,23 @@ class OpenAIModel(BaseModelBackend):
# Update Langfuse trace with current agent session and metadata
agent_session_id = get_current_agent_session_id()
+ model_type_str = str(self.model_type)
+ if not agent_session_id:
+ agent_session_id = "no-session-id"
+ metadata = {
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": model_type_str,
+ }
+ metadata = {k: str(v) for k, v in metadata.items()}
if agent_session_id:
update_langfuse_trace(
session_id=agent_session_id,
- metadata={
- "source": "camel",
- "agent_id": agent_session_id,
- "agent_type": "camel_chat_agent",
- "model_type": str(self.model_type),
- },
- tags=["CAMEL-AI", str(self.model_type)],
+ metadata=metadata,
+ tags=["CAMEL-AI", model_type_str],
)
+ logger.info(f"metadata: {metadata}")
messages = self._adapt_messages_for_o1_models(messages)
response_format = response_format or self.model_config_dict.get(
@@ -342,17 +351,22 @@ class OpenAIModel(BaseModelBackend):
# Update Langfuse trace with current agent session and metadata
agent_session_id = get_current_agent_session_id()
- if agent_session_id:
+ model_type_str = str(self.model_type)
+ if not agent_session_id:
+ agent_session_id = "no-session-id"
+ metadata = {
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": model_type_str,
+ }
+ metadata = {k: str(v) for k, v in metadata.items()}
update_langfuse_trace(
session_id=agent_session_id,
- metadata={
- "source": "camel",
- "agent_id": agent_session_id,
- "agent_type": "camel_chat_agent",
- "model_type": str(self.model_type),
- },
- tags=["CAMEL-AI", str(self.model_type)],
+ metadata=metadata,
+ tags=["CAMEL-AI", model_type_str],
)
+ logger.info(f"metadata: {metadata}")
messages = self._adapt_messages_for_o1_models(messages)
response_format = response_format or self.model_config_dict.get(
diff --git a/camel/types/enums.py b/camel/types/enums.py
index 2a935a1bd..22c9dbd6d 100644
--- a/camel/types/enums.py
+++ b/camel/types/enums.py
@@ -13,7 +13,7 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import os
from enum import Enum, EnumMeta
-from typing import cast
+from typing import Union, cast
from camel.logger import get_logger
from camel.types.unified_model_type import UnifiedModelType
@@ -420,11 +420,14 @@ class ModelType(UnifiedModelType, Enum):
def __str__(self):
return self.value
- def __new__(cls, value) -> "ModelType":
+ def __repr__(self):
+ return self.value
+
+ def __new__(cls, value: Union["ModelType", str]) -> "ModelType":
return cast("ModelType", UnifiedModelType.__new__(cls, value))
@classmethod
- def from_name(cls, name):
+ def from_name(cls, name: str) -> "ModelType":
r"""Returns the ModelType enum value from a string."""
for model_type in cls:
if model_type.value == name:
diff --git a/camel/types/unified_model_type.py b/camel/types/unified_model_type.py
index e47d0666b..b6b339eb2 100644
--- a/camel/types/unified_model_type.py
+++ b/camel/types/unified_model_type.py
@@ -12,6 +12,7 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import logging
+from enum import Enum
from threading import Lock
from typing import TYPE_CHECKING, ClassVar, Dict, Union, cast
@@ -32,17 +33,28 @@ class UnifiedModelType(str):
_lock: ClassVar[Lock] = Lock()
def __new__(cls, value: Union["ModelType", str]) -> "UnifiedModelType":
+ if isinstance(value, Enum):
+ str_value = value.value
+ else:
+ str_value = str(value)
+
with cls._lock:
- if value not in cls._cache:
- instance = super().__new__(cls, value)
- cls._cache[value] = cast(UnifiedModelType, instance)
+ if str_value not in cls._cache:
+ instance = super().__new__(cls, str_value)
+ cls._cache[str_value] = cast(UnifiedModelType, instance)
else:
- instance = cls._cache[value]
+ instance = cls._cache[str_value]
return instance
def __init__(self, value: Union["ModelType", str]) -> None:
pass
+ def __repr__(self) -> str:
+ return super().__str__()
+
+ def __str__(self) -> str:
+ return super().__str__()
+
@property
def value_for_tiktoken(self) -> str:
r"""Returns the model name for TikToken."""
diff --git a/pyproject.toml b/pyproject.toml
index cf4a65aed..33138f37c 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -180,7 +180,7 @@ dev_tools = [
"daytona-sdk==0.20.0",
"aci-sdk>=1.0.0b1",
"langfuse>=2.60.5",
- "traceroot==0.0.3",
+ "traceroot==0.0.4a2",
]
model_platforms = [
"litellm>=1.38.1,<2",
@@ -390,7 +390,7 @@ all = [
"psycopg[binary]>=3.1.18,<4",
"pgvector>=0.2.4,<0.3",
"chromadb>=0.6.0,<1.0.0",
- "traceroot==0.0.3",
+ "traceroot==0.0.4a2",
"reportlab>=4.4.2",
]
diff --git a/uv.lock b/uv.lock
index 1be83c48a..a12439edc 100644
--- a/uv.lock
+++ b/uv.lock
@@ -1302,8 +1302,8 @@ requires-dist = [
{ name = "textblob", marker = "extra == 'data-tools'", specifier = ">=0.17.1,<0.18" },
{ name = "tiktoken", specifier = ">=0.7.0,<0.8" },
{ name = "toml", marker = "extra == 'dev'", specifier = ">=0.10.2" },
- { name = "traceroot", marker = "extra == 'all'", specifier = "==0.0.3" },
- { name = "traceroot", marker = "extra == 'dev-tools'", specifier = "==0.0.3" },
+ { name = "traceroot", marker = "extra == 'all'", specifier = "==0.0.4a2" },
+ { name = "traceroot", marker = "extra == 'dev-tools'", specifier = "==0.0.4a2" },
{ name = "transformers", marker = "extra == 'all'", specifier = ">=4,<5" },
{ name = "transformers", marker = "extra == 'huggingface'", specifier = ">=4,<5" },
{ name = "transformers", marker = "extra == 'owl'", specifier = ">=4,<5" },
@@ -7811,7 +7811,7 @@ wheels = [
[[package]]
name = "traceroot"
-version = "0.0.3"
+version = "0.0.4a2"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "opentelemetry-api" },
@@ -7832,9 +7832,9 @@ dependencies = [
{ name = "pyyaml" },
{ name = "watchtower" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/bb/e6/5a7ff00e4844ded949cc3a9afcbc78d5f16bd463182c1c057df612e868d3/traceroot-0.0.3.tar.gz", hash = "sha256:5ce193776fa675fa0d971d85e7255ef67cafce5dd811da1168dfda0271fd849a", size = 15366, upload-time = "2025-07-11T22:17:21.362Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/79/8e/39c1f331750975e81dd15f2f1aef727784985694832142dded1cda12197c/traceroot-0.0.4a2.tar.gz", hash = "sha256:fd88177441cd6f4be3d911f0008ea766613ba0a1a89c1f0ec011a717dc21a5c3", size = 15562, upload-time = "2025-07-13T21:16:41.574Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/52/aa/b7e327bd9e672284f4a9ca0920c55ae156fc58768017931f048ec43f6b90/traceroot-0.0.3-py3-none-any.whl", hash = "sha256:e5f2b5aee19c79b681ff6e6ce1747679c8c69f8bb0e664939b2ffb0245a5d23f", size = 12815, upload-time = "2025-07-11T22:17:20.198Z" },
+ { url = "https://files.pythonhosted.org/packages/06/64/bb8467edf1a83555fd7441743d17de39363382d9163ff954104b2240ac81/traceroot-0.0.4a2-py3-none-any.whl", hash = "sha256:e59051cc5c9d6bd9079b75bab7e78aed818d660c6c044ee5f9938d427f787ccc", size = 13021, upload-time = "2025-07-13T21:16:39.697Z" },
]
[[package]]