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

Commit 8657549

Browse files
vansangpfievsangjanainguyenhoangthuan99
authored
fix: use relative path for model information (#1399)
* feat: relative path for data (part1) * chore: unit tests * fix: unit tests * fix: more * fix: windows * fix: comments * fix: use fs path * chore: move logic to file manager utils (#1405) --------- Co-authored-by: vansangpfiev <sang@jan.ai> Co-authored-by: nguyenhoangthuan99 <35255081+nguyenhoangthuan99@users.noreply.github.com>
1 parent 65b1d20 commit 8657549

File tree

13 files changed

+234
-56
lines changed

13 files changed

+234
-56
lines changed

engine/commands/chat_completion_cmd.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include "chat_completion_cmd.h"
22
#include "httplib.h"
33

4+
#include "config/yaml_config.h"
45
#include "cortex_upd_cmd.h"
56
#include "database/models.h"
67
#include "model_status_cmd.h"
78
#include "run_cmd.h"
89
#include "server_start_cmd.h"
910
#include "trantor/utils/Logger.h"
1011
#include "utils/logging_utils.h"
11-
#include "config/yaml_config.h"
1212

1313
namespace commands {
1414
namespace {
@@ -41,6 +41,8 @@ struct ChunkParser {
4141

4242
void ChatCompletionCmd::Exec(const std::string& host, int port,
4343
const std::string& model_handle, std::string msg) {
44+
namespace fs = std::filesystem;
45+
namespace fmu = file_manager_utils;
4446
cortex::db::Models modellist_handler;
4547
config::YamlHandler yaml_handler;
4648
try {
@@ -49,7 +51,10 @@ void ChatCompletionCmd::Exec(const std::string& host, int port,
4951
CLI_LOG("Error: " + model_entry.error());
5052
return;
5153
}
52-
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
54+
yaml_handler.ModelConfigFromFile(
55+
fmu::ToAbsoluteCortexDataPath(
56+
fs::path(model_entry.value().path_to_model_yaml))
57+
.string());
5358
auto mc = yaml_handler.GetModelConfig();
5459
Exec(host, port, model_handle, mc, std::move(msg));
5560
} catch (const std::exception& e) {

engine/commands/model_get_cmd.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
namespace commands {
1212

1313
void ModelGetCmd::Exec(const std::string& model_handle) {
14+
namespace fs = std::filesystem;
15+
namespace fmu = file_manager_utils;
1416
cortex::db::Models modellist_handler;
1517
config::YamlHandler yaml_handler;
1618
try {
@@ -19,7 +21,10 @@ void ModelGetCmd::Exec(const std::string& model_handle) {
1921
CLI_LOG("Error: " + model_entry.error());
2022
return;
2123
}
22-
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
24+
yaml_handler.ModelConfigFromFile(
25+
fmu::ToAbsoluteCortexDataPath(
26+
fs::path(model_entry.value().path_to_model_yaml))
27+
.string());
2328
auto model_config = yaml_handler.GetModelConfig();
2429

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

engine/commands/model_import_cmd.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ ModelImportCmd::ModelImportCmd(std::string model_handle, std::string model_path)
1414
model_path_(std::move(model_path)) {}
1515

1616
void ModelImportCmd::Exec() {
17+
namespace fs = std::filesystem;
18+
namespace fmu = file_manager_utils;
1719
config::GGUFHandler gguf_handler;
1820
config::YamlHandler yaml_handler;
1921
cortex::db::Models modellist_utils_obj;
@@ -22,10 +24,13 @@ void ModelImportCmd::Exec() {
2224
std::filesystem::path("imported") /
2325
std::filesystem::path(model_handle_ + ".yml"))
2426
.string();
25-
cortex::db::ModelEntry model_entry{
26-
model_handle_, "local", "imported",
27-
model_yaml_path, model_handle_};
2827
try {
28+
// Use relative path for model_yaml_path. In case of import, we use absolute path for model
29+
auto yaml_rel_path =
30+
fmu::ToRelativeCortexDataPath(fs::path(model_yaml_path));
31+
cortex::db::ModelEntry model_entry{model_handle_, "local", "imported",
32+
yaml_rel_path.string(), model_handle_};
33+
2934
std::filesystem::create_directories(
3035
std::filesystem::path(model_yaml_path).parent_path());
3136
gguf_handler.Parse(model_path_);

engine/commands/model_list_cmd.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace commands {
1111

1212
void ModelListCmd::Exec() {
13+
namespace fs = std::filesystem;
14+
namespace fmu = file_manager_utils;
1315
auto models_path = file_manager_utils::GetModelsContainerPath();
1416
cortex::db::Models modellist_handler;
1517
config::YamlHandler yaml_handler;
@@ -29,7 +31,10 @@ void ModelListCmd::Exec() {
2931
// auto model_entry = modellist_handler.GetModelInfo(model_handle);
3032
try {
3133
count += 1;
32-
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
34+
yaml_handler.ModelConfigFromFile(
35+
fmu::ToAbsoluteCortexDataPath(
36+
fs::path(model_entry.path_to_model_yaml))
37+
.string());
3338
auto model_config = yaml_handler.GetModelConfig();
3439
table.add_row({std::to_string(count), model_entry.model,
3540
model_entry.model_alias, model_config.engine,

engine/commands/model_upd_cmd.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "model_upd_cmd.h"
2-
2+
#include "utils/file_manager_utils.h"
33
#include "utils/logging_utils.h"
44

55
namespace commands {
@@ -9,13 +9,17 @@ ModelUpdCmd::ModelUpdCmd(std::string model_handle)
99

1010
void ModelUpdCmd::Exec(
1111
const std::unordered_map<std::string, std::string>& options) {
12+
namespace fs = std::filesystem;
13+
namespace fmu = file_manager_utils;
1214
try {
1315
auto model_entry = model_list_utils_.GetModelInfo(model_handle_);
1416
if (model_entry.has_error()) {
1517
CLI_LOG("Error: " + model_entry.error());
1618
return;
1719
}
18-
yaml_handler_.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
20+
auto yaml_fp = fmu::ToAbsoluteCortexDataPath(
21+
fs::path(model_entry.value().path_to_model_yaml));
22+
yaml_handler_.ModelConfigFromFile(yaml_fp.string());
1923
model_config_ = yaml_handler_.GetModelConfig();
2024

2125
for (const auto& [key, value] : options) {
@@ -25,7 +29,7 @@ void ModelUpdCmd::Exec(
2529
}
2630

2731
yaml_handler_.UpdateModelConfig(model_config_);
28-
yaml_handler_.WriteYamlFile(model_entry.value().path_to_model_yaml);
32+
yaml_handler_.WriteYamlFile(yaml_fp.string());
2933
CLI_LOG("Successfully updated model ID '" + model_handle_ + "'!");
3034
} catch (const std::exception& e) {
3135
CLI_LOG("Failed to update model with model ID '" + model_handle_ +

engine/commands/run_cmd.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ void RunCmd::Exec(bool chat_flag) {
3232
}
3333

3434
try {
35+
namespace fs = std::filesystem;
36+
namespace fmu = file_manager_utils;
3537
auto model_entry = modellist_handler.GetModelInfo(*model_id);
3638
if (model_entry.has_error()) {
3739
CLI_LOG("Error: " + model_entry.error());
3840
return;
3941
}
40-
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
42+
yaml_handler.ModelConfigFromFile(
43+
fmu::ToAbsoluteCortexDataPath(
44+
fs::path(model_entry.value().path_to_model_yaml))
45+
.string());
4146
auto mc = yaml_handler.GetModelConfig();
4247

4348
// Check if engine existed. If not, download it

engine/config/yaml_config.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#include <fstream>
44
#include <iostream>
55
#include <string>
6-
using namespace std;
76

87
#include "utils/format_utils.h"
8+
#include "utils/file_manager_utils.h"
99
#include "yaml_config.h"
1010
namespace config {
1111
// Method to read YAML file
@@ -14,6 +14,8 @@ void YamlHandler::Reset() {
1414
yaml_node_.reset();
1515
};
1616
void YamlHandler::ReadYamlFile(const std::string& file_path) {
17+
namespace fs = std::filesystem;
18+
namespace fmu = file_manager_utils;
1719
try {
1820
yaml_node_ = YAML::LoadFile(file_path);
1921
// incase of model.yml file, we don't have files yet, create them
@@ -24,8 +26,9 @@ void YamlHandler::ReadYamlFile(const std::string& file_path) {
2426
std::vector<std::string> v;
2527
if (yaml_node_["engine"] &&
2628
yaml_node_["engine"].as<std::string>() == "cortex.llamacpp") {
27-
// TODO: change prefix to models:// with source from cortexso
28-
v.emplace_back(s.substr(0, s.find_last_of('/')) + "/model.gguf");
29+
auto abs_path = s.substr(0, s.find_last_of('/')) + "/model.gguf";
30+
auto rel_path = fmu::ToRelativeCortexDataPath(fs::path(abs_path));
31+
v.emplace_back(rel_path.string());
2932
} else {
3033
v.emplace_back(s.substr(0, s.find_last_of('/')));
3134
}
@@ -286,9 +289,7 @@ void YamlHandler::WriteYamlFile(const std::string& file_path) const {
286289
outFile << "version: " << yaml_node_["version"].as<std::string>() << "\n";
287290
}
288291
if (yaml_node_["files"] && yaml_node_["files"].size()) {
289-
outFile << "files: # can be universal protocol (models://) "
290-
"OR absolute local file path (file://) OR https remote URL "
291-
"(https://)\n";
292+
outFile << "files: # Can be relative OR absolute local file path\n";
292293
for (const auto& source : yaml_node_["files"]) {
293294
outFile << " - " << source << "\n";
294295
}

engine/controllers/models.cc

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ void Models::PullModel(const HttpRequestPtr& req,
5959
void Models::ListModel(
6060
const HttpRequestPtr& req,
6161
std::function<void(const HttpResponsePtr&)>&& callback) const {
62+
namespace fs = std::filesystem;
63+
namespace fmu = file_manager_utils;
6264
Json::Value ret;
6365
ret["object"] = "list";
6466
Json::Value data(Json::arrayValue);
@@ -73,8 +75,10 @@ void Models::ListModel(
7375
for (const auto& model_entry : list_entry.value()) {
7476
// auto model_entry = modellist_handler.GetModelInfo(model_handle);
7577
try {
76-
77-
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
78+
yaml_handler.ModelConfigFromFile(
79+
fmu::ToAbsoluteCortexDataPath(
80+
fs::path(model_entry.path_to_model_yaml))
81+
.string());
7882
auto model_config = yaml_handler.GetModelConfig();
7983
Json::Value obj = model_config.ToJson();
8084

@@ -106,6 +110,8 @@ void Models::ListModel(
106110
void Models::GetModel(const HttpRequestPtr& req,
107111
std::function<void(const HttpResponsePtr&)>&& callback,
108112
const std::string& model_id) const {
113+
namespace fs = std::filesystem;
114+
namespace fmu = file_manager_utils;
109115
LOG_DEBUG << "GetModel, Model handle: " << model_id;
110116
Json::Value ret;
111117
ret["object"] = "list";
@@ -125,7 +131,10 @@ void Models::GetModel(const HttpRequestPtr& req,
125131
callback(resp);
126132
return;
127133
}
128-
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
134+
yaml_handler.ModelConfigFromFile(
135+
fmu::ToAbsoluteCortexDataPath(
136+
fs::path(model_entry.value().path_to_model_yaml))
137+
.string());
129138
auto model_config = yaml_handler.GetModelConfig();
130139

131140
Json::Value obj = model_config.ToJson();
@@ -137,8 +146,8 @@ void Models::GetModel(const HttpRequestPtr& req,
137146
resp->setStatusCode(k200OK);
138147
callback(resp);
139148
} catch (const std::exception& e) {
140-
std::string message = "Fail to get model information with ID '" +
141-
model_id + "': " + e.what();
149+
std::string message =
150+
"Fail to get model information with ID '" + model_id + "': " + e.what();
142151
LOG_ERROR << message;
143152
ret["data"] = data;
144153
ret["result"] = "Fail to get model information";
@@ -171,16 +180,20 @@ void Models::DeleteModel(const HttpRequestPtr& req,
171180
void Models::UpdateModel(const HttpRequestPtr& req,
172181
std::function<void(const HttpResponsePtr&)>&& callback,
173182
const std::string& model_id) const {
183+
namespace fs = std::filesystem;
184+
namespace fmu = file_manager_utils;
174185
auto json_body = *(req->getJsonObject());
175186
try {
176187
cortex::db::Models model_list_utils;
177188
auto model_entry = model_list_utils.GetModelInfo(model_id);
178189
config::YamlHandler yaml_handler;
179-
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
190+
auto yaml_fp = fmu::ToAbsoluteCortexDataPath(
191+
fs::path(model_entry.value().path_to_model_yaml));
192+
yaml_handler.ModelConfigFromFile(yaml_fp.string());
180193
config::ModelConfig model_config = yaml_handler.GetModelConfig();
181194
model_config.FromJson(json_body);
182195
yaml_handler.UpdateModelConfig(model_config);
183-
yaml_handler.WriteYamlFile(model_entry.value().path_to_model_yaml);
196+
yaml_handler.WriteYamlFile(yaml_fp.string());
184197
std::string message = "Successfully update model ID '" + model_id +
185198
"': " + json_body.toStyledString();
186199
LOG_INFO << message;
@@ -210,6 +223,8 @@ void Models::UpdateModel(const HttpRequestPtr& req,
210223
void Models::ImportModel(
211224
const HttpRequestPtr& req,
212225
std::function<void(const HttpResponsePtr&)>&& callback) const {
226+
namespace fs = std::filesystem;
227+
namespace fmu = file_manager_utils;
213228
if (!http_util::HasFieldInReq(req, callback, "model") ||
214229
!http_util::HasFieldInReq(req, callback, "modelPath")) {
215230
return;
@@ -219,14 +234,18 @@ void Models::ImportModel(
219234
config::GGUFHandler gguf_handler;
220235
config::YamlHandler yaml_handler;
221236
cortex::db::Models modellist_utils_obj;
222-
223237
std::string model_yaml_path = (file_manager_utils::GetModelsContainerPath() /
224238
std::filesystem::path("imported") /
225239
std::filesystem::path(modelHandle + ".yml"))
226240
.string();
227-
cortex::db::ModelEntry model_entry{modelHandle, "local", "imported",
228-
model_yaml_path, modelHandle};
241+
229242
try {
243+
// Use relative path for model_yaml_path. In case of import, we use absolute path for model
244+
auto yaml_rel_path =
245+
fmu::ToRelativeCortexDataPath(fs::path(model_yaml_path));
246+
cortex::db::ModelEntry model_entry{modelHandle, "local", "imported",
247+
yaml_rel_path.string(), modelHandle};
248+
230249
std::filesystem::create_directories(
231250
std::filesystem::path(model_yaml_path).parent_path());
232251
gguf_handler.Parse(modelPath);
@@ -295,13 +314,13 @@ void Models::SetModelAlias(
295314
if (result.has_error()) {
296315
std::string message = result.error();
297316
LOG_ERROR << message;
298-
Json::Value ret;
299-
ret["result"] = "Set alias failed!";
300-
ret["modelHandle"] = model_handle;
301-
ret["message"] = message;
302-
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
303-
resp->setStatusCode(k400BadRequest);
304-
callback(resp);
317+
Json::Value ret;
318+
ret["result"] = "Set alias failed!";
319+
ret["modelHandle"] = model_handle;
320+
ret["message"] = message;
321+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
322+
resp->setStatusCode(k400BadRequest);
323+
callback(resp);
305324
} else {
306325
if (result.value()) {
307326
std::string message = "Successfully set model alias '" + model_alias +

engine/main.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ void RunServer() {
4646
std::filesystem::path(config.logFolderPath) /
4747
std::filesystem::path(cortex_utils::logs_folder));
4848
trantor::FileLogger asyncFileLogger;
49-
asyncFileLogger.setFileName(config.logFolderPath + "/" +
50-
cortex_utils::logs_base_name);
49+
asyncFileLogger.setFileName((std::filesystem::path(config.logFolderPath) /
50+
std::filesystem::path(cortex_utils::logs_base_name)).string());
5151
asyncFileLogger.setMaxLines(config.maxLogLines); // Keep last 100000 lines
5252
asyncFileLogger.startLogging();
5353
trantor::Logger::setOutputFunction(

engine/services/download_service.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ cpp::result<bool, std::string> DownloadService::Download(
173173
CTL_INF("Existing file size: " << download_item.downloadUrl << " - "
174174
<< download_item.localPath.string()
175175
<< " - " << existing_file_size);
176+
CTL_INF("Download item size: " << download_item.bytes.value());
176177
auto missing_bytes = download_item.bytes.value() - existing_file_size;
177178
if (missing_bytes > 0) {
178179
CLI_LOG("Found unfinished download! Additional "

0 commit comments

Comments
 (0)