Skip to content

Commit 73e207a

Browse files
committed
Address comments
1 parent 1eb18ec commit 73e207a

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

ollama/_client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def __init__(
106106
)
107107

108108

109+
CONNECTION_ERROR_MESSAGE = 'Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download'
110+
111+
109112
class Client(BaseClient):
110113
def __init__(self, host: Optional[str] = None, **kwargs) -> None:
111114
super().__init__(httpx.Client, host, **kwargs)
@@ -118,7 +121,7 @@ def _request_raw(self, *args, **kwargs):
118121
except httpx.HTTPStatusError as e:
119122
raise ResponseError(e.response.text, e.response.status_code) from None
120123
except httpx.ConnectError:
121-
raise ConnectionError('Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download') from None
124+
raise ConnectionError(CONNECTION_ERROR_MESSAGE) from None
122125

123126
@overload
124127
def _request(
@@ -622,7 +625,7 @@ async def _request_raw(self, *args, **kwargs):
622625
except httpx.HTTPStatusError as e:
623626
raise ResponseError(e.response.text, e.response.status_code) from None
624627
except httpx.ConnectError:
625-
raise ConnectionError('Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download') from None
628+
raise ConnectionError(CONNECTION_ERROR_MESSAGE) from None
626629

627630
@overload
628631
async def _request(

tests/test_client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pytest_httpserver import HTTPServer, URIPattern
1010
from werkzeug.wrappers import Request, Response
1111

12-
from ollama._client import AsyncClient, Client, _copy_tools
12+
from ollama._client import CONNECTION_ERROR_MESSAGE, AsyncClient, Client, _copy_tools
1313

1414
PNG_BASE64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgYGAAAAAEAAH2FzhVAAAAAElFTkSuQmCC'
1515
PNG_BYTES = base64.b64decode(PNG_BASE64)
@@ -1116,15 +1116,15 @@ def test_tool_validation():
11161116

11171117
def test_client_connection_error():
11181118
client = Client('http://localhost:1234')
1119-
with pytest.raises(ConnectionError) as exc_info:
1119+
1120+
with pytest.raises(ConnectionError, match=CONNECTION_ERROR_MESSAGE):
11201121
client.chat('model', messages=[{'role': 'user', 'content': 'prompt'}])
1121-
assert str(exc_info.value) == 'Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download'
1122-
with pytest.raises(ConnectionError) as exc_info:
1122+
with pytest.raises(ConnectionError, match=CONNECTION_ERROR_MESSAGE):
1123+
client.chat('model', messages=[{'role': 'user', 'content': 'prompt'}])
1124+
with pytest.raises(ConnectionError, match=CONNECTION_ERROR_MESSAGE):
11231125
client.generate('model', 'prompt')
1124-
assert str(exc_info.value) == 'Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download'
1125-
with pytest.raises(ConnectionError) as exc_info:
1126+
with pytest.raises(ConnectionError, match=CONNECTION_ERROR_MESSAGE):
11261127
client.show('model')
1127-
assert str(exc_info.value) == 'Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download'
11281128

11291129

11301130
@pytest.mark.asyncio

0 commit comments

Comments
 (0)