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.

Environment variables are now handled much better.

Daniel Petti6cd6ce3090a5
diff --git a/Dockerfile b/Dockerfile
index 5418fa0a76..a8bf8183ae 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -13,20 +13,16 @@ RUN mkdir -p /usr/local/lib/python3.13/site-packages/local_deep_research/default
RUN echo "# Add your environment variables here" > /usr/local/lib/python3.13/site-packages/local_deep_research/defaults/.env.template
# Environment variables for configuration
-ENV LDR_WEB__PORT=5000
-ENV LDR_SEARCH__TOOL="auto"
-ENV LDR_SEARCH__ITERATIONS=2
-ENV LDR_SEARCH__QUESTIONS_PER_ITERATION=2
-ENV LDR_GENERAL__ENABLE_FACT_CHECKING=false
-ENV LDR_LLM__PROVIDER="ollama"
-ENV LDR_LLM__MODEL="mistral"
-ENV LDR_LLM__TEMPERATURE=0.7
-ENV LDR_LLM__MAX_TOKENS=30000
-ENV OLLAMA_BASE_URL="http://localhost:11434"
+ENV LDR_WEB_PORT=5000
+ENV LDR_LLM_PROVIDER="ollama"
+ENV LDR_LLM_MODEL="mistral"
+ENV LDR_LLM_TEMPERATURE=0.7
+ENV LDR_LLM_MAX_TOKENS=30000
+ENV LDR_LLM_OLLAMA_URL="http://localhost:11434"
# Create volume for persistent configuration
VOLUME /root/.config/local_deep_research
EXPOSE 5000
-CMD [ "python", "-m", "local_deep_research.web.app" ]
\ No newline at end of file
+CMD [ "python", "-m", "local_deep_research.web.app" ]
diff --git a/src/local_deep_research/config/llm_config.py b/src/local_deep_research/config/llm_config.py
index 3518377f89..8bab9363e0 100644
--- a/src/local_deep_research/config/llm_config.py
+++ b/src/local_deep_research/config/llm_config.py
@@ -25,14 +25,6 @@ VALID_PROVIDERS = [
]
-# Environment variables to use. Will override DB settings if set.
-OPENAI_API_KEY_ENV = "LDR_OPENAI_API_KEY"
-ANTHROPIC_API_KEY_ENV = "LDR_ANTHROPIC_API_KEY"
-OPENAI_ENDPOINT_API_KEY_ENV = "LDR_OPENAI_ENDPOINT_API_KEY"
-OLLAMA_BASE_URL_ENV = "LDR_OLLAMA_BASE_URL"
-LMSTUDIO_URL_ENV = "LDR_LMSTUDIO_URL"
-
-
def get_llm(model_name=None, temperature=None, provider=None, openai_endpoint_url=None):
"""
Get LLM instance based on model name and provider.
@@ -86,9 +78,7 @@ def get_llm(model_name=None, temperature=None, provider=None, openai_endpoint_ur
# Handle different providers
if provider == "anthropic":
- api_key = os.getenv(ANTHROPIC_API_KEY_ENV)
- if not api_key:
- api_key = get_db_setting("llm.anthropic.api_key")
+ api_key = get_db_setting("llm.anthropic.api_key")
if not api_key:
logger.warning(
"ANTHROPIC_API_KEY not found. Falling back to default model."
@@ -101,9 +91,7 @@ def get_llm(model_name=None, temperature=None, provider=None, openai_endpoint_ur
return wrap_llm_without_think_tags(llm)
elif provider == "openai":
- api_key = os.getenv(OPENAI_API_KEY_ENV)
- if not api_key:
- api_key = get_db_setting("llm.openai.api_key")
+ api_key = get_db_setting("llm.openai.api_key")
if not api_key:
logger.warning("OPENAI_API_KEY not found. Falling back to default model.")
return get_fallback_model(temperature)
@@ -112,9 +100,7 @@ def get_llm(model_name=None, temperature=None, provider=None, openai_endpoint_ur
return wrap_llm_without_think_tags(llm)
elif provider == "openai_endpoint":
- api_key = os.getenv(OPENAI_ENDPOINT_API_KEY_ENV)
- if not api_key:
- api_key = get_db_setting("llm.openai_endpoint.api_key")
+ api_key = get_db_setting("llm.openai_endpoint.api_key")
if not api_key:
logger.warning(
"OPENAI_ENDPOINT_API_KEY not found. Falling back to default model."
@@ -154,9 +140,7 @@ def get_llm(model_name=None, temperature=None, provider=None, openai_endpoint_ur
elif provider == "ollama":
try:
# Use the configurable Ollama base URL
- base_url = os.getenv(OLLAMA_BASE_URL_ENV)
- if not base_url:
- base_url = get_db_setting("llm.ollama.url", "http://localhost:11434")
+ base_url = get_db_setting("llm.ollama.url", "http://localhost:11434")
# Check if Ollama is available before trying to use it
if not is_ollama_available():
@@ -220,9 +204,7 @@ def get_llm(model_name=None, temperature=None, provider=None, openai_endpoint_ur
elif provider == "lmstudio":
# LM Studio supports OpenAI API format, so we can use ChatOpenAI directly
- lmstudio_url = os.getenv(LMSTUDIO_URL_ENV)
- if not lmstudio_url:
- lmstudio_url = get_db_setting("llm.lmstudio.url", lmstudio_url)
+ lmstudio_url = get_db_setting("llm.lmstudio.url", "http://localhost:1234")
llm = ChatOpenAI(
model=model_name,
@@ -341,9 +323,7 @@ def get_available_provider_types():
def is_openai_available():
"""Check if OpenAI is available"""
try:
- api_key = os.getenv(OPENAI_API_KEY_ENV)
- if not api_key:
- api_key = get_db_setting("llm.openai.api_key")
+ api_key = get_db_setting("llm.openai.api_key")
return bool(api_key)
except Exception:
return False
@@ -352,9 +332,7 @@ def is_openai_available():
def is_anthropic_available():
"""Check if Anthropic is available"""
try:
- api_key = os.getenv(ANTHROPIC_API_KEY_ENV)
- if not api_key:
- api_key = get_db_setting("llm.anthropic.api_key")
+ api_key = get_db_setting("llm.anthropic.api_key")
return bool(api_key)
except Exception:
return False
@@ -363,9 +341,7 @@ def is_anthropic_available():
def is_openai_endpoint_available():
"""Check if OpenAI endpoint is available"""
try:
- api_key = os.getenv(OPENAI_ENDPOINT_API_KEY_ENV)
- if not api_key:
- api_key = get_db_setting("llm.openai_endpoint.api_key")
+ api_key = get_db_setting("llm.openai_endpoint.api_key")
return bool(api_key)
except Exception:
return False
@@ -376,10 +352,7 @@ def is_ollama_available():
try:
import requests
- base_url = os.getenv(
- OLLAMA_BASE_URL_ENV,
- get_db_setting("llm.ollama.url", "http://localhost:11434"),
- )
+ base_url = get_db_setting("llm.ollama.url", "http://localhost:11434")
logger.info(f"Checking Ollama availability at {base_url}/api/tags")
try:
@@ -421,9 +394,7 @@ def is_lmstudio_available():
try:
import requests
- lmstudio_url = os.getenv(LMSTUDIO_URL_ENV)
- if not lmstudio_url:
- lmstudio_url = get_db_setting("llm.lmstudio.url", "http://localhost:1234")
+ lmstudio_url = get_db_setting("llm.lmstudio.url", "http://localhost:1234")
# LM Studio typically uses OpenAI-compatible endpoints
response = requests.get(f"{lmstudio_url}/v1/models", timeout=1.0)
return response.status_code == 200
diff --git a/src/local_deep_research/utilities/db_utils.py b/src/local_deep_research/utilities/db_utils.py
index 182e9c2548..5680ed7eee 100644
--- a/src/local_deep_research/utilities/db_utils.py
+++ b/src/local_deep_research/utilities/db_utils.py
@@ -1,11 +1,12 @@
import logging
import os
from functools import cache
+from typing import Any, Dict
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
-from ..web.services.settings_manager import SettingsManager
+from ..web.services.settings_manager import SettingsManager, check_env_setting
logger = logging.getLogger(__name__)
@@ -36,8 +37,24 @@ def get_settings_manager() -> SettingsManager:
return SettingsManager(db_session=get_db_session())
-def get_db_setting(key, default_value=None):
- """Get a setting from the database with fallback to default value"""
+def get_db_setting(
+ key: str, default_value: Any | None = None, check_env: bool = True
+) -> str | Dict[str, Any] | None:
+ """
+ Get a setting from the database with fallback to default value
+
+ Args:
+ key: The setting key.
+ default_value: If the setting is not found, it will return this instead.
+ check_env: If true, it will check the corresponding environment
+ variable before checking the DB and return that if it is set.
+
+ """
+ if check_env:
+ env_value = check_env_setting(key)
+ if env_value is not None:
+ return env_value
+
try:
# Get settings manager which handles database access
value = get_settings_manager().get_setting(key)
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
index 46deff2945..8ebb193bf2 100644
--- a/src/local_deep_research/web/routes/settings_routes.py
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -84,56 +84,6 @@ def validate_setting(setting: Setting, value: Any) -> Tuple[bool, Optional[str]]
return True, None
-def get_all_settings_json():
- """Get all settings as a JSON-serializable dictionary
-
- Returns:
- List of setting dictionaries
- """
- db_session = get_db_session()
- settings_list = []
-
- # Get all settings
- settings = (
- db_session.query(Setting)
- .order_by(Setting.type, Setting.category, Setting.name)
- .all()
- )
-
- # Convert to dictionaries
- for setting in settings:
- # Ensure objects are properly serialized
- value = setting.value
-
- # Convert objects to properly formatted JSON strings for display
- if isinstance(value, (dict, list)) and value:
- try:
- # For frontend display, we'll keep objects as they are
- # The javascript will handle formatting them
- pass
- except Exception as e:
- logger.error(f"Error serializing setting {setting.key}: {e}")
-
- setting_dict = {
- "key": setting.key,
- "value": value,
- "type": setting.type.value if setting.type else None,
- "name": setting.name,
- "description": setting.description,
- "category": setting.category,
- "ui_element": setting.ui_element,
- "options": setting.options,
- "min_value": setting.min_value,
- "max_value": setting.max_value,
- "step": setting.step,
- "visible": setting.visible,
- "editable": setting.editable,
- }
- settings_list.append(setting_dict)
-
- return settings_list
-
-
@settings_bp.route("/", methods=["GET"])
def settings_page():
"""Main settings dashboard with links to specialized config pages"""
@@ -431,13 +381,6 @@ def reset_to_defaults():
)
-@settings_bp.route("/all_settings", methods=["GET"])
-def get_all_settings_route():
- """Get all settings for the unified dashboard"""
- settings_list = get_all_settings_json()
- return jsonify({"status": "success", "settings": settings_list})
-
-
# API Routes
@settings_bp.route("/api", methods=["GET"])
def api_get_all_settings():
@@ -467,7 +410,7 @@ def api_get_all_settings():
settings = filtered_settings
- return jsonify({"settings": settings})
+ return jsonify({"status": "success", "settings": settings})
except Exception as e:
logger.error(f"Error getting settings: {e}")
return jsonify({"error": str(e)}), 500
diff --git a/src/local_deep_research/web/services/settings_manager.py b/src/local_deep_research/web/services/settings_manager.py
index db0472c55f..7ce168f641 100644
--- a/src/local_deep_research/web/services/settings_manager.py
+++ b/src/local_deep_research/web/services/settings_manager.py
@@ -1,6 +1,7 @@
import importlib.resources as pkg_resources
import json
import logging
+import os
from typing import Any, Dict, Optional, Union
from sqlalchemy import func
@@ -21,6 +22,25 @@ from ..models.settings import (
logger = logging.getLogger(__name__)
+def check_env_setting(key: str) -> str | None:
+ """
+ Checks environment variables for a particular setting.
+
+ Args:
+ key: The database key for the setting.
+
+ Returns:
+ The setting from the environment variables, or None if the variable
+ is not set.
+
+ """
+ env_variable_name = f"LDR_{"_".join(key.split(".")).upper()}"
+ env_value = os.getenv(env_variable_name)
+ if env_value is not None:
+ logger.debug(f"Overriding {key} setting from environment variable.")
+ return env_value
+
+
class SettingsManager:
"""
Manager for handling application settings with database storage and file fallback.
@@ -41,17 +61,24 @@ class SettingsManager:
default_settings = pkg_resources.read_text(defaults, "default_settings.json")
self.default_settings = json.loads(default_settings)
- def get_setting(self, key: str, default: Any = None) -> Any:
+ def get_setting(self, key: str, default: Any = None, check_env: bool = True) -> Any:
"""
Get a setting value
Args:
key: Setting key
default: Default value if setting is not found
+ check_env: If true, it will check the environment variable for
+ this setting before reading from the DB.
Returns:
Setting value or default if not found
"""
+ if check_env:
+ env_value = check_env_setting(key)
+ if env_value is not None:
+ return env_value
+
# If using database first approach and session available, check database
if self.db_first and self.db_session:
try:
@@ -160,6 +187,15 @@ class SettingsManager:
visible=setting.visible,
editable=setting.editable,
)
+
+ # Override from the environment variables if needed.
+ env_value = check_env_setting(setting.key)
+ if env_value is not None:
+ result[setting.key]["value"] = env_value
+ # Mark it as non-editable, because changes to the DB
+ # value have no effect as long as the environment
+ # variable is set.
+ result[setting.key]["editable"] = False
except SQLAlchemyError as e:
logger.error(f"Error retrieving all settings from database: {e}")
diff --git a/src/local_deep_research/web/static/js/components/settings.js b/src/local_deep_research/web/static/js/components/settings.js
index 09ef200b48..b3458bead0 100644
--- a/src/local_deep_research/web/static/js/components/settings.js
+++ b/src/local_deep_research/web/static/js/components/settings.js
@@ -984,7 +984,7 @@
// Only run this for the main settings dashboard
if (!settingsContent) return;
- fetch('/research/settings/all_settings')
+ fetch('/research/settings/api')
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
@@ -3095,7 +3095,15 @@
* Process settings to handle object values
*/
function processSettings(settings) {
- return settings.map(setting => {
+ // Convert to a list.
+ const settingsList = [];
+ for (const key in settings) {
+ const setting = settings[key];
+ setting["key"] = key
+ settingsList.push(setting);
+ }
+
+ return settingsList.map(setting => {
const processedSetting = {...setting};
// Convert object values to JSON strings for display
diff --git a/src/local_deep_research/web/static/js/research_form.js b/src/local_deep_research/web/static/js/research_form.js
index b1045dd0ad..eb02545e30 100644
--- a/src/local_deep_research/web/static/js/research_form.js
+++ b/src/local_deep_research/web/static/js/research_form.js
@@ -18,7 +18,7 @@ function initResearchForm() {
const questionsInput = document.getElementById('questions_per_iteration');
// Fetch all settings at once (more efficient)
- fetch('/research/settings/all_settings')
+ fetch('/research/settings/api')
.then(response => {
if (!response.ok) {
throw new Error('Failed to fetch settings');
@@ -32,13 +32,14 @@ function initResearchForm() {
const settings = data.settings;
// Look for the iterations setting
- for (const setting of settings) {
- if (setting.key === 'search.iterations') {
+ for (const key in settings) {
+ const setting = settings[key];
+ if (key === 'search.iterations') {
console.log('Found iterations setting:', setting.value);
iterationsInput.value = setting.value;
}
- if (setting.key === 'search.questions_per_iteration') {
+ if (key === 'search.questions_per_iteration') {
console.log('Found questions setting:', setting.value);
questionsInput.value = setting.value;
}
diff --git a/src/local_deep_research/web_search_engines/engines/meta_search_engine.py b/src/local_deep_research/web_search_engines/engines/meta_search_engine.py
index e95250d6dc..e74d926314 100644
--- a/src/local_deep_research/web_search_engines/engines/meta_search_engine.py
+++ b/src/local_deep_research/web_search_engines/engines/meta_search_engine.py
@@ -1,5 +1,4 @@
import logging
-import os
from typing import Any, Dict, List, Optional
from ...utilities.db_utils import get_db_setting
@@ -77,8 +76,7 @@ class MetaSearchEngine(BaseSearchEngine):
continue
if config_.get("requires_api_key", False):
- api_key_env = config_.get("api_key_env")
- api_key = os.getenv(api_key_env) if api_key_env else None
+ api_key = config_.get("api_key")
if not api_key:
continue
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 0704571e0a..f6fb013d15 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
@@ -31,7 +31,6 @@ from langchain_core.language_models import BaseLLM
from langchain_text_splitters import RecursiveCharacterTextSplitter
from ...config import search_config
-from ...config.llm_config import OLLAMA_BASE_URL_ENV
from ...utilities.db_utils import get_db_setting
from ..search_engine_base import BaseSearchEngine
@@ -169,8 +168,6 @@ class LocalEmbeddingManager:
try:
if self.embedding_model_type == "ollama":
# Use Ollama for embeddings
- if not self.ollama_base_url:
- self.ollama_base_url = os.getenv(OLLAMA_BASE_URL_ENV)
if not self.ollama_base_url:
self.ollama_base_url = get_db_setting(
"llm.ollama.url", "http://localhost:11434"