22
22
overload ,
23
23
)
24
24
25
+ import anyio
25
26
from pydantic .json_schema import JsonSchemaValue
26
27
27
28
from ollama ._utils import convert_function_to_tool
@@ -75,6 +76,7 @@ def __init__(
75
76
self ,
76
77
client ,
77
78
host : Optional [str ] = None ,
79
+ * ,
78
80
follow_redirects : bool = True ,
79
81
timeout : Any = None ,
80
82
headers : Optional [Mapping [str , str ]] = None ,
@@ -253,7 +255,7 @@ def generate(
253
255
stream = stream ,
254
256
raw = raw ,
255
257
format = format ,
256
- images = [ image for image in _copy_images (images )] if images else None ,
258
+ images = list ( _copy_images (images )) if images else None ,
257
259
options = options ,
258
260
keep_alive = keep_alive ,
259
261
).model_dump (exclude_none = True ),
@@ -336,8 +338,8 @@ def add_two_numbers(a: int, b: int) -> int:
336
338
'/api/chat' ,
337
339
json = ChatRequest (
338
340
model = model ,
339
- messages = [ message for message in _copy_messages (messages )] ,
340
- tools = [ tool for tool in _copy_tools (tools )] ,
341
+ messages = list ( _copy_messages (messages )) ,
342
+ tools = list ( _copy_tools (tools )) ,
341
343
stream = stream ,
342
344
format = format ,
343
345
options = options ,
@@ -756,7 +758,7 @@ async def generate(
756
758
stream = stream ,
757
759
raw = raw ,
758
760
format = format ,
759
- images = [ image for image in _copy_images (images )] if images else None ,
761
+ images = list ( _copy_images (images )) if images else None ,
760
762
options = options ,
761
763
keep_alive = keep_alive ,
762
764
).model_dump (exclude_none = True ),
@@ -840,8 +842,8 @@ def add_two_numbers(a: int, b: int) -> int:
840
842
'/api/chat' ,
841
843
json = ChatRequest (
842
844
model = model ,
843
- messages = [ message for message in _copy_messages (messages )] ,
844
- tools = [ tool for tool in _copy_tools (tools )] ,
845
+ messages = list ( _copy_messages (messages )) ,
846
+ tools = list ( _copy_tools (tools )) ,
845
847
stream = stream ,
846
848
format = format ,
847
849
options = options ,
@@ -991,7 +993,7 @@ async def create(
991
993
parameters : Optional [Union [Mapping [str , Any ], Options ]] = None ,
992
994
messages : Optional [Sequence [Union [Mapping [str , Any ], Message ]]] = None ,
993
995
* ,
994
- stream : Literal [True ] = True ,
996
+ stream : Literal [False ] = False ,
995
997
) -> ProgressResponse : ...
996
998
997
999
@overload
@@ -1054,19 +1056,19 @@ async def create(
1054
1056
1055
1057
async def create_blob (self , path : Union [str , Path ]) -> str :
1056
1058
sha256sum = sha256 ()
1057
- with open (path , 'rb' ) as r :
1059
+ async with await anyio . open_file (path , 'rb' ) as r :
1058
1060
while True :
1059
- chunk = r .read (32 * 1024 )
1061
+ chunk = await r .read (32 * 1024 )
1060
1062
if not chunk :
1061
1063
break
1062
1064
sha256sum .update (chunk )
1063
1065
1064
1066
digest = f'sha256:{ sha256sum .hexdigest ()} '
1065
1067
1066
1068
async def upload_bytes ():
1067
- with open (path , 'rb' ) as r :
1069
+ async with await anyio . open_file (path , 'rb' ) as r :
1068
1070
while True :
1069
- chunk = r .read (32 * 1024 )
1071
+ chunk = await r .read (32 * 1024 )
1070
1072
if not chunk :
1071
1073
break
1072
1074
yield chunk
@@ -1133,7 +1135,7 @@ def _copy_images(images: Optional[Sequence[Union[Image, Any]]]) -> Iterator[Imag
1133
1135
def _copy_messages (messages : Optional [Sequence [Union [Mapping [str , Any ], Message ]]]) -> Iterator [Message ]:
1134
1136
for message in messages or []:
1135
1137
yield Message .model_validate (
1136
- {k : [ image for image in _copy_images (v )] if k == 'images' else v for k , v in dict (message ).items () if v },
1138
+ {k : list ( _copy_images (v )) if k == 'images' else v for k , v in dict (message ).items () if v },
1137
1139
)
1138
1140
1139
1141
@@ -1143,7 +1145,7 @@ def _copy_tools(tools: Optional[Sequence[Union[Mapping[str, Any], Tool, Callable
1143
1145
1144
1146
1145
1147
def _as_path (s : Optional [Union [str , PathLike ]]) -> Union [Path , None ]:
1146
- if isinstance (s , str ) or isinstance ( s , Path ):
1148
+ if isinstance (s , ( str , Path ) ):
1147
1149
try :
1148
1150
if (p := Path (s )).exists ():
1149
1151
return p
@@ -1225,7 +1227,7 @@ def _parse_host(host: Optional[str]) -> str:
1225
1227
elif scheme == 'https' :
1226
1228
port = 443
1227
1229
1228
- split = urllib .parse .urlsplit (' ://' . join ([ scheme , hostport ]) )
1230
+ split = urllib .parse .urlsplit (f' { scheme } ://{ hostport } ' )
1229
1231
host = split .hostname or '127.0.0.1'
1230
1232
port = split .port or port
1231
1233
0 commit comments