Skip to content

Commit f0db966

Browse files
committed
- Streamlined the model loading process in SmartModel and DefaultEntitiesVectorAdapter by consolidating error handling and loading logic, improving robustness and maintainability.
- Enhanced error handling to provide clearer feedback when loading fails, including retry logic in `SmartModel`.
1 parent cdaa190 commit f0db966

5 files changed

Lines changed: 46 additions & 28 deletions

File tree

smart-embed-model/adapters/ollama.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -179,32 +179,27 @@ export class SmartEmbedOllamaAdapter extends SmartEmbedModelApiAdapter {
179179
*/
180180
async get_models(refresh = false) {
181181
if(!this.model_data || refresh) {
182-
try {
183-
const list_resp = await this.http_adapter.request({
184-
url: this.models_endpoint,
185-
method: 'GET',
182+
const list_resp = await this.http_adapter.request({
183+
url: this.models_endpoint,
184+
method: 'GET',
185+
});
186+
if (list_resp.ok === false) {
187+
throw new Error(`Failed to fetch models list: ${list_resp.statusText}`);
188+
}
189+
const list_data = await list_resp.json();
190+
const models_raw = [];
191+
for (const m of filter_embedding_models(list_data.models || [])) {
192+
const detail_resp = await this.http_adapter.request({
193+
url: 'http://localhost:11434/api/show',
194+
method: 'POST',
195+
body: JSON.stringify({ model: m.name }),
186196
});
187-
if (list_resp.ok === false) {
188-
throw new Error(`Failed to fetch models list: ${list_resp.statusText}`);
189-
}
190-
const list_data = await list_resp.json();
191-
const models_raw = [];
192-
for (const m of filter_embedding_models(list_data.models || [])) {
193-
const detail_resp = await this.http_adapter.request({
194-
url: 'http://localhost:11434/api/show',
195-
method: 'POST',
196-
body: JSON.stringify({ model: m.name }),
197-
});
198-
models_raw.push({ ...(await detail_resp.json()), name: m.name });
199-
}
200-
const model_data = this.parse_model_data(models_raw);
201-
this.model_data = model_data;
202-
this.model.re_render_settings();
203-
return model_data;
204-
} catch (error) {
205-
console.error('Failed to fetch model data:', error);
206-
return { "_": { id: `Failed to fetch models from ${this.model.adapter_name}` } };
197+
models_raw.push({ ...(await detail_resp.json()), name: m.name });
207198
}
199+
const model_data = this.parse_model_data(models_raw);
200+
this.model_data = model_data;
201+
this.model.re_render_settings();
202+
return model_data;
208203
}
209204
return this.model_data;
210205
}

smart-entities/adapters/default.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ export class DefaultEntitiesVectorAdapter extends EntitiesVectorAdapter {
112112
return;
113113
}
114114
this._is_processing_embed_queue = true;
115+
// load the embed_model if not already loaded
116+
try {
117+
if(!this.collection.embed_model.is_loaded) {
118+
await this.collection.embed_model.load();
119+
}
120+
} catch (e) {
121+
this.notices?.show('Failed to load embed_model');
122+
return;
123+
}
115124

116125
try {
117126
const embed_queue = this.collection.embed_queue;

smart-model/adapters/_adapter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export class SmartModelAdapter {
116116

117117
/**
118118
* Set the adapter's state.
119+
* @deprecated should be handled in SmartModel (only handle once)
119120
* @param {('unloaded'|'loading'|'loaded'|'unloading')} new_state - The new state
120121
* @throws {Error} If the state is invalid
121122
*/

smart-model/smart_model.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,23 @@ export class SmartModel {
163163
*/
164164
async load() {
165165
this.set_state('loading');
166-
if (!this.adapter?.is_loaded) {
167-
await this.invoke_adapter_method('load');
166+
try {
167+
if (!this.adapter?.is_loaded) {
168+
await this.invoke_adapter_method('load');
169+
}
170+
} catch (err) {
171+
this.set_state('unloaded');
172+
173+
// try to reload once per minute
174+
if(!this.reload_model_timeout) {
175+
this.reload_model_timeout = setTimeout(async () => {
176+
this.reload_model_timeout = null;
177+
await this.load();
178+
this.set_state('loaded');
179+
this.notices?.show('Loaded model: ' + this.model_key);
180+
}, 60000);
181+
}
182+
throw new Error(`Failed to load model: ${err.message}`);
168183
}
169184
this.set_state('loaded');
170185
}

smart-notices/notices.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ export const NOTICES = {
170170
en: 'Reloaded sources in {{time_ms}}ms'
171171
},
172172

173-
174-
175173
};
176174

177175

0 commit comments

Comments
 (0)