Skip to content

Commit 91df8bf

Browse files
committed
server : (webui) let server send default webui settings
1 parent 0a5a3b5 commit 91df8bf

File tree

6 files changed

+56
-1
lines changed

6 files changed

+56
-1
lines changed

common/arg.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3416,5 +3416,13 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
34163416
}
34173417
).set_examples({LLAMA_EXAMPLE_SERVER}));
34183418

3419+
add_opt(common_arg(
3420+
{"--default-client-config"}, "JSON_FNAME",
3421+
string_format("JSON file containing the default client config"),
3422+
[](common_params & params, const std::string & value) {
3423+
params.public_default_client_config = value;
3424+
}
3425+
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_DEFAULT_CLIENT_CONFIG"));
3426+
34193427
return ctx_arg;
34203428
}

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ struct common_params {
370370

371371
std::string hostname = "127.0.0.1";
372372
std::string public_path = ""; // NOLINT
373+
std::string public_default_client_config = ""; // NOLINT
373374
std::string chat_template = ""; // NOLINT
374375
bool use_jinja = false; // NOLINT
375376
bool enable_chat_template = true;

tools/server/public/index.html.gz

166 Bytes
Binary file not shown.

tools/server/server.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <cstddef>
2525
#include <cinttypes>
2626
#include <deque>
27+
#include <fstream>
2728
#include <memory>
2829
#include <mutex>
2930
#include <signal.h>
@@ -1905,6 +1906,7 @@ struct server_context {
19051906
// slots / clients
19061907
std::vector<server_slot> slots;
19071908
json default_generation_settings_for_props;
1909+
json default_client_config = json::object();
19081910

19091911
server_queue queue_tasks;
19101912
server_response queue_results;
@@ -2097,6 +2099,15 @@ struct server_context {
20972099

20982100
default_generation_settings_for_props = slots[0].to_json();
20992101

2102+
if (!params_base.public_default_client_config.empty()) {
2103+
std::ifstream file(params_base.public_default_client_config);
2104+
LOG_INF("%s: Loading default client config from %s\n", __func__, params_base.public_default_client_config.c_str());
2105+
if (!file.is_open()) {
2106+
throw std::runtime_error("Error: default client config file not open");
2107+
}
2108+
file >> default_client_config;
2109+
}
2110+
21002111
// the update_slots() logic will always submit a maximum of n_batch or n_parallel tokens
21012112
// note that n_batch can be > n_ctx (e.g. for non-causal attention models such as BERT where the KV cache is not used)
21022113
{
@@ -3851,6 +3862,11 @@ int main(int argc, char ** argv) {
38513862
res_ok(res, health);
38523863
};
38533864

3865+
const auto handle_default_config = [&](const httplib::Request &, httplib::Response & res) {
3866+
// default client-side config
3867+
res_ok(res, ctx_server.default_client_config);
3868+
};
3869+
38543870
const auto handle_slots = [&](const httplib::Request & req, httplib::Response & res) {
38553871
if (!params.endpoint_slots) {
38563872
res_error(res, format_error_response("This server does not support slots endpoint. Start it with `--slots`", ERROR_TYPE_NOT_SUPPORTED));
@@ -4830,6 +4846,7 @@ int main(int argc, char ** argv) {
48304846

48314847
// register API routes
48324848
svr->Get ("/health", handle_health); // public endpoint (no API key check)
4849+
svr->Get ("/defaultConfig.json", handle_default_config); // public endpoint (no API key check)
48334850
svr->Get ("/metrics", handle_metrics);
48344851
svr->Get ("/props", handle_props);
48354852
svr->Post("/props", handle_props_change);

tools/server/webui/src/components/SettingDialog.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react';
1+
import { useState, useEffect } from 'react';
22
import { useAppContext } from '../utils/app.context';
33
import { CONFIG_DEFAULT, CONFIG_INFO } from '../Config';
44
import { isDev } from '../Config';
@@ -285,6 +285,18 @@ export default function SettingDialog({
285285
);
286286
const { showConfirm, showAlert } = useModals();
287287

288+
// get default client settings
289+
useEffect(() => {
290+
StorageUtils.setDefaultConfig().then((wasChanged: boolean) => {
291+
if (wasChanged) {
292+
console.log('Setting default config');
293+
const newConfig = StorageUtils.getConfig();
294+
saveConfig(newConfig);
295+
setLocalConfig(JSON.parse(JSON.stringify(newConfig)));
296+
}
297+
});
298+
}, []);
299+
288300
const resetConfig = async () => {
289301
if (await showConfirm('Are you sure you want to reset all settings?')) {
290302
setLocalConfig(CONFIG_DEFAULT);

tools/server/webui/src/utils/storage.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ const StorageUtils = {
213213
localStorage.setItem('theme', theme);
214214
}
215215
},
216+
async setDefaultConfig(): Promise<boolean> {
217+
if (localStorage.getItem('config') === null) {
218+
try {
219+
const response = await fetch('/defaultConfig.json');
220+
const defaultConfig = await response.json();
221+
222+
// Ensure there still is no config when we overwrite it
223+
if (localStorage.getItem('config') === null) {
224+
localStorage.setItem('config', JSON.stringify(defaultConfig));
225+
}
226+
return true;
227+
} catch (e) {
228+
console.error(e);
229+
}
230+
}
231+
return false;
232+
},
216233
};
217234

218235
export default StorageUtils;

0 commit comments

Comments
 (0)