|
1 |
| -from __future__ import annotations |
| 1 | +# from __future__ import annotations |
2 | 2 |
|
3 |
| -from types import UnionType |
4 |
| -from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Union, get_args, get_origin |
| 3 | +from typing import Any, Callable, List, Mapping, Optional, Sequence, Union, get_args, get_origin |
5 | 4 | from ollama._types import Tool
|
| 5 | +from icecream import ic |
6 | 6 |
|
| 7 | +# Map both the type and the type reference to the same JSON type |
7 | 8 | PYTHON_TO_JSON_TYPES = {
|
8 |
| - str: 'string', |
9 | 9 | int: 'integer',
|
| 10 | + 'int': 'integer', |
| 11 | + str: 'string', |
| 12 | + 'str': 'string', |
10 | 13 | float: 'number',
|
| 14 | + 'float': 'number', |
11 | 15 | bool: 'boolean',
|
| 16 | + 'bool': 'boolean', |
12 | 17 | list: 'array',
|
| 18 | + 'list': 'array', |
13 | 19 | dict: 'object',
|
14 |
| - List: 'array', |
15 |
| - Dict: 'object', |
16 |
| - None: 'null', |
| 20 | + 'dict': 'object', |
| 21 | + type(None): 'null', |
| 22 | + 'None': 'null', |
17 | 23 | }
|
18 | 24 |
|
19 | 25 | """
|
|
26 | 32 |
|
27 | 33 |
|
28 | 34 | def _get_json_type(python_type: Any) -> str | List[str]:
|
| 35 | + # Print for debugging |
| 36 | + ic(f'Processing type: {python_type}') |
| 37 | + |
29 | 38 | # Handle Optional types (Union[type, None] and type | None)
|
30 | 39 | origin = get_origin(python_type)
|
31 |
| - if origin is UnionType or origin is Union: |
| 40 | + if origin is type(int | str) or origin is Union: |
32 | 41 | args = get_args(python_type)
|
33 | 42 | # Filter out None/NoneType from union args
|
34 | 43 | non_none_args = [arg for arg in args if arg not in (None, type(None))]
|
35 |
| - non_none_args_types = [PYTHON_TO_JSON_TYPES[arg] for arg in non_none_args] |
36 | 44 | if non_none_args:
|
37 | 45 | if len(non_none_args) == 1:
|
38 | 46 | return _get_json_type(non_none_args[0])
|
39 | 47 | else:
|
40 |
| - return non_none_args_types |
41 |
| - |
| 48 | + return [_get_json_type(arg) for arg in non_none_args] |
42 | 49 | return 'null'
|
43 | 50 |
|
44 |
| - # Get basic type mapping |
| 51 | + # Direct type check first (like int, str, etc.) |
45 | 52 | if python_type in PYTHON_TO_JSON_TYPES:
|
46 | 53 | return PYTHON_TO_JSON_TYPES[python_type]
|
47 | 54 |
|
48 | 55 | # Handle typing.List, typing.Dict etc.
|
49 |
| - if origin in PYTHON_TO_JSON_TYPES: |
50 |
| - return PYTHON_TO_JSON_TYPES[origin] |
| 56 | + if origin is not None: |
| 57 | + return PYTHON_TO_JSON_TYPES.get(origin, 'string') |
| 58 | + |
| 59 | + # Default to string if type is unknown |
| 60 | + raise ValueError(f'Unknown type: {python_type}') |
| 61 | + |
51 | 62 |
|
52 |
| - # TODO: Default to string if type is unknown - define beahvior |
53 |
| - return 'string' |
| 63 | +# return 'string' |
54 | 64 |
|
55 | 65 |
|
56 | 66 | def _is_optional_type(python_type: Any) -> bool:
|
57 | 67 | origin = get_origin(python_type)
|
58 |
| - if origin is UnionType or origin is Union: |
| 68 | + if origin is type(int | str) or origin is Union: |
59 | 69 | args = get_args(python_type)
|
60 | 70 | return any(arg in (None, type(None)) for arg in args)
|
61 | 71 | return False
|
@@ -89,6 +99,7 @@ def convert_function_to_tool(func: Callable) -> Tool:
|
89 | 99 |
|
90 | 100 | # Build parameters dict
|
91 | 101 | for param_name, param_type in func.__annotations__.items():
|
| 102 | + ic(f'Param name: {param_name}, param type: {param_type}') |
92 | 103 | if param_name == 'return':
|
93 | 104 | continue
|
94 | 105 |
|
@@ -120,6 +131,7 @@ def convert_function_to_tool(func: Callable) -> Tool:
|
120 | 131 | 'parameters': parameters,
|
121 | 132 | },
|
122 | 133 | }
|
| 134 | + ic(tool_dict) |
123 | 135 | return Tool.model_validate(tool_dict)
|
124 | 136 |
|
125 | 137 |
|
|
0 commit comments