:zap: [Enhance] EndpointStorage: fetch_available_models and set select and default_model with promise
Browse files- storages/endpoint_storage.js +66 -37
storages/endpoint_storage.js
CHANGED
@@ -1,21 +1,17 @@
|
|
1 |
-
import {
|
2 |
-
available_models,
|
3 |
-
AvailableModelsRequester,
|
4 |
-
} from "../networks/llm_requester.js";
|
5 |
-
|
6 |
-
class EndpointStorageItem {}
|
7 |
|
8 |
class EndpointStorage {
|
9 |
constructor() {
|
10 |
this.init_database();
|
11 |
-
this.fill_available_models_select("user-customized");
|
12 |
this.load_local_endpoints();
|
13 |
this.create_endpoint_and_api_key_items();
|
|
|
14 |
}
|
15 |
init_database() {
|
16 |
this.db = new Dexie("endpoints");
|
17 |
this.db.version(1).stores({
|
18 |
-
endpoints:
|
|
|
19 |
});
|
20 |
this.db.endpoints.count((count) => {
|
21 |
console.log(`${count} endpoints loaded.`);
|
@@ -104,9 +100,6 @@ class EndpointStorage {
|
|
104 |
endpoint_and_api_key_items.append(endpoint_and_api_key_item);
|
105 |
this.bind_endpoint_and_api_key_buttons(endpoint_and_api_key_item);
|
106 |
});
|
107 |
-
endpoints.each((row) => {
|
108 |
-
this.fill_available_models_select(row.endpoint);
|
109 |
-
});
|
110 |
endpoint_and_api_key_items.hide();
|
111 |
}
|
112 |
bind_endpoint_and_api_key_buttons(endpoint_and_api_key_item) {
|
@@ -159,48 +152,84 @@ class EndpointStorage {
|
|
159 |
// TODO: remove models of current endpoint from available_models_select
|
160 |
});
|
161 |
}
|
162 |
-
|
163 |
-
let select = $("#available-models-select");
|
164 |
console.log("fetch available models for endpoint:", endpoint);
|
165 |
-
// if endpoint not starts with http
|
|
|
166 |
if (endpoint.startsWith("http")) {
|
167 |
let available_models_requester = new AvailableModelsRequester(
|
168 |
endpoint
|
169 |
);
|
170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
} else {
|
|
|
172 |
}
|
173 |
-
available_models[endpoint].forEach((value, index) => {
|
174 |
-
const option = new Option(value, value);
|
175 |
-
select.append(option);
|
176 |
-
});
|
177 |
-
this.set_default_model();
|
178 |
}
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
});
|
184 |
-
|
185 |
|
186 |
-
|
187 |
let storage_default_model = localStorage.getItem("default_model");
|
188 |
-
|
|
|
|
|
|
|
189 |
if (
|
190 |
storage_default_model &&
|
191 |
-
|
192 |
) {
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
197 |
} else {
|
198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
}
|
200 |
-
|
201 |
-
let select = $("#available-models-select");
|
202 |
-
select.val(default_model);
|
203 |
-
console.log(`default_model is set to: ${select.val()}`);
|
204 |
}
|
205 |
}
|
206 |
|
|
|
1 |
+
import { AvailableModelsRequester } from "../networks/llm_requester.js";
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
class EndpointStorage {
|
4 |
constructor() {
|
5 |
this.init_database();
|
|
|
6 |
this.load_local_endpoints();
|
7 |
this.create_endpoint_and_api_key_items();
|
8 |
+
this.fill_available_models_select();
|
9 |
}
|
10 |
init_database() {
|
11 |
this.db = new Dexie("endpoints");
|
12 |
this.db.version(1).stores({
|
13 |
+
endpoints:
|
14 |
+
"index, endpoint, api_key, need_protect, available_models",
|
15 |
});
|
16 |
this.db.endpoints.count((count) => {
|
17 |
console.log(`${count} endpoints loaded.`);
|
|
|
100 |
endpoint_and_api_key_items.append(endpoint_and_api_key_item);
|
101 |
this.bind_endpoint_and_api_key_buttons(endpoint_and_api_key_item);
|
102 |
});
|
|
|
|
|
|
|
103 |
endpoint_and_api_key_items.hide();
|
104 |
}
|
105 |
bind_endpoint_and_api_key_buttons(endpoint_and_api_key_item) {
|
|
|
152 |
// TODO: remove models of current endpoint from available_models_select
|
153 |
});
|
154 |
}
|
155 |
+
fetch_available_models(endpoint) {
|
|
|
156 |
console.log("fetch available models for endpoint:", endpoint);
|
157 |
+
// if endpoint not starts with http(s), skip
|
158 |
+
// such as "user-customized", which is used for other local functions or agents
|
159 |
if (endpoint.startsWith("http")) {
|
160 |
let available_models_requester = new AvailableModelsRequester(
|
161 |
endpoint
|
162 |
);
|
163 |
+
// update available_models field of endpoint index in db.endpoints
|
164 |
+
return available_models_requester.get().then((available_models) => {
|
165 |
+
this.db.endpoints.update(endpoint, {
|
166 |
+
available_models: JSON.stringify(available_models),
|
167 |
+
});
|
168 |
+
return available_models;
|
169 |
+
});
|
170 |
} else {
|
171 |
+
return Promise.resolve([]);
|
172 |
}
|
|
|
|
|
|
|
|
|
|
|
173 |
}
|
174 |
+
fill_available_models_select() {
|
175 |
+
// fetch available_models for all endpoints, and then fill available_models_select
|
176 |
+
let available_models_select = $("#available-models-select");
|
177 |
+
available_models_select.empty();
|
178 |
+
this.db.endpoints.toArray().then((endpoints) => {
|
179 |
+
let promises = endpoints.map((row) => {
|
180 |
+
return this.fetch_available_models(row.endpoint).then(
|
181 |
+
(available_models) => {
|
182 |
+
available_models.forEach((model_id) => {
|
183 |
+
// if model duplicated in available_models_select values,
|
184 |
+
// then attach endpoint host name
|
185 |
+
let model_name = model_id;
|
186 |
+
let endpoint_hostname = new URL(
|
187 |
+
row.endpoint
|
188 |
+
).hostname
|
189 |
+
.split(".")[0]
|
190 |
+
.split("-")[0];
|
191 |
+
let model_id_with_endpoint = `${row.endpoint}/models/${model_id}`;
|
192 |
+
|
193 |
+
model_name = `${model_id} (${endpoint_hostname})`;
|
194 |
+
const option = new Option(
|
195 |
+
model_name,
|
196 |
+
model_id_with_endpoint
|
197 |
+
);
|
198 |
+
available_models_select.append(option);
|
199 |
+
});
|
200 |
+
}
|
201 |
+
);
|
202 |
+
});
|
203 |
+
Promise.all(promises).then(() => {
|
204 |
+
this.set_default_model();
|
205 |
+
});
|
206 |
});
|
207 |
+
}
|
208 |
|
209 |
+
set_default_model() {
|
210 |
let storage_default_model = localStorage.getItem("default_model");
|
211 |
+
// format of storage_default_model is `{endpoint}/models/{model_id}`
|
212 |
+
// if storage_default_model is null, or not in the available_models_select,
|
213 |
+
// set as the first one of available_models_select
|
214 |
+
let select = $("#available-models-select");
|
215 |
if (
|
216 |
storage_default_model &&
|
217 |
+
select.find(`option[value="${storage_default_model}"]`).length > 0
|
218 |
) {
|
219 |
+
select.val(storage_default_model);
|
220 |
+
console.log(
|
221 |
+
"load default model:",
|
222 |
+
localStorage.getItem("default_model")
|
223 |
+
);
|
224 |
} else {
|
225 |
+
let new_storage_default_model = select.find("option:first").val();
|
226 |
+
select.val(new_storage_default_model);
|
227 |
+
localStorage.setItem("default_model", new_storage_default_model);
|
228 |
+
console.log(
|
229 |
+
"set new default model:",
|
230 |
+
localStorage.getItem("default_model")
|
231 |
+
);
|
232 |
}
|
|
|
|
|
|
|
|
|
233 |
}
|
234 |
}
|
235 |
|