Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/model import cmd #1248

Merged
merged 16 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add model import command
  • Loading branch information
nguyenhoangthuan99 committed Sep 18, 2024
commit f606e66f07c07cdfb87b6b2bbb181b482f4371bf
52 changes: 52 additions & 0 deletions engine/commands/model_import_cmd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "model_import_cmd.h"
#include <filesystem>
#include <iostream>
#include <vector>
#include "config/gguf_parser.h"
#include "config/yaml_config.h"
#include "trantor/utils/Logger.h"
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"
#include "utils/modellist_utils.h"

namespace commands {

ModelImportCmd::ModelImportCmd(std::string model_handle, std::string model_path)
: model_handle_(std::move(model_handle)),
model_path_(std::move(model_path)) {}

void ModelImportCmd::Exec() {
config::GGUFHandler gguf_handler;
config::YamlHandler yaml_handler;
modellist_utils::ModelListUtils modellist_utils_obj;

std::string model_yaml_path = (file_manager_utils::GetModelsContainerPath() /
std::filesystem::path("imported") /
std::filesystem::path(model_handle_ + ".yml"))
.string();
modellist_utils::ModelEntry model_entry{
model_handle_, "local", "imported",
model_yaml_path, model_handle_, modellist_utils::ModelStatus::READY};
try {
std::filesystem::create_directories(
std::filesystem::path(model_yaml_path).parent_path());
gguf_handler.Parse(model_path_);
config::ModelConfig model_config = gguf_handler.GetModelConfig();
nguyenhoangthuan99 marked this conversation as resolved.
Show resolved Hide resolved
model_config.files.push_back(model_path_);
yaml_handler.UpdateModelConfig(model_config);

if(modellist_utils_obj.AddModelEntry(model_entry)){
yaml_handler.WriteYamlFile(model_yaml_path);
CLI_LOG("Model is imported successfully!");
}
else{
CLI_LOG("Fail to import model, model_id '"+model_handle_+"' already exists!" );
}

} catch (const std::exception& e) {
std::remove(model_yaml_path.c_str());
CTL_ERR("Error importing model '" << model_path_ << "' with model_id '"
<< model_handle_ << "': " << e.what());
}
}
} // namespace commands
16 changes: 16 additions & 0 deletions engine/commands/model_import_cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <cmath> // For std::isnan
nguyenhoangthuan99 marked this conversation as resolved.
Show resolved Hide resolved
#include <string>
namespace commands {

class ModelImportCmd {
public:
ModelImportCmd(std::string model_handle, std::string model_path);
void Exec();

private:
std::string model_handle_;
std::string model_path_;
};
} // namespace commands
17 changes: 16 additions & 1 deletion engine/controllers/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "commands/engine_uninstall_cmd.h"
#include "commands/model_del_cmd.h"
#include "commands/model_get_cmd.h"
#include "commands/model_import_cmd.h"
#include "commands/model_list_cmd.h"
#include "commands/model_pull_cmd.h"
#include "commands/model_start_cmd.h"
Expand Down Expand Up @@ -155,6 +156,20 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
auto model_update_cmd =
models_cmd->add_subcommand("update", "Update configuration of a model");

std::string model_path;
auto model_import_cmd = models_cmd->add_subcommand(
"import", "Import a gguf model from local file");
model_import_cmd->add_option("--model_id", model_id, "");
model_import_cmd->require_option();
nguyenhoangthuan99 marked this conversation as resolved.
Show resolved Hide resolved
model_import_cmd->add_option("--model_path", model_path,
"Absolute path to .gguf model, the path should "
"include the gguf file name");
model_import_cmd->require_option();
model_import_cmd->callback([&model_id,&model_path]() {
commands::ModelImportCmd command(model_id, model_path);
command.Exec();
});

// Default version is latest
std::string version{"latest"};
// engines group commands
Expand Down Expand Up @@ -238,7 +253,7 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
auto ps_cmd =
app_.add_subcommand("ps", "Show running models and their status");
ps_cmd->group(kSystemGroup);

CLI11_PARSE(app_, argc, argv);
if (argc == 1) {
CLI_LOG(app_.help());
Expand Down