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: aci toolkit async support (#3044)

Xiaotian Jinbca70a2f70f3
diff --git a/camel/toolkits/aci_toolkit.py b/camel/toolkits/aci_toolkit.py
index 0639b39f8..6e85a231a 100644
--- a/camel/toolkits/aci_toolkit.py
+++ b/camel/toolkits/aci_toolkit.py
@@ -12,6 +12,7 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+import asyncio
import os
from typing import TYPE_CHECKING, Dict, List, Optional, Union
@@ -408,6 +409,38 @@ class ACIToolkit(BaseToolkit):
)
return result
+ async def aexecute_function(
+ self,
+ function_name: str,
+ function_arguments: Dict,
+ linked_account_owner_id: str,
+ allowed_apps_only: bool = False,
+ ) -> Dict:
+ r"""Execute a function call asynchronously.
+
+ Args:
+ function_name (str): Name of the function to execute.
+ function_arguments (Dict): Arguments to pass to the function.
+ linked_account_owner_id (str): To specify the end-user (account
+ owner) on behalf of whom you want to execute functions
+ You need to first link corresponding account with the same
+ owner id in the ACI dashboard (https://platform.aci.dev).
+ allowed_apps_only (bool): If true, only returns functions/apps
+ that are allowed to be used by the agent/accessor, identified
+ by the api key. (default: :obj:`False`)
+
+ Returns:
+ Dict: Result of the function execution
+ """
+ result = await asyncio.to_thread(
+ self.client.handle_function_call,
+ function_name,
+ function_arguments,
+ linked_account_owner_id,
+ allowed_apps_only,
+ )
+ return result
+
def get_tools(self) -> List[FunctionTool]:
r"""Get a list of tools (functions) available in the configured apps.
@@ -434,6 +467,8 @@ class ACIToolkit(BaseToolkit):
FunctionTool(self.delete_linked_account),
FunctionTool(self.function_definition),
FunctionTool(self.search_function),
+ FunctionTool(self.execute_function),
+ FunctionTool(self.aexecute_function),
]
for function in _all_function:
@@ -448,6 +483,16 @@ class ACIToolkit(BaseToolkit):
linked_account_owner_id=self.linked_account_owner_id,
)
+ async def async_dummy_func(*, schema=schema, **kwargs):
+ return await self.aexecute_function(
+ function_name=schema['function']['name'],
+ function_arguments=kwargs,
+ linked_account_owner_id=self.linked_account_owner_id,
+ )
+
+ # Add async_call method to the sync function for compatibility
+ dummy_func.async_call = async_dummy_func # type: ignore[attr-defined]
+
tool = FunctionTool(
func=dummy_func,
openai_tool_schema=schema,
diff --git a/test/toolkits/test_aci_toolkit.py b/test/toolkits/test_aci_toolkit.py
index 78e9ffeaa..ac3364c94 100644
--- a/test/toolkits/test_aci_toolkit.py
+++ b/test/toolkits/test_aci_toolkit.py
@@ -249,7 +249,7 @@ def test_get_tools(mock_aci):
result = toolkit.get_tools()
# Verify results
- assert len(result) == 14
+ assert len(result) == 16
mock_client.app_configurations.list.assert_called_once()
mock_client.functions.search.assert_called_once()
mock_client.functions.get_definition.assert_called_once_with(
@@ -279,3 +279,31 @@ def test_execute_function(mock_aci):
mock_client.handle_function_call.assert_called_once_with(
"test_function", {"arg1": "value1"}, "test_owner", True
)
+
+
+@patch("aci.ACI")
+async def test_aexecute_function(mock_aci):
+ r"""Test aexecute_function async method."""
+ # Setup mock
+ mock_client = MagicMock()
+ mock_aci.return_value = mock_client
+ mock_client.handle_function_call.return_value = {"result": "async_success"}
+
+ # Initialize toolkit and call async method
+ toolkit = ACIToolkit(api_key="test_key")
+ result = await toolkit.aexecute_function(
+ function_name="test_async_function",
+ function_arguments={"arg1": "async_value1"},
+ linked_account_owner_id="test_async_owner",
+ allowed_apps_only=False,
+ )
+
+ # Verify results
+ assert result == {"result": "async_success"}
+ mock_client.handle_function_call.assert_called_once_with(
+ "test_async_function",
+ {"arg1": "async_value1"},
+ "test_async_owner",
+ False,
+ )