Skip to content

Commit 43d3648

Browse files
authored
Merge pull request #16 from jmorganca/ollama-host
fix: update OLLAMA_HOST parsing to match ollama CLI
2 parents bf46f31 + 3119736 commit 43d3648

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

ollama/_client.py

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import io
33
import json
44
import httpx
5+
import urllib.parse
56
from os import PathLike
67
from pathlib import Path
78
from 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

4947
class 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

310308
class 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}'

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ indent-style = "space"
3535
[tool.ruff.lint]
3636
select = ["E", "F", "B"]
3737
ignore = ["E501"]
38+
39+
[tool.pytest.ini_options]
40+
addopts = '--doctest-modules --ignore examples'

0 commit comments

Comments
 (0)