Spaces:
Running
Running
File size: 4,789 Bytes
5f923cd | 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 160 161 162 163 164 165 166 167 | # Copyright 2026 The ODML Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections.abc import Sequence
from absl.testing import absltest
from absl.testing import parameterized
import litert_lm
# Helper functions for parameterized tests
def multiply(a: int, b: int) -> int:
"""Multiplies two integers.
Args:
a: The first integer.
b: The second integer.
"""
return a * b
def greet(name: str, greeting: str = "Hello") -> str:
"""Greets a person."""
return f"{greeting}, {name}!"
def get_weather(location: str):
"""Gets weather for a location.
Args:
location (str): The name of the city.
"""
return f"Weather in {location} is sunny."
class ToolTest(parameterized.TestCase):
@parameterized.named_parameters(
dict(
testcase_name="basic_function",
func=multiply,
expected_desc={
"type": "function",
"function": {
"name": "multiply",
"description": "Multiplies two integers.",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "integer",
"description": "The first integer.",
},
"b": {
"type": "integer",
"description": "The second integer.",
},
},
"required": ["a", "b"],
},
},
},
execute_cases=[({"a": 2, "b": 3}, 6)],
),
dict(
testcase_name="function_with_defaults",
func=greet,
expected_name="greet",
expected_required=["name"],
execute_cases=[
({"name": "Alice"}, "Hello, Alice!"),
({"name": "Bob", "greeting": "Hi"}, "Hi, Bob!"),
],
),
dict(
testcase_name="type_hints_in_docstring",
func=get_weather,
expected_properties={
"location": {
"type": "string",
"description": "The name of the city.",
}
},
execute_cases=[
({"location": "London"}, "Weather in London is sunny.")
],
),
)
def test_tool_from_function_cases(
self,
func,
execute_cases,
expected_desc=None,
expected_name=None,
expected_required=None,
expected_properties=None,
):
tool = litert_lm.tool_from_function(func)
self.assertIsInstance(tool, litert_lm.Tool)
desc = tool.get_tool_description()
if expected_desc is not None:
self.assertEqual(desc, expected_desc)
if expected_name is not None:
self.assertEqual(desc["function"]["name"], expected_name)
if expected_required is not None:
self.assertEqual(
desc["function"]["parameters"]["required"], expected_required
)
if expected_properties is not None:
self.assertEqual(
desc["function"]["parameters"]["properties"], expected_properties
)
for args, expected_result in execute_cases:
result = tool.execute(args)
self.assertEqual(result, expected_result)
@parameterized.named_parameters(
("list", list),
("sequence", Sequence),
)
def test_tool_from_function_array(self, type_hint):
def product(numbers: type_hint[float]) -> float:
"""Calculates the product of a list of numbers.
Args:
numbers: List of numbers to multiply.
"""
res = 1.0
for n in numbers:
res *= n
return res
tool = litert_lm.tool_from_function(product)
desc = tool.get_tool_description()
self.assertEqual(desc["function"]["name"], "product")
expected_properties = {
"numbers": {
"type": "array",
"items": {"type": "number"},
"description": "List of numbers to multiply.",
}
}
self.assertEqual(
desc["function"]["parameters"]["properties"], expected_properties
)
self.assertEqual(tool.execute({"numbers": [2.0, 3.5]}), 7.0)
if __name__ == "__main__":
absltest.main()
|