-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cortex models delete command (#1189)
* feat: cortex models delete command * feat: add server API * feat: add server API * chore: add e2e tests for model delete
- Loading branch information
1 parent
6abf575
commit 554bb84
Showing
8 changed files
with
136 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "model_del_cmd.h" | ||
#include "cmd_info.h" | ||
#include "config/yaml_config.h" | ||
#include "utils/file_manager_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); | ||
} | ||
} | ||
} | ||
|
||
// 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; | ||
} | ||
} | ||
} | ||
} | ||
|
||
CLI_LOG("Model does not exist: " << model_id); | ||
|
||
return false; | ||
} | ||
} // namespace commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace commands { | ||
|
||
class ModelDelCmd { | ||
public: | ||
bool Exec(const std::string& model_id); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pytest | ||
from test_runner import run | ||
|
||
|
||
class TestCliModelDelete: | ||
|
||
@pytest.fixture(autouse=True) | ||
def setup_and_teardown(self): | ||
# Setup | ||
# Pull model | ||
run("Pull Model", ["pull", "tinyllama"], 120) | ||
|
||
yield | ||
|
||
# Teardown | ||
# Clean up | ||
run("Delete model", ["models", "delete", "tinyllama"]) | ||
|
||
def test_models_delete_should_be_successful(self): | ||
exit_code, output, error = run( | ||
"Delete model", ["models", "delete", "tinyllama"] | ||
) | ||
assert "The model tinyllama was deleted" in output | ||
assert exit_code == 0, f"Model does not exist: {error}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters