Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ollama/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@
ps = _client.ps
web_search = _client.web_search
web_fetch = _client.web_fetch
ping = _client.ping
8 changes: 8 additions & 0 deletions ollama/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@ def ps(self) -> ProcessResponse:
'GET',
'/api/ps',
)

def ping(self) -> str:
r = self._request_raw('GET', '/')
return r.text

def web_search(self, query: str, max_results: int = 3) -> WebSearchResponse:
"""
Expand Down Expand Up @@ -1305,6 +1309,10 @@ async def show(self, model: str) -> ShowResponse:
).model_dump(exclude_none=True),
)

async def ping(self) -> str:
r = await self._request_raw('GET', '/')
return r.text

async def ps(self) -> ProcessResponse:
return await self._request(
ProcessResponse,
Expand Down
26 changes: 26 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,19 @@ def test_client_copy(httpserver: HTTPServer):
assert response['status'] == 'success'


def test_client_ping(httpserver: HTTPServer):
httpserver.expect_ordered_request('/', method='GET').respond_with_response(Response('Ollama is running', status=200))
client = Client(httpserver.url_for('/'))
response = client.ping()
assert response == 'Ollama is running'


def test_client_ping_connection_error():
client = Client('http://localhost:1')
with pytest.raises(ConnectionError, match=CONNECTION_ERROR_MESSAGE):
client.ping()


async def test_async_client_chat(httpserver: HTTPServer):
httpserver.expect_ordered_request(
'/api/chat',
Expand Down Expand Up @@ -1256,6 +1269,19 @@ async def test_async_client_copy(httpserver: HTTPServer):
assert response['status'] == 'success'


async def test_async_client_ping(httpserver: HTTPServer):
httpserver.expect_ordered_request('/', method='GET').respond_with_response(Response('Ollama is running', status=200))
client = AsyncClient(httpserver.url_for('/'))
response = await client.ping()
assert response == 'Ollama is running'


async def test_async_client_ping_connection_error():
client = AsyncClient('http://localhost:1')
with pytest.raises(ConnectionError, match=CONNECTION_ERROR_MESSAGE):
await client.ping()


def test_headers():
client = Client()
assert client._client.headers['content-type'] == 'application/json'
Expand Down