Skip to content
Merged
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
31 changes: 13 additions & 18 deletions api/ollama_patch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
import requests

import os

# Configure logging
Expand All @@ -23,30 +23,25 @@ def check_ollama_model_exists(model_name: str, ollama_host: str = None) -> bool:
Returns:
bool: True if model exists, False otherwise
"""
import ollama

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Import httpx alongside ollama to properly catch connection and request errors raised by the underlying HTTP client.

    import ollama
    import httpx

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in 592523e

import httpx
if ollama_host is None:
ollama_host = os.getenv("OLLAMA_HOST", "http://localhost:11434")

try:
# Remove /api prefix if present and add it back
if ollama_host.endswith('/api'):
ollama_host = ollama_host[:-4]

response = requests.get(f"{ollama_host}/api/tags", timeout=5)
if response.status_code == 200:
models_data = response.json()
available_models = [model.get('name', '').split(':')[0] for model in models_data.get('models', [])]
model_base_name = model_name.split(':')[0] # Remove tag if present

is_available = model_base_name in available_models
if is_available:
logger.info(f"Ollama model '{model_name}' is available")
else:
logger.warning(f"Ollama model '{model_name}' is not available. Available models: {available_models}")
return is_available
ret: ollama.ListResponse = ollama.Client(host=ollama_host, timeout=5).list()
is_available = any(model_name == model.model for model in ret.models)
if is_available:
logger.info("Ollama model '%s' is available", model_name)
else:
logger.warning(f"Could not check Ollama models, status code: {response.status_code}")
return False
except requests.exceptions.RequestException as e:
logger.warning(
"Ollama model '%s' is not available. Available models: %s. ",
model_name,
str([model.model for model in ret.models]))
Comment on lines +35 to +42

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The new matching logic performs an exact match (model_name == model.model), which breaks the original tag-agnostic matching behavior. For example, if a user checks for llama3 but the installed model is llama3:latest, the check will incorrectly return False.\n\nTo preserve the original behavior, we should compare the base names (by splitting on :).

Suggested change
is_available = any(model_name == model.model for model in ret.models)
if is_available:
logger.info("Ollama model '%s' is available", model_name)
else:
logger.warning(f"Could not check Ollama models, status code: {response.status_code}")
return False
except requests.exceptions.RequestException as e:
logger.warning(
"Ollama model '%s' is not available. Available models: %s. ",
model_name,
str([model.model for model in ret.models]))
model_base = model_name.split(':')[0]
available_models = [model.model for model in ret.models]
is_available = any(model_base == m.split(':')[0] for m in available_models)
if is_available:
logger.info("Ollama model '%s' is available", model_name)
else:
logger.warning(
"Ollama model '%s' is not available. Available models: %s.",
model_name,
str(available_models))

return is_available
except (httpx.ConnectTimeout, ConnectionError) as e:
logger.warning(f"Could not connect to Ollama to check models: {e}")
return False
except Exception as e:
Expand Down
Loading