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
2 changes: 2 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ find_package(nlohmann_json CONFIG REQUIRED)
find_package(CLI11 CONFIG REQUIRED)
find_package(unofficial-minizip CONFIG REQUIRED)
find_package(LibArchive REQUIRED)
find_package(tabulate CONFIG REQUIRED)

# Build using CMAKE-JS
if(DEFINED CMAKE_JS_INC)
Expand Down Expand Up @@ -136,6 +137,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE jinja2cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE CLI11::CLI11)
target_link_libraries(${PROJECT_NAME} PRIVATE unofficial::minizip::minizip)
target_link_libraries(${PROJECT_NAME} PRIVATE LibArchive::LibArchive)
target_link_libraries(${PROJECT_NAME} PRIVATE tabulate::tabulate)

# Build using CMAKE-JS
if(DEFINED CMAKE_JS_INC)
Expand Down
81 changes: 81 additions & 0 deletions engine/commands/engine_list_cmd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// clang-format off
#include "utils/cortex_utils.h"
// clang-format on
#include "engine_list_cmd.h"
#include <filesystem>
#include <tabulate/table.hpp>
#include <utility>
#include "trantor/utils/Logger.h"

namespace commands {

bool EngineListCmd::Exec() {
tabulate::Table table;
table.add_row(
{"(Index)", "name", "description", "version", "product name", "status"});
table.format().font_color(tabulate::Color::green);
#ifdef _WIN32
if (std::filesystem::exists(std::filesystem::current_path().string() +
cortex_utils::kOnnxLibPath)) {
table.add_row({"1", "cortex.onnx",
"This extension enables chat completion API calls using the "
"Onnx engine",
"0.0.1", "Onnx Inference Engine", "ready"});
} else {
table.add_row({"1", "cortex.onnx",
"This extension enables chat completion API calls using the "
"Onnx engine",
"0.0.1", "Onnx Inference Engine", "not_initialized"});
}

#else
table.add_row(
{"1", "cortex.onnx",
"This extension enables chat completion API calls using the Onnx engine",
"0.0.1", "Onnx Inference Engine", "not_supported"});
#endif
// lllamacpp
if (std::filesystem::exists(std::filesystem::current_path().string() +
cortex_utils::kLlamaLibPath)) {
table.add_row({"2", "cortex.llamacpp",
"This extension enables chat completion API calls using the "
"LlamaCPP engine",
"0.0.1", "LlamaCPP Inference Engine", "ready"});
} else {
table.add_row({"2", "cortex.llamacpp",
"This extension enables chat completion API calls using the "
"LlamaCPP engine",
"0.0.1", "LlamaCPP Inference Engine", "not_initialized"});
}
// tensorrt llm
if (std::filesystem::exists(std::filesystem::current_path().string() +
cortex_utils::kTensorrtLlmPath)) {
table.add_row({"3", "cortex.tensorrt-llm",
"This extension enables chat completion API calls using the "
"TensorrtLLM engine",
"0.0.1", "TensorrtLLM Inference Engine", "ready"});
} else {
table.add_row({"3", "cortex.tensorrt-llm",
"This extension enables chat completion API calls using the "
"TensorrtLLM engine",
"0.0.1", "TensorrtLLM Inference Engine", "not_initialized"});
}
for (int i = 0; i < 6; 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 < 4; i++) {
table[i][0]
.format()
.font_color(tabulate::Color::white) // Set font color
.font_align(tabulate::FontAlign::center);
}

std::cout << table << std::endl;
return true;
}

}; // namespace commands
11 changes: 11 additions & 0 deletions engine/commands/engine_list_cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <string>

namespace commands {
class EngineListCmd {
public:
bool Exec() ;
};

} // namespace commands
37 changes: 32 additions & 5 deletions engine/commands/model_list_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,30 +1,57 @@
// clang-format off
#include "utils/cortex_utils.h"
// clang-format on
#include "model_list_cmd.h"
#include <filesystem>
#include <iostream>
#include <tabulate/table.hpp>
#include <vector>
#include "utils/cortex_utils.h"
#include "config/yaml_config.h"
#include "trantor/utils/Logger.h"
namespace commands {

void ModelListCmd::Exec() {
if (std::filesystem::exists(cortex_utils::models_folder) &&
std::filesystem::is_directory(cortex_utils::models_folder)) {
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)) {
if (entry.is_regular_file() && entry.path().extension() == ".yaml") {
try {
config::YamlHandler handler;
handler.ModelConfigFromFile(entry.path().string());
std::cout<<"Model ID: "<< entry.path().stem().string() <<", Engine: "<< handler.GetModelConfig().engine <<std::endl;

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) {
LOG_ERROR << "Error reading yaml file '" << entry.path().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;
}
}
}; // namespace commands
6 changes: 6 additions & 0 deletions engine/controllers/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "commands/chat_cmd.h"
#include "commands/cmd_info.h"
#include "commands/engine_init_cmd.h"
#include "commands/engine_list_cmd.h"
#include "commands/model_get_cmd.h"
#include "commands/model_list_cmd.h"
#include "commands/model_pull_cmd.h"
Expand Down Expand Up @@ -111,6 +112,11 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
auto engines_cmd = app_.add_subcommand("engines", "Get cortex engines");
auto list_engines_cmd =
engines_cmd->add_subcommand("list", "List all cortex engines");
list_engines_cmd->callback([]() {
commands::EngineListCmd command;
command.Exec();
});

auto get_engine_cmd = engines_cmd->add_subcommand("get", "Get an engine");

EngineInstall(engines_cmd, "cortex.llamacpp", version);
Expand Down
66 changes: 65 additions & 1 deletion engine/controllers/engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ void Engines::InitEngine(const HttpRequestPtr& req,
}}};

DownloadService().AddAsyncDownloadTask(
downloadTask, [](const std::string& absolute_path, bool unused) {
downloadTask,
[](const std::string& absolute_path, bool unused) {
// try to unzip the downloaded file
std::filesystem::path downloadedEnginePath{absolute_path};
LOG_INFO << "Downloaded engine path: "
Expand Down Expand Up @@ -108,4 +109,67 @@ void Engines::InitEngine(const HttpRequestPtr& req,
auto err = res.error();
LOG_ERROR << "HTTP error: " << httplib::to_string(err);
}
}

void Engines::ListEngine(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) const {
Json::Value ret;
ret["object"] = "list";
Json::Value data(Json::arrayValue);
Json::Value obj_onnx, obj_llamacpp, obj_tensorrt;
obj_onnx["name"] = "cortex.onnx";
obj_onnx["description"] =
"This extension enables chat completion API calls using the Onnx engine";
obj_onnx["version"] = "0.0.1";
obj_onnx["productName"] = "Onnx Inference Engine";

obj_llamacpp["name"] = "cortex.llamacpp";
obj_llamacpp["description"] =
"This extension enables chat completion API calls using the LlamaCPP "
"engine";
obj_llamacpp["version"] = "0.0.1";
obj_llamacpp["productName"] = "LlamaCPP Inference Engine";

obj_tensorrt["name"] = "cortex.tensorrt-llm";
obj_tensorrt["description"] =
"This extension enables chat completion API calls using the TensorrtLLM "
"engine";
obj_tensorrt["version"] = "0.0.1";
obj_tensorrt["productName"] = "TensorrtLLM Inference Engine";

#ifdef _WIN32
if (std::filesystem::exists(std::filesystem::current_path().string() +
cortex_utils::kOnnxLibPath)) {
obj_onnx["status"] = "ready";
} else {
obj_onnx["status"] = "not_initialized";
}
#else
obj_onnx["status"] = "not_supported";
#endif
// lllamacpp
if (std::filesystem::exists(std::filesystem::current_path().string() +
cortex_utils::kLlamaLibPath)) {

obj_llamacpp["status"] = "ready";
} else {
obj_llamacpp["status"] = "not_initialized";
}
// tensorrt llm
if (std::filesystem::exists(std::filesystem::current_path().string() +
cortex_utils::kTensorrtLlmPath)) {
obj_tensorrt["status"] = "ready";
} else {
obj_tensorrt["status"] = "not_initialized";
}

data.append(std::move(obj_onnx));
data.append(std::move(obj_llamacpp));
data.append(std::move(obj_tensorrt));
ret["data"] = data;
ret["result"] = "OK";
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
resp->setStatusCode(k200OK);
callback(resp);
}
3 changes: 3 additions & 0 deletions engine/controllers/engines.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ class Engines : public drogon::HttpController<Engines> {
public:
METHOD_LIST_BEGIN
METHOD_ADD(Engines::InitEngine, "/{1}/init", Post);
METHOD_ADD(Engines::ListEngine, "/list", Get);
METHOD_LIST_END

void InitEngine(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& engine) const;
void ListEngine(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) const;
};
3 changes: 2 additions & 1 deletion engine/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"minizip",
"nlohmann-json",
"yaml-cpp",
"libarchive"
"libarchive",
"tabulate"
]
}