Skip to content

Commit c85f246

Browse files
committed
test: add default_tools test, adapt tool set
1 parent d379f1b commit c85f246

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

tests/example_tools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,13 @@ def slow(duration: int) -> str:
8484

8585
time.sleep(duration)
8686
return "Done"
87+
88+
89+
def speak(text: str) -> str:
90+
"""
91+
Loudly say something to the user via speakers.
92+
93+
:param text: The text to speak.
94+
:return: The quotient of a and b.
95+
"""
96+
return f"Successfully said `{text}`."

tests/test_tool_library.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def test_init(self):
5252
"multiply",
5353
"divide",
5454
"slow",
55+
"speak",
5556
},
5657
"Initializing tool library with functions failed.",
5758
)
@@ -71,6 +72,7 @@ def test_init_verbose_id(self):
7172
"tests__example_tools__multiply",
7273
"tests__example_tools__divide",
7374
"tests__example_tools__slow",
75+
"tests__example_tools__speak",
7476
},
7577
"Initializing tool library with functions failed.",
7678
)
@@ -128,7 +130,7 @@ def test_init_file_unspecified(self):
128130
functions = tulip.collection.get(include=[])["ids"]
129131
self.assertEqual(
130132
len(functions),
131-
5,
133+
6,
132134
"Functions from file were not added in first initialization.",
133135
)
134136
tulip = ToolLibrary(chroma_sub_dir="test/")
@@ -232,6 +234,7 @@ def test_load_file(self):
232234
"multiply",
233235
"divide",
234236
"slow",
237+
"speak",
235238
},
236239
"Loading entire file failed.",
237240
)
@@ -294,6 +297,7 @@ def test_remove_tool(self):
294297
"multiply",
295298
"divide",
296299
"slow",
300+
"speak",
297301
},
298302
"Removing function failed.",
299303
)
@@ -313,9 +317,7 @@ def test_execute(self):
313317
tulip = ToolLibrary(
314318
chroma_sub_dir="test/", file_imports=[("tests.example_tools", ["multiply"])]
315319
)
316-
res, error = tulip.execute(
317-
tool_id="multiply", arguments={"a": 2.0, "b": 2.0}
318-
)
320+
res, error = tulip.execute(tool_id="multiply", arguments={"a": 2.0, "b": 2.0})
319321
self.assertEqual(error, False, "Function execution failed.")
320322
self.assertEqual(
321323
res,
@@ -329,9 +331,7 @@ def test_execute_timeout(self):
329331
file_imports=[("tests.example_tools", ["slow"])],
330332
default_timeout=1,
331333
)
332-
res, error = tulip.execute(
333-
tool_id="slow", arguments={"duration": 2}
334-
)
334+
res, error = tulip.execute(tool_id="slow", arguments={"duration": 2})
335335
self.assertEqual(error, True, "Function execution succeeded despite timeout.")
336336
self.assertEqual(
337337
res,
@@ -343,9 +343,7 @@ def test_execute_unknown_tool(self):
343343
tulip = ToolLibrary(
344344
chroma_sub_dir="test/", file_imports=[("tests.example_tools", [])]
345345
)
346-
res, error = tulip.execute(
347-
tool_id="unknown", arguments={}
348-
)
346+
res, error = tulip.execute(tool_id="unknown", arguments={})
349347
self.assertEqual(error, True, "Calling unknown function not caught.")
350348
self.assertEqual(
351349
res,
@@ -357,9 +355,7 @@ def test_execute_invalid_arguments(self):
357355
tulip = ToolLibrary(
358356
chroma_sub_dir="test/", file_imports=[("tests.example_tools", ["multiply"])]
359357
)
360-
res, error = tulip.execute(
361-
tool_id="multiply", arguments={"a": 1, "wrong": 2}
362-
)
358+
res, error = tulip.execute(tool_id="multiply", arguments={"a": 1, "wrong": 2})
363359
self.assertEqual(
364360
error, True, "Function execution succeeded despite wrong arguments."
365361
)

tests/test_tulip_agent.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,25 @@ def test_cot_tulip_query_with_instance(self):
127127
"LLM query failed.",
128128
)
129129

130+
def test_default_tools(self):
131+
character = (
132+
"You must solve the task provided by the user using a tool. "
133+
"Eventually use the speak function to tell them the result."
134+
)
135+
agent = MinimalTulipAgent(
136+
tool_library=self.tulip,
137+
default_tools=[self.tulip.tools["speak"]],
138+
top_k_functions=1,
139+
instructions=character,
140+
)
141+
res = agent.query(prompt="What is 2+2?")
142+
self.assertTrue(
143+
any(s in res.lower() for s in ("4", "four"))
144+
and agent.messages[-2]["role"] == "tool"
145+
and agent.messages[-2]["name"] == "speak",
146+
"Using default_tool failed.",
147+
)
148+
130149

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

0 commit comments

Comments
 (0)