Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

Refactor: Clean up imports in settings routes

hashedviking40cb08b0e9c3
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
index 20330b3a51..87650dcbee 100644
--- a/src/local_deep_research/web/routes/settings_routes.py
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -108,13 +108,14 @@ def validate_setting(setting: Setting, value: Any) -> Tuple[bool, Optional[str]]
elif setting.ui_element == "select":
# Check if value is in the allowed options
if setting.options:
- allowed_values = [opt.get("value") for opt in setting.options]
- if value not in allowed_values:
- return (
- False,
- f"Value must be one of: {', '.join(str(v) for v in allowed_values)}",
- )
-
+ # Skip options validation for dynamically populated dropdowns
+ if setting.key not in ["llm.provider", "llm.model"]:
+ allowed_values = [opt.get("value") for opt in setting.options]
+ if value not in allowed_values:
+ return (
+ False,
+ f"Value must be one of: {', '.join(str(v) for v in allowed_values)}",
+ )
# All checks passed
return True, None
@@ -897,49 +898,129 @@ def api_get_available_models():
ollama_models = []
try:
import json
+ import re
import requests
+ from flask import current_app
# Try to query the Ollama API directly
try:
+ current_app.logger.info("Attempting to connect to Ollama API")
ollama_response = requests.get(
- "http://localhost:11434/api/tags", timeout=2
+ "http://localhost:11434/api/tags", timeout=5
+ )
+
+ current_app.logger.debug(
+ f"Ollama API response: Status {ollama_response.status_code}"
+ )
+
+ # Try to parse the response even if status code is not 200 to help with debugging
+ response_text = ollama_response.text
+ current_app.logger.debug(
+ f"Ollama API raw response: {response_text[:500]}..."
)
+
if ollama_response.status_code == 200:
- ollama_data = ollama_response.json()
- if "models" in ollama_data:
- # Format for newer Ollama API
- for model in ollama_data.get("models", []):
- name = model.get("name", "")
- if name:
- display_name = name.replace(":", " ").title()
- ollama_models.append(
- {"value": name, "label": f"{display_name} (Ollama)"}
- )
- else:
- # Format for older Ollama API
- for model in ollama_data:
- name = model.get("name", "")
- if name:
- display_name = name.replace(":", " ").title()
- ollama_models.append(
- {"value": name, "label": f"{display_name} (Ollama)"}
- )
+ try:
+ ollama_data = ollama_response.json()
+ current_app.logger.debug(
+ f"Ollama API JSON data: {json.dumps(ollama_data)[:500]}..."
+ )
+
+ if "models" in ollama_data:
+ # Format for newer Ollama API
+ current_app.logger.info(
+ f"Found {len(ollama_data.get('models', []))} models in newer Ollama API format"
+ )
+ for model in ollama_data.get("models", []):
+ # Extract name correctly from the model object
+ name = model.get("name", "")
+ if name:
+ # Improved display name formatting
+ display_name = re.sub(r"[:/]", " ", name).strip()
+ display_name = " ".join(
+ word.capitalize()
+ for word in display_name.split()
+ )
+ # Create the model entry with value and label
+ ollama_models.append(
+ {
+ "value": name, # Original model name as value (for API calls)
+ "label": f"{display_name} (Ollama)", # Pretty name as label
+ }
+ )
+ current_app.logger.debug(
+ f"Added Ollama model: {name} -> {display_name}"
+ )
+ else:
+ # Format for older Ollama API
+ current_app.logger.info(
+ f"Found {len(ollama_data)} models in older Ollama API format"
+ )
+ for model in ollama_data:
+ name = model.get("name", "")
+ if name:
+ # Improved display name formatting
+ display_name = re.sub(r"[:/]", " ", name).strip()
+ display_name = " ".join(
+ word.capitalize()
+ for word in display_name.split()
+ )
+ ollama_models.append(
+ {
+ "value": name,
+ "label": f"{display_name} (Ollama)",
+ }
+ )
+ current_app.logger.debug(
+ f"Added Ollama model: {name} -> {display_name}"
+ )
+
+ # Sort models alphabetically
+ ollama_models.sort(key=lambda x: x["label"])
+
+ except json.JSONDecodeError as json_err:
+ current_app.logger.error(
+ f"Failed to parse Ollama API response as JSON: {json_err}"
+ )
+ raise Exception(f"Ollama API returned invalid JSON: {json_err}")
+ else:
+ current_app.logger.warning(
+ f"Ollama API returned non-200 status code: {ollama_response.status_code}"
+ )
+ raise Exception(
+ f"Ollama API returned status code {ollama_response.status_code}"
+ )
+
except requests.exceptions.RequestException as e:
- print(f"Could not connect to Ollama API: {str(e)}")
+ current_app.logger.warning(f"Could not connect to Ollama API: {str(e)}")
# Fallback to default models if Ollama is not running
+ current_app.logger.info(
+ "Using fallback Ollama models due to connection error"
+ )
ollama_models = [
- {"value": "llama2", "label": "Llama 2 (Ollama)"},
+ {"value": "llama3", "label": "Llama 3 (Ollama)"},
{"value": "mistral", "label": "Mistral (Ollama)"},
{"value": "gemma:latest", "label": "Gemma (Ollama)"},
]
+ # Always set the ollama_models in providers, whether we got real or fallback models
providers["ollama_models"] = ollama_models
+ current_app.logger.info(f"Final Ollama models count: {len(ollama_models)}")
+
+ # Log some model names for debugging
+ if ollama_models:
+ model_names = [m["value"] for m in ollama_models[:5]]
+ current_app.logger.info(
+ f"Sample Ollama models: {', '.join(model_names)}"
+ )
+
except Exception as e:
- print(f"Error getting Ollama models: {str(e)}")
+ current_app.logger.error(f"Error getting Ollama models: {str(e)}")
# Use fallback models
+ current_app.logger.info("Using fallback Ollama models due to error")
providers["ollama_models"] = [
- {"value": "llama2", "label": "Llama 2 (Ollama)"},
+ {"value": "llama3", "label": "Llama 3 (Ollama)"},
{"value": "mistral", "label": "Mistral (Ollama)"},
{"value": "gemma:latest", "label": "Gemma (Ollama)"},
]
@@ -972,7 +1053,9 @@ def api_get_available_models():
import traceback
error_trace = traceback.format_exc()
- print(f"Error getting available models: {str(e)}\n{error_trace}")
+ current_app.logger.error(
+ f"Error getting available models: {str(e)}\n{error_trace}"
+ )
return jsonify({"status": "error", "message": str(e)}), 500