2222 overload ,
2323)
2424
25+ import anyio
2526from pydantic .json_schema import JsonSchemaValue
2627
2728from ollama ._utils import convert_function_to_tool
@@ -75,6 +76,7 @@ def __init__(
7576 self ,
7677 client ,
7778 host : Optional [str ] = None ,
79+ * ,
7880 follow_redirects : bool = True ,
7981 timeout : Any = None ,
8082 headers : Optional [Mapping [str , str ]] = None ,
@@ -253,7 +255,7 @@ def generate(
253255 stream = stream ,
254256 raw = raw ,
255257 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 ,
257259 options = options ,
258260 keep_alive = keep_alive ,
259261 ).model_dump (exclude_none = True ),
@@ -336,8 +338,8 @@ def add_two_numbers(a: int, b: int) -> int:
336338 '/api/chat' ,
337339 json = ChatRequest (
338340 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 )) ,
341343 stream = stream ,
342344 format = format ,
343345 options = options ,
@@ -756,7 +758,7 @@ async def generate(
756758 stream = stream ,
757759 raw = raw ,
758760 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 ,
760762 options = options ,
761763 keep_alive = keep_alive ,
762764 ).model_dump (exclude_none = True ),
@@ -840,8 +842,8 @@ def add_two_numbers(a: int, b: int) -> int:
840842 '/api/chat' ,
841843 json = ChatRequest (
842844 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 )) ,
845847 stream = stream ,
846848 format = format ,
847849 options = options ,
@@ -991,7 +993,7 @@ async def create(
991993 parameters : Optional [Union [Mapping [str , Any ], Options ]] = None ,
992994 messages : Optional [Sequence [Union [Mapping [str , Any ], Message ]]] = None ,
993995 * ,
994- stream : Literal [True ] = True ,
996+ stream : Literal [False ] = False ,
995997 ) -> ProgressResponse : ...
996998
997999 @overload
@@ -1054,19 +1056,19 @@ async def create(
10541056
10551057 async def create_blob (self , path : Union [str , Path ]) -> str :
10561058 sha256sum = sha256 ()
1057- with open (path , 'rb' ) as r :
1059+ async with await anyio . open_file (path , 'rb' ) as r :
10581060 while True :
1059- chunk = r .read (32 * 1024 )
1061+ chunk = await r .read (32 * 1024 )
10601062 if not chunk :
10611063 break
10621064 sha256sum .update (chunk )
10631065
10641066 digest = f'sha256:{ sha256sum .hexdigest ()} '
10651067
10661068 async def upload_bytes ():
1067- with open (path , 'rb' ) as r :
1069+ async with await anyio . open_file (path , 'rb' ) as r :
10681070 while True :
1069- chunk = r .read (32 * 1024 )
1071+ chunk = await r .read (32 * 1024 )
10701072 if not chunk :
10711073 break
10721074 yield chunk
@@ -1133,7 +1135,7 @@ def _copy_images(images: Optional[Sequence[Union[Image, Any]]]) -> Iterator[Imag
11331135def _copy_messages (messages : Optional [Sequence [Union [Mapping [str , Any ], Message ]]]) -> Iterator [Message ]:
11341136 for message in messages or []:
11351137 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 },
11371139 )
11381140
11391141
@@ -1143,7 +1145,7 @@ def _copy_tools(tools: Optional[Sequence[Union[Mapping[str, Any], Tool, Callable
11431145
11441146
11451147def _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 ) ):
11471149 try :
11481150 if (p := Path (s )).exists ():
11491151 return p
@@ -1225,7 +1227,7 @@ def _parse_host(host: Optional[str]) -> str:
12251227 elif scheme == 'https' :
12261228 port = 443
12271229
1228- split = urllib .parse .urlsplit (' ://' . join ([ scheme , hostport ]) )
1230+ split = urllib .parse .urlsplit (f' { scheme } ://{ hostport } ' )
12291231 host = split .hostname or '127.0.0.1'
12301232 port = split .port or port
12311233
0 commit comments