From e9599e1f633b9cd20d9f2440e374dc2ee5b44999 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Mon, 23 Sep 2024 16:03:53 +0700 Subject: [PATCH 1/2] feat: models delete for new models data folder structure --- engine/commands/model_del_cmd.cc | 73 +++++++++++++------------------- engine/commands/model_del_cmd.h | 2 +- 2 files changed, 31 insertions(+), 44 deletions(-) diff --git a/engine/commands/model_del_cmd.cc b/engine/commands/model_del_cmd.cc index f2023f5c1..8c9e336fc 100644 --- a/engine/commands/model_del_cmd.cc +++ b/engine/commands/model_del_cmd.cc @@ -2,55 +2,42 @@ #include "cmd_info.h" #include "config/yaml_config.h" #include "utils/file_manager_utils.h" +#include "utils/modellist_utils.h" namespace commands { -bool ModelDelCmd::Exec(const std::string& model_id) { - // TODO this implentation may be changed after we have a decision - // on https://github.com/janhq/cortex.cpp/issues/1154 but the logic should be similar - CmdInfo ci(model_id); - std::string model_file = - ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch; - auto models_path = file_manager_utils::GetModelsContainerPath(); - if (std::filesystem::exists(models_path) && - std::filesystem::is_directory(models_path)) { - // Iterate through directory - for (const auto& entry : std::filesystem::directory_iterator(models_path)) { - if (entry.is_regular_file() && entry.path().extension() == ".yaml") { - try { - config::YamlHandler handler; - handler.ModelConfigFromFile(entry.path().string()); - auto cfg = handler.GetModelConfig(); - if (entry.path().stem().string() == model_file) { - // Delete data - if (cfg.files.size() > 0) { - std::filesystem::path f(cfg.files[0]); - auto rel = std::filesystem::relative(f, models_path); - // Only delete model data if it is stored in our models folder - if (!rel.empty()) { - if (cfg.engine == "cortex.llamacpp") { - std::filesystem::remove_all(f.parent_path()); - } else { - std::filesystem::remove_all(f); - } - } - } +bool ModelDelCmd::Exec(const std::string& model_handle) { + modellist_utils::ModelListUtils modellist_handler; + config::YamlHandler yaml_handler; - // Delete yaml file - std::filesystem::remove(entry); - CLI_LOG("The model " << model_id << " was deleted"); - return true; - } - } catch (const std::exception& e) { - CTL_WRN("Error reading yaml file '" << entry.path().string() - << "': " << e.what()); - return false; + try { + auto model_entry = modellist_handler.GetModelInfo(model_handle); + yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml); + auto mc = yaml_handler.GetModelConfig(); + // Remove model file if it is not imported locally + if (model_entry.branch_name != "imported") { + if (mc.files.size() > 0) { + std::filesystem::path f(mc.files[0]); + if (mc.engine == "cortex.llamacpp") { + std::filesystem::remove_all(f.parent_path()); + } else { + std::filesystem::remove_all(f); } + } else { + CTL_WRN("model config files are empty!"); } } - } - - CLI_LOG("Model does not exist: " << model_id); - return false; + // update model.list + if (modellist_handler.DeleteModelEntry(model_handle)) { + CLI_LOG("The model " << model_handle << " was deleted"); + return true; + } else { + CTL_ERR("Could not delete model: " << model_handle); + return false; + } + } catch (const std::exception& e) { + CLI_LOG("Fail to delete model with ID '" + model_handle + "': " + e.what()); + false; + } } } // namespace commands \ No newline at end of file diff --git a/engine/commands/model_del_cmd.h b/engine/commands/model_del_cmd.h index 0dd41f74e..437564208 100644 --- a/engine/commands/model_del_cmd.h +++ b/engine/commands/model_del_cmd.h @@ -6,6 +6,6 @@ namespace commands { class ModelDelCmd { public: - bool Exec(const std::string& model_id); + bool Exec(const std::string& model_handle); }; } \ No newline at end of file From 2cd686d927e7b75729458abbc29522ac64b516bd Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Tue, 24 Sep 2024 08:48:38 +0700 Subject: [PATCH 2/2] feat: delete model --- engine/commands/model_del_cmd.cc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/engine/commands/model_del_cmd.cc b/engine/commands/model_del_cmd.cc index 8c9e336fc..7f6b6d32a 100644 --- a/engine/commands/model_del_cmd.cc +++ b/engine/commands/model_del_cmd.cc @@ -13,13 +13,18 @@ bool ModelDelCmd::Exec(const std::string& model_handle) { auto model_entry = modellist_handler.GetModelInfo(model_handle); yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml); auto mc = yaml_handler.GetModelConfig(); - // Remove model file if it is not imported locally + // Remove yaml file + std::filesystem::remove(model_entry.path_to_model_yaml); + // Remove model files if they are not imported locally if (model_entry.branch_name != "imported") { if (mc.files.size() > 0) { - std::filesystem::path f(mc.files[0]); if (mc.engine == "cortex.llamacpp") { - std::filesystem::remove_all(f.parent_path()); + for (auto& file : mc.files) { + std::filesystem::path gguf_p(file); + std::filesystem::remove(gguf_p); + } } else { + std::filesystem::path f(mc.files[0]); std::filesystem::remove_all(f); } } else {