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: address PR review feedback for URL normalization

LearningCircuit4292ebe5a530
diff --git a/src/local_deep_research/config/llm_config.py b/src/local_deep_research/config/llm_config.py
index 0f21a341e1..11cc86917e 100644
--- a/src/local_deep_research/config/llm_config.py
+++ b/src/local_deep_research/config/llm_config.py
@@ -9,7 +9,7 @@ from langchain_openai import ChatOpenAI
from ..utilities.db_utils import get_db_setting
from ..utilities.search_utilities import remove_think_tags
-from ..utilities.url_utils import normalize_ollama_url
+from ..utilities.url_utils import normalize_url
# Setup logging
logger = logging.getLogger(__name__)
@@ -143,7 +143,11 @@ def get_llm(model_name=None, temperature=None, provider=None, openai_endpoint_ur
try:
# Use the configurable Ollama base URL
raw_base_url = get_db_setting("llm.ollama.url", "http://localhost:11434")
- base_url = normalize_ollama_url(raw_base_url)
+ base_url = (
+ normalize_url(raw_base_url)
+ if raw_base_url
+ else "http://localhost:11434"
+ )
# Check if Ollama is available before trying to use it
if not is_ollama_available():
@@ -374,7 +378,9 @@ def is_ollama_available():
import requests
raw_base_url = get_db_setting("llm.ollama.url", "http://localhost:11434")
- base_url = normalize_ollama_url(raw_base_url)
+ base_url = (
+ normalize_url(raw_base_url) if raw_base_url else "http://localhost:11434"
+ )
logger.info(f"Checking Ollama availability at {base_url}/api/tags")
try:
diff --git a/src/local_deep_research/utilities/url_utils.py b/src/local_deep_research/utilities/url_utils.py
index be6d327ee7..fe2f4fb77c 100644
--- a/src/local_deep_research/utilities/url_utils.py
+++ b/src/local_deep_research/utilities/url_utils.py
@@ -5,9 +5,9 @@ import logging
logger = logging.getLogger(__name__)
-def normalize_ollama_url(raw_url: str) -> str:
+def normalize_url(raw_url: str) -> str:
"""
- Normalize an Ollama server URL to ensure it has a proper scheme and format.
+ Normalize a URL to ensure it has a proper scheme and format.
Args:
raw_url: The raw URL string to normalize
@@ -16,15 +16,15 @@ def normalize_ollama_url(raw_url: str) -> str:
A properly formatted URL string
Examples:
- >>> normalize_ollama_url("localhost:11434")
+ >>> normalize_url("localhost:11434")
'http://localhost:11434'
- >>> normalize_ollama_url("https://example.com:11434")
+ >>> normalize_url("https://example.com:11434")
'https://example.com:11434'
- >>> normalize_ollama_url("http:example.com")
+ >>> normalize_url("http:example.com")
'http://example.com'
"""
if not raw_url:
- return "http://localhost:11434"
+ raise ValueError("URL cannot be empty")
# Clean up the URL
raw_url = raw_url.strip()
diff --git a/src/local_deep_research/web/routes/api_routes.py b/src/local_deep_research/web/routes/api_routes.py
index b1af17cfe0..72c2cbb795 100644
--- a/src/local_deep_research/web/routes/api_routes.py
+++ b/src/local_deep_research/web/routes/api_routes.py
@@ -1,11 +1,10 @@
import json
import logging
-import os
import requests
from flask import Blueprint, current_app, jsonify, request
-from ...utilities.url_utils import normalize_ollama_url
+from ...utilities.url_utils import normalize_url
from ..models.database import get_db_connection
from ..routes.research_routes import active_research, termination_flags
from ..services.research_service import (
@@ -256,12 +255,15 @@ def check_ollama_status():
{"running": True, "message": f"Using provider: {provider}, not Ollama"}
)
- # Get Ollama API URL
- raw_ollama_base_url = os.getenv(
- "OLLAMA_BASE_URL",
- llm_config.get("ollama_base_url", "http://localhost:11434"),
+ # Get Ollama API URL from LLM config
+ raw_ollama_base_url = llm_config.get(
+ "ollama_base_url", "http://localhost:11434"
+ )
+ ollama_base_url = (
+ normalize_url(raw_ollama_base_url)
+ if raw_ollama_base_url
+ else "http://localhost:11434"
)
- ollama_base_url = normalize_ollama_url(raw_ollama_base_url)
logger.info(f"Checking Ollama status at: {ollama_base_url}")
@@ -382,11 +384,15 @@ def check_ollama_model():
# Log which model we're checking for debugging
logger.info(f"Checking availability of Ollama model: {model_name}")
- raw_ollama_base_url = os.getenv(
- "OLLAMA_BASE_URL",
- llm_config.get("ollama_base_url", "http://localhost:11434"),
+ # Get Ollama API URL from LLM config
+ raw_ollama_base_url = llm_config.get(
+ "ollama_base_url", "http://localhost:11434"
+ )
+ ollama_base_url = (
+ normalize_url(raw_ollama_base_url)
+ if raw_ollama_base_url
+ else "http://localhost:11434"
)
- ollama_base_url = normalize_ollama_url(raw_ollama_base_url)
# Check if the model is available
try:
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
index 2b376ed1d1..faae4b0cfe 100644
--- a/src/local_deep_research/web/routes/settings_routes.py
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -19,7 +19,7 @@ from flask_wtf.csrf import generate_csrf
from sqlalchemy.orm import Session
from ...utilities.db_utils import get_db_setting
-from ...utilities.url_utils import normalize_ollama_url
+from ...utilities.url_utils import normalize_url
from ..database.models import Setting, SettingType
from ..services.settings_service import (
create_or_update_setting,
@@ -671,7 +671,11 @@ def api_get_available_models():
raw_base_url = get_db_setting(
"llm.ollama.url", "http://localhost:11434"
)
- base_url = normalize_ollama_url(raw_base_url)
+ base_url = (
+ normalize_url(raw_base_url)
+ if raw_base_url
+ else "http://localhost:11434"
+ )
ollama_response = requests.get(f"{base_url}/api/tags", timeout=5)
@@ -1274,14 +1278,11 @@ def fix_corrupted_settings():
def check_ollama_status():
"""Check if Ollama is running and available"""
try:
- # Set a shorter timeout for the request
- raw_base_url = os.getenv(
- "OLLAMA_BASE_URL",
- get_db_setting(
- "llm.ollama.url", "http://localhost:11434"
- ), # Use db setting as fallback
+ # Get Ollama URL from settings
+ raw_base_url = get_db_setting("llm.ollama.url", "http://localhost:11434")
+ base_url = (
+ normalize_url(raw_base_url) if raw_base_url else "http://localhost:11434"
)
- base_url = normalize_ollama_url(raw_base_url)
response = requests.get(f"{base_url}/api/version", timeout=2.0)
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_local.py b/src/local_deep_research/web_search_engines/engines/search_engine_local.py
index e52edc5e3d..7f6df0dc11 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_local.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_local.py
@@ -32,7 +32,7 @@ from langchain_text_splitters import RecursiveCharacterTextSplitter
from ...config import search_config
from ...utilities.db_utils import get_db_setting
-from ...utilities.url_utils import normalize_ollama_url
+from ...utilities.url_utils import normalize_url
from ..search_engine_base import BaseSearchEngine
# Setup logging
@@ -173,10 +173,14 @@ class LocalEmbeddingManager:
raw_ollama_base_url = get_db_setting(
"llm.ollama.url", "http://localhost:11434"
)
- self.ollama_base_url = normalize_ollama_url(raw_ollama_base_url)
+ self.ollama_base_url = (
+ normalize_url(raw_ollama_base_url)
+ if raw_ollama_base_url
+ else "http://localhost:11434"
+ )
else:
# Ensure scheme is present if ollama_base_url was passed in constructor
- self.ollama_base_url = normalize_ollama_url(self.ollama_base_url)
+ self.ollama_base_url = normalize_url(self.ollama_base_url)
logger.info(
f"Initializing Ollama embeddings with model {self.embedding_model} and base_url {self.ollama_base_url}"
diff --git a/tests/test_url_utils.py b/tests/test_url_utils.py
index c53e409e1d..cd9bfef65e 100644
--- a/tests/test_url_utils.py
+++ b/tests/test_url_utils.py
@@ -2,66 +2,53 @@
import pytest
-from local_deep_research.utilities.url_utils import normalize_ollama_url
+from local_deep_research.utilities.url_utils import normalize_url
-class TestNormalizeOllamaUrl:
- """Test cases for the normalize_ollama_url function."""
+class TestNormalizeUrl:
+ """Test cases for the normalize_url function."""
def test_localhost_without_scheme(self):
"""Test that localhost addresses get http:// prefix."""
- assert normalize_ollama_url("localhost:11434") == "http://localhost:11434"
- assert normalize_ollama_url("127.0.0.1:11434") == "http://127.0.0.1:11434"
- assert normalize_ollama_url("[::1]:11434") == "http://[::1]:11434"
- assert normalize_ollama_url("0.0.0.0:11434") == "http://0.0.0.0:11434"
+ assert normalize_url("localhost:11434") == "http://localhost:11434"
+ assert normalize_url("127.0.0.1:11434") == "http://127.0.0.1:11434"
+ assert normalize_url("[::1]:11434") == "http://[::1]:11434"
+ assert normalize_url("0.0.0.0:11434") == "http://0.0.0.0:11434"
def test_external_host_without_scheme(self):
"""Test that external hosts get https:// prefix."""
- assert normalize_ollama_url("example.com:11434") == "https://example.com:11434"
- assert normalize_ollama_url("api.example.com") == "https://api.example.com"
+ assert normalize_url("example.com:11434") == "https://example.com:11434"
+ assert normalize_url("api.example.com") == "https://api.example.com"
def test_malformed_url_with_scheme(self):
"""Test correction of malformed URLs like 'http:hostname'."""
- assert normalize_ollama_url("http:localhost:11434") == "http://localhost:11434"
- assert (
- normalize_ollama_url("https:example.com:11434")
- == "https://example.com:11434"
- )
+ assert normalize_url("http:localhost:11434") == "http://localhost:11434"
+ assert normalize_url("https:example.com:11434") == "https://example.com:11434"
def test_well_formed_urls(self):
"""Test that well-formed URLs are unchanged."""
+ assert normalize_url("http://localhost:11434") == "http://localhost:11434"
+ assert normalize_url("https://example.com:11434") == "https://example.com:11434"
assert (
- normalize_ollama_url("http://localhost:11434") == "http://localhost:11434"
- )
- assert (
- normalize_ollama_url("https://example.com:11434")
- == "https://example.com:11434"
- )
- assert (
- normalize_ollama_url("http://192.168.1.100:11434")
- == "http://192.168.1.100:11434"
+ normalize_url("http://192.168.1.100:11434") == "http://192.168.1.100:11434"
)
def test_urls_with_double_slash_prefix(self):
"""Test URLs that start with //."""
- assert normalize_ollama_url("//localhost:11434") == "http://localhost:11434"
- assert (
- normalize_ollama_url("//example.com:11434") == "https://example.com:11434"
- )
+ assert normalize_url("//localhost:11434") == "http://localhost:11434"
+ assert normalize_url("//example.com:11434") == "https://example.com:11434"
def test_empty_or_none_url(self):
"""Test handling of empty or None URLs."""
- assert normalize_ollama_url("") == "http://localhost:11434"
- assert normalize_ollama_url(None) == "http://localhost:11434"
+ with pytest.raises(ValueError):
+ normalize_url("")
+ with pytest.raises(ValueError):
+ normalize_url(None)
def test_url_with_path(self):
"""Test URLs with paths."""
- assert (
- normalize_ollama_url("localhost:11434/api") == "http://localhost:11434/api"
- )
- assert (
- normalize_ollama_url("example.com/api/v1") == "https://example.com/api/v1"
- )
+ assert normalize_url("localhost:11434/api") == "http://localhost:11434/api"
+ assert normalize_url("example.com/api/v1") == "https://example.com/api/v1"
if __name__ == "__main__":