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: PR3123 DingTalk toolkit integration (#3182)

Wendong-Fan106602465f26
diff --git a/camel/toolkits/dingtalk.py b/camel/toolkits/dingtalk.py
index 9c89bc56b..ef2bb1784 100644
--- a/camel/toolkits/dingtalk.py
+++ b/camel/toolkits/dingtalk.py
@@ -420,7 +420,8 @@ class DingtalkToolkit(BaseToolkit):
msgtype (Literal): Message type (text, markdown, link, actionCard).
title (Optional[str]): Message title (required for markdown).
webhook_url (Optional[str]): Webhook URL. If None, uses env var.
- webhook_secret (Optional[str]): Webhook secret. If None, uses env var.
+ webhook_secret (Optional[str]): Webhook secret. If None, uses env
+ var.
Returns:
str: Success or error message.
@@ -430,7 +431,9 @@ class DingtalkToolkit(BaseToolkit):
"""
# Get webhook configuration
url = webhook_url or os.environ.get("DINGTALK_WEBHOOK_URL", "")
- secret = webhook_secret or os.environ.get("DINGTALK_WEBHOOK_SECRET", "")
+ secret = webhook_secret or os.environ.get(
+ "DINGTALK_WEBHOOK_SECRET", ""
+ )
if not url:
return "Error: Webhook URL not provided or set in environment"
@@ -479,7 +482,9 @@ class DingtalkToolkit(BaseToolkit):
if result.get("errcode") == 0:
return "Webhook message sent successfully"
else:
- return f"Webhook error: {result.get('errmsg', 'Unknown error')}"
+ return (
+ f"Webhook error: {result.get('errmsg', 'Unknown error')}"
+ )
except Exception as e:
return f"Failed to send webhook message: {e!s}"
@@ -691,17 +696,19 @@ class DingtalkToolkit(BaseToolkit):
r"""Gets user information by mobile number.
Args:
- mobile (str): User's mobile number. Should be a valid Chinese
+ mobile (str): User's mobile number. Should be a valid Chinese
mobile number format (11 digits starting with 1).
Returns:
Dict[str, Any]: User information or error information.
"""
- # Validate mobile number format (Chinese mobile number: 11 digits starting with 1)
+ # Validate mobile number format (Chinese mobile number: 11 digits
+ # starting with 1)
mobile_pattern = r'^1[3-9]\d{9}$'
if not re.match(mobile_pattern, mobile):
return {
- "error": "Invalid mobile number format. Expected 11 digits starting with 1 (e.g., 13800000000)."
+ "error": "Invalid mobile number format. Expected 11 digits "
+ "starting with 1 (e.g., 13800000000)."
}
payload = {"mobile": mobile}
@@ -727,9 +734,9 @@ class DingtalkToolkit(BaseToolkit):
r"""Gets user information by unionid.
Args:
- unionid (str): User's unique identifier across all DingTalk
- organizations. This is a global identifier that remains
- consistent even if the user belongs to multiple DingTalk
+ unionid (str): User's unique identifier across all DingTalk
+ organizations. This is a global identifier that remains
+ consistent even if the user belongs to multiple DingTalk
organizations, unlike userid which is organization-specific.
Returns:
@@ -891,10 +898,10 @@ class DingtalkToolkit(BaseToolkit):
name (Optional[str]): New group name.
owner (Optional[str]): New group owner userid.
add_useridlist (Optional[List[str]]): List of user IDs to add.
- Note: Internally converted to comma-separated string as
+ Note: Internally converted to comma-separated string as
required by the DingTalk API.
del_useridlist (Optional[List[str]]): List of user IDs to remove.
- Note: Internally converted to comma-separated string as
+ Note: Internally converted to comma-separated string as
required by the DingTalk API.
Returns:
@@ -909,7 +916,8 @@ class DingtalkToolkit(BaseToolkit):
payload["name"] = name
if owner:
payload["owner"] = owner
- # Note: DingTalk update group API requires comma-separated string format
+ # Note: DingTalk update group API requires comma-separated string
+ # format
# This is different from send_work_notification which uses array format
if add_useridlist:
payload["add_useridlist"] = ",".join(add_useridlist)
@@ -991,18 +999,15 @@ class DingtalkToolkit(BaseToolkit):
(None, "DINGTALK_APP_SECRET"),
]
)
- def dingtalk_get_userid_by_phone(
- self,
- phone_number: str
- ) -> str:
+ def dingtalk_get_userid_by_phone(self, phone_number: str) -> str:
r"""Gets user ID by phone number for LLM agents.
-
+
Args:
phone_number (str): User's phone number.
-
+
Returns:
str: User ID or error message.
-
+
References:
https://open.dingtalk.com/document/orgapp-server/query-user-details
"""
@@ -1021,27 +1026,25 @@ class DingtalkToolkit(BaseToolkit):
(None, "DINGTALK_APP_SECRET"),
]
)
- def dingtalk_get_userid_by_name(
- self,
- user_name: str
- ) -> str:
+ def dingtalk_get_userid_by_name(self, user_name: str) -> str:
r"""Gets user ID by user name for LLM agents.
-
+
Args:
user_name (str): User's display name.
-
+
Returns:
str: User ID or error message.
-
+
References:
https://open.dingtalk.com/document/orgapp-server/query-users
"""
try:
search_result = self.dingtalk_search_users_by_name(user_name)
- if 'result' in search_result and search_result['result']:
+ if search_result.get('result'):
# Return the first match
- return search_result['result'][0].get('userid',
- f"No userid found for user: {user_name}")
+ return search_result['result'][0].get(
+ 'userid', f"No userid found for user: {user_name}"
+ )
else:
return f"User not found with name: {user_name}"
except Exception as e:
@@ -1053,15 +1056,12 @@ class DingtalkToolkit(BaseToolkit):
(None, "DINGTALK_APP_SECRET"),
]
)
- def dingtalk_get_department_id_by_name(
- self,
- department_name: str
- ) -> str:
+ def dingtalk_get_department_id_by_name(self, department_name: str) -> str:
r"""Gets department ID by department name for LLM agents.
-
+
Args:
department_name (str): Department name to search for.
-
+
Returns:
str: Department ID or error message.
"""
@@ -1073,23 +1073,20 @@ class DingtalkToolkit(BaseToolkit):
return str(dept.get('id', ''))
return f"Department not found: {department_name}"
else:
- return f"Failed to get department list"
+ return "Failed to get department list"
except Exception as e:
return f"Failed to get department ID by name: {e!s}"
- def dingtalk_get_chatid_by_group_name(
- self,
- group_name: str
- ) -> str:
+ def dingtalk_get_chatid_by_group_name(self, group_name: str) -> str:
r"""Gets chat ID by group name for LLM agents.
-
+
Note: This function provides guidance as Dingtalk API doesn't directly
support searching groups by name. Users should use the group creation
response or group management features to obtain chat IDs.
-
+
Args:
group_name (str): Group name to search for.
-
+
Returns:
str: Guidance message for obtaining chat ID.
"""
@@ -1135,4 +1132,4 @@ class DingtalkToolkit(BaseToolkit):
]
-# many other functionalities, cards etc build on top after initial works
\ No newline at end of file
+# many other functionalities, cards etc build on top after initial works
diff --git a/camel/types/__init__.py b/camel/types/__init__.py
index 4b78ede24..ec5a3a5f4 100644
--- a/camel/types/__init__.py
+++ b/camel/types/__init__.py
@@ -41,8 +41,8 @@ from .openai_types import (
ChatCompletionAssistantMessageParam,
ChatCompletionChunk,
ChatCompletionMessage,
- ChatCompletionMessageParam,
ChatCompletionMessageFunctionToolCall,
+ ChatCompletionMessageParam,
ChatCompletionSystemMessageParam,
ChatCompletionToolMessageParam,
ChatCompletionUserMessageParam,
diff --git a/examples/external_tools/use_external_tools.py b/examples/external_tools/use_external_tools.py
index 91df17659..50a1b0f6a 100644
--- a/examples/external_tools/use_external_tools.py
+++ b/examples/external_tools/use_external_tools.py
@@ -70,5 +70,5 @@ if __name__ == "__main__":
Output:
The video game "Portal" was released in 2007 as part of a bundle called The Orange Box for Windows, Xbox 360, and PlayStation 3.
-ChatCompletionMessageToolCall(id='call_U5Xju7vYtAQAEW4D1M8R1kgs', function=Function(arguments='{"a": 2007, "b": 1776}', name='sub'), type='function')
+ChatCompletionMessageFunctionToolCall(id='call_U5Xju7vYtAQAEW4D1M8R1kgs', function=Function(arguments='{"a": 2007, "b": 1776}', name='sub'), type='function')
"""
diff --git a/examples/toolkits/github_toolkit.py b/examples/toolkits/github_toolkit.py
index b761e0c13..a3a626400 100644
--- a/examples/toolkits/github_toolkit.py
+++ b/examples/toolkits/github_toolkit.py
@@ -70,7 +70,7 @@ from typing import (
Union,
)
-from openai.types.chat import ChatCompletionMessageToolCall
+from openai.types.chat import ChatCompletionMessageFunctionToolCall
f
===============================================================================
"""
diff --git a/test/agents/test_chat_agent.py b/test/agents/test_chat_agent.py
index 373951d79..36b645112 100644
--- a/test/agents/test_chat_agent.py
+++ b/test/agents/test_chat_agent.py
@@ -19,7 +19,7 @@ from typing import List
from unittest.mock import AsyncMock, MagicMock
import pytest
-from openai.types.chat import ChatCompletionMessageToolCall
+from openai.types.chat import ChatCompletionMessageFunctionToolCall
from openai.types.chat.chat_completion import Choice
from openai.types.chat.chat_completion_message import ChatCompletionMessage
from openai.types.chat.chat_completion_message_function_tool_call import (
@@ -267,7 +267,7 @@ def test_chat_agent_step_with_external_tools(step_call_count=3):
audio=None,
function_call=None,
tool_calls=[
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id='call_mock_123456',
function=Function(
arguments='{ \
@@ -277,7 +277,7 @@ def test_chat_agent_step_with_external_tools(step_call_count=3):
),
type='function',
),
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id='call_mock_123457',
function=Function(
arguments='{ \
@@ -314,7 +314,7 @@ def test_chat_agent_step_with_external_tools(step_call_count=3):
audio=None,
function_call=None,
tool_calls=[
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id='call_mock_123456',
function=Function(
arguments='{"a":1776,"b":2007}',
@@ -399,7 +399,7 @@ async def test_chat_agent_astep_with_external_tools(step_call_count=3):
audio=None,
function_call=None,
tool_calls=[
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id='call_mock_123456',
function=Function(
arguments='{ \
@@ -409,7 +409,7 @@ async def test_chat_agent_astep_with_external_tools(step_call_count=3):
),
type='function',
),
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id='call_mock_123457',
function=Function(
arguments='{ \
@@ -446,7 +446,7 @@ async def test_chat_agent_astep_with_external_tools(step_call_count=3):
audio=None,
function_call=None,
tool_calls=[
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id='call_mock_123456',
function=Function(
arguments='{"a":1776,"b":2007}',
@@ -906,7 +906,7 @@ def test_tool_calling_sync(step_call_count=3):
audio=None,
function_call=None,
tool_calls=[
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id='call_mock_123456',
function=Function(
arguments='{ \
@@ -918,7 +918,7 @@ def test_tool_calling_sync(step_call_count=3):
),
type='function',
),
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id='call_mock_123457',
function=Function(
arguments='{"a": 0, "b": 10}',
@@ -952,7 +952,7 @@ def test_tool_calling_sync(step_call_count=3):
audio=None,
function_call=None,
tool_calls=[
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id='call_kIJby7Y6As6gAbWoxDqxD6HG',
function=Function(
arguments='{"a":16,"b":10}',
@@ -1079,7 +1079,7 @@ async def test_tool_calling_math_async(step_call_count=3):
audio=None,
function_call=None,
tool_calls=[
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id='call_mock_123456',
function=Function(
arguments='{ \
@@ -1186,7 +1186,7 @@ async def test_tool_calling_async(step_call_count=3):
# Reset tool_calls at the beginning of each new round of step() call
if model.run.call_count % 2 == 1:
model_backend_rsp_tool_async.choices[0].message.tool_calls = [
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id='call_mock_123456',
function=Function(
arguments='{"second":1}', name='async_sleep'
diff --git a/test/models/test_sglang_model.py b/test/models/test_sglang_model.py
index c44801dcb..08b4f6fdd 100644
--- a/test/models/test_sglang_model.py
+++ b/test/models/test_sglang_model.py
@@ -99,7 +99,7 @@ def test_sglang_function_call(sglang_model_cleanup):
# Create a mock response object
from openai.types.chat.chat_completion_message_function_tool_call import (
- ChatCompletionMessageToolCall,
+ ChatCompletionMessageFunctionToolCall,
)
from camel.types import (
@@ -122,7 +122,7 @@ def test_sglang_function_call(sglang_model_cleanup):
role="assistant",
content=None,
tool_calls=[
- ChatCompletionMessageToolCall(
+ ChatCompletionMessageFunctionToolCall(
id="0",
type="function",
function={"name": "test_tool", "arguments": "{}"},
diff --git a/test/toolkits/test_dingtalk.py b/test/toolkits/test_dingtalk.py
index 81c35c795..56743454e 100644
--- a/test/toolkits/test_dingtalk.py
+++ b/test/toolkits/test_dingtalk.py
@@ -82,7 +82,9 @@ def test_send_text_message(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.send_text_message('test_userid', 'Hello World')
+ result = toolkit.dingtalk_send_text_message(
+ 'test_userid', 'Hello World'
+ )
assert 'Message sent successfully' in result
@@ -96,7 +98,7 @@ def test_send_markdown_message(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.send_markdown_message(
+ result = toolkit.dingtalk_send_markdown_message(
'test_userid', 'Test Title', '# Hello World'
)
assert 'Markdown message sent successfully' in result
@@ -117,7 +119,7 @@ def test_get_user_info(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.get_user_info('test_userid')
+ result = toolkit.dingtalk_get_user_info('test_userid')
assert result == expected_data
@@ -133,7 +135,7 @@ def test_send_webhook_message(mock_post):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.send_webhook_message('Hello from webhook!')
+ result = toolkit.dingtalk_send_webhook_message('Hello from webhook!')
assert 'Webhook message sent successfully' in result
@@ -154,7 +156,7 @@ def test_get_department_list(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.get_department_list()
+ result = toolkit.dingtalk_get_department_list()
assert result == expected_data
@@ -175,7 +177,7 @@ def test_search_users_by_name(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.search_users_by_name('John')
+ result = toolkit.dingtalk_search_users_by_name('John')
assert result == expected_data
@@ -197,7 +199,7 @@ def test_missing_webhook_url():
# Test with no webhook URL in environment
with patch.dict('os.environ', {}, clear=True):
- result = toolkit.send_webhook_message('Hello')
+ result = toolkit.dingtalk_send_webhook_message('Hello')
assert 'Webhook URL not provided' in result
@@ -231,7 +233,7 @@ def test_send_text_message_error_handling(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.send_text_message('test_userid', 'Hello')
+ result = toolkit.dingtalk_send_text_message('test_userid', 'Hello')
assert 'Failed to send message' in result
@@ -245,7 +247,7 @@ def test_get_user_info_error_handling(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.get_user_info('test_userid')
+ result = toolkit.dingtalk_get_user_info('test_userid')
assert 'error' in result
assert 'Failed to get user info' in result['error']
@@ -277,7 +279,7 @@ def test_webhook_with_signature(mock_post):
toolkit = DingtalkToolkit()
# Test webhook with custom secret
- result = toolkit.send_webhook_message(
+ result = toolkit.dingtalk_send_webhook_message(
'Hello with signature!', webhook_secret='custom_secret'
)
assert 'Webhook message sent successfully' in result
@@ -315,7 +317,7 @@ def test_send_link_message(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.send_link_message(
+ result = toolkit.dingtalk_send_link_message(
'test_userid', 'Test Link', 'Click to visit', 'https://example.com'
)
assert 'Link message' in result and 'sent successfully' in result
@@ -331,7 +333,7 @@ def test_send_action_card_message(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.send_action_card_message(
+ result = toolkit.dingtalk_send_action_card_message(
'test_userid',
'Action Required',
'Please click the button below',
@@ -371,13 +373,13 @@ def test_get_user_by_mobile_validation():
# Test invalid mobile numbers
invalid_mobiles = [
- '1234567890', # 10 digits
+ '1234567890', # 10 digits
'123456789012', # 12 digits
- '23800000000', # doesn't start with 1
- '12800000000', # second digit is 2 (invalid)
- 'abc1380000000', # contains letters
- '138-0000-0000', # contains hyphens
- '', # empty string
+ '23800000000', # doesn't start with 1
+ '12800000000', # second digit is 2 (invalid)
+ 'abc1380000000', # contains letters
+ '138-0000-0000', # contains hyphens
+ '', # empty string
]
for invalid_mobile in invalid_mobiles:
@@ -402,7 +404,7 @@ def test_get_user_by_unionid(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.get_user_by_unionid('test_unionid')
+ result = toolkit.dingtalk_get_user_by_unionid('test_unionid')
assert result == expected_data
@@ -423,7 +425,7 @@ def test_get_department_detail(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.get_department_detail(1)
+ result = toolkit.dingtalk_get_department_detail(1)
assert result == expected_data
@@ -437,7 +439,7 @@ def test_send_oa_message(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.send_oa_message(
+ result = toolkit.dingtalk_send_oa_message(
'test_userid',
'https://example.com',
'FFBBBBBB',
@@ -465,7 +467,7 @@ def test_get_group_info(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.get_group_info('test_chatid')
+ result = toolkit.dingtalk_get_group_info('test_chatid')
assert result == expected_data
@@ -480,7 +482,7 @@ def test_update_group(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.update_group(
+ result = toolkit.dingtalk_update_group(
'test_chatid',
name='New Group Name',
add_useridlist=['user1', 'user2'],
@@ -498,7 +500,7 @@ def test_send_work_notification(mock_request):
) as mock_token:
mock_token.return_value = "test_access_token"
toolkit = DingtalkToolkit()
- result = toolkit.send_work_notification(
+ result = toolkit.dingtalk_send_work_notification(
['user1', 'user2', 'user3'],
'Important announcement for all team members.',
)
@@ -514,12 +516,12 @@ def test_send_work_notification_validation():
toolkit = DingtalkToolkit()
# Test empty user list
- result = toolkit.send_work_notification([], 'test message')
+ result = toolkit.dingtalk_send_work_notification([], 'test message')
assert 'userid_list cannot be empty' in result
# Test too many users
large_user_list = [f'user{i}' for i in range(101)]
- result = toolkit.send_work_notification(
+ result = toolkit.dingtalk_send_work_notification(
large_user_list, 'test message'
)
assert 'Cannot send to more than 100 users' in result
@@ -534,12 +536,12 @@ def test_extended_tools_count():
toolkit = DingtalkToolkit()
tools = toolkit.get_tools()
- # Should have original 9 tools + 9 new tools = 18 tools
- assert len(tools) == 18
+ # Should have 22 tools total
+ assert len(tools) == 22
# Check some specific new tools are present
tool_names = [tool.func.__name__ for tool in tools]
- assert 'send_link_message' in tool_names
- assert 'send_action_card_message' in tool_names
- assert 'get_user_by_mobile' in tool_names
- assert 'send_work_notification' in tool_names
+ assert 'dingtalk_send_link_message' in tool_names
+ assert 'dingtalk_send_action_card_message' in tool_names
+ assert 'dingtalk_get_user_by_mobile' in tool_names
+ assert 'dingtalk_send_work_notification' in tool_names