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

fix: use relative path for model information #1399

Merged
merged 10 commits into from
Oct 3, 2024
Next Next commit
feat: relative path for data (part1)
  • Loading branch information
vansangpfiev committed Oct 3, 2024
commit c8d60b9dcb9065dc0483040345780339c2a5a760
9 changes: 7 additions & 2 deletions engine/commands/chat_completion_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "chat_completion_cmd.h"
#include "httplib.h"

#include "config/yaml_config.h"
#include "cortex_upd_cmd.h"
#include "database/models.h"
#include "model_status_cmd.h"
#include "run_cmd.h"
#include "server_start_cmd.h"
#include "trantor/utils/Logger.h"
#include "utils/logging_utils.h"
#include "config/yaml_config.h"

namespace commands {
namespace {
Expand Down Expand Up @@ -41,6 +41,8 @@ struct ChunkParser {

void ChatCompletionCmd::Exec(const std::string& host, int port,
const std::string& model_handle, std::string msg) {
namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
cortex::db::Models modellist_handler;
config::YamlHandler yaml_handler;
try {
Expand All @@ -49,7 +51,10 @@ void ChatCompletionCmd::Exec(const std::string& host, int port,
CLI_LOG("Error: " + model_entry.error());
return;
}
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
yaml_handler.ModelConfigFromFile(
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.value().path_to_model_yaml))
.string());
auto mc = yaml_handler.GetModelConfig();
Exec(host, port, model_handle, mc, std::move(msg));
} catch (const std::exception& e) {
Expand Down
7 changes: 6 additions & 1 deletion engine/commands/model_get_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace commands {

void ModelGetCmd::Exec(const std::string& model_handle) {
namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
cortex::db::Models modellist_handler;
config::YamlHandler yaml_handler;
try {
Expand All @@ -19,7 +21,10 @@ void ModelGetCmd::Exec(const std::string& model_handle) {
CLI_LOG("Error: " + model_entry.error());
return;
}
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
yaml_handler.ModelConfigFromFile(
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.value().path_to_model_yaml))
.string());
auto model_config = yaml_handler.GetModelConfig();

std::cout << model_config.ToString() << std::endl;
Expand Down
7 changes: 6 additions & 1 deletion engine/commands/model_list_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace commands {

void ModelListCmd::Exec() {
namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
auto models_path = file_manager_utils::GetModelsContainerPath();
cortex::db::Models modellist_handler;
config::YamlHandler yaml_handler;
Expand All @@ -29,7 +31,10 @@ void ModelListCmd::Exec() {
// auto model_entry = modellist_handler.GetModelInfo(model_handle);
try {
count += 1;
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
yaml_handler.ModelConfigFromFile(
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.path_to_model_yaml))
.string());
auto model_config = yaml_handler.GetModelConfig();
table.add_row({std::to_string(count), model_entry.model,
model_entry.model_alias, model_config.engine,
Expand Down
11 changes: 8 additions & 3 deletions engine/commands/model_upd_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "model_upd_cmd.h"

#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"

namespace commands {
Expand All @@ -9,13 +9,18 @@ ModelUpdCmd::ModelUpdCmd(std::string model_handle)

void ModelUpdCmd::Exec(
const std::unordered_map<std::string, std::string>& options) {
namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
try {
auto model_entry = model_list_utils_.GetModelInfo(model_handle_);
if (model_entry.has_error()) {
CLI_LOG("Error: " + model_entry.error());
return;
}
yaml_handler_.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
auto yaml_fp =
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.value().path_to_model_yaml));
yaml_handler_.ModelConfigFromFile(yaml_fp.string());
model_config_ = yaml_handler_.GetModelConfig();

for (const auto& [key, value] : options) {
Expand All @@ -25,7 +30,7 @@ void ModelUpdCmd::Exec(
}

yaml_handler_.UpdateModelConfig(model_config_);
yaml_handler_.WriteYamlFile(model_entry.value().path_to_model_yaml);
yaml_handler_.WriteYamlFile(yaml_fp.string());
CLI_LOG("Successfully updated model ID '" + model_handle_ + "'!");
} catch (const std::exception& e) {
CLI_LOG("Failed to update model with model ID '" + model_handle_ +
Expand Down
7 changes: 6 additions & 1 deletion engine/commands/run_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ void RunCmd::Exec(bool chat_flag) {
}

try {
namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
auto model_entry = modellist_handler.GetModelInfo(*model_id);
if (model_entry.has_error()) {
CLI_LOG("Error: " + model_entry.error());
return;
}
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
yaml_handler.ModelConfigFromFile(
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.value().path_to_model_yaml))
.string());
auto mc = yaml_handler.GetModelConfig();

// Check if engine existed. If not, download it
Expand Down
42 changes: 28 additions & 14 deletions engine/controllers/models.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ void Models::PullModel(const HttpRequestPtr& req,
void Models::ListModel(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) const {
namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
Json::Value ret;
ret["object"] = "list";
Json::Value data(Json::arrayValue);
Expand All @@ -73,8 +75,10 @@ void Models::ListModel(
for (const auto& model_entry : list_entry.value()) {
// auto model_entry = modellist_handler.GetModelInfo(model_handle);
try {

yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
yaml_handler.ModelConfigFromFile(
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.path_to_model_yaml))
.string());
auto model_config = yaml_handler.GetModelConfig();
Json::Value obj = model_config.ToJson();

Expand Down Expand Up @@ -106,6 +110,8 @@ void Models::ListModel(
void Models::GetModel(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& model_id) const {
namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
LOG_DEBUG << "GetModel, Model handle: " << model_id;
Json::Value ret;
ret["object"] = "list";
Expand All @@ -125,7 +131,10 @@ void Models::GetModel(const HttpRequestPtr& req,
callback(resp);
return;
}
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
yaml_handler.ModelConfigFromFile(
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.value().path_to_model_yaml))
.string());
auto model_config = yaml_handler.GetModelConfig();

Json::Value obj = model_config.ToJson();
Expand All @@ -137,8 +146,8 @@ void Models::GetModel(const HttpRequestPtr& req,
resp->setStatusCode(k200OK);
callback(resp);
} catch (const std::exception& e) {
std::string message = "Fail to get model information with ID '" +
model_id + "': " + e.what();
std::string message =
"Fail to get model information with ID '" + model_id + "': " + e.what();
LOG_ERROR << message;
ret["data"] = data;
ret["result"] = "Fail to get model information";
Expand Down Expand Up @@ -171,16 +180,21 @@ void Models::DeleteModel(const HttpRequestPtr& req,
void Models::UpdateModel(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& model_id) const {
namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
auto json_body = *(req->getJsonObject());
try {
cortex::db::Models model_list_utils;
auto model_entry = model_list_utils.GetModelInfo(model_id);
config::YamlHandler yaml_handler;
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
auto yaml_fp =
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.value().path_to_model_yaml));
yaml_handler.ModelConfigFromFile(yaml_fp.string());
config::ModelConfig model_config = yaml_handler.GetModelConfig();
model_config.FromJson(json_body);
yaml_handler.UpdateModelConfig(model_config);
yaml_handler.WriteYamlFile(model_entry.value().path_to_model_yaml);
yaml_handler.WriteYamlFile(yaml_fp.string());
std::string message = "Successfully update model ID '" + model_id +
"': " + json_body.toStyledString();
LOG_INFO << message;
Expand Down Expand Up @@ -295,13 +309,13 @@ void Models::SetModelAlias(
if (result.has_error()) {
std::string message = result.error();
LOG_ERROR << message;
Json::Value ret;
ret["result"] = "Set alias failed!";
ret["modelHandle"] = model_handle;
ret["message"] = message;
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
resp->setStatusCode(k400BadRequest);
callback(resp);
Json::Value ret;
ret["result"] = "Set alias failed!";
ret["modelHandle"] = model_handle;
ret["message"] = message;
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
resp->setStatusCode(k400BadRequest);
callback(resp);
} else {
if (result.value()) {
std::string message = "Successfully set model alias '" + model_alias +
Expand Down
53 changes: 39 additions & 14 deletions engine/services/model_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ void ParseGguf(const DownloadItem& ggufDownloadItem,
auto branch = url_obj.pathParams[3];
CTL_INF("Adding model to modellist with branch: " << branch);

auto rel = file_manager_utils::Subtract(
yaml_name, file_manager_utils::GetCortexDataPath());
CTL_INF("path_to_model_yaml: " << rel.string());

auto author_id = author.has_value() ? author.value() : "cortexso";
cortex::db::Models modellist_utils_obj;
cortex::db::ModelEntry model_entry{.model = ggufDownloadItem.id,
.author_repo_id = author_id,
.branch_name = branch,
.path_to_model_yaml = yaml_name.string(),
.path_to_model_yaml = rel.string(),
.model_alias = ggufDownloadItem.id};
modellist_utils_obj.AddModelEntry(model_entry, true);
}
Expand Down Expand Up @@ -295,13 +299,16 @@ cpp::result<std::string, std::string> ModelService::DownloadModelFromCortexso(
yaml_handler.UpdateModelConfig(mc);
yaml_handler.WriteYamlFile(model_yml_item->localPath.string());

auto rel = file_manager_utils::Subtract(
model_yml_item->localPath, file_manager_utils::GetCortexDataPath());
CTL_INF("path_to_model_yaml: " << rel.string());

cortex::db::Models modellist_utils_obj;
cortex::db::ModelEntry model_entry{
.model = model_id,
.author_repo_id = "cortexso",
.branch_name = branch,
.path_to_model_yaml = model_yml_item->localPath.string(),
.model_alias = model_id};
cortex::db::ModelEntry model_entry{.model = model_id,
.author_repo_id = "cortexso",
.branch_name = branch,
.path_to_model_yaml = rel.string(),
.model_alias = model_id};
modellist_utils_obj.AddModelEntry(model_entry);
};

Expand Down Expand Up @@ -353,7 +360,8 @@ ModelService::DownloadHuggingFaceGgufModel(const std::string& author,

cpp::result<void, std::string> ModelService::DeleteModel(
const std::string& model_handle) {

namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
cortex::db::Models modellist_handler;
config::YamlHandler yaml_handler;

Expand All @@ -363,10 +371,13 @@ cpp::result<void, std::string> ModelService::DeleteModel(
CTL_WRN("Error: " + model_entry.error());
return cpp::fail(model_entry.error());
}
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
auto yaml_fp =
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.value().path_to_model_yaml));
yaml_handler.ModelConfigFromFile(yaml_fp.string());
auto mc = yaml_handler.GetModelConfig();
// Remove yaml file
std::filesystem::remove(model_entry.value().path_to_model_yaml);
std::filesystem::remove(yaml_fp);
// Remove model files if they are not imported locally
if (model_entry.value().branch_name != "imported") {
if (mc.files.size() > 0) {
Expand Down Expand Up @@ -398,7 +409,8 @@ cpp::result<void, std::string> ModelService::DeleteModel(

cpp::result<bool, std::string> ModelService::StartModel(
const std::string& host, int port, const std::string& model_handle) {

namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
cortex::db::Models modellist_handler;
config::YamlHandler yaml_handler;

Expand All @@ -408,7 +420,10 @@ cpp::result<bool, std::string> ModelService::StartModel(
CTL_WRN("Error: " + model_entry.error());
return cpp::fail(model_entry.error());
}
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
yaml_handler.ModelConfigFromFile(
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.value().path_to_model_yaml))
.string());
auto mc = yaml_handler.GetModelConfig();

httplib::Client cli(host + ":" + std::to_string(port));
Expand Down Expand Up @@ -453,6 +468,8 @@ cpp::result<bool, std::string> ModelService::StartModel(

cpp::result<bool, std::string> ModelService::StopModel(
const std::string& host, int port, const std::string& model_handle) {
namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
cortex::db::Models modellist_handler;
config::YamlHandler yaml_handler;

Expand All @@ -462,7 +479,10 @@ cpp::result<bool, std::string> ModelService::StopModel(
CTL_WRN("Error: " + model_entry.error());
return cpp::fail(model_entry.error());
}
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
yaml_handler.ModelConfigFromFile(
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.value().path_to_model_yaml))
.string());
auto mc = yaml_handler.GetModelConfig();

httplib::Client cli(host + ":" + std::to_string(port));
Expand Down Expand Up @@ -497,6 +517,8 @@ cpp::result<bool, std::string> ModelService::StopModel(

cpp::result<bool, std::string> ModelService::GetModelStatus(
const std::string& host, int port, const std::string& model_handle) {
namespace fs = std::filesystem;
namespace fmu = file_manager_utils;
cortex::db::Models modellist_handler;
config::YamlHandler yaml_handler;

Expand All @@ -506,7 +528,10 @@ cpp::result<bool, std::string> ModelService::GetModelStatus(
CTL_WRN("Error: " + model_entry.error());
return cpp::fail(model_entry.error());
}
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
yaml_handler.ModelConfigFromFile(
fmu::GetAbsolutePath(fmu::GetCortexDataPath(),
fs::path(model_entry.value().path_to_model_yaml))
.string());
auto mc = yaml_handler.GetModelConfig();

httplib::Client cli(host + ":" + std::to_string(port));
Expand Down
Loading