test_tool_manager.py
4.57 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from app.service.tool_manager import ToolManager
class TestToolManagerRegister:
def test_register_function(self):
tm = ToolManager()
def get_weather(city: str) -> str:
"""查询城市天气"""
return f"{city}: 晴天 25°C"
tm.register_tool(get_weather)
assert "get_weather" in tm._tools
assert tm._tools["get_weather"].group == "basic"
assert "查询城市天气" in tm._tools["get_weather"].description
def test_register_with_custom_name_and_group(self):
tm = ToolManager()
def f(x: str) -> str:
"""desc"""
return x
tm.register_tool(f, name="custom_name", group="custom_group", description="自定义描述")
assert "custom_name" in tm._tools
assert tm._tools["custom_name"].group == "custom_group"
assert tm._tools["custom_name"].description == "自定义描述"
assert "custom_group" in tm.groups
def test_register_decorator(self):
tm = ToolManager()
@tm.register(group="vision")
def analyze_image(image_url: str) -> str:
"""分析图片内容"""
return f"分析结果: {image_url}"
assert "analyze_image" in tm._tools
assert tm._tools["analyze_image"].group == "vision"
assert tm.groups["vision"].active is False # 非 basic 组默认未激活
schema = tm._tools["analyze_image"].input_schema
assert "image_url" in schema["properties"]
class TestToolManagerCall:
def test_call_tool_success(self):
tm = ToolManager()
def add(a: int, b: int) -> int:
return a + b
tm.register_tool(add)
result = tm.call_tool({"name": "add", "input": {"a": 1, "b": 2}})
assert result == {"result": 3}
def test_call_tool_not_found(self):
tm = ToolManager()
result = tm.call_tool({"name": "nonexistent", "input": {}})
assert "error" in result
assert "未注册" in result["error"]
def test_call_tool_inactive_group(self):
tm = ToolManager()
def secret() -> str:
return "secret"
tm.register_tool(secret, group="admin")
result = tm.call_tool({"name": "secret", "input": {}})
assert "error" in result
assert "未激活" in result["error"]
def test_call_tool_execution_error(self):
tm = ToolManager()
def crash() -> str:
raise ValueError("boom")
tm.register_tool(crash)
result = tm.call_tool({"name": "crash", "input": {}})
assert "error" in result
assert "boom" in result["error"]
class TestToolManagerSchemas:
def test_get_tool_schemas_only_active_groups(self):
tm = ToolManager()
def tool_a(x: str) -> str:
"""Tool A"""
return x
def tool_b(y: int) -> int:
"""Tool B"""
return y
tm.register_tool(tool_a, group="basic")
tm.register_tool(tool_b, group="admin")
schemas = tm.get_tool_schemas()
assert len(schemas) == 1
assert schemas[0]["function"]["name"] == "tool_a"
def test_get_tool_schemas_empty(self):
tm = ToolManager()
schemas = tm.get_tool_schemas()
assert schemas == []
def test_schema_format_openai_compatible(self):
tm = ToolManager()
def search(query: str, limit: int = 10) -> str:
"""搜索知识库"""
return f"results for {query}"
tm.register_tool(search)
schemas = tm.get_tool_schemas()
assert len(schemas) == 1
s = schemas[0]
assert s["type"] == "function"
assert s["function"]["name"] == "search"
assert s["function"]["description"] == "搜索知识库"
assert "query" in s["function"]["parameters"]["properties"]
assert s["function"]["parameters"]["required"] == ["query"]
class TestToolManagerGroups:
def test_reset_equipped_tools(self):
tm = ToolManager()
def tool_a(x: str) -> str:
"""A"""
return x
def tool_b(y: str) -> str:
"""B"""
return y
tm.register_tool(tool_a, group="basic")
tm.register_tool(tool_b, group="vision")
assert len(tm.get_tool_schemas()) == 1
tm.reset_equipped_tools(basic=False, vision=True)
schemas = tm.get_tool_schemas()
assert len(schemas) == 1
assert schemas[0]["function"]["name"] == "tool_b"
def test_default_groups(self):
tm = ToolManager()
assert "basic" in tm.groups
assert tm.groups["basic"].active is True