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 @@ -54,6 +54,7 @@
list = _client.list
copy = _client.copy
show = _client.show
exists = _client.exists
ps = _client.ps
web_search = _client.web_search
web_fetch = _client.web_fetch
44 changes: 44 additions & 0 deletions ollama/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,28 @@ def show(self, model: str) -> ShowResponse:
).model_dump(exclude_none=True),
)


def exists(self, model: str) -> bool:
"""
Check whether a model is available locally.

Args:
model: Name of the model to check (e.g. 'llama3.2' or 'llama3.2:latest').

Returns:
True if the model is present locally, False otherwise.

Example::

if not client.exists('llama3.2'):
client.pull('llama3.2')
"""
try:
self.show(model)
return True
except ResponseError:
return False

def ps(self) -> ProcessResponse:
return self._request(
ProcessResponse,
Expand Down Expand Up @@ -1305,6 +1327,28 @@ async def show(self, model: str) -> ShowResponse:
).model_dump(exclude_none=True),
)


async def exists(self, model: str) -> bool:
"""
Check whether a model is available locally.

Args:
model: Name of the model to check (e.g. 'llama3.2' or 'llama3.2:latest').

Returns:
True if the model is present locally, False otherwise.

Example::

if not await client.exists('llama3.2'):
await client.pull('llama3.2')
"""
try:
await self.show(model)
return True
except ResponseError:
return False

async def ps(self) -> ProcessResponse:
return await self._request(
ProcessResponse,
Expand Down
3 changes: 2 additions & 1 deletion ollama/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ class ShowResponse(SubscriptableBaseModel):

details: Optional[ModelDetails] = None

modelinfo: Optional[Mapping[str, Any]] = Field(alias='model_info')
modelinfo: Optional[Mapping[str, Any]] = Field(default=None, alias='model_info')
'Per-model metadata. Absent for some cloud models.'

parameters: Optional[str] = None

Expand Down