Routes for hot-reloading and reading current vector composition
This commit is contained in:
parent
bd9f6b9dcf
commit
7b9e8726d1
2 changed files with 75 additions and 0 deletions
|
@ -3183,6 +3183,74 @@ int main(int argc, char ** argv) {
|
|||
res.status = 200; // HTTP OK
|
||||
};
|
||||
|
||||
const auto handle_get_control_vectors = [&ctx_server](const httplib::Request & req, httplib::Response & res) {
|
||||
json vectors = json::array();
|
||||
|
||||
for (const auto & vec : ctx_server.params.control_vectors) {
|
||||
vectors.push_back(json {
|
||||
{ "fname", vec.fname },
|
||||
{ "strength", vec.strength }
|
||||
});
|
||||
}
|
||||
json data = {
|
||||
{ "vectors", vectors },
|
||||
{ "layer_start", ctx_server.params.control_vector_layer_start },
|
||||
{ "layer_end", ctx_server.params.control_vector_layer_end }
|
||||
};
|
||||
res.set_content(data.dump(), "application/json; charset=utf-8");
|
||||
};
|
||||
|
||||
const auto handle_set_control_vectors = [&ctx_server, &res_error, &handle_get_control_vectors](const httplib::Request & req, httplib::Response & res) {
|
||||
res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin"));
|
||||
|
||||
json data = json::parse(req.body);
|
||||
std::vector<llama_control_vector_load_info> vec_params;
|
||||
|
||||
if (data.contains("vectors") && data["vectors"].is_array()) {
|
||||
for (const auto &item : data["vectors"]) {
|
||||
auto v = item.get<llama_control_vector_load_info>();
|
||||
std::cout << "Add vector: " << v.fname << " " << v.strength << "\n";
|
||||
vec_params.push_back(v);
|
||||
}
|
||||
} else {
|
||||
std::cerr << "No vectors passed\n";
|
||||
res_error(res, format_error_response("No vectors passed", ERROR_TYPE_SERVER));
|
||||
return;
|
||||
}
|
||||
const auto cvec = llama_control_vector_load(vec_params);
|
||||
if (cvec.n_embd == -1) {
|
||||
std::cerr << "Could not load control vector\n";
|
||||
res_error(res, format_error_response("Could not load control vector", ERROR_TYPE_SERVER));
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx_server.params.control_vector_layer_start <= 0) {
|
||||
ctx_server.params.control_vector_layer_start = 1;
|
||||
}
|
||||
if (ctx_server.params.control_vector_layer_end <= 0){
|
||||
ctx_server.params.control_vector_layer_end = llama_n_layer(ctx_server.model);
|
||||
}
|
||||
int err = llama_control_vector_apply(ctx_server.ctx,
|
||||
cvec.data.data(),
|
||||
cvec.data.size(),
|
||||
cvec.n_embd,
|
||||
ctx_server.params.control_vector_layer_start,
|
||||
ctx_server.params.control_vector_layer_end);
|
||||
if (err) {
|
||||
std::cerr << "Could not apply control vector\n";
|
||||
res_error(res, format_error_response("Could not apply control vector", ERROR_TYPE_SERVER));
|
||||
return;
|
||||
}
|
||||
ctx_server.params.control_vectors.clear();
|
||||
for (auto v : vec_params) {
|
||||
std::cout << "set vector param: " << v.fname << " " << v.strength << "\n";
|
||||
ctx_server.params.control_vectors.push_back(v);
|
||||
}
|
||||
|
||||
handle_get_control_vectors(req, res);
|
||||
};
|
||||
|
||||
|
||||
const auto handle_props = [&ctx_server](const httplib::Request & req, httplib::Response & res) {
|
||||
res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin"));
|
||||
json data = {
|
||||
|
@ -3534,6 +3602,8 @@ int main(int argc, char ** argv) {
|
|||
svr->Get ("/metrics", handle_metrics);
|
||||
svr->Get ("/props", handle_props);
|
||||
svr->Get ("/v1/models", handle_models);
|
||||
svr->Get ("/control-vectors", handle_get_control_vectors);
|
||||
svr->Post("/control-vectors", handle_set_control_vectors);
|
||||
svr->Post("/completion", handle_completions); // legacy
|
||||
svr->Post("/completions", handle_completions);
|
||||
svr->Post("/v1/completions", handle_completions);
|
||||
|
|
|
@ -615,3 +615,8 @@ static json format_error_response(const std::string & message, const enum error_
|
|||
{"type", type_str},
|
||||
};
|
||||
}
|
||||
|
||||
void from_json(const json& j, llama_control_vector_load_info& l) {
|
||||
j.at("strength").get_to(l.strength);
|
||||
j.at("fname").get_to(l.fname);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue