diff --git a/ollama/__init__.py b/ollama/__init__.py index 92bba280..708d0cf9 100644 --- a/ollama/__init__.py +++ b/ollama/__init__.py @@ -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 diff --git a/ollama/_client.py b/ollama/_client.py index 18cb0fb4..c97db898 100644 --- a/ollama/_client.py +++ b/ollama/_client.py @@ -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, @@ -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, diff --git a/ollama/_types.py b/ollama/_types.py index 96529d63..785b8d37 100644 --- a/ollama/_types.py +++ b/ollama/_types.py @@ -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