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: Agent tool call try expect, update image log (#1841)

Wendong-Fan52cfd27c926a
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml
index 8413cfd85..25337f8e6 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.28
+ placeholder: E.g., 0.2.29
validations:
required: true
diff --git a/camel/__init__.py b/camel/__init__.py
index 27b6850da..ee326a04c 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.28'
+__version__ = '0.2.29'
__all__ = [
'__version__',
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 9452bb4df..231d0ba2f 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -707,10 +707,13 @@ class ChatAgent(BaseAgent):
f"did not run successfully. Error: {error_info}"
)
+ sanitized_messages = self._sanitize_messages_for_logging(
+ openai_messages
+ )
logger.info(
f"Model {self.model_backend.model_type}, "
f"index {self.model_backend.current_model_index}, "
- f"processed these messages: {openai_messages}"
+ f"processed these messages: {sanitized_messages}"
)
if isinstance(response, ChatCompletion):
@@ -752,10 +755,13 @@ class ChatAgent(BaseAgent):
f"did not run successfully. Error: {error_info}"
)
+ sanitized_messages = self._sanitize_messages_for_logging(
+ openai_messages
+ )
logger.info(
f"Model {self.model_backend.model_type}, "
f"index {self.model_backend.current_model_index}, "
- f"processed these messages: {openai_messages}"
+ f"processed these messages: {sanitized_messages}"
)
if isinstance(response, ChatCompletion):
@@ -763,6 +769,134 @@ class ChatAgent(BaseAgent):
else:
return await self._ahandle_stream_response(response, num_tokens)
+ def _sanitize_messages_for_logging(self, messages):
+ 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.
+
+ Returns:
+ List[OpenAIMessage]: The sanitized OpenAI messages.
+ """
+ import hashlib
+ import os
+ import re
+ import tempfile
+
+ # Create a copy of messages for logging to avoid modifying the
+ # original messages
+ sanitized_messages = []
+ for msg in messages:
+ if isinstance(msg, dict):
+ sanitized_msg = msg.copy()
+ # Check if content is a list (multimodal content with images)
+ if isinstance(sanitized_msg.get('content'), list):
+ content_list = []
+ for item in sanitized_msg['content']:
+ if (
+ isinstance(item, dict)
+ and item.get('type') == 'image_url'
+ ):
+ # Handle image URL
+ image_url = item.get('image_url', {}).get(
+ 'url', ''
+ )
+ if image_url and image_url.startswith(
+ 'data:image'
+ ):
+ # Extract image data and format
+ match = re.match(
+ r'data:image/([^;]+);base64,(.+)',
+ image_url,
+ )
+ if match:
+ img_format, base64_data = match.groups()
+
+ # Create a hash of the image data to use
+ # as filename
+ img_hash = hashlib.md5(
+ base64_data[:100].encode()
+ ).hexdigest()[:10]
+ img_filename = (
+ f"image_{img_hash}.{img_format}"
+ )
+
+ # Save image to temp directory for viewing
+ try:
+ import base64
+
+ temp_dir = tempfile.gettempdir()
+ img_path = os.path.join(
+ temp_dir, img_filename
+ )
+
+ # Only save if file doesn't exist
+ if not os.path.exists(img_path):
+ with open(img_path, 'wb') as f:
+ f.write(
+ base64.b64decode(
+ base64_data
+ )
+ )
+
+ # Create a file:// URL that can be
+ # opened
+ file_url = f"file://{img_path}"
+
+ content_list.append(
+ {
+ 'type': 'image_url',
+ 'image_url': {
+ 'url': f'{file_url}',
+ 'detail': item.get(
+ 'image_url', {}
+ ).get('detail', 'auto'),
+ },
+ }
+ )
+ except Exception as e:
+ # If saving fails, fall back to simple
+ # message
+ content_list.append(
+ {
+ 'type': 'image_url',
+ 'image_url': {
+ 'url': '[base64 '
+ + 'image - error saving: '
+ + str(e)
+ + ']',
+ 'detail': item.get(
+ 'image_url', {}
+ ).get('detail', 'auto'),
+ },
+ }
+ )
+ else:
+ # If regex fails, fall back to simple
+ # message
+ content_list.append(
+ {
+ 'type': 'image_url',
+ 'image_url': {
+ 'url': '[base64 '
+ + 'image - invalid format]',
+ 'detail': item.get(
+ 'image_url', {}
+ ).get('detail', 'auto'),
+ },
+ }
+ )
+ else:
+ content_list.append(item)
+ else:
+ content_list.append(item)
+ sanitized_msg['content'] = content_list
+ sanitized_messages.append(sanitized_msg)
+ else:
+ sanitized_messages.append(msg)
+ return sanitized_messages
+
def _step_get_info(
self,
output_messages: List[BaseMessage],
@@ -1043,7 +1177,13 @@ class ChatAgent(BaseAgent):
args = tool_call_request.args
tool_call_id = tool_call_request.tool_call_id
tool = self._internal_tools[func_name]
- result = tool(**args)
+ try:
+ result = tool(**args)
+ except Exception as e:
+ # Capture the error message to prevent framework crash
+ error_msg = f"Error executing tool '{func_name}': {e!s}"
+ result = {"error": error_msg}
+ logging.warning(error_msg)
return self._record_tool_calling(func_name, args, result, tool_call_id)
@@ -1055,7 +1195,14 @@ class ChatAgent(BaseAgent):
args = tool_call_request.args
tool_call_id = tool_call_request.tool_call_id
tool = self._internal_tools[func_name]
- result = await tool.async_call(**args)
+ try:
+ result = await tool.async_call(**args)
+ except Exception as e:
+ # Capture the error message to prevent framework crash
+ error_msg = f"Error executing async tool '{func_name}': {e!s}"
+ result = {"error": error_msg}
+ logging.warning(error_msg)
+
return self._record_tool_calling(func_name, args, result, tool_call_id)
def _record_tool_calling(
diff --git a/docs/conf.py b/docs/conf.py
index a0a2fa208..ef68ea028 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.28'
+release = '0.2.29'
html_favicon = (
'https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'
diff --git a/docs/key_modules/loaders.md b/docs/key_modules/loaders.md
index 8ce0b2442..9356ead78 100644
--- a/docs/key_modules/loaders.md
+++ b/docs/key_modules/loaders.md
@@ -340,14 +340,14 @@ response = jina_reader.read_content("https://docs.camel-ai.org/")
print(response)
```
```markdown
->>>Welcome to CAMEL’s documentation! — CAMEL 0.2.28 documentation
+>>>Welcome to CAMEL’s documentation! — CAMEL 0.2.29 documentation
===============
[Skip to main content](https://docs.camel-ai.org/#main-content)
Back to top Ctrl+K
- [![Image 1](https://raw.githubusercontent.com/camel-ai/camel/master/misc/logo_light.png) ![Image 2](https://raw.githubusercontent.com/camel-ai/camel/master/misc/logo_light.png)CAMEL 0.2.28](https://docs.camel-ai.org/#)
+ [![Image 1](https://raw.githubusercontent.com/camel-ai/camel/master/misc/logo_light.png) ![Image 2](https://raw.githubusercontent.com/camel-ai/camel/master/misc/logo_light.png)CAMEL 0.2.29](https://docs.camel-ai.org/#)
Search Ctrl+K
diff --git a/pyproject.toml b/pyproject.toml
index 38362c374..0a2dea985 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "camel-ai"
-version = "0.2.28"
+version = "0.2.29"
description = "Communicative Agents for AI Society Study"
authors = [{ name = "CAMEL-AI.org" }]
requires-python = ">=3.10,<3.13"
diff --git a/uv.lock b/uv.lock
index c37a307a2..d2660a77d 100644
--- a/uv.lock
+++ b/uv.lock
@@ -510,7 +510,7 @@ wheels = [
[[package]]
name = "camel-ai"
-version = "0.2.28"
+version = "0.2.29"
source = { editable = "." }
dependencies = [
{ name = "colorama" },