test_toolkit.py
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""AgentScope Toolkit behavior tests — verify group & schema management."""
import pytest
from agentscope.tool import Toolkit
class TestToolkitRegister:
def test_register_function_to_basic_group(self):
"""Register to implicit basic group — always returns schema."""
toolkit = Toolkit()
async def get_weather(city: str) -> str:
"""Query city weather."""
return f"{city}: sunny 25°C"
toolkit.register_tool_function(get_weather, group_name="basic")
schemas = toolkit.get_json_schemas()
names = [s["function"]["name"] for s in schemas]
assert "get_weather" in names
def test_register_to_uncreated_group_raises(self):
"""AgentScope: non-basic groups must be created first or ValueError."""
toolkit = Toolkit()
async def secret_tool(x: str) -> str:
"""Secret"""
return x
with pytest.raises(ValueError, match="not found"):
toolkit.register_tool_function(secret_tool, group_name="admin")
def test_register_with_created_group(self):
"""create_tool_group then register; default active=False, schemas hidden."""
toolkit = Toolkit()
toolkit.create_tool_group("admin", "Admin tools", active=False)
async def admin_tool(x: str) -> str:
"""Admin"""
return x
toolkit.register_tool_function(admin_tool, group_name="admin")
assert "admin" in toolkit.groups
assert toolkit.groups["admin"].active is False
schemas = toolkit.get_json_schemas()
assert len(schemas) == 0 # admin group inactive, no schemas exposed
class TestToolkitGroups:
def test_basic_group_not_in_groups_dict(self):
"""basic is implicit, always active, and not in self.groups."""
toolkit = Toolkit()
assert "basic" not in toolkit.groups
def test_update_tool_groups_activation(self):
"""update_tool_groups activates/deactivates tool groups."""
toolkit = Toolkit()
toolkit.create_tool_group("vision", "Vision tools", active=False)
async def tool_a(x: str) -> str:
"""A"""
return x
async def tool_b(y: str) -> str:
"""B"""
return y
toolkit.register_tool_function(tool_a, group_name="basic")
toolkit.register_tool_function(tool_b, group_name="vision")
assert len(toolkit.get_json_schemas()) == 1 # only basic
# Activate vision group
toolkit.update_tool_groups(["vision"], active=True)
schemas = toolkit.get_json_schemas()
assert len(schemas) == 2
names = [s["function"]["name"] for s in schemas]
assert "tool_a" in names
assert "tool_b" in names
def test_basic_group_always_active(self):
"""basic group cannot be deactivated via update_tool_groups."""
toolkit = Toolkit()
async def t(x: str) -> str:
"""T"""
return x
toolkit.register_tool_function(t, group_name="basic")
assert len(toolkit.get_json_schemas()) == 1
toolkit.update_tool_groups(["basic"], active=False)
assert len(toolkit.get_json_schemas()) == 1 # unchanged
class TestToolkitSchemaFormat:
def test_schema_openai_compatible(self):
toolkit = Toolkit()
async def search(query: str, limit: int = 10) -> str:
"""Search knowledge base."""
return f"results for {query}"
toolkit.register_tool_function(search, group_name="basic")
schemas = toolkit.get_json_schemas()
s = schemas[0]
assert s["type"] == "function"
assert s["function"]["name"] == "search"
assert "query" in s["function"]["parameters"]["properties"]