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
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
11 changes: 6 additions & 5 deletions engine/commands/model_get_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "config/yaml_config.h"
#include "trantor/utils/Logger.h"
#include "utils/cortex_utils.h"
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"

namespace commands {
Expand All @@ -14,15 +15,15 @@ ModelGetCmd::ModelGetCmd(std::string model_handle)
: model_handle_(std::move(model_handle)) {}

void ModelGetCmd::Exec() {
if (std::filesystem::exists(cortex_utils::models_folder) &&
std::filesystem::is_directory(cortex_utils::models_folder)) {
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(cortex_utils::models_folder)) {
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {

if (entry.is_regular_file() && entry.path().stem() == model_file &&
entry.path().extension() == ".yaml") {
Expand Down Expand Up @@ -137,7 +138,7 @@ void ModelGetCmd::Exec() {
break;
} catch (const std::exception& e) {
CTL_ERR("Error reading yaml file '" << entry.path().string()
<< "': " << e.what());
<< "': " << e.what());
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions engine/commands/model_list_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
#include <vector>
#include "config/yaml_config.h"
#include "trantor/utils/Logger.h"
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"

namespace commands {

void ModelListCmd::Exec() {
if (std::filesystem::exists(cortex_utils::models_folder) &&
std::filesystem::is_directory(cortex_utils::models_folder)) {
auto models_path = file_manager_utils::GetModelsContainerPath();
if (std::filesystem::exists(models_path) &&
std::filesystem::is_directory(models_path)) {
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(cortex_utils::models_folder)) {
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {
if (entry.is_regular_file() && entry.path().extension() == ".yaml") {
try {
count += 1;
Expand All @@ -32,7 +34,7 @@ void ModelListCmd::Exec() {
model_config.engine, model_config.version});
} catch (const std::exception& e) {
CTL_ERR("Error reading yaml file '" << entry.path().string()
<< "': " << e.what());
<< "': " << e.what());
}
}
}
Expand Down
24 changes: 11 additions & 13 deletions engine/commands/run_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "model_start_cmd.h"
#include "trantor/utils/Logger.h"
#include "utils/cortex_utils.h"
#include "utils/file_manager_utils.h"

namespace commands {

Expand Down Expand Up @@ -42,8 +43,9 @@ void RunCmd::Exec() {

// Start model
config::YamlHandler yaml_handler;
yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() + "/models/" +
model_file + ".yaml");
yaml_handler.ModelConfigFromFile(
file_manager_utils::GetModelsContainerPath().string() + "/" + model_file +
".yaml");
{
ModelStartCmd msc(host_, port_, yaml_handler.GetModelConfig());
if (!msc.Exec()) {
Expand All @@ -59,14 +61,11 @@ void RunCmd::Exec() {
}

bool RunCmd::IsModelExisted(const std::string& model_id) {
if (std::filesystem::exists(cortex_utils::GetCurrentPath() + "/" +
cortex_utils::models_folder) &&
std::filesystem::is_directory(cortex_utils::GetCurrentPath() + "/" +
cortex_utils::models_folder)) {
auto models_path = file_manager_utils::GetModelsContainerPath();
if (std::filesystem::exists(models_path) &&
std::filesystem::is_directory(models_path)) {
// Iterate through directory
for (const auto& entry : std::filesystem::directory_iterator(
cortex_utils::GetCurrentPath() + "/" +
cortex_utils::models_folder)) {
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {
if (entry.is_regular_file() && entry.path().extension() == ".yaml") {
try {
config::YamlHandler handler;
Expand All @@ -85,10 +84,9 @@ bool RunCmd::IsModelExisted(const std::string& model_id) {
}

bool RunCmd::IsEngineExisted(const std::string& e) {
if (std::filesystem::exists(cortex_utils::GetCurrentPath() + "/" +
"engines") &&
std::filesystem::exists(cortex_utils::GetCurrentPath() + "/" +
"engines/" + e)) {
auto engines_path = file_manager_utils::GetEnginesContainerPath();
if (std::filesystem::exists(engines_path) &&
std::filesystem::exists(engines_path.string() + "/" + e)) {
return true;
}
return false;
Expand Down
16 changes: 10 additions & 6 deletions engine/controllers/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "httplib.h"
#include "services/engine_service.h"
#include "utils/cortex_utils.h"
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"

CommandLineParser::CommandLineParser()
Expand All @@ -38,8 +39,9 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
std::string model_file =
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
config::YamlHandler yaml_handler;
yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() +
"/models/" + model_file + ".yaml");
yaml_handler.ModelConfigFromFile(
file_manager_utils::GetModelsContainerPath().string() + "/" +
model_file + ".yaml");
commands::ModelStartCmd msc("127.0.0.1", 3928,
yaml_handler.GetModelConfig());
msc.Exec();
Expand All @@ -53,8 +55,9 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
std::string model_file =
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
config::YamlHandler yaml_handler;
yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() +
"/models/" + model_file + ".yaml");
yaml_handler.ModelConfigFromFile(
file_manager_utils::GetModelsContainerPath().string() + "/" +
model_file + ".yaml");
commands::ModelStopCmd smc("127.0.0.1", 3928,
yaml_handler.GetModelConfig());
smc.Exec();
Expand Down Expand Up @@ -107,8 +110,9 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
std::string model_file =
ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch;
config::YamlHandler yaml_handler;
yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() +
"/models/" + model_file + ".yaml");
yaml_handler.ModelConfigFromFile(
file_manager_utils::GetModelsContainerPath().string() + "/" +
model_file + ".yaml");
commands::ChatCmd cc("127.0.0.1", 3928, yaml_handler.GetModelConfig());
cc.Exec(msg);
});
Expand Down
9 changes: 5 additions & 4 deletions engine/controllers/models.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "config/yaml_config.h"
#include "trantor/utils/Logger.h"
#include "utils/cortex_utils.h"
#include "utils/file_manager_utils.h"
#include "utils/model_callback_utils.h"

void Models::PullModel(
Expand Down Expand Up @@ -49,11 +50,11 @@ void Models::ListModel(
Json::Value ret;
ret["object"] = "list";
Json::Value data(Json::arrayValue);
if (std::filesystem::exists(cortex_utils::models_folder) &&
std::filesystem::is_directory(cortex_utils::models_folder)) {
auto models_path = file_manager_utils::GetModelsContainerPath();
if (std::filesystem::exists(models_path) &&
std::filesystem::is_directory(models_path)) {
// Iterate through directory
for (const auto& entry :
std::filesystem::directory_iterator(cortex_utils::models_folder)) {
for (const auto& entry : std::filesystem::directory_iterator(models_path)) {
if (entry.is_regular_file() && entry.path().extension() == ".yaml") {
try {
config::YamlHandler handler;
Expand Down
4 changes: 3 additions & 1 deletion engine/controllers/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "utils/cortex_utils.h"
#include "utils/cpuid/cpu_info.h"
#include "utils/logging_utils.h"
#include "utils/file_manager_utils.h"

using namespace inferences;
using json = nlohmann::json;
Expand Down Expand Up @@ -290,8 +291,9 @@ void server::LoadModel(const HttpRequestPtr& req,

std::string abs_path =
(getenv("ENGINE_PATH") ? getenv("ENGINE_PATH")
: cortex_utils::GetCurrentPath()) +
: file_manager_utils::GetCortexDataPath().string()) +
get_engine_path(engine_type);
std::cout << abs_path << std::endl;
engines_[engine_type].dl =
std::make_unique<cortex_cpp::dylib>(abs_path, "engine");

Expand Down
5 changes: 2 additions & 3 deletions engine/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "controllers/command_line_parser.h"
#include "cortex-common/cortexpythoni.h"
#include "utils/archive_utils.h"
#include "utils/config_yaml_utils.h"
#include "utils/cortex_utils.h"
#include "utils/dylib.h"
#include "utils/file_manager_utils.h"
Expand All @@ -26,7 +25,7 @@
#endif

void RunServer() {
auto config = config_yaml_utils::GetCortexConfig();
auto config = file_manager_utils::GetCortexConfig();
LOG_INFO << "Host: " << config.host << " Port: " << config.port << "\n";

// Create logs/ folder and setup log to file
Expand Down Expand Up @@ -127,7 +126,7 @@ void ForkProcess() {
}

int main(int argc, char* argv[]) {
{ config_yaml_utils::CreateConfigFileIfNotExist(); }
{ file_manager_utils::CreateConfigFileIfNotExist(); }

// Check if this process is for python execution
if (argc > 1) {
Expand Down
26 changes: 2 additions & 24 deletions engine/utils/config_yaml_utils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"
#include "yaml-cpp/yaml.h"

Expand Down Expand Up @@ -39,24 +39,6 @@ inline void DumpYamlConfig(const CortexConfig& config,
}
}

inline void CreateConfigFileIfNotExist() {
auto config_path = file_manager_utils::GetConfigurationPath();
if (std::filesystem::exists(config_path)) {
// already exists
return;
}
CLI_LOG("Config file not found. Creating one at " + config_path.string());
auto defaultDataFolderPath =
file_manager_utils::GetHomeDirectoryPath() / kCortexFolderName;
auto config = CortexConfig{
.dataFolderPath = defaultDataFolderPath.string(),
.host = kDefaultHost,
.port = kDefaultPort,
};
std::cout << "config: " << config.dataFolderPath << "\n";
DumpYamlConfig(config, config_path.string());
}

inline CortexConfig FromYaml(const std::string& path,
const std::string& variant) {
std::filesystem::path config_file_path{path};
Expand All @@ -78,9 +60,5 @@ inline CortexConfig FromYaml(const std::string& path,
}
}

inline CortexConfig GetCortexConfig() {
auto config_path = file_manager_utils::GetConfigurationPath();
std::string variant = ""; // TODO: empty for now
return FromYaml(config_path.string(), variant);
}

} // namespace config_yaml_utils
52 changes: 42 additions & 10 deletions engine/utils/file_manager_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string_view>
#include "logging_utils.h"
#include "services/download_service.h"
#include "utils/config_yaml_utils.h"

#if defined(__APPLE__) && defined(__MACH__)
#include <mach-o/dyld.h>
Expand Down Expand Up @@ -84,23 +85,54 @@ inline std::filesystem::path GetConfigurationPath() {
return configuration_path;
}

inline std::filesystem::path GetCortexPath() {
inline void CreateConfigFileIfNotExist() {
auto config_path = file_manager_utils::GetConfigurationPath();
if (std::filesystem::exists(config_path)) {
// already exists
return;
}
CLI_LOG("Config file not found. Creating one at " + config_path.string());
auto defaultDataFolderPath =
file_manager_utils::GetHomeDirectoryPath() / config_yaml_utils::kCortexFolderName;
auto config = config_yaml_utils::CortexConfig{
.dataFolderPath = defaultDataFolderPath.string(),
.host = config_yaml_utils::kDefaultHost,
.port = config_yaml_utils::kDefaultPort,
};
std::cout << "config: " << config.dataFolderPath << "\n";
DumpYamlConfig(config, config_path.string());
}

inline config_yaml_utils::CortexConfig GetCortexConfig() {
auto config_path = GetConfigurationPath();
std::string variant = ""; // TODO: empty for now
return config_yaml_utils::FromYaml(config_path.string(), variant);
}

inline std::filesystem::path GetCortexDataPath() {
// TODO: We will need to support user to move the data folder to other place.
// TODO: get the variant of cortex. As discussed, we will have: prod, beta, nightly
// currently we will store cortex data at ~/.cortex
// currently we will store cortex data at ~/cortexcpp
auto config = GetCortexConfig();
std::filesystem::path data_folder_path;
if (!config.dataFolderPath.empty()) {
data_folder_path =
std::filesystem::path(config.dataFolderPath);
} else {
auto home_path = GetHomeDirectoryPath();
data_folder_path = home_path / config_yaml_utils::kCortexFolderName;
}

auto home_path = GetHomeDirectoryPath();
auto cortex_path = home_path / ".cortex";
if (!std::filesystem::exists(cortex_path)) {
if (!std::filesystem::exists(data_folder_path)) {
CTL_INF("Cortex home folder not found. Create one: " +
cortex_path.string());
std::filesystem::create_directory(cortex_path);
data_folder_path.string());
std::filesystem::create_directory(data_folder_path);
}
return cortex_path;
return data_folder_path;
}

inline std::filesystem::path GetModelsContainerPath() {
auto cortex_path = GetCortexPath();
auto cortex_path = GetCortexDataPath();
auto models_container_path = cortex_path / "models";

if (!std::filesystem::exists(models_container_path)) {
Expand All @@ -113,7 +145,7 @@ inline std::filesystem::path GetModelsContainerPath() {
}

inline std::filesystem::path GetEnginesContainerPath() {
auto cortex_path = GetCortexPath();
auto cortex_path = GetCortexDataPath();
auto engines_container_path = cortex_path / "engines";

if (!std::filesystem::exists(engines_container_path)) {
Expand Down