Skip to content

Commit 7148520

Browse files
committed
WIP trying tests out
1 parent d79538e commit 7148520

File tree

3 files changed

+99
-59
lines changed

3 files changed

+99
-59
lines changed

ollama/_types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime
55
from typing import (
66
Any,
7+
List,
78
Literal,
89
Mapping,
910
Optional,
@@ -229,13 +230,13 @@ class Function(SubscriptableBaseModel):
229230
description: str
230231

231232
class Parameters(SubscriptableBaseModel):
232-
type: Union[str, list[str]]
233+
type: Union[str, List[str]]
233234
required: Optional[Sequence[str]] = None
234235
properties: Optional[JsonSchemaValue] = None
235236

236237
parameters: Parameters
237238

238-
return_type: Optional[str | list[str]] = None
239+
return_type: Optional[Union[str, List[str]]] = None
239240

240241
function: Function
241242

tests/test_utils.py

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import sys
2+
3+
import pytest
4+
5+
if sys.version_info < (3, 10):
6+
pytest.skip('Python 3.10 or higher is required', allow_module_level=True)
7+
8+
19
from ollama._utils import _get_json_type, convert_function_to_tool, process_tools
210

311

@@ -15,7 +23,9 @@ def test_json_type_conversion():
1523

1624

1725
def test_function_to_tool_conversion():
18-
def add_numbers(x: int, y: int | None = None) -> int:
26+
from typing import Optional
27+
28+
def add_numbers(x: int, y: Optional[int] = None) -> int:
1929
"""Add two numbers together.
2030
Args:
2131
x (integer): The first number
@@ -84,62 +94,6 @@ def all_types(
8494
assert set(tool.function.return_type) == {'string', 'integer', 'array', 'object'}
8595

8696

87-
# assert tool.type == 'asjdlf'
88-
89-
90-
def test_function_with_all_typing_types():
91-
from typing import Dict, List, Mapping, Optional, Sequence, Set, Tuple, Union
92-
93-
def all_types(
94-
x: int,
95-
y: str,
96-
z: Sequence[int],
97-
w: Mapping[str, int],
98-
d: Dict[str, int],
99-
s: Set[int],
100-
t: Tuple[int, str],
101-
l: List[int], # noqa: E741
102-
o: Optional[int],
103-
) -> Union[Mapping[str, int], str, None]:
104-
"""
105-
A function with all types.
106-
Args:
107-
x (integer): The first number
108-
y (string): The second number
109-
z (array): The third number
110-
w (object): The fourth number
111-
d (object): The fifth number
112-
s (array): The sixth number
113-
t (array): The seventh number
114-
l (array): The eighth number
115-
o (integer | None): The ninth number
116-
"""
117-
pass
118-
119-
tool = convert_function_to_tool(all_types)
120-
assert tool.function.parameters.properties['x']['type'] == 'integer'
121-
assert tool.function.parameters.properties['y']['type'] == 'string'
122-
assert tool.function.parameters.properties['z']['type'] == 'array'
123-
assert tool.function.parameters.properties['w']['type'] == 'object'
124-
assert tool.function.parameters.properties['d']['type'] == 'object'
125-
assert tool.function.parameters.properties['s']['type'] == 'array'
126-
assert tool.function.parameters.properties['t']['type'] == 'array'
127-
assert tool.function.parameters.properties['l']['type'] == 'array'
128-
assert tool.function.parameters.properties['o']['type'] == 'integer'
129-
assert set(tool.function.return_type) == {'string', 'object'}
130-
131-
132-
def test_function_without_docstring():
133-
def simple_func(x: int):
134-
pass
135-
136-
try:
137-
convert_function_to_tool(simple_func)
138-
assert AssertionError('Should raise ValueError')
139-
except ValueError:
140-
pass
141-
142-
14397
def test_process_tools():
14498
def func1(x: int) -> str:
14599
"""Simple function 1.

tests/test_utils_legacy.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)