-
-
Notifications
You must be signed in to change notification settings - Fork 2k
update check_ollama_model_exists using ollama.list api.
#549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||||||||||||||
| import requests | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| # Configure logging | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||||||||||||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new matching logic performs an exact match (
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| 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: | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import
httpxalongsideollamato properly catch connection and request errors raised by the underlying HTTP client.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added in 592523e