Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f606e66
add model import command
nguyenhoangthuan99 Sep 18, 2024
cc4200b
Add name to model.yml
nguyenhoangthuan99 Sep 19, 2024
f9af680
add e2e test
nguyenhoangthuan99 Sep 19, 2024
e372d94
Merge branch 'dev' of github.com:janhq/cortex into feat/model-import-cmd
nguyenhoangthuan99 Sep 19, 2024
c8bd76e
Add API for import model
nguyenhoangthuan99 Sep 19, 2024
4560f90
Merge branch 'dev' into feat/model-import-cmd
nguyenhoangthuan99 Sep 19, 2024
db17979
Merge branch 'dev' of github.com:janhq/cortex into feat/model-import-cmd
nguyenhoangthuan99 Sep 19, 2024
17973f0
Merge branch 'feat/model-import-cmd' of github.com:janhq/cortex into …
nguyenhoangthuan99 Sep 19, 2024
68e7c69
Merge branch 'dev' into feat/model-import-cmd
nguyenhoangthuan99 Sep 19, 2024
4a6d08d
Update model_import_cmd.cc
nguyenhoangthuan99 Sep 19, 2024
009139d
Fix comment
nguyenhoangthuan99 Sep 19, 2024
f9d09dd
Merge branch 'dev' of github.com:janhq/cortex into feat/model-import-cmd
nguyenhoangthuan99 Sep 19, 2024
b1c26f0
Merge branch 'feat/model-import-cmd' of github.com:janhq/cortex into …
nguyenhoangthuan99 Sep 19, 2024
5cc0161
Merge branch 'dev' into feat/model-import-cmd
nguyenhoangthuan99 Sep 19, 2024
0faedf5
Merge branch 'dev' of github.com:janhq/cortex.cpp into feat/model-get
nguyenhoangthuan99 Sep 19, 2024
a70ae57
add get/list model api
nguyenhoangthuan99 Sep 19, 2024
337b199
add model list command
nguyenhoangthuan99 Sep 19, 2024
ad72ea4
Fix comment
nguyenhoangthuan99 Sep 20, 2024
054203e
merge from dev
nguyenhoangthuan99 Sep 20, 2024
2094023
Merge branch 'feat/model-import-cmd' of github.com:janhq/cortex.cpp i…
nguyenhoangthuan99 Sep 20, 2024
acf831f
Fix comment
nguyenhoangthuan99 Sep 20, 2024
86710e5
Merge branch 'dev' into feat/model-get
nguyenhoangthuan99 Sep 20, 2024
73f7553
Merge branch 'dev' into feat/model-get
nguyenhoangthuan99 Sep 20, 2024
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
149 changes: 16 additions & 133 deletions engine/commands/model_get_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,147 +1,30 @@
#include "model_get_cmd.h"
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <vector>
#include "cmd_info.h"
#include "config/yaml_config.h"
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"
#include "utils/modellist_utils.h"

namespace commands {

void ModelGetCmd::Exec(const std::string& model_handle) {
auto models_path = file_manager_utils::GetModelsContainerPath();
if (std::filesystem::exists(models_path) &&
std::filesystem::is_directory(models_path)) {
CmdInfo ci(model_handle);
std::string model_file =
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
bool found_model = false;
// Iterate through directory
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {

if (entry.is_regular_file() && entry.path().stem() == model_file &&
entry.path().extension() == ".yaml") {
try {
config::YamlHandler handler;
handler.ModelConfigFromFile(entry.path().string());
const auto& model_config = handler.GetModelConfig();
std::cout << "ModelConfig Details:\n";
std::cout << "-------------------\n";

// Print non-null strings
if (!model_config.id.empty())
std::cout << "id: " << model_config.id << "\n";
if (!model_config.name.empty())
std::cout << "name: " << model_config.name << "\n";
if (!model_config.model.empty())
std::cout << "model: " << model_config.model << "\n";
if (!model_config.version.empty())
std::cout << "version: " << model_config.version << "\n";

// Print non-empty vectors
if (!model_config.stop.empty()) {
std::cout << "stop: [";
for (size_t i = 0; i < model_config.stop.size(); ++i) {
std::cout << model_config.stop[i];
if (i < model_config.stop.size() - 1)
std::cout << ", ";
}
std::cout << "]\n";
}
// Print valid numbers
if (!std::isnan(static_cast<double>(model_config.top_p)))
std::cout << "top_p: " << model_config.top_p << "\n";
if (!std::isnan(static_cast<double>(model_config.temperature)))
std::cout << "temperature: " << model_config.temperature << "\n";
if (!std::isnan(static_cast<double>(model_config.frequency_penalty)))
std::cout << "frequency_penalty: " << model_config.frequency_penalty
<< "\n";
if (!std::isnan(static_cast<double>(model_config.presence_penalty)))
std::cout << "presence_penalty: " << model_config.presence_penalty
<< "\n";
if (!std::isnan(static_cast<double>(model_config.max_tokens)))
std::cout << "max_tokens: " << model_config.max_tokens << "\n";
if (!std::isnan(static_cast<double>(model_config.stream)))

std::cout << "stream: " << std::boolalpha << model_config.stream
<< "\n";
if (!std::isnan(static_cast<double>(model_config.ngl)))
std::cout << "ngl: " << model_config.ngl << "\n";
if (!std::isnan(static_cast<double>(model_config.ctx_len)))
std::cout << "ctx_len: " << model_config.ctx_len << "\n";

// Print non-null strings
if (!model_config.engine.empty())
std::cout << "engine: " << model_config.engine << "\n";
if (!model_config.prompt_template.empty())

std::cout << "prompt_template: " << model_config.prompt_template
<< "\n";
if (!model_config.system_template.empty())
std::cout << "system_template: " << model_config.system_template
<< "\n";
if (!model_config.user_template.empty())
std::cout << "user_template: " << model_config.user_template
<< "\n";
if (!model_config.ai_template.empty())
std::cout << "ai_template: " << model_config.ai_template << "\n";
if (!model_config.os.empty())
std::cout << "os: " << model_config.os << "\n";
if (!model_config.gpu_arch.empty())
std::cout << "gpu_arch: " << model_config.gpu_arch << "\n";
if (!model_config.quantization_method.empty())

std::cout << "quantization_method: "
<< model_config.quantization_method << "\n";
if (!model_config.precision.empty())
std::cout << "precision: " << model_config.precision << "\n";

if (!std::isnan(static_cast<double>(model_config.tp)))
std::cout << "tp: " << model_config.tp << "\n";

// Print non-null strings
if (!model_config.trtllm_version.empty())

std::cout << "trtllm_version: " << model_config.trtllm_version
<< "\n";
if (!std::isnan(static_cast<double>(model_config.text_model)))
std::cout << "text_model: " << std::boolalpha
<< model_config.text_model << "\n";

// Print non-empty vectors
if (!model_config.files.empty()) {
std::cout << "files: [";
for (size_t i = 0; i < model_config.files.size(); ++i) {
std::cout << model_config.files[i];
if (i < model_config.files.size() - 1)
std::cout << ", ";
}
std::cout << "]\n";
}

// Print valid size_t number
if (model_config.created != 0)
std::cout << "created: " << model_config.created << "\n";

if (!model_config.object.empty())
std::cout << "object: " << model_config.object << "\n";
if (!model_config.owned_by.empty())
std::cout << "owned_by: " << model_config.owned_by << "\n";

found_model = true;
break;
} catch (const std::exception& e) {
CTL_ERR("Error reading yaml file '" << entry.path().string()
<< "': " << e.what());
}
}
}
if (!found_model) {
CLI_LOG("Model not found!");
}
} else {
CLI_LOG("Model not found!");
modellist_utils::ModelListUtils modellist_handler;
config::YamlHandler yaml_handler;
try {
auto model_entry = modellist_handler.GetModelInfo(model_handle);
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
auto model_config = yaml_handler.GetModelConfig();

std::cout << model_config.ToString() << std::endl;

} catch (const std::exception& e) {
CLI_LOG("Fail to get model information with ID '" + model_handle +
"': " + e.what());
}
}
}; // namespace commands

} // namespace commands
3 changes: 2 additions & 1 deletion engine/commands/model_import_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void ModelImportCmd::Exec() {
}

} catch (const std::exception& e) {
std::remove(model_yaml_path.c_str());
// don't need to remove yml file here, because it's written only if model entry is successfully added,
// remove file here can make it fail with edge case when user try to import new model with existed model_id
CLI_LOG("Error importing model path '" + model_path_ + "' with model_id '" +
model_handle_ + "': " + e.what());
}
Expand Down
84 changes: 46 additions & 38 deletions engine/commands/model_list_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,59 @@
#include "config/yaml_config.h"
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"
#include "utils/modellist_utils.h"

namespace commands {

void ModelListCmd::Exec() {
auto models_path = file_manager_utils::GetModelsContainerPath();
if (std::filesystem::exists(models_path) &&
std::filesystem::is_directory(models_path)) {
tabulate::Table table;
modellist_utils::ModelListUtils modellist_handler;
config::YamlHandler yaml_handler;
tabulate::Table table;

table.add_row({"(Index)", "ID", "engine", "version"});
table.format().font_color(tabulate::Color::green);
int count = 0;
// Iterate through directory
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {
if (entry.is_regular_file() && entry.path().extension() == ".yaml") {
try {
count += 1;
config::YamlHandler handler;
handler.ModelConfigFromFile(entry.path().string());
const auto& model_config = handler.GetModelConfig();
table.add_row({std::to_string(count), model_config.id,
model_config.engine, model_config.version});
} catch (const std::exception& e) {
CTL_ERR("Error reading yaml file '" << entry.path().string()
<< "': " << e.what());
}
table.add_row({"(Index)", "ID", "model alias", "engine", "version"});
table.format().font_color(tabulate::Color::green);
int count = 0;
// Iterate through directory

try {
auto list_entry = modellist_handler.LoadModelList();
for (const auto& model_entry : list_entry) {
// auto model_entry = modellist_handler.GetModelInfo(model_handle);
try {
count += 1;
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
auto model_config = yaml_handler.GetModelConfig();
table.add_row({std::to_string(count), model_entry.model_id,
model_entry.model_alias, model_config.engine,
model_config.version});
yaml_handler.Reset();
} catch (const std::exception& e) {
CTL_ERR("Fail to get list model information: " + std::string(e.what()));
}
}
for (int i = 0; i < 4; i++) {
table[0][i]
.format()
.font_color(tabulate::Color::white) // Set font color
.font_style({tabulate::FontStyle::bold})
.font_align(tabulate::FontAlign::center);
}
for (int i = 1; i <= count; i++) {
table[i][0] //index value
.format()
.font_color(tabulate::Color::white) // Set font color
.font_align(tabulate::FontAlign::center);
table[i][3] //version value
.format()
.font_align(tabulate::FontAlign::center);
}
std::cout << table << std::endl;
} catch (const std::exception& e) {
CTL_ERR("Fail to get list model information: " + std::string(e.what()));
}

for (int i = 0; i < 5; i++) {
table[0][i]
.format()
.font_color(tabulate::Color::white) // Set font color
.font_style({tabulate::FontStyle::bold})
.font_align(tabulate::FontAlign::center);
}
for (int i = 1; i <= count; i++) {
table[i][0] //index value
.format()
.font_color(tabulate::Color::white) // Set font color
.font_align(tabulate::FontAlign::center);
table[i][4] //version value
.format()
.font_align(tabulate::FontAlign::center);
}
std::cout << table << std::endl;
}
}; // namespace commands
}

; // namespace commands
Loading