Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 7cf2797

Browse files
Feat/model get (#1271)
* add model import command * Add name to model.yml * add e2e test * Add API for import model * Update model_import_cmd.cc * Fix comment * add get/list model api * add model list command * Fix comment * Fix comment
1 parent 3f7d3ec commit 7cf2797

File tree

6 files changed

+315
-274
lines changed

6 files changed

+315
-274
lines changed

engine/commands/model_get_cmd.cc

Lines changed: 16 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,30 @@
11
#include "model_get_cmd.h"
22
#include <filesystem>
3+
#include <iomanip>
34
#include <iostream>
45
#include <vector>
56
#include "cmd_info.h"
67
#include "config/yaml_config.h"
78
#include "utils/file_manager_utils.h"
89
#include "utils/logging_utils.h"
10+
#include "utils/modellist_utils.h"
911

1012
namespace commands {
1113

1214
void ModelGetCmd::Exec(const std::string& model_handle) {
13-
auto models_path = file_manager_utils::GetModelsContainerPath();
14-
if (std::filesystem::exists(models_path) &&
15-
std::filesystem::is_directory(models_path)) {
16-
CmdInfo ci(model_handle);
17-
std::string model_file =
18-
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
19-
bool found_model = false;
20-
// Iterate through directory
21-
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {
22-
23-
if (entry.is_regular_file() && entry.path().stem() == model_file &&
24-
entry.path().extension() == ".yaml") {
25-
try {
26-
config::YamlHandler handler;
27-
handler.ModelConfigFromFile(entry.path().string());
28-
const auto& model_config = handler.GetModelConfig();
29-
std::cout << "ModelConfig Details:\n";
30-
std::cout << "-------------------\n";
31-
32-
// Print non-null strings
33-
if (!model_config.id.empty())
34-
std::cout << "id: " << model_config.id << "\n";
35-
if (!model_config.name.empty())
36-
std::cout << "name: " << model_config.name << "\n";
37-
if (!model_config.model.empty())
38-
std::cout << "model: " << model_config.model << "\n";
39-
if (!model_config.version.empty())
40-
std::cout << "version: " << model_config.version << "\n";
41-
42-
// Print non-empty vectors
43-
if (!model_config.stop.empty()) {
44-
std::cout << "stop: [";
45-
for (size_t i = 0; i < model_config.stop.size(); ++i) {
46-
std::cout << model_config.stop[i];
47-
if (i < model_config.stop.size() - 1)
48-
std::cout << ", ";
49-
}
50-
std::cout << "]\n";
51-
}
52-
// Print valid numbers
53-
if (!std::isnan(static_cast<double>(model_config.top_p)))
54-
std::cout << "top_p: " << model_config.top_p << "\n";
55-
if (!std::isnan(static_cast<double>(model_config.temperature)))
56-
std::cout << "temperature: " << model_config.temperature << "\n";
57-
if (!std::isnan(static_cast<double>(model_config.frequency_penalty)))
58-
std::cout << "frequency_penalty: " << model_config.frequency_penalty
59-
<< "\n";
60-
if (!std::isnan(static_cast<double>(model_config.presence_penalty)))
61-
std::cout << "presence_penalty: " << model_config.presence_penalty
62-
<< "\n";
63-
if (!std::isnan(static_cast<double>(model_config.max_tokens)))
64-
std::cout << "max_tokens: " << model_config.max_tokens << "\n";
65-
if (!std::isnan(static_cast<double>(model_config.stream)))
66-
67-
std::cout << "stream: " << std::boolalpha << model_config.stream
68-
<< "\n";
69-
if (!std::isnan(static_cast<double>(model_config.ngl)))
70-
std::cout << "ngl: " << model_config.ngl << "\n";
71-
if (!std::isnan(static_cast<double>(model_config.ctx_len)))
72-
std::cout << "ctx_len: " << model_config.ctx_len << "\n";
73-
74-
// Print non-null strings
75-
if (!model_config.engine.empty())
76-
std::cout << "engine: " << model_config.engine << "\n";
77-
if (!model_config.prompt_template.empty())
78-
79-
std::cout << "prompt_template: " << model_config.prompt_template
80-
<< "\n";
81-
if (!model_config.system_template.empty())
82-
std::cout << "system_template: " << model_config.system_template
83-
<< "\n";
84-
if (!model_config.user_template.empty())
85-
std::cout << "user_template: " << model_config.user_template
86-
<< "\n";
87-
if (!model_config.ai_template.empty())
88-
std::cout << "ai_template: " << model_config.ai_template << "\n";
89-
if (!model_config.os.empty())
90-
std::cout << "os: " << model_config.os << "\n";
91-
if (!model_config.gpu_arch.empty())
92-
std::cout << "gpu_arch: " << model_config.gpu_arch << "\n";
93-
if (!model_config.quantization_method.empty())
94-
95-
std::cout << "quantization_method: "
96-
<< model_config.quantization_method << "\n";
97-
if (!model_config.precision.empty())
98-
std::cout << "precision: " << model_config.precision << "\n";
99-
100-
if (!std::isnan(static_cast<double>(model_config.tp)))
101-
std::cout << "tp: " << model_config.tp << "\n";
102-
103-
// Print non-null strings
104-
if (!model_config.trtllm_version.empty())
105-
106-
std::cout << "trtllm_version: " << model_config.trtllm_version
107-
<< "\n";
108-
if (!std::isnan(static_cast<double>(model_config.text_model)))
109-
std::cout << "text_model: " << std::boolalpha
110-
<< model_config.text_model << "\n";
111-
112-
// Print non-empty vectors
113-
if (!model_config.files.empty()) {
114-
std::cout << "files: [";
115-
for (size_t i = 0; i < model_config.files.size(); ++i) {
116-
std::cout << model_config.files[i];
117-
if (i < model_config.files.size() - 1)
118-
std::cout << ", ";
119-
}
120-
std::cout << "]\n";
121-
}
122-
123-
// Print valid size_t number
124-
if (model_config.created != 0)
125-
std::cout << "created: " << model_config.created << "\n";
126-
127-
if (!model_config.object.empty())
128-
std::cout << "object: " << model_config.object << "\n";
129-
if (!model_config.owned_by.empty())
130-
std::cout << "owned_by: " << model_config.owned_by << "\n";
131-
132-
found_model = true;
133-
break;
134-
} catch (const std::exception& e) {
135-
CTL_ERR("Error reading yaml file '" << entry.path().string()
136-
<< "': " << e.what());
137-
}
138-
}
139-
}
140-
if (!found_model) {
141-
CLI_LOG("Model not found!");
142-
}
143-
} else {
144-
CLI_LOG("Model not found!");
15+
modellist_utils::ModelListUtils modellist_handler;
16+
config::YamlHandler yaml_handler;
17+
try {
18+
auto model_entry = modellist_handler.GetModelInfo(model_handle);
19+
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
20+
auto model_config = yaml_handler.GetModelConfig();
21+
22+
std::cout << model_config.ToString() << std::endl;
23+
24+
} catch (const std::exception& e) {
25+
CLI_LOG("Fail to get model information with ID '" + model_handle +
26+
"': " + e.what());
14527
}
14628
}
147-
}; // namespace commands
29+
30+
} // namespace commands

engine/commands/model_import_cmd.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ void ModelImportCmd::Exec() {
4545
}
4646

4747
} catch (const std::exception& e) {
48-
std::remove(model_yaml_path.c_str());
48+
// don't need to remove yml file here, because it's written only if model entry is successfully added,
49+
// remove file here can make it fail with edge case when user try to import new model with existed model_id
4950
CLI_LOG("Error importing model path '" + model_path_ + "' with model_id '" +
5051
model_handle_ + "': " + e.what());
5152
}

engine/commands/model_list_cmd.cc

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,59 @@
66
#include "config/yaml_config.h"
77
#include "utils/file_manager_utils.h"
88
#include "utils/logging_utils.h"
9+
#include "utils/modellist_utils.h"
910

1011
namespace commands {
1112

1213
void ModelListCmd::Exec() {
1314
auto models_path = file_manager_utils::GetModelsContainerPath();
14-
if (std::filesystem::exists(models_path) &&
15-
std::filesystem::is_directory(models_path)) {
16-
tabulate::Table table;
15+
modellist_utils::ModelListUtils modellist_handler;
16+
config::YamlHandler yaml_handler;
17+
tabulate::Table table;
1718

18-
table.add_row({"(Index)", "ID", "engine", "version"});
19-
table.format().font_color(tabulate::Color::green);
20-
int count = 0;
21-
// Iterate through directory
22-
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {
23-
if (entry.is_regular_file() && entry.path().extension() == ".yaml") {
24-
try {
25-
count += 1;
26-
config::YamlHandler handler;
27-
handler.ModelConfigFromFile(entry.path().string());
28-
const auto& model_config = handler.GetModelConfig();
29-
table.add_row({std::to_string(count), model_config.id,
30-
model_config.engine, model_config.version});
31-
} catch (const std::exception& e) {
32-
CTL_ERR("Error reading yaml file '" << entry.path().string()
33-
<< "': " << e.what());
34-
}
19+
table.add_row({"(Index)", "ID", "model alias", "engine", "version"});
20+
table.format().font_color(tabulate::Color::green);
21+
int count = 0;
22+
// Iterate through directory
23+
24+
try {
25+
auto list_entry = modellist_handler.LoadModelList();
26+
for (const auto& model_entry : list_entry) {
27+
// auto model_entry = modellist_handler.GetModelInfo(model_handle);
28+
try {
29+
count += 1;
30+
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
31+
auto model_config = yaml_handler.GetModelConfig();
32+
table.add_row({std::to_string(count), model_entry.model_id,
33+
model_entry.model_alias, model_config.engine,
34+
model_config.version});
35+
yaml_handler.Reset();
36+
} catch (const std::exception& e) {
37+
CTL_ERR("Fail to get list model information: " + std::string(e.what()));
3538
}
3639
}
37-
for (int i = 0; i < 4; i++) {
38-
table[0][i]
39-
.format()
40-
.font_color(tabulate::Color::white) // Set font color
41-
.font_style({tabulate::FontStyle::bold})
42-
.font_align(tabulate::FontAlign::center);
43-
}
44-
for (int i = 1; i <= count; i++) {
45-
table[i][0] //index value
46-
.format()
47-
.font_color(tabulate::Color::white) // Set font color
48-
.font_align(tabulate::FontAlign::center);
49-
table[i][3] //version value
50-
.format()
51-
.font_align(tabulate::FontAlign::center);
52-
}
53-
std::cout << table << std::endl;
40+
} catch (const std::exception& e) {
41+
CTL_ERR("Fail to get list model information: " + std::string(e.what()));
42+
}
43+
44+
for (int i = 0; i < 5; i++) {
45+
table[0][i]
46+
.format()
47+
.font_color(tabulate::Color::white) // Set font color
48+
.font_style({tabulate::FontStyle::bold})
49+
.font_align(tabulate::FontAlign::center);
50+
}
51+
for (int i = 1; i <= count; i++) {
52+
table[i][0] //index value
53+
.format()
54+
.font_color(tabulate::Color::white) // Set font color
55+
.font_align(tabulate::FontAlign::center);
56+
table[i][4] //version value
57+
.format()
58+
.font_align(tabulate::FontAlign::center);
5459
}
60+
std::cout << table << std::endl;
5561
}
56-
}; // namespace commands
62+
}
63+
64+
; // namespace commands

0 commit comments

Comments
 (0)