This commit is contained in:
RaccoonLabs 2026-04-12 09:23:34 -03:00 committed by GitHub
commit 62fdd65ba5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 108 additions and 0 deletions

View File

@ -52,6 +52,7 @@ push = _client.push
create = _client.create
delete = _client.delete
list = _client.list
exists = _client.exists
copy = _client.copy
show = _client.show
ps = _client.ps

View File

@ -629,6 +629,18 @@ class Client(BaseClient):
'/api/tags',
)
def exists(self, model: str) -> bool:
"""Check if a model is available locally.
Args:
model: The model name to check (e.g. 'llama3.1:8b').
Returns:
True if the model exists locally, False otherwise.
"""
models = self.list().models or []
return any(m.model == model for m in models)
def delete(self, model: str) -> StatusResponse:
r = self._request_raw(
'DELETE',
@ -1270,6 +1282,19 @@ class AsyncClient(BaseClient):
'/api/tags',
)
async def exists(self, model: str) -> bool:
"""Check if a model is available locally.
Args:
model: The model name to check (e.g. 'llama3.1:8b').
Returns:
True if the model exists locally, False otherwise.
"""
resp = await self.list()
models = resp.models or []
return any(m.model == model for m in models)
async def delete(self, model: str) -> StatusResponse:
r = await self._request_raw(
'DELETE',

82
tests/test_exists.py Normal file
View File

@ -0,0 +1,82 @@
import json
import pytest
from pytest_httpserver import HTTPServer
from ollama._client import AsyncClient, Client
pytestmark = pytest.mark.anyio
@pytest.fixture
def anyio_backend():
return 'asyncio'
def test_client_exists_true(httpserver: HTTPServer):
"""exists() returns True when model is present."""
httpserver.expect_ordered_request(
'/api/tags',
method='GET',
).respond_with_json({
'models': [
{'name': 'llama3.1:8b', 'model': 'llama3.1:8b', 'size': 4661224676},
{'name': 'qwen2.5:latest', 'model': 'qwen2.5:latest', 'size': 4430121000},
]
})
client = Client(host=f'http://{httpserver.host}:{httpserver.port}')
assert client.exists('llama3.1:8b') is True
def test_client_exists_false(httpserver: HTTPServer):
"""exists() returns False when model is not present."""
httpserver.expect_ordered_request(
'/api/tags',
method='GET',
).respond_with_json({
'models': [
{'name': 'llama3.1:8b', 'model': 'llama3.1:8b', 'size': 4661224676},
]
})
client = Client(host=f'http://{httpserver.host}:{httpserver.port}')
assert client.exists('gemma2:2b') is False
def test_client_exists_empty_list(httpserver: HTTPServer):
"""exists() returns False when no models are available."""
httpserver.expect_ordered_request(
'/api/tags',
method='GET',
).respond_with_json({'models': []})
client = Client(host=f'http://{httpserver.host}:{httpserver.port}')
assert client.exists('llama3.1:8b') is False
async def test_async_client_exists_true(httpserver: HTTPServer):
"""Async exists() returns True when model is present."""
httpserver.expect_ordered_request(
'/api/tags',
method='GET',
).respond_with_json({
'models': [
{'name': 'llama3.1:8b', 'model': 'llama3.1:8b', 'size': 4661224676},
]
})
client = AsyncClient(host=f'http://{httpserver.host}:{httpserver.port}')
assert await client.exists('llama3.1:8b') is True
async def test_async_client_exists_false(httpserver: HTTPServer):
"""Async exists() returns False when model is not present."""
httpserver.expect_ordered_request(
'/api/tags',
method='GET',
).respond_with_json({'models': []})
client = AsyncClient(host=f'http://{httpserver.host}:{httpserver.port}')
assert await client.exists('nonexistent') is False