fix the Ollama embedding model setting option breaking

This commit is contained in:
perf3ct 2025-03-08 22:28:14 +00:00
parent d3013c925e
commit 553f7dd498
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232
2 changed files with 31 additions and 24 deletions

View File

@ -252,45 +252,52 @@ export default class AiSettingsWidget extends OptionsWidget {
const $refreshModels = this.$widget.find('.refresh-models'); const $refreshModels = this.$widget.find('.refresh-models');
$refreshModels.on('click', async () => { $refreshModels.on('click', async () => {
$refreshModels.prop('disabled', true); $refreshModels.prop('disabled', true);
$refreshModels.text(t("ai_llm.refresh_models")); $refreshModels.text(t("ai_llm.refreshing_models"));
try { try {
const ollamaBaseUrl = this.$widget.find('.ollama-base-url').val() as string; const ollamaBaseUrl = this.$widget.find('.ollama-base-url').val() as string;
const response = await server.post<OllamaModelResponse>('ollama/list-models', { baseUrl: ollamaBaseUrl }); const response = await server.post<OllamaModelResponse>('ollama/list-models', { baseUrl: ollamaBaseUrl });
if (response && response.models) { if (response && response.success && response.models && response.models.length > 0) {
const $embedModelSelect = this.$widget.find('.ollama-embedding-model'); const $embedModelSelect = this.$widget.find('.ollama-embedding-model');
const currentValue = $embedModelSelect.val(); const currentValue = $embedModelSelect.val();
// Clear existing options // Clear existing options
$embedModelSelect.empty(); $embedModelSelect.empty();
// Add embedding-specific models first // Add embedding-specific models first
const embeddingModels = response.models.filter(model => const embeddingModels = response.models.filter(model =>
model.name.includes('embed') || model.name.includes('bert')); model.name.includes('embed') || model.name.includes('bert'));
embeddingModels.forEach(model => { embeddingModels.forEach(model => {
$embedModelSelect.append(`<option value="${model.name}">${model.name}</option>`); $embedModelSelect.append(`<option value="${model.name}">${model.name}</option>`);
}); });
// Add separator // Add separator if we have both types
$embedModelSelect.append(`<option disabled>───────────</option>`); if (embeddingModels.length > 0) {
$embedModelSelect.append(`<option disabled>───────────</option>`);
}
// Add other models (LLMs can also generate embeddings) // Add other models (LLMs can also generate embeddings)
const otherModels = response.models.filter(model => const otherModels = response.models.filter(model =>
!model.name.includes('embed') && !model.name.includes('bert')); !model.name.includes('embed') && !model.name.includes('bert'));
otherModels.forEach(model => { otherModels.forEach(model => {
$embedModelSelect.append(`<option value="${model.name}">${model.name}</option>`); $embedModelSelect.append(`<option value="${model.name}">${model.name}</option>`);
}); });
// Restore previous selection if possible // Restore previous selection if possible
if (currentValue) { if (currentValue) {
$embedModelSelect.val(currentValue); $embedModelSelect.val(currentValue);
} }
toastService.showMessage("Models refreshed successfully");
} else {
toastService.showError("No models found from Ollama server");
} }
} catch (error) { } catch (error: any) {
console.error("Error refreshing Ollama models:", error); console.error("Error refreshing Ollama models:", error);
toastService.showError(`Error refreshing models: ${error.message || 'Unknown error'}`);
} finally { } finally {
$refreshModels.prop('disabled', false); $refreshModels.prop('disabled', false);
$refreshModels.text(t("ai_llm.refresh_models")); $refreshModels.text(t("ai_llm.refresh_models"));

View File

@ -20,18 +20,18 @@ async function listModels(req: Request, res: Response) {
}); });
// Return the models list // Return the models list
return res.send({ const models = response.data.models || [];
// Important: don't use "return res.send()" - just return the data
return {
success: true, success: true,
models: response.data.models || [] models: models
}); };
} catch (error: any) { } catch (error: any) {
log.error(`Error listing Ollama models: ${error.message || 'Unknown error'}`); log.error(`Error listing Ollama models: ${error.message || 'Unknown error'}`);
return res.status(500).send({ // Properly throw the error to be handled by the global error handler
success: false, throw new Error(`Failed to list Ollama models: ${error.message || 'Unknown error'}`);
message: error.message || 'Failed to list Ollama models',
error: error.toString()
});
} }
} }