add try/except around ollama initialization

This commit is contained in:
Josh Hawkins 2024-10-21 09:31:34 -05:00
parent 8002e59031
commit 059475e6bb

View File

@ -21,12 +21,20 @@ class OllamaClient(GenAIClient):
def _init_provider(self): def _init_provider(self):
"""Initialize the client.""" """Initialize the client."""
client = ApiClient(host=self.genai_config.base_url, timeout=self.timeout) try:
response = client.pull(self.genai_config.model) client = ApiClient(host=self.genai_config.base_url, timeout=self.timeout)
if response["status"] != "success": # ensure the model is available locally
logger.error("Failed to pull %s model from Ollama", self.genai_config.model) response = client.show(self.genai_config.model)
if response.get("error"):
logger.error(
"Ollama error: %s",
response["error"],
)
return None
return client
except Exception as e:
logger.warning("Error initializing Ollama: %s", str(e))
return None return None
return client
def _send(self, prompt: str, images: list[bytes]) -> Optional[str]: def _send(self, prompt: str, images: list[bytes]) -> Optional[str]:
"""Submit a request to Ollama""" """Submit a request to Ollama"""