forked from ag2ai/ag2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tool.py
More file actions
62 lines (47 loc) · 1.84 KB
/
Copy pathtest_tool.py
File metadata and controls
62 lines (47 loc) · 1.84 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
# Copyright (c) 2023 - 2025, AG2ai, Inc., AG2ai open-source projects maintainers and core contributors
#
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
from autogen import AssistantAgent, UserProxyAgent
from autogen.tools import Tool
class TestTool:
@pytest.fixture(autouse=True)
def setup(self) -> None:
def f(x: str) -> str:
return x + "!"
self.tool = Tool(name="test_tool", description="A test tool", func_or_tool=f)
def test_init(self) -> None:
assert self.tool.name == "test_tool"
assert self.tool.description == "A test tool"
def test_register_for_llm(self) -> None:
config_list = [{"api_type": "openai", "model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY"]}]
agent = AssistantAgent(
name="agent",
llm_config={"config_list": config_list},
)
self.tool.register_for_llm(agent=agent)
expected_tools = [
{
"type": "function",
"function": {
"description": "A test tool",
"name": "test_tool",
"parameters": {
"type": "object",
"properties": {"x": {"type": "string", "description": "x"}},
"required": ["x"],
},
},
}
]
assert agent.llm_config["tools"] == expected_tools # type: ignore[index]
def test_register_for_execution(self) -> None:
user_proxy = UserProxyAgent(
name="user",
)
self.tool.register_for_execution(user_proxy)
assert user_proxy.can_execute_function("test_tool")
assert user_proxy.function_map["test_tool"]("Hello") == "Hello!"
def test__call__(self) -> None:
assert self.tool("Hello") == "Hello!"