|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
| 4 | +from typing import Dict, List, Mapping, Optional, Sequence, Set, Tuple, Union |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +if sys.version_info >= (3, 10): |
| 9 | + pytest.skip('Python 3.9 or lower is required', allow_module_level=True) |
| 10 | + |
| 11 | +from ollama._utils import _get_json_type, convert_function_to_tool |
| 12 | + |
| 13 | + |
| 14 | +def test_json_type_conversion(): |
| 15 | + # Test basic types |
| 16 | + assert _get_json_type(str) == 'string' |
| 17 | + assert _get_json_type(int) == 'integer' |
| 18 | + assert _get_json_type(List) == 'array' |
| 19 | + assert _get_json_type(Dict) == 'object' |
| 20 | + |
| 21 | + # Test Optional |
| 22 | + assert _get_json_type(Optional[str]) == 'string' |
| 23 | + |
| 24 | + |
| 25 | +def test_function_to_tool_conversion(): |
| 26 | + def add_numbers(x: int, y: Union[int, None] = None) -> int: # Changed Optional to Union |
| 27 | + """Add two numbers together. |
| 28 | + Args: |
| 29 | + x (integer): The first number |
| 30 | + y (integer, optional): The second number |
| 31 | +
|
| 32 | + Returns: |
| 33 | + integer: The sum of x and y |
| 34 | + """ |
| 35 | + return x + y |
| 36 | + |
| 37 | + tool = convert_function_to_tool(add_numbers) |
| 38 | + |
| 39 | + assert tool['type'] == 'function' |
| 40 | + assert tool['function']['name'] == 'add_numbers' |
| 41 | + assert tool['function']['description'] == 'Add two numbers together.' |
| 42 | + assert tool['function']['parameters']['type'] == 'object' |
| 43 | + assert tool['function']['parameters']['properties']['x']['type'] == 'integer' |
| 44 | + assert tool['function']['parameters']['properties']['x']['description'] == 'The first number' |
| 45 | + assert tool['function']['parameters']['required'] == ['x'] |
| 46 | + |
| 47 | + |
| 48 | +def test_function_with_all_typing_types(): |
| 49 | + def all_types( |
| 50 | + x: int, |
| 51 | + y: str, |
| 52 | + z: Sequence[int], |
| 53 | + w: Mapping[str, int], |
| 54 | + d: Dict[str, int], |
| 55 | + s: Set[int], |
| 56 | + t: Tuple[int, str], |
| 57 | + l: List[int], # noqa: E741 |
| 58 | + o: Union[int, None], # Changed Optional to Union |
| 59 | + ) -> Union[Mapping[str, int], str, None]: |
| 60 | + """ |
| 61 | + A function with all types. |
| 62 | + Args: |
| 63 | + x (integer): The first number |
| 64 | + y (string): The second number |
| 65 | + z (array): The third number |
| 66 | + w (object): The fourth number |
| 67 | + d (object): The fifth number |
| 68 | + s (array): The sixth number |
| 69 | + t (array): The seventh number |
| 70 | + l (array): The eighth number |
| 71 | + o (integer | None): The ninth number |
| 72 | + """ |
| 73 | + pass |
| 74 | + |
| 75 | + tool = convert_function_to_tool(all_types) |
| 76 | + assert tool.function.parameters.properties['x']['type'] == 'integer' |
| 77 | + assert tool.function.parameters.properties['y']['type'] == 'string' |
| 78 | + assert tool.function.parameters.properties['z']['type'] == 'array' |
| 79 | + assert tool.function.parameters.properties['w']['type'] == 'object' |
| 80 | + assert tool.function.parameters.properties['d']['type'] == 'object' |
| 81 | + assert tool.function.parameters.properties['s']['type'] == 'array' |
| 82 | + assert tool.function.parameters.properties['t']['type'] == 'array' |
| 83 | + assert tool.function.parameters.properties['l']['type'] == 'array' |
| 84 | + assert tool.function.parameters.properties['o']['type'] == 'integer' |
| 85 | + assert set(tool.function.return_type) == {'string', 'object'} |
0 commit comments