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: Cortex CLI should be a layer on top of the API Server #1489

Merged
merged 19 commits into from
Oct 16, 2024
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
51 changes: 40 additions & 11 deletions engine/commands/engine_get_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
#include "engine_get_cmd.h"
#include <iostream>
#include <tabulate/table.hpp>
#include "services/engine_service.h"

#include "httplib.h"
#include "json/json.h"
#include "server_start_cmd.h"
#include "utils/logging_utils.h"

// clang-format off
#include <tabulate/table.hpp>
// clang-format on

namespace commands {

void EngineGetCmd::Exec(const std::string& engine_name) const {
auto engine = engine_service_.GetEngineInfo(engine_name);
if (engine.has_error()) {
CLI_LOG(engine.error());
return;
void EngineGetCmd::Exec(const std::string& host, int port,
const std::string& engine_name) const {
// Start server if server is not started yet
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return;
}
}

auto version = engine->version.value_or("");
auto variant = engine->variant.value_or("");
tabulate::Table table;
table.add_row({"Name", "Supported Formats", "Version", "Variant", "Status"});
table.add_row(
{engine->product_name, engine->format, version, variant, engine->status});
httplib::Client cli(host + ":" + std::to_string(port));
auto res = cli.Get("/v1/engines/" + engine_name);
if (res) {
if (res->status == httplib::StatusCode::OK_200) {
// CLI_LOG(res->body);
Json::Value v;
Json::Reader reader;
reader.parse(res->body, v);

table.add_row({v["name"].asString(), v["format"].asString(),
v["version"].asString(), v["variant"].asString(),
v["status"].asString()});

} else {
CTL_ERR("Failed to get engine list with status code: " << res->status);
return;
}
} else {
auto err = res.error();
CTL_ERR("HTTP error: " << httplib::to_string(err));
return;
}

std::cout << table << std::endl;
}
}; // namespace commands
11 changes: 3 additions & 8 deletions engine/commands/engine_get_cmd.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#pragma once

#include "services/engine_service.h"
#include <string>

namespace commands {
class EngineGetCmd {
public:
explicit EngineGetCmd(std::shared_ptr<DownloadService> download_service)
: engine_service_{EngineService(download_service)} {};

void Exec(const std::string& engineName) const;
void Exec(const std::string& host, int port,
const std::string& engineName) const;

private:
EngineService engine_service_;
};
} // namespace commands
50 changes: 41 additions & 9 deletions engine/commands/engine_list_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
#include "engine_list_cmd.h"
#include "httplib.h"
#include "json/json.h"
#include "server_start_cmd.h"
#include "utils/logging_utils.h"
// clang-format off
#include <tabulate/table.hpp>
// clang-format on

namespace commands {

bool EngineListCmd::Exec() {
auto status_list = engine_service_.GetEngineInfoList();
bool EngineListCmd::Exec(const std::string& host, int port) {
// Start server if server is not started yet
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return false;
}
}

tabulate::Table table;
table.add_row(
{"#", "Name", "Supported Formats", "Version", "Variant", "Status"});
for (int i = 0; i < status_list.size(); i++) {
auto engine_status = status_list[i];
std::string index = std::to_string(i + 1);
auto variant = engine_status.variant.value_or("");
auto version = engine_status.version.value_or("");
table.add_row({index, engine_status.product_name, engine_status.format,
version, variant, engine_status.status});

httplib::Client cli(host + ":" + std::to_string(port));
auto res = cli.Get("/v1/engines");
if (res) {
if (res->status == httplib::StatusCode::OK_200) {
int count = 0;
// CLI_LOG(res->body);
Json::Value body;
Json::Reader reader;
reader.parse(res->body, body);
if (!body["data"].isNull()) {
for (auto const& v : body["data"]) {
count += 1;
table.add_row({std::to_string(count), v["name"].asString(),
v["format"].asString(), v["version"].asString(),
v["variant"].asString(), v["status"].asString()});
}
}
} else {
CTL_ERR("Failed to get engine list with status code: " << res->status);
return false;
}
} else {
auto err = res.error();
CTL_ERR("HTTP error: " << httplib::to_string(err));
return false;
}

std::cout << table << std::endl;
Expand Down
10 changes: 2 additions & 8 deletions engine/commands/engine_list_cmd.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#pragma once

#include "services/engine_service.h"
#include <string>

namespace commands {
class EngineListCmd {
public:
explicit EngineListCmd(std::shared_ptr<DownloadService> download_service)
: engine_service_{EngineService(download_service)} {};

bool Exec();

private:
EngineService engine_service_;
bool Exec(const std::string& host, int port);
};

} // namespace commands
29 changes: 23 additions & 6 deletions engine/commands/engine_uninstall_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
#include "engine_uninstall_cmd.h"
#include "services/engine_service.h"
#include "httplib.h"
#include "server_start_cmd.h"
#include "utils/logging_utils.h"

namespace commands {

void EngineUninstallCmd::Exec(const std::string& engine) {
auto result = engine_service_.UninstallEngine(engine);
void EngineUninstallCmd::Exec(const std::string& host, int port,
const std::string& engine) {
// Start server if server is not started yet
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return;
}
}

if (result.has_error()) {
CLI_LOG(result.error());
// Call API to delete engine
httplib::Client cli(host + ":" + std::to_string(port));
auto res = cli.Delete("/v1/engines/" + engine);
if (res) {
if (res->status == httplib::StatusCode::OK_200) {
CLI_LOG("Engine " + engine + " uninstalled successfully");
} else {
CTL_ERR("Engine failed to uninstall with status code: " << res->status);
}
} else {
CLI_LOG("Engine " + engine + " uninstalled successfully!");
auto err = res.error();
CTL_ERR("HTTP error: " << httplib::to_string(err));
}
}
}; // namespace commands
10 changes: 1 addition & 9 deletions engine/commands/engine_uninstall_cmd.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
#pragma once

#include <memory>
#include <string>
#include "services/engine_service.h"

namespace commands {
class EngineUninstallCmd {
public:
explicit EngineUninstallCmd(std::shared_ptr<DownloadService> download_service)
: engine_service_{EngineService(download_service)} {};

void Exec(const std::string& engine);

private:
EngineService engine_service_;
void Exec(const std::string& host, int port, const std::string& engine);
};
} // namespace commands
47 changes: 30 additions & 17 deletions engine/commands/model_alias_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
#include "model_alias_cmd.h"
#include "database/models.h"
#include "httplib.h"
#include "server_start_cmd.h"
#include "json/json.h"

namespace commands {

void ModelAliasCmd::Exec(const std::string& model_handle,
void ModelAliasCmd::Exec(const std::string& host, int port,
const std::string& model_handle,
const std::string& model_alias) {
cortex::db::Models modellist_handler;
try {
auto result = modellist_handler.UpdateModelAlias(model_handle, model_alias);
if (result.has_error()) {
CLI_LOG(result.error());
} else {
if (result.value()) {
CLI_LOG("Successfully set model alias '" + model_alias +
"' for modeID '" + model_handle + "'.");
} else {
CLI_LOG("Unable to set model alias for modelID '" + model_handle +
"': model alias '" + model_alias + "' is not unique!");
}
// Start server if server is not started yet
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return;
}
}

} catch (const std::exception& e) {
CLI_LOG("Error when setting model alias ('" + model_alias +
"') for modelID '" + model_handle + "':" + e.what());
// Call API to delete model
httplib::Client cli(host + ":" + std::to_string(port));
Json::Value json_data;
json_data["model"] = model_handle;
json_data["modelAlias"] = model_alias;
auto data_str = json_data.toStyledString();
auto res = cli.Post("/v1/models/alias", httplib::Headers(), data_str.data(),
data_str.size(), "application/json");
if (res) {
if (res->status == httplib::StatusCode::OK_200) {
CLI_LOG("Successfully set model alias '" + model_alias +
"' for modeID '" + model_handle + "'.");
} else {
CTL_ERR("Model failed to set alias with status code: " << res->status);
}
} else {
auto err = res.error();
CTL_ERR("HTTP error: " << httplib::to_string(err));
}
}

Expand Down
3 changes: 2 additions & 1 deletion engine/commands/model_alias_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace commands {

class ModelAliasCmd {
public:
void Exec(const std::string& model_handle, const std::string& model_alias);
void Exec(const std::string& host, int port, const std::string& model_handle,
const std::string& model_alias);
};
} // namespace commands
30 changes: 25 additions & 5 deletions engine/commands/model_del_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
#include "model_del_cmd.h"
#include "httplib.h"
#include "server_start_cmd.h"
#include "utils/logging_utils.h"

namespace commands {
void ModelDelCmd::Exec(const std::string& model_handle) {
auto result = model_service_.DeleteModel(model_handle);
if (result.has_error()) {
CLI_LOG(result.error());

void ModelDelCmd::Exec(const std::string& host, int port,
const std::string& model_handle) {
// Start server if server is not started yet
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return;
}
}

// Call API to delete model
httplib::Client cli(host + ":" + std::to_string(port));
auto res = cli.Delete("/v1/models/" + model_handle);
if (res) {
if (res->status == httplib::StatusCode::OK_200) {
CLI_LOG("Model " + model_handle + " deleted successfully");
} else {
CTL_ERR("Model failed to delete with status code: " << res->status);
}
} else {
CLI_LOG("Model " + model_handle + " deleted successfully");
auto err = res.error();
CTL_ERR("HTTP error: " << httplib::to_string(err));
}
}
} // namespace commands
9 changes: 1 addition & 8 deletions engine/commands/model_del_cmd.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
#pragma once

#include <string>
#include "services/model_service.h"

namespace commands {

class ModelDelCmd {
public:
explicit ModelDelCmd(std::shared_ptr<DownloadService> download_service)
: model_service_{ModelService(download_service)} {};

void Exec(const std::string& model_handle);

private:
ModelService model_service_;
void Exec(const std::string& host, int port, const std::string& model_handle);
};
} // namespace commands
Loading
Loading