22import io
33import json
44import httpx
5+ import urllib .parse
56from os import PathLike
67from pathlib import Path
78from hashlib import sha256
@@ -23,32 +24,29 @@ class BaseClient:
2324 def __init__ (
2425 self ,
2526 client ,
26- base_url : Optional [str ] = None ,
27+ host : Optional [str ] = None ,
2728 follow_redirects : bool = True ,
2829 timeout : Any = None ,
2930 ** kwargs ,
3031 ) -> None :
3132 """
3233 Creates a httpx client. Default parameters are the same as those defined in httpx
3334 except for the following:
34-
35- - `base_url`: http://127.0.0.1:11434
3635 - `follow_redirects`: True
3736 - `timeout`: None
38-
3937 `kwargs` are passed to the httpx client.
4038 """
4139 self ._client = client (
42- base_url = base_url or os .getenv ('OLLAMA_HOST' , 'http://127.0.0.1:11434' ),
40+ base_url = _parse_host ( host or os .getenv ('OLLAMA_HOST' ) ),
4341 follow_redirects = follow_redirects ,
4442 timeout = timeout ,
4543 ** kwargs ,
4644 )
4745
4846
4947class Client (BaseClient ):
50- def __init__ (self , base_url : Optional [str ] = None , ** kwargs ) -> None :
51- super ().__init__ (httpx .Client , base_url , ** kwargs )
48+ def __init__ (self , host : Optional [str ] = None , ** kwargs ) -> None :
49+ super ().__init__ (httpx .Client , host , ** kwargs )
5250
5351 def _request (self , method : str , url : str , ** kwargs ) -> httpx .Response :
5452 response = self ._client .request (method , url , ** kwargs )
@@ -308,8 +306,8 @@ def show(self, model: str) -> Mapping[str, Any]:
308306
309307
310308class AsyncClient (BaseClient ):
311- def __init__ (self , base_url : Optional [str ] = None , ** kwargs ) -> None :
312- super ().__init__ (httpx .AsyncClient , base_url , ** kwargs )
309+ def __init__ (self , host : Optional [str ] = None , ** kwargs ) -> None :
310+ super ().__init__ (httpx .AsyncClient , host , ** kwargs )
313311
314312 async def _request (self , method : str , url : str , ** kwargs ) -> httpx .Response :
315313 response = await self ._client .request (method , url , ** kwargs )
@@ -607,3 +605,53 @@ def _as_bytesio(s: Any) -> Union[io.BytesIO, None]:
607605 elif isinstance (s , bytes ):
608606 return io .BytesIO (s )
609607 return None
608+
609+
610+ def _parse_host (host : Optional [str ]) -> str :
611+ """
612+ >>> _parse_host(None)
613+ 'http://127.0.0.1:11434'
614+ >>> _parse_host('')
615+ 'http://127.0.0.1:11434'
616+ >>> _parse_host('1.2.3.4')
617+ 'http://1.2.3.4:11434'
618+ >>> _parse_host(':56789')
619+ 'http://127.0.0.1:56789'
620+ >>> _parse_host('1.2.3.4:56789')
621+ 'http://1.2.3.4:56789'
622+ >>> _parse_host('http://1.2.3.4')
623+ 'http://1.2.3.4:80'
624+ >>> _parse_host('https://1.2.3.4')
625+ 'https://1.2.3.4:443'
626+ >>> _parse_host('https://1.2.3.4:56789')
627+ 'https://1.2.3.4:56789'
628+ >>> _parse_host('example.com')
629+ 'http://example.com:11434'
630+ >>> _parse_host('example.com:56789')
631+ 'http://example.com:56789'
632+ >>> _parse_host('http://example.com')
633+ 'http://example.com:80'
634+ >>> _parse_host('https://example.com')
635+ 'https://example.com:443'
636+ >>> _parse_host('https://example.com:56789')
637+ 'https://example.com:56789'
638+ >>> _parse_host('example.com/')
639+ 'http://example.com:11434'
640+ >>> _parse_host('example.com:56789/')
641+ 'http://example.com:56789'
642+ """
643+
644+ host , port = host or '' , 11434
645+ scheme , _ , hostport = host .partition ('://' )
646+ if not hostport :
647+ scheme , hostport = 'http' , host
648+ elif scheme == 'http' :
649+ port = 80
650+ elif scheme == 'https' :
651+ port = 443
652+
653+ split = urllib .parse .urlsplit ('://' .join ([scheme , hostport ]))
654+ host = split .hostname or '127.0.0.1'
655+ port = split .port or port
656+
657+ return f'{ scheme } ://{ host } :{ port } '
0 commit comments