Skip to content

Commit

Permalink
Fix the error within service toolkit.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavdGao committed Feb 14, 2025
1 parent 62b6734 commit 705b332
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
10 changes: 2 additions & 8 deletions src/agentscope/service/execute_code/exec_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

def execute_python_code(
code: str,
timeout: Optional[Union[int, float]] = 300,
use_docker: Optional[Union[bool, str]] = None,
timeout: Optional[int] = 300,
use_docker: bool = False,
maximum_memory_bytes: Optional[int] = None,
) -> ServiceResponse:
"""
Expand Down Expand Up @@ -184,12 +184,6 @@ def _execute_python_code_sys(
being maliciously harmful is low, yet there exists a risk of unintended
destructive behavior arising from the model's limitations or misalignment.
"""
logger.warning(
"Executing code in system environments. There exists a risk of "
"unintended destructive behavior. Please consider using a "
"containerized environment.",
)

manager = multiprocessing.Manager()
shared_list = manager.list()

Expand Down
15 changes: 13 additions & 2 deletions src/agentscope/service/service_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
parse = None


def _get_type_str(cls: Any) -> Optional[Union[str, list]]:
def _get_type_str( # pylint: disable=too-many-branches
cls: Any,
) -> Optional[Union[str, list]]:
"""Get the type string."""
type_str = None
if hasattr(cls, "__origin__"):
Expand All @@ -50,8 +52,10 @@ def _get_type_str(cls: Any) -> Optional[Union[str, list]]:
# Normal class
if cls is str:
type_str = "string"
elif cls in [float, int, complex]:
elif cls in [float, complex]:
type_str = "number"
elif cls is int:
type_str = "integer"
elif cls is bool:
type_str = "boolean"
elif cls in [list, tuple]:
Expand Down Expand Up @@ -555,6 +559,13 @@ def bing_search(query: str, api_key: str, num_results: int=10):
try:
required_type = _get_type_str(args_types[key])
arg_property["type"] = required_type
if required_type == "array":
array_item_typs = get_args(args_types[key])
if len(array_item_typs) != 0:
arg_property["items"] = {
"type": _get_type_str(array_item_typs[0]),
}

except Exception:
logger.warning(
f"Fail and skip to get the type of the "
Expand Down
65 changes: 65 additions & 0 deletions tests/service_toolkit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,71 @@ def test_multi_tagged_content(self) -> None:
},
)

def test_array_argument(self) -> None:
"""Test array argument in tool function."""

def func(
a: list,
b: list[int],
c: list[str],
d: list[float],
e: list[bool],
) -> list:
return a + b + c + d + e

toolkit = ServiceToolkit()
toolkit.add(func)

self.assertDictEqual(
toolkit.json_schemas["func"],
{
"type": "function",
"function": {
"name": "func",
"description": "",
"parameters": {
"type": "object",
"properties": {
"d": {
"type": "array",
"items": {
"type": "number",
},
},
"a": {
"type": "array",
},
"e": {
"type": "array",
"items": {
"type": "boolean",
},
},
"c": {
"type": "array",
"items": {
"type": "string",
},
},
"b": {
"type": "array",
"items": {
"type": "integer",
},
},
},
"required": [
"a",
"b",
"c",
"d",
"e",
],
},
},
},
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 705b332

Please sign in to comment.