From 375dec11ba0e48576319d97fb99e6c6354d25992 Mon Sep 17 00:00:00 2001 From: nguyenhoangthuan99 Date: Thu, 3 Oct 2024 16:13:48 +0700 Subject: [PATCH] chore: move logic to file manager utils --- engine/commands/chat_completion_cmd.cc | 4 +-- engine/commands/model_get_cmd.cc | 4 +-- engine/commands/model_import_cmd.cc | 2 +- engine/commands/model_list_cmd.cc | 6 ++--- engine/commands/model_upd_cmd.cc | 5 ++-- engine/commands/run_cmd.cc | 4 +-- engine/config/yaml_config.cc | 2 +- engine/controllers/models.cc | 15 +++++------ engine/services/model_service.cc | 37 ++++++++++++-------------- engine/utils/file_manager_utils.h | 10 +++++++ 10 files changed, 47 insertions(+), 42 deletions(-) diff --git a/engine/commands/chat_completion_cmd.cc b/engine/commands/chat_completion_cmd.cc index 5b934d607..5a92c4b95 100644 --- a/engine/commands/chat_completion_cmd.cc +++ b/engine/commands/chat_completion_cmd.cc @@ -52,8 +52,8 @@ void ChatCompletionCmd::Exec(const std::string& host, int port, return; } yaml_handler.ModelConfigFromFile( - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.value().path_to_model_yaml)) + fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.value().path_to_model_yaml)) .string()); auto mc = yaml_handler.GetModelConfig(); Exec(host, port, model_handle, mc, std::move(msg)); diff --git a/engine/commands/model_get_cmd.cc b/engine/commands/model_get_cmd.cc index 7b21f88df..3cf46bd84 100644 --- a/engine/commands/model_get_cmd.cc +++ b/engine/commands/model_get_cmd.cc @@ -22,8 +22,8 @@ void ModelGetCmd::Exec(const std::string& model_handle) { return; } yaml_handler.ModelConfigFromFile( - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.value().path_to_model_yaml)) + fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.value().path_to_model_yaml)) .string()); auto model_config = yaml_handler.GetModelConfig(); diff --git a/engine/commands/model_import_cmd.cc b/engine/commands/model_import_cmd.cc index 3e9f58e39..15fc6f897 100644 --- a/engine/commands/model_import_cmd.cc +++ b/engine/commands/model_import_cmd.cc @@ -27,7 +27,7 @@ void ModelImportCmd::Exec() { try { // Use relative path for model_yaml_path. In case of import, we use absolute path for model auto yaml_rel_path = - fmu::Subtract(fs::path(model_yaml_path), fmu::GetCortexDataPath()); + fmu::ToRelativeCortexDataPath(fs::path(model_yaml_path)); cortex::db::ModelEntry model_entry{model_handle_, "local", "imported", yaml_rel_path.string(), model_handle_}; diff --git a/engine/commands/model_list_cmd.cc b/engine/commands/model_list_cmd.cc index 58c1c3d8a..a340c999a 100644 --- a/engine/commands/model_list_cmd.cc +++ b/engine/commands/model_list_cmd.cc @@ -32,9 +32,9 @@ void ModelListCmd::Exec() { try { count += 1; yaml_handler.ModelConfigFromFile( - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.path_to_model_yaml)) - .string()); + fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.path_to_model_yaml)) + .string()); auto model_config = yaml_handler.GetModelConfig(); table.add_row({std::to_string(count), model_entry.model, model_entry.model_alias, model_config.engine, diff --git a/engine/commands/model_upd_cmd.cc b/engine/commands/model_upd_cmd.cc index 3f68f34a8..0864c227a 100644 --- a/engine/commands/model_upd_cmd.cc +++ b/engine/commands/model_upd_cmd.cc @@ -17,9 +17,8 @@ void ModelUpdCmd::Exec( CLI_LOG("Error: " + model_entry.error()); return; } - auto yaml_fp = - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.value().path_to_model_yaml)); + auto yaml_fp = fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.value().path_to_model_yaml)); yaml_handler_.ModelConfigFromFile(yaml_fp.string()); model_config_ = yaml_handler_.GetModelConfig(); diff --git a/engine/commands/run_cmd.cc b/engine/commands/run_cmd.cc index 103932115..1bf85afa6 100644 --- a/engine/commands/run_cmd.cc +++ b/engine/commands/run_cmd.cc @@ -40,8 +40,8 @@ void RunCmd::Exec(bool chat_flag) { return; } yaml_handler.ModelConfigFromFile( - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.value().path_to_model_yaml)) + fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.value().path_to_model_yaml)) .string()); auto mc = yaml_handler.GetModelConfig(); diff --git a/engine/config/yaml_config.cc b/engine/config/yaml_config.cc index 1e169e8f5..bfefd2d7f 100644 --- a/engine/config/yaml_config.cc +++ b/engine/config/yaml_config.cc @@ -27,7 +27,7 @@ void YamlHandler::ReadYamlFile(const std::string& file_path) { if (yaml_node_["engine"] && yaml_node_["engine"].as() == "cortex.llamacpp") { auto abs_path = s.substr(0, s.find_last_of('/')) + "/model.gguf"; - auto rel_path = fmu::Subtract(fs::path(abs_path), fmu::GetCortexDataPath()); + auto rel_path = fmu::ToRelativeCortexDataPath(fs::path(abs_path)); v.emplace_back(rel_path.string()); } else { v.emplace_back(s.substr(0, s.find_last_of('/'))); diff --git a/engine/controllers/models.cc b/engine/controllers/models.cc index 806c3ee52..739f614c9 100644 --- a/engine/controllers/models.cc +++ b/engine/controllers/models.cc @@ -76,8 +76,8 @@ void Models::ListModel( // auto model_entry = modellist_handler.GetModelInfo(model_handle); try { yaml_handler.ModelConfigFromFile( - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.path_to_model_yaml)) + fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.path_to_model_yaml)) .string()); auto model_config = yaml_handler.GetModelConfig(); Json::Value obj = model_config.ToJson(); @@ -132,8 +132,8 @@ void Models::GetModel(const HttpRequestPtr& req, return; } yaml_handler.ModelConfigFromFile( - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.value().path_to_model_yaml)) + fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.value().path_to_model_yaml)) .string()); auto model_config = yaml_handler.GetModelConfig(); @@ -187,9 +187,8 @@ void Models::UpdateModel(const HttpRequestPtr& req, cortex::db::Models model_list_utils; auto model_entry = model_list_utils.GetModelInfo(model_id); config::YamlHandler yaml_handler; - auto yaml_fp = - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.value().path_to_model_yaml)); + auto yaml_fp = fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.value().path_to_model_yaml)); yaml_handler.ModelConfigFromFile(yaml_fp.string()); config::ModelConfig model_config = yaml_handler.GetModelConfig(); model_config.FromJson(json_body); @@ -243,7 +242,7 @@ void Models::ImportModel( try { // Use relative path for model_yaml_path. In case of import, we use absolute path for model auto yaml_rel_path = - fmu::Subtract(fs::path(model_yaml_path), fmu::GetCortexDataPath()); + fmu::ToRelativeCortexDataPath(fs::path(model_yaml_path)); cortex::db::ModelEntry model_entry{modelHandle, "local", "imported", yaml_rel_path.string(), modelHandle}; diff --git a/engine/services/model_service.cc b/engine/services/model_service.cc index 518ce894d..080b88a88 100644 --- a/engine/services/model_service.cc +++ b/engine/services/model_service.cc @@ -27,8 +27,8 @@ void ParseGguf(const DownloadItem& ggufDownloadItem, model_config.id = ggufDownloadItem.localPath.parent_path().filename().string(); // use relative path for files - auto file_rel_path = fmu::Subtract(fs::path(ggufDownloadItem.localPath), - fmu::GetCortexDataPath()); + auto file_rel_path = + fmu::ToRelativeCortexDataPath(fs::path(ggufDownloadItem.localPath)); model_config.files = {file_rel_path.string()}; model_config.model = ggufDownloadItem.id; yaml_handler.UpdateModelConfig(model_config); @@ -44,8 +44,7 @@ void ParseGguf(const DownloadItem& ggufDownloadItem, auto branch = url_obj.pathParams[3]; CTL_INF("Adding model to modellist with branch: " << branch); - auto rel = file_manager_utils::Subtract( - yaml_name, file_manager_utils::GetCortexDataPath()); + auto rel = file_manager_utils::ToRelativeCortexDataPath(yaml_name); CTL_INF("path_to_model_yaml: " << rel.string()); auto author_id = author.has_value() ? author.value() : "cortexso"; @@ -304,8 +303,8 @@ cpp::result ModelService::DownloadModelFromCortexso( yaml_handler.UpdateModelConfig(mc); yaml_handler.WriteYamlFile(model_yml_item->localPath.string()); - auto rel = file_manager_utils::Subtract( - model_yml_item->localPath, file_manager_utils::GetCortexDataPath()); + auto rel = + file_manager_utils::ToRelativeCortexDataPath(model_yml_item->localPath); CTL_INF("path_to_model_yaml: " << rel.string()); cortex::db::Models modellist_utils_obj; @@ -376,9 +375,8 @@ cpp::result ModelService::DeleteModel( CTL_WRN("Error: " + model_entry.error()); return cpp::fail(model_entry.error()); } - auto yaml_fp = - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.value().path_to_model_yaml)); + auto yaml_fp = fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.value().path_to_model_yaml)); yaml_handler.ModelConfigFromFile(yaml_fp.string()); auto mc = yaml_handler.GetModelConfig(); // Remove yaml file @@ -389,12 +387,12 @@ cpp::result ModelService::DeleteModel( if (mc.engine == "cortex.llamacpp") { for (auto& file : mc.files) { std::filesystem::path gguf_p( - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), fs::path(file))); + fmu::ToAbsoluteCortexDataPath(fs::path(file))); std::filesystem::remove(gguf_p); } } else { - std::filesystem::path f(fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(mc.files[0]))); + std::filesystem::path f( + fmu::ToAbsoluteCortexDataPath(fs::path(mc.files[0]))); std::filesystem::remove_all(f); } } else { @@ -428,8 +426,8 @@ cpp::result ModelService::StartModel( return cpp::fail(model_entry.error()); } yaml_handler.ModelConfigFromFile( - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.value().path_to_model_yaml)) + fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.value().path_to_model_yaml)) .string()); auto mc = yaml_handler.GetModelConfig(); @@ -439,8 +437,7 @@ cpp::result ModelService::StartModel( if (mc.files.size() > 0) { // TODO(sang) support multiple files json_data["model_path"] = - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), fs::path(mc.files[0])) - .string(); + fmu::ToAbsoluteCortexDataPath(fs::path(mc.files[0])).string(); } else { LOG_WARN << "model_path is empty"; return false; @@ -489,8 +486,8 @@ cpp::result ModelService::StopModel( return cpp::fail(model_entry.error()); } yaml_handler.ModelConfigFromFile( - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.value().path_to_model_yaml)) + fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.value().path_to_model_yaml)) .string()); auto mc = yaml_handler.GetModelConfig(); @@ -538,8 +535,8 @@ cpp::result ModelService::GetModelStatus( return cpp::fail(model_entry.error()); } yaml_handler.ModelConfigFromFile( - fmu::GetAbsolutePath(fmu::GetCortexDataPath(), - fs::path(model_entry.value().path_to_model_yaml)) + fmu::ToAbsoluteCortexDataPath( + fs::path(model_entry.value().path_to_model_yaml)) .string()); auto mc = yaml_handler.GetModelConfig(); diff --git a/engine/utils/file_manager_utils.h b/engine/utils/file_manager_utils.h index f213a83f9..2d9801249 100644 --- a/engine/utils/file_manager_utils.h +++ b/engine/utils/file_manager_utils.h @@ -317,4 +317,14 @@ inline std::filesystem::path Subtract(const std::filesystem::path& path, } } +inline std::filesystem::path ToRelativeCortexDataPath( + const std::filesystem::path& path) { + return Subtract(path, GetCortexDataPath()); +} + +inline std::filesystem::path ToAbsoluteCortexDataPath( + const std::filesystem::path& path) { + return GetAbsolutePath(GetCortexDataPath(), path); +} + } // namespace file_manager_utils