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.

feat: Migrate Custom OpenAI and LM Studio to auto-discovery system

LearningCircuit31c4be359552
diff --git a/src/local_deep_research/llm/providers/custom_openai.py b/src/local_deep_research/llm/providers/custom_openai.py
new file mode 100644
index 0000000000..a5c29b13f0
--- /dev/null
+++ b/src/local_deep_research/llm/providers/custom_openai.py
@@ -0,0 +1,113 @@
+"""Custom OpenAI-compatible endpoint provider for Local Deep Research."""
+
+from loguru import logger
+
+from ...config.thread_settings import (
+ get_setting_from_snapshot as _get_setting_from_snapshot,
+)
+from ...utilities.url_utils import normalize_url
+from ..llm_registry import register_llm
+from .openai_base import OpenAICompatibleProvider
+
+
+def get_setting_from_snapshot(
+ key, default=None, username=None, settings_snapshot=None
+):
+ """Get setting from context only - no database access from threads.
+
+ This is a wrapper around the shared function that enables fallback LLM check.
+ """
+ return _get_setting_from_snapshot(
+ key, default, username, settings_snapshot, check_fallback_llm=True
+ )
+
+
+class CustomOpenAIProvider(OpenAICompatibleProvider):
+ """Custom OpenAI-compatible endpoint provider.
+
+ This provider allows users to connect to any OpenAI-compatible API endpoint
+ by specifying the URL and API key in settings.
+ """
+
+ provider_name = "Custom OpenAI Endpoint"
+ api_key_setting = "llm.openai_endpoint.api_key"
+ default_model = "gpt-3.5-turbo" # Default model, user should override
+
+ # Metadata for auto-discovery
+ provider_key = "OPENAI_ENDPOINT"
+ company_name = "Custom"
+ region = "Custom"
+ country = "User-defined"
+ data_location = "User-defined"
+ is_cloud = True # Assume cloud by default
+
+ @classmethod
+ def create_llm(cls, model_name=None, temperature=0.7, **kwargs):
+ """Override to get URL from settings."""
+ settings_snapshot = kwargs.get("settings_snapshot")
+
+ # Get custom endpoint URL from settings
+ custom_url = get_setting_from_snapshot(
+ "llm.openai_endpoint.url",
+ default="https://api.openai.com/v1",
+ settings_snapshot=settings_snapshot,
+ )
+
+ # Normalize and pass the custom URL to parent implementation
+ kwargs["base_url"] = (
+ normalize_url(custom_url)
+ if custom_url
+ else "https://api.openai.com/v1"
+ )
+
+ return super().create_llm(model_name, temperature, **kwargs)
+
+ @classmethod
+ def list_models_for_api(cls, api_key=None):
+ """List available models using the custom endpoint.
+
+ For custom endpoints, we need to get the URL from settings.
+ This is tricky in API context, so we return empty list for now.
+ """
+ # Custom endpoints need URL from settings which we can't easily access here
+ # Users will need to manually enter model names for custom endpoints
+ logger.debug(
+ "Custom OpenAI endpoint - model listing not supported in API context"
+ )
+ return []
+
+
+# Keep the standalone functions for backward compatibility and registration
+def create_custom_openai_llm(model_name=None, temperature=0.7, **kwargs):
+ """Factory function for Custom OpenAI-compatible endpoint LLMs.
+
+ Args:
+ model_name: Name of the model to use
+ temperature: Model temperature (0.0-1.0)
+ **kwargs: Additional arguments including settings_snapshot
+
+ Returns:
+ A configured ChatOpenAI instance pointing to custom endpoint
+
+ Raises:
+ ValueError: If API key is not configured
+ """
+ return CustomOpenAIProvider.create_llm(model_name, temperature, **kwargs)
+
+
+def is_custom_openai_available(settings_snapshot=None):
+ """Check if Custom OpenAI endpoint is available.
+
+ Args:
+ settings_snapshot: Optional settings snapshot to use
+
+ Returns:
+ True if API key is configured, False otherwise
+ """
+ return CustomOpenAIProvider.is_available(settings_snapshot)
+
+
+def register_custom_openai_provider():
+ """Register the Custom OpenAI endpoint provider with the LLM registry."""
+ register_llm("openai_endpoint", create_custom_openai_llm)
+ logger.info("Registered Custom OpenAI Endpoint LLM provider")
diff --git a/src/local_deep_research/llm/providers/lmstudio.py b/src/local_deep_research/llm/providers/lmstudio.py
new file mode 100644
index 0000000000..fb4f189e1a
--- /dev/null
+++ b/src/local_deep_research/llm/providers/lmstudio.py
@@ -0,0 +1,108 @@
+"""LM Studio LLM provider for Local Deep Research."""
+
+from loguru import logger
+
+from ..llm_registry import register_llm
+from .openai_base import OpenAICompatibleProvider
+
+
+class LMStudioProvider(OpenAICompatibleProvider):
+ """LM Studio provider using OpenAI-compatible endpoint.
+
+ LM Studio provides a local OpenAI-compatible API for running models.
+ """
+
+ provider_name = "LM Studio"
+ api_key_setting = None # LM Studio doesn't need a real API key
+ default_base_url = "http://localhost:1234/v1"
+ default_model = "local-model" # User should specify their loaded model
+
+ # Metadata for auto-discovery
+ provider_key = "LMSTUDIO"
+ company_name = "LM Studio"
+ region = "Local"
+ country = "Local"
+ data_location = "Local"
+ is_cloud = False # Local provider
+
+ @classmethod
+ def create_llm(cls, model_name=None, temperature=0.7, **kwargs):
+ """Override to handle LM Studio specifics."""
+ from ...config.thread_settings import get_setting_from_snapshot
+
+ settings_snapshot = kwargs.get("settings_snapshot")
+
+ # Get LM Studio URL from settings
+ lmstudio_url = get_setting_from_snapshot(
+ "llm.lmstudio.url",
+ "http://localhost:1234",
+ settings_snapshot=settings_snapshot,
+ )
+
+ # LM Studio uses /v1 endpoint
+ kwargs["base_url"] = f"{lmstudio_url}/v1"
+
+ # LM Studio doesn't require a real API key, just use a placeholder
+ kwargs["api_key"] = "lm-studio" # pragma: allowlist secret
+
+ # Use parent's create_llm but bypass API key check
+ return super()._create_llm_instance(model_name, temperature, **kwargs)
+
+ @classmethod
+ def is_available(cls, settings_snapshot=None):
+ """Check if LM Studio is available."""
+ try:
+ import requests
+ from ...config.thread_settings import get_setting_from_snapshot
+
+ lmstudio_url = get_setting_from_snapshot(
+ "llm.lmstudio.url",
+ "http://localhost:1234",
+ settings_snapshot=settings_snapshot,
+ )
+ # LM Studio typically uses OpenAI-compatible endpoints
+ response = requests.get(f"{lmstudio_url}/v1/models", timeout=1.0)
+ return response.status_code == 200
+ except Exception:
+ return False
+
+ @classmethod
+ def requires_auth_for_models(cls):
+ """LM Studio doesn't require authentication for listing models."""
+ return False
+
+
+# Keep the standalone functions for backward compatibility and registration
+def create_lmstudio_llm(model_name=None, temperature=0.7, **kwargs):
+ """Factory function for LM Studio LLMs.
+
+ Args:
+ model_name: Name of the model to use
+ temperature: Model temperature (0.0-1.0)
+ **kwargs: Additional arguments including settings_snapshot
+
+ Returns:
+ A configured ChatOpenAI instance pointing to LM Studio
+
+ Raises:
+ ValueError: If LM Studio is not available
+ """
+ return LMStudioProvider.create_llm(model_name, temperature, **kwargs)
+
+
+def is_lmstudio_available(settings_snapshot=None):
+ """Check if LM Studio is available.
+
+ Args:
+ settings_snapshot: Optional settings snapshot to use
+
+ Returns:
+ True if LM Studio is running, False otherwise
+ """
+ return LMStudioProvider.is_available(settings_snapshot)
+
+
+def register_lmstudio_provider():
+ """Register the LM Studio provider with the LLM registry."""
+ register_llm("lmstudio", create_lmstudio_llm)
+ logger.info("Registered LM Studio LLM provider")
diff --git a/src/local_deep_research/llm/providers/openai_base.py b/src/local_deep_research/llm/providers/openai_base.py
index 5775bdd77c..b0b83cd996 100644
--- a/src/local_deep_research/llm/providers/openai_base.py
+++ b/src/local_deep_research/llm/providers/openai_base.py
@@ -52,20 +52,26 @@ class OpenAICompatibleProvider:
"""
settings_snapshot = kwargs.get("settings_snapshot")
- # Get API key from settings
- api_key = get_setting_from_snapshot(
- cls.api_key_setting,
- default=None,
- settings_snapshot=settings_snapshot,
- )
-
- if not api_key:
- logger.error(f"{cls.provider_name} API key not found in settings")
- raise ValueError(
- f"{cls.provider_name} API key not configured. "
- f"Please set {cls.api_key_setting} in settings."
+ # Get API key from settings (if provider requires one)
+ if cls.api_key_setting:
+ api_key = get_setting_from_snapshot(
+ cls.api_key_setting,
+ default=None,
+ settings_snapshot=settings_snapshot,
)
+ if not api_key:
+ logger.error(
+ f"{cls.provider_name} API key not found in settings"
+ )
+ raise ValueError(
+ f"{cls.provider_name} API key not configured. "
+ f"Please set {cls.api_key_setting} in settings."
+ )
+ else:
+ # Provider doesn't require API key (e.g., LM Studio)
+ api_key = kwargs.get("api_key", "dummy-key")
+
# Use default model if none specified
if not model_name:
model_name = cls.default_model
@@ -137,6 +143,47 @@ class OpenAICompatibleProvider:
return ChatOpenAI(**llm_params)
+ @classmethod
+ def _create_llm_instance(cls, model_name=None, temperature=0.7, **kwargs):
+ """Internal method to create LLM instance with provided parameters.
+
+ This bypasses API key checking for providers that handle auth differently.
+ """
+ settings_snapshot = kwargs.get("settings_snapshot")
+
+ # Use default model if none specified
+ if not model_name:
+ model_name = cls.default_model
+
+ # Get endpoint URL (can be overridden in kwargs for flexibility)
+ base_url = kwargs.get("base_url", cls.default_base_url)
+ base_url = normalize_url(base_url) if base_url else cls.default_base_url
+
+ # Get API key from kwargs (caller is responsible for providing it)
+ api_key = kwargs.get("api_key", "dummy-key")
+
+ # Build parameters for OpenAI client
+ llm_params = {
+ "model": model_name,
+ "api_key": api_key,
+ "base_url": base_url,
+ "temperature": temperature,
+ }
+
+ # Add optional parameters (same as in create_llm)
+ try:
+ max_tokens = get_setting_from_snapshot(
+ "llm.max_tokens",
+ default=None,
+ settings_snapshot=settings_snapshot,
+ )
+ if max_tokens:
+ llm_params["max_tokens"] = int(max_tokens)
+ except NoSettingsContextError:
+ pass
+
+ return ChatOpenAI(**llm_params)
+
@classmethod
def is_available(cls, settings_snapshot=None):
"""Check if this provider is available.
@@ -145,9 +192,13 @@ class OpenAICompatibleProvider:
settings_snapshot: Optional settings snapshot to use
Returns:
- True if API key is configured, False otherwise
+ True if API key is configured (or not needed), False otherwise
"""
try:
+ # If provider doesn't require API key, it's available
+ if not cls.api_key_setting:
+ return True
+
# Check if API key is configured
api_key = get_setting_from_snapshot(
cls.api_key_setting,
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
index 61959f48f1..a9cce1721c 100644
--- a/src/local_deep_research/web/routes/settings_routes.py
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -947,12 +947,10 @@ def api_get_available_models():
discovered_options = get_discovered_provider_options()
provider_options.extend(discovered_options)
- # Add remaining hardcoded providers
+ # Add remaining hardcoded providers (complex local providers not yet migrated)
provider_options.extend(
[
- {"value": "OPENAI_ENDPOINT", "label": "Custom OpenAI Endpoint"},
{"value": "VLLM", "label": "vLLM (Local)"},
- {"value": "LMSTUDIO", "label": "LM Studio (Local)"},
{"value": "LLAMACPP", "label": "Llama.cpp (Local)"},
]
)