|
| 1 | +# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 14 | +import pytest |
| 15 | + |
| 16 | +from camel.toolkits import FunctionTool, tool |
| 17 | + |
| 18 | + |
| 19 | +@tool() |
| 20 | +def add( |
| 21 | + a: int, |
| 22 | + b: int = 0, |
| 23 | +) -> int: |
| 24 | + r"""Add two numbers and return their sum. |
| 25 | +
|
| 26 | + Args: |
| 27 | + a (int): The first number to add. |
| 28 | + b (int, optional): The second number to add. |
| 29 | + (default: :obj:`0`) |
| 30 | +
|
| 31 | + Returns: |
| 32 | + int: The sum of the two numbers. |
| 33 | + """ |
| 34 | + return a + b |
| 35 | + |
| 36 | + |
| 37 | +@tool(synthesize_output=True) |
| 38 | +def format_result( |
| 39 | + result: int, |
| 40 | +) -> str: |
| 41 | + r"""Format the calculation result. |
| 42 | +
|
| 43 | + Args: |
| 44 | + result (int): The number to format. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + str: A formatted string. |
| 48 | + """ |
| 49 | + return f"Result: {result}" |
| 50 | + |
| 51 | + |
| 52 | +def test_basic_tool_decorator(): |
| 53 | + r"""Test basic functionality of the tool decorator.""" |
| 54 | + assert isinstance(add, FunctionTool) |
| 55 | + |
| 56 | + assert add(1, 2) == 3 |
| 57 | + assert add(5) == 5 |
| 58 | + |
| 59 | + |
| 60 | +def test_tool_schema_generation(): |
| 61 | + r"""Test schema generation of the decorated function.""" |
| 62 | + schema = add.get_openai_tool_schema() |
| 63 | + |
| 64 | + assert schema["type"] == "function" |
| 65 | + assert "function" in schema |
| 66 | + |
| 67 | + func_schema = schema["function"] |
| 68 | + assert func_schema["name"] == "add" |
| 69 | + assert "description" in func_schema |
| 70 | + assert "parameters" in func_schema |
| 71 | + |
| 72 | + params = func_schema["parameters"] |
| 73 | + assert "properties" in params |
| 74 | + assert "a" in params["properties"] |
| 75 | + assert "b" in params["properties"] |
| 76 | + assert params["properties"]["a"]["type"] == "integer" |
| 77 | + assert "description" in params["properties"]["a"] |
| 78 | + |
| 79 | + |
| 80 | +def test_tool_with_synthesis(): |
| 81 | + r"""Test the tool decorator with output synthesis enabled.""" |
| 82 | + assert format_result.synthesize_output is True |
| 83 | + |
| 84 | + result = format_result(42) |
| 85 | + assert isinstance(result, str) |
| 86 | + assert "42" in result |
| 87 | + |
| 88 | + |
| 89 | +def test_tool_without_parentheses(): |
| 90 | + r"""Test the tool decorator without parentheses (@tool instead of |
| 91 | + @tool()). |
| 92 | + """ |
| 93 | + |
| 94 | + @tool |
| 95 | + def subtract(a: int, b: int = 0) -> int: |
| 96 | + r"""Subtract two numbers. |
| 97 | +
|
| 98 | + Args: |
| 99 | + a (int): The first number. |
| 100 | + b (int, optional): The number to subtract. (default: :obj:`0`) |
| 101 | +
|
| 102 | + Returns: |
| 103 | + int: The difference. |
| 104 | + """ |
| 105 | + return a - b |
| 106 | + |
| 107 | + assert isinstance(subtract, FunctionTool) |
| 108 | + assert subtract(5, 3) == 2 |
| 109 | + assert subtract(10) == 10 |
| 110 | + |
| 111 | + schema = subtract.get_openai_tool_schema() |
| 112 | + assert schema["function"]["name"] == "subtract" |
| 113 | + |
| 114 | + |
| 115 | +def test_custom_schema(): |
| 116 | + r"""Test the tool decorator with custom schema.""" |
| 117 | + custom_schema = { |
| 118 | + "type": "function", |
| 119 | + "function": { |
| 120 | + "name": "custom_add", |
| 121 | + "description": "Custom add function", |
| 122 | + "parameters": { |
| 123 | + "type": "object", |
| 124 | + "properties": { |
| 125 | + "a": {"type": "integer", "description": "First number"}, |
| 126 | + "b": {"type": "integer", "description": "Second number"}, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + @tool(openai_tool_schema=custom_schema) |
| 133 | + def custom_add(a: int, b: int = 0) -> int: |
| 134 | + r"""Custom add function.""" |
| 135 | + return a + b |
| 136 | + |
| 137 | + schema = custom_add.get_openai_tool_schema() |
| 138 | + assert schema["function"]["name"] == "custom_add" |
| 139 | + assert schema["function"]["description"] == "Custom add function" |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + pytest.main([__file__]) |
0 commit comments