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.

fix: enhance model handling and dropdown initialization in settings

hashedviking4c47c2466e8e
diff --git a/src/local_deep_research/advanced_search_system/questions/standard_question.py b/src/local_deep_research/advanced_search_system/questions/standard_question.py
index fa1760d755..da7a650e67 100644
--- a/src/local_deep_research/advanced_search_system/questions/standard_question.py
+++ b/src/local_deep_research/advanced_search_system/questions/standard_question.py
@@ -40,9 +40,18 @@ class StandardQuestionGenerator(BaseQuestionGenerator):
prompt = f" You will have follow up questions. First, identify if your knowledge is outdated (high chance). Today: {current_time}. Generate {questions_per_iteration} high-quality internet search questions to exactly answer: {query}\n\n\nFormat: One question per line, e.g. \n Q: question1 \n Q: question2\n\n"
response = self.model.invoke(prompt)
+
+ # Handle both string responses and responses with .content attribute
+ response_text = ""
+ if hasattr(response, "content"):
+ response_text = response.content
+ else:
+ # Handle string responses
+ response_text = str(response)
+
questions = [
q.replace("Q:", "").strip()
- for q in response.content.split("\n")
+ for q in response_text.split("\n")
if q.strip().startswith("Q:")
][:questions_per_iteration]
@@ -83,7 +92,14 @@ Only provide the numbered sub-questions, nothing else."""
try:
response = self.model.invoke(prompt)
- content = response.content
+
+ # Handle both string responses and responses with .content attribute
+ content = ""
+ if hasattr(response, "content"):
+ content = response.content
+ else:
+ # Handle string responses
+ content = str(response)
# Parse sub-questions from the response
sub_questions = []
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
index 13e66fea58..3642db0d88 100644
--- a/src/local_deep_research/web/routes/settings_routes.py
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -937,6 +937,7 @@ def api_get_available_models():
{
"value": name, # Original model name as value (for API calls)
"label": f"{display_name} (Ollama)", # Pretty name as label
+ "provider": "OLLAMA", # Add provider field for consistency
}
)
current_app.logger.debug(
@@ -960,6 +961,7 @@ def api_get_available_models():
{
"value": name,
"label": f"{display_name} (Ollama)",
+ "provider": "OLLAMA", # Add provider field for consistency
}
)
current_app.logger.debug(
@@ -989,9 +991,21 @@ def api_get_available_models():
"Using fallback Ollama models due to connection error"
)
ollama_models = [
- {"value": "llama3", "label": "Llama 3 (Ollama)"},
- {"value": "mistral", "label": "Mistral (Ollama)"},
- {"value": "gemma:latest", "label": "Gemma (Ollama)"},
+ {
+ "value": "llama3",
+ "label": "Llama 3 (Ollama)",
+ "provider": "OLLAMA",
+ },
+ {
+ "value": "mistral",
+ "label": "Mistral (Ollama)",
+ "provider": "OLLAMA",
+ },
+ {
+ "value": "gemma:latest",
+ "label": "Gemma (Ollama)",
+ "provider": "OLLAMA",
+ },
]
# Always set the ollama_models in providers, whether we got real or fallback models
@@ -1010,9 +1024,13 @@ def api_get_available_models():
# Use fallback models
current_app.logger.info("Using fallback Ollama models due to error")
providers["ollama_models"] = [
- {"value": "llama3", "label": "Llama 3 (Ollama)"},
- {"value": "mistral", "label": "Mistral (Ollama)"},
- {"value": "gemma:latest", "label": "Gemma (Ollama)"},
+ {"value": "llama3", "label": "Llama 3 (Ollama)", "provider": "OLLAMA"},
+ {"value": "mistral", "label": "Mistral (Ollama)", "provider": "OLLAMA"},
+ {
+ "value": "gemma:latest",
+ "label": "Gemma (Ollama)",
+ "provider": "OLLAMA",
+ },
]
# Add OpenAI models
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 7021ea27f1..0640d63ef0 100644
--- a/src/local_deep_research/web/static/js/components/settings.js
+++ b/src/local_deep_research/web/static/js/components/settings.js
@@ -2689,38 +2689,68 @@
function processModelData(data) {
console.log('Processing model data:', data);
- // Process models from API
+ // Create a new array to store all formatted models
+ const formattedModels = [];
+
+ // Process provider options first
if (data.provider_options) {
- console.log('Found provider options:', data.provider_options);
- modelOptions = data.provider_options;
+ console.log('Found provider options:', data.provider_options.length);
+ }
- // Check for Ollama models
- if (data.providers && data.providers.ollama_models && data.providers.ollama_models.length > 0) {
- // Add models with provider info
- const ollama_models = data.providers.ollama_models;
- console.log('Found Ollama models:', ollama_models);
- modelOptions = [...modelOptions, ...ollama_models];
- }
+ // Check for Ollama models
+ if (data.providers && data.providers.ollama_models && data.providers.ollama_models.length > 0) {
+ const ollama_models = data.providers.ollama_models;
+ console.log('Found Ollama models:', ollama_models.length);
- // Add OpenAI models if available
- if (data.providers && data.providers.openai_models && data.providers.openai_models.length > 0) {
- const openai_models = data.providers.openai_models;
- console.log('Found OpenAI models:', openai_models);
- modelOptions = [...modelOptions, ...openai_models];
- }
+ // Add provider information to each model
+ ollama_models.forEach(model => {
+ formattedModels.push({
+ value: model.value,
+ label: model.label,
+ provider: 'OLLAMA' // Ensure provider field is added
+ });
+ });
+ }
- // Add Anthropic models if available
- if (data.providers && data.providers.anthropic_models && data.providers.anthropic_models.length > 0) {
- const anthropic_models = data.providers.anthropic_models;
- console.log('Found Anthropic models:', anthropic_models);
- modelOptions = [...modelOptions, ...anthropic_models];
- }
+ // Add OpenAI models if available
+ if (data.providers && data.providers.openai_models && data.providers.openai_models.length > 0) {
+ const openai_models = data.providers.openai_models;
+ console.log('Found OpenAI models:', openai_models.length);
+
+ // Add provider information to each model
+ openai_models.forEach(model => {
+ formattedModels.push({
+ value: model.value,
+ label: model.label,
+ provider: 'OPENAI' // Ensure provider field is added
+ });
+ });
}
- console.log('Final modelOptions:', modelOptions);
+ // Add Anthropic models if available
+ if (data.providers && data.providers.anthropic_models && data.providers.anthropic_models.length > 0) {
+ const anthropic_models = data.providers.anthropic_models;
+ console.log('Found Anthropic models:', anthropic_models.length);
- // Always initialize model dropdowns when receiving new data
- initializeModelDropdowns();
+ // Add provider information to each model
+ anthropic_models.forEach(model => {
+ formattedModels.push({
+ value: model.value,
+ label: model.label,
+ provider: 'ANTHROPIC' // Ensure provider field is added
+ });
+ });
+ }
+
+ // Update the global modelOptions array
+ modelOptions = formattedModels;
+ console.log('Final modelOptions:', modelOptions.length, 'models');
+
+ // Cache the processed models
+ cacheData('deepResearch.availableModels', formattedModels);
+
+ // Return the processed models
+ return formattedModels;
}
/**
@@ -2780,71 +2810,39 @@
return; // Don't proceed if required elements are missing
}
- // Ensure custom dropdown script is loaded
- if (!window.setupCustomDropdown) {
- console.warn('Custom dropdown script not loaded. Skipping initialization.');
- return; // Exit if custom dropdown script is not available
- }
-
- // Mark as initialized to prevent redundant calls
+ // Mark as initialized to prevent redundant setup
window.modelDropdownsInitialized = true;
- // Get current settings from database or localStorage
- let currentProvider = '';
- let currentModel = '';
-
- // Try to get from allSettings if loaded
- if (typeof allSettings !== 'undefined' && Array.isArray(allSettings)) {
- const providerSetting = allSettings.find(s => s.key === 'llm.provider');
- const modelSetting = allSettings.find(s => s.key === 'llm.model');
-
- if (providerSetting && providerSetting.value) {
- currentProvider = providerSetting.value;
- }
-
- if (modelSetting && modelSetting.value) {
- currentModel = modelSetting.value;
- }
- }
-
- // If settings not loaded, try localStorage
- if (!currentProvider) {
- currentProvider = localStorage.getItem('lastUsedProvider') || 'ollama';
- }
-
- if (!currentModel) {
- currentModel = localStorage.getItem('lastUsedModel') || '';
- }
+ // Load model options first
+ loadModelOptions().then(() => {
+ console.log(`Models loaded, available options: ${modelOptions.length}`);
- console.log('Current settings:', { provider: currentProvider, model: currentModel });
+ // Get current settings from hidden inputs
+ const currentProvider = providerHiddenInput.value || 'ollama';
+ const currentModel = modelHiddenInput.value || 'gemma3:12b';
- // Set provider hidden input value if available
- if (providerHiddenInput) {
- providerHiddenInput.value = currentProvider.toLowerCase();
- console.log('Set provider hidden input value:', currentProvider.toLowerCase());
- }
+ console.log('Current settings:', { provider: currentProvider, model: currentModel });
- // Set model hidden input value if available
- if (modelHiddenInput && currentModel) {
- modelHiddenInput.value = currentModel;
- console.log('Set model hidden input value:', currentModel);
- }
+ // Setup provider dropdown
+ if (settingsProviderInput && providerDropdownList && window.setupCustomDropdown) {
+ // Set hidden input value first for provider (prevents race conditions)
+ if (providerHiddenInput) {
+ console.log('Set provider hidden input value:', currentProvider);
+ providerHiddenInput.value = currentProvider;
+ }
- // Load models first, then set up dropdowns
- loadModelOptions().then(() => {
- console.log('Models loaded, available options:', modelOptions.length);
+ // Set hidden input value for model too
+ if (modelHiddenInput) {
+ console.log('Set model hidden input value:', currentModel);
+ modelHiddenInput.value = currentModel;
+ }
- // First set up the provider dropdown
- if (settingsProviderInput && window.setupCustomDropdown) {
- const providerList = document.querySelector('#llm-provider-dropdown-list');
- if (providerList) {
- const MODEL_PROVIDERS = [
- { value: 'ollama', label: 'Ollama (Local)' },
- { value: 'openai', label: 'OpenAI API' },
- { value: 'anthropic', label: 'Anthropic API' },
- { value: 'openai_endpoint', label: 'Custom OpenAI-compatible API' }
- ];
+ // If there are available options, create or update the dropdowns
+ if (MODEL_PROVIDERS && MODEL_PROVIDERS.length > 0) {
+ // Cache references to DOM elements to prevent lookups
+ const providerList = providerDropdownList;
+ // Create provider dropdown
const providerDropdown = window.setupCustomDropdown(
settingsProviderInput,
providerList,
@@ -2886,15 +2884,12 @@
}
}
- // Now set up the model dropdown
+ // Create model dropdown with full list of models first
if (settingsModelInput && modelDropdownList && modelHiddenInput && window.setupCustomDropdown) {
- // Filter models based on current provider
- const providerToUse = currentProvider.toUpperCase();
-
- // Initialize the dropdown
+ // Initialize the dropdown with ALL models first, don't filter yet
const modelDropdownControl = window.setupCustomDropdown(
settingsModelInput,
- modelDropdownList, // Use correct variable name
+ modelDropdownList,
() => modelOptions.length > 0 ? modelOptions : [
{ value: 'gpt-4o', label: 'GPT-4o (OpenAI)' },
{ value: 'gpt-3.5-turbo', label: 'GPT-3.5 Turbo (OpenAI)' },
@@ -2919,12 +2914,9 @@
true // Allow custom values
);
- // Set initial value and filter based on provider
+ // Set initial model value
if (modelDropdownControl) {
- // Filter models for the current provider
- filterModelOptionsForProvider(providerToUse);
-
- // Set initial value if we have one
+ // Set the current model without filtering first
if (currentModel) {
console.log('Setting initial model:', currentModel);
modelDropdownControl.setValue(currentModel, false); // Don't fire event
@@ -2932,6 +2924,11 @@
modelHiddenInput.value = currentModel;
}
+ // Now filter models for the current provider - AFTER setting the initial value
+ setTimeout(() => {
+ filterModelOptionsForProvider(currentProvider);
+ }, 100); // Small delay to ensure value is set first
+
// --- ADD CHANGE LISTENER TO HIDDEN INPUT ---
modelHiddenInput.removeEventListener('change', handleInputChange); // Remove old listener first
modelHiddenInput.addEventListener('change', handleInputChange);
@@ -2971,6 +2968,8 @@
setupProviderChangeListener();
}).catch(err => {
console.error('Error initializing model dropdowns:', err);
+ // Show a warning to the user
+ showAlert('Failed to load model options. Using fallback values.', 'warning');
});
}
@@ -3627,7 +3626,33 @@
const filteredModels = modelOptions.filter(model => {
if (!model || typeof model !== 'object') return false;
- // Check if model has provider property
+ // For Ollama, use more flexible matching due to model name variations
+ if (providerUpper === 'OLLAMA') {
+ // Check model provider property first
+ if (model.provider && model.provider.toUpperCase() === 'OLLAMA') {
+ return true;
+ }
+
+ // Check label for Ollama mentions
+ if (model.label && model.label.toUpperCase().includes('OLLAMA')) {
+ return true;
+ }
+
+ // Check value for common Ollama model name patterns
+ if (model.value) {
+ const value = model.value.toLowerCase();
+ // Common Ollama model name patterns
+ if (value.includes('llama') || value.includes('mistral') ||
+ value.includes('gemma') || value.includes('falcon') ||
+ value.includes('codellama') || value.includes('phi')) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ // For other providers, use standard matching
if (model.provider) {
return model.provider.toUpperCase() === providerUpper;
}
@@ -3635,8 +3660,6 @@
// If provider is missing, check label for provider hints
if (model.label) {
const label = model.label.toUpperCase();
- if (providerUpper === 'OLLAMA' && (label.includes('OLLAMA') || label.includes('LLAMA')))
- return true;
if (providerUpper === 'OPENAI' && label.includes('OPENAI'))
return true;
if (providerUpper === 'ANTHROPIC' && (label.includes('ANTHROPIC') || label.includes('CLAUDE')))
@@ -3810,125 +3833,47 @@
* @returns {Promise} Promise that resolves with model options
*/
function loadModelOptions(forceRefresh = false) {
- return new Promise((resolve, reject) => {
- // Check cache first if not forcing refresh
- if (!forceRefresh) {
- const cachedData = getCachedData('deepResearch.availableModels');
- const cacheTimestamp = getCachedData('deepResearch.cacheTimestamp');
-
- // Use cache if it exists and isn't expired (24 hours)
- if (cachedData && cacheTimestamp && (Date.now() - cacheTimestamp < 24 * 60 * 60 * 1000)) {
- console.log('Using cached model data');
- modelOptions = cachedData;
- setupCustomDropdowns();
- resolve(cachedData);
- return;
- }
- }
-
- // Fetch from API if cache is invalid or refresh is forced
- fetchModelProviders(forceRefresh)
- .then(data => {
- if (data && data.providers) {
- console.log('Got model data from API:', data);
-
- // Process the data to format for dropdowns
- const ollama_models = data.providers.ollama_models || [];
- const openai_models = data.providers.openai_models || [];
- const anthropic_models = data.providers.anthropic_models || [];
-
- // Format the models for dropdown
- const formatted_models = [];
-
- // Add Ollama models with provider information
- if (ollama_models.length > 0) {
- ollama_models.forEach(model => {
- formatted_models.push({
- value: model.value,
- label: model.label,
- provider: 'OLLAMA',
- id: model.value
- });
- });
- } else {
- // Add fallback Ollama models if none available
- formatted_models.push(
- { value: 'llama3', label: 'Llama 3 (Ollama)', provider: 'OLLAMA', id: 'llama3' },
- { value: 'mistral', label: 'Mistral (Ollama)', provider: 'OLLAMA', id: 'mistral' },
- { value: 'gemma:latest', label: 'Gemma (Ollama)', provider: 'OLLAMA', id: 'gemma:latest' }
- );
- }
-
- // Add OpenAI models
- openai_models.forEach(model => {
- formatted_models.push({
- value: model.value,
- label: model.label,
- provider: 'OPENAI',
- id: model.value
- });
- });
-
- // Add Anthropic models
- anthropic_models.forEach(model => {
- formatted_models.push({
- value: model.value,
- label: model.label,
- provider: 'ANTHROPIC',
- id: model.value
- });
- });
-
- // Update global modelOptions
- modelOptions = formatted_models;
+ // Check if we already have cached models and haven't forced a refresh
+ const cachedModels = getCachedData('deepResearch.availableModels');
+ const cacheTimestamp = getCachedData('deepResearch.cacheTimestamp');
- // Cache the data for future use
- cacheData('deepResearch.availableModels', formatted_models);
- cacheData('deepResearch.cacheTimestamp', Date.now());
+ if (!forceRefresh && cachedModels && Array.isArray(cachedModels) && cachedModels.length > 0 &&
+ cacheTimestamp && (Date.now() - cacheTimestamp < 3600000)) { // 1 hour cache
+ console.log('Using cached model options, count:', cachedModels.length);
+ modelOptions = cachedModels;
+ return Promise.resolve(cachedModels);
+ }
- // Update any existing dropdowns
- setupCustomDropdowns();
+ console.log('Loading model options from API' + (forceRefresh ? ' (forced refresh)' : ''));
- resolve(formatted_models);
- } else {
- // Use defaults if no data
- modelOptions = [
- { value: 'llama3', label: 'Llama 3 (Ollama)', provider: 'OLLAMA', id: 'llama3' },
- { value: 'mistral', label: 'Mistral (Ollama)', provider: 'OLLAMA', id: 'mistral' },
- { value: 'gemma:latest', label: 'Gemma (Ollama)', provider: 'OLLAMA', id: 'gemma:latest' },
- { value: 'gpt-4o', label: 'GPT-4o (OpenAI)', provider: 'OPENAI', id: 'gpt-4o' },
- { value: 'gpt-3.5-turbo', label: 'GPT-3.5 Turbo (OpenAI)', provider: 'OPENAI', id: 'gpt-3.5-turbo' },
- { value: 'claude-3-5-sonnet-latest', label: 'Claude 3.5 Sonnet (Anthropic)', provider: 'ANTHROPIC', id: 'claude-3-5-sonnet-latest' }
- ];
-
- // Cache the fallback data
- cacheData('deepResearch.availableModels', modelOptions);
- cacheData('deepResearch.cacheTimestamp', Date.now());
-
- setupCustomDropdowns();
- resolve(modelOptions);
- }
- })
- .catch(error => {
- console.error('Error loading model options:', error);
- // Fallback to defaults
+ return fetchModelProviders(forceRefresh)
+ .then(data => {
+ // Don't overwrite our model options if the result is empty
+ if (data && Array.isArray(data) && data.length > 0) {
+ modelOptions = data;
+ cacheData('deepResearch.availableModels', data);
+ console.log('Stored model options, count:', data.length);
+ } else {
+ console.warn('API returned empty model data, keeping existing options');
+ }
+ return modelOptions;
+ })
+ .catch(error => {
+ console.error('Error loading model options:', error);
+ // Log but don't throw, so we can continue with default models if needed
+ if (!modelOptions || modelOptions.length === 0) {
+ console.log('Using fallback model options due to error');
modelOptions = [
- { value: 'llama3', label: 'Llama 3 (Ollama)', provider: 'OLLAMA', id: 'llama3' },
- { value: 'mistral', label: 'Mistral (Ollama)', provider: 'OLLAMA', id: 'mistral' },
- { value: 'gemma:latest', label: 'Gemma (Ollama)', provider: 'OLLAMA', id: 'gemma:latest' },
- { value: 'gpt-4o', label: 'GPT-4o (OpenAI)', provider: 'OPENAI', id: 'gpt-4o' },
- { value: 'gpt-3.5-turbo', label: 'GPT-3.5 Turbo (OpenAI)', provider: 'OPENAI', id: 'gpt-3.5-turbo' },
- { value: 'claude-3-5-sonnet-latest', label: 'Claude 3.5 Sonnet (Anthropic)', provider: 'ANTHROPIC', id: 'claude-3-5-sonnet-latest' }
+ { value: 'gpt-4o', label: 'GPT-4o (OpenAI)', provider: 'OPENAI' },
+ { value: 'gpt-3.5-turbo', label: 'GPT-3.5 Turbo (OpenAI)', provider: 'OPENAI' },
+ { value: 'claude-3-5-sonnet-latest', label: 'Claude 3.5 Sonnet (Anthropic)', provider: 'ANTHROPIC' },
+ { value: 'llama3', label: 'Llama 3 (Ollama)', provider: 'OLLAMA' },
+ { value: 'mistral', label: 'Mistral (Ollama)', provider: 'OLLAMA' },
+ { value: 'gemma:latest', label: 'Gemma (Ollama)', provider: 'OLLAMA' }
];
-
- // Cache the fallback data
- cacheData('deepResearch.availableModels', modelOptions);
- cacheData('deepResearch.cacheTimestamp', Date.now());
-
- setupCustomDropdowns();
- resolve(modelOptions);
- });
- });
+ }
+ return modelOptions;
+ });
}
/**