Skip to content

Commit 6f8cd93

Browse files
committed
wip
1 parent 1ef75a7 commit 6f8cd93

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

ollama/_utils.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
from __future__ import annotations
1+
# from __future__ import annotations
22

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
54
from ollama._types import Tool
5+
from icecream import ic
66

7+
# Map both the type and the type reference to the same JSON type
78
PYTHON_TO_JSON_TYPES = {
8-
str: 'string',
99
int: 'integer',
10+
'int': 'integer',
11+
str: 'string',
12+
'str': 'string',
1013
float: 'number',
14+
'float': 'number',
1115
bool: 'boolean',
16+
'bool': 'boolean',
1217
list: 'array',
18+
'list': 'array',
1319
dict: 'object',
14-
List: 'array',
15-
Dict: 'object',
16-
None: 'null',
20+
'dict': 'object',
21+
type(None): 'null',
22+
'None': 'null',
1723
}
1824

1925
"""
@@ -26,36 +32,40 @@
2632

2733

2834
def _get_json_type(python_type: Any) -> str | List[str]:
35+
# Print for debugging
36+
ic(f'Processing type: {python_type}')
37+
2938
# Handle Optional types (Union[type, None] and type | None)
3039
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:
3241
args = get_args(python_type)
3342
# Filter out None/NoneType from union args
3443
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]
3644
if non_none_args:
3745
if len(non_none_args) == 1:
3846
return _get_json_type(non_none_args[0])
3947
else:
40-
return non_none_args_types
41-
48+
return [_get_json_type(arg) for arg in non_none_args]
4249
return 'null'
4350

44-
# Get basic type mapping
51+
# Direct type check first (like int, str, etc.)
4552
if python_type in PYTHON_TO_JSON_TYPES:
4653
return PYTHON_TO_JSON_TYPES[python_type]
4754

4855
# 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+
5162

52-
# TODO: Default to string if type is unknown - define beahvior
53-
return 'string'
63+
# return 'string'
5464

5565

5666
def _is_optional_type(python_type: Any) -> bool:
5767
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:
5969
args = get_args(python_type)
6070
return any(arg in (None, type(None)) for arg in args)
6171
return False
@@ -89,6 +99,7 @@ def convert_function_to_tool(func: Callable) -> Tool:
8999

90100
# Build parameters dict
91101
for param_name, param_type in func.__annotations__.items():
102+
ic(f'Param name: {param_name}, param type: {param_type}')
92103
if param_name == 'return':
93104
continue
94105

@@ -120,6 +131,7 @@ def convert_function_to_tool(func: Callable) -> Tool:
120131
'parameters': parameters,
121132
},
122133
}
134+
ic(tool_dict)
123135
return Tool.model_validate(tool_dict)
124136

125137

0 commit comments

Comments
 (0)