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 2 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 @@ -78,6 +78,7 @@ find_package(LibArchive REQUIRED)
find_package(tabulate CONFIG REQUIRED)
find_package(CURL REQUIRED)
find_package(SQLiteCpp REQUIRED)
find_package(eventpp CONFIG REQUIRED)

add_executable(${TARGET_NAME} main.cc
${CMAKE_CURRENT_SOURCE_DIR}/utils/cpuid/cpu_info.cc
Expand All @@ -94,6 +95,7 @@ target_link_libraries(${TARGET_NAME} PRIVATE CURL::libcurl)
target_link_libraries(${TARGET_NAME} PRIVATE JsonCpp::JsonCpp Drogon::Drogon OpenSSL::SSL OpenSSL::Crypto yaml-cpp::yaml-cpp
${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(${TARGET_NAME} PRIVATE SQLiteCpp)
target_link_libraries(${TARGET_NAME} PRIVATE eventpp::eventpp)

# ##############################################################################

Expand Down
11 changes: 5 additions & 6 deletions engine/commands/chat_completion_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#include "chat_completion_cmd.h"
#include "httplib.h"

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

namespace commands {
Expand Down Expand Up @@ -79,7 +77,8 @@ void ChatCompletionCmd::Exec(const std::string& host, int port,
// Only check if llamacpp engine
if ((mc.engine.find(kLlamaEngine) != std::string::npos ||
mc.engine.find(kLlamaRepo) != std::string::npos) &&
!commands::ModelStatusCmd().IsLoaded(host, port, model_handle)) {
!commands::ModelStatusCmd(model_service_)
.IsLoaded(host, port, model_handle)) {
CLI_LOG("Model is not loaded yet!");
return;
}
Expand Down Expand Up @@ -155,4 +154,4 @@ void ChatCompletionCmd::Exec(const std::string& host, int port,
}
}

}; // namespace commands
}; // namespace commands
9 changes: 7 additions & 2 deletions engine/commands/chat_completion_cmd.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
#pragma once

#include <string>
#include <vector>
#include "config/model_config.h"
#include "nlohmann/json.hpp"
#include "services/model_service.h"

namespace commands {
class ChatCompletionCmd {
public:
explicit ChatCompletionCmd(const ModelService& model_service)
: model_service_{model_service} {};

void Exec(const std::string& host, int port, const std::string& model_handle,
std::string msg);
void Exec(const std::string& host, int port, const std::string& model_handle,
const config::ModelConfig& mc, std::string msg);

private:
std::vector<Json::Value> histories_;
ModelService model_service_;
};
} // namespace commands
} // namespace commands
12 changes: 7 additions & 5 deletions engine/commands/cortex_upd_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ bool CortexUpdCmd::HandleGithubRelease(const nlohmann::json& assets,
.localPath = local_path,
}}}};

DownloadService().AddDownloadTask(
auto result = DownloadService().AddDownloadTask(
download_task, [](const DownloadTask& finishedTask) {
// try to unzip the downloaded file
CTL_INF("Downloaded engine path: "
Expand All @@ -410,6 +410,9 @@ bool CortexUpdCmd::HandleGithubRelease(const nlohmann::json& assets,

CTL_INF("Finished!");
});
if (result.has_error()) {
CTL_ERR("Failed to download: " << result.error());
}
break;
}
}
Expand Down Expand Up @@ -457,7 +460,7 @@ bool CortexUpdCmd::GetNightly(const std::string& v) {
.localPath = localPath,
}}};

auto res = DownloadService().AddDownloadTask(
auto result = DownloadService().AddDownloadTask(
download_task, [](const DownloadTask& finishedTask) {
// try to unzip the downloaded file
CTL_INF("Downloaded engine path: "
Expand All @@ -471,9 +474,8 @@ bool CortexUpdCmd::GetNightly(const std::string& v) {

CTL_INF("Finished!");
});

if (res.has_error()) {
CLI_LOG("Download failed!");
if (result.has_error()) {
CTL_ERR("Failed to download: " << result.error());
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion engine/commands/engine_get_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace commands {
class EngineGetCmd {
public:
explicit EngineGetCmd() : engine_service_{EngineService()} {};
explicit EngineGetCmd()
: engine_service_{EngineService(std::make_shared<DownloadService>())} {};

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

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

class EngineInstallCmd {
public:
explicit EngineInstallCmd() : engine_service_{EngineService()} {};
explicit EngineInstallCmd()
: engine_service_{EngineService(std::make_shared<DownloadService>())} {};

void Exec(const std::string& engine, const std::string& version = "latest",
const std::string& src = "");
Expand Down
4 changes: 1 addition & 3 deletions engine/commands/engine_list_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include "engine_list_cmd.h"
#include <tabulate/table.hpp>
#include "services/engine_service.h"

namespace commands {

bool EngineListCmd::Exec() {
auto engine_service = EngineService();
auto status_list = engine_service.GetEngineInfoList();
auto status_list = engine_service_.GetEngineInfoList();

tabulate::Table table;
table.add_row(
Expand Down
8 changes: 8 additions & 0 deletions engine/commands/engine_list_cmd.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#pragma once

#include "services/engine_service.h"

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

bool Exec();

private:
EngineService engine_service_;
};

} // namespace commands
4 changes: 3 additions & 1 deletion engine/commands/engine_uninstall_cmd.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

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

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

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

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

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

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

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

class ModelPullCmd {
public:
explicit ModelPullCmd() : model_service_{ModelService()} {};
explicit ModelPullCmd()
: model_service_{ModelService(std::make_shared<DownloadService>())} {};
void Exec(const std::string& input);

private:
Expand Down
13 changes: 2 additions & 11 deletions engine/commands/model_start_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
#include "model_start_cmd.h"
#include "cortex_upd_cmd.h"
#include "database/models.h"
#include "httplib.h"
#include "model_status_cmd.h"
#include "nlohmann/json.hpp"
#include "server_start_cmd.h"
#include "services/model_service.h"
#include "trantor/utils/Logger.h"
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"

namespace commands {
bool ModelStartCmd::Exec(const std::string& host, int port,
const std::string& model_handle) {
ModelService ms;
auto res = ms.StartModel(host, port, model_handle);
auto res = model_service_.StartModel(host, port, model_handle);

if (res.has_error()) {
CLI_LOG("Error: " + res.error());
return false;
}

CLI_LOG("Model loaded!");
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions engine/commands/model_start_cmd.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#pragma once
#include <string>
#include "services/model_service.h"

namespace commands {

class ModelStartCmd {
public:
explicit ModelStartCmd(const ModelService& model_service)
: model_service_{model_service} {};

bool Exec(const std::string& host, int port, const std::string& model_handle);

private:
ModelService model_service_;
};
} // namespace commands
12 changes: 3 additions & 9 deletions engine/commands/model_status_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
#include "model_status_cmd.h"
#include "config/yaml_config.h"
#include "database/models.h"
#include "httplib.h"
#include "nlohmann/json.hpp"
#include "utils/logging_utils.h"
#include "services/model_service.h"

namespace commands {
bool ModelStatusCmd::IsLoaded(const std::string& host, int port,
const std::string& model_handle) {
ModelService ms;
auto res = ms.GetModelStatus(host, port, model_handle);
auto res = model_service_.GetModelStatus(host, port, model_handle);

if (res.has_error()) {
// CLI_LOG("Error: " + res.error());
CTL_ERR("Error: " + res.error());
return false;
}
return true;
}
} // namespace commands
} // namespace commands
9 changes: 8 additions & 1 deletion engine/commands/model_status_cmd.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#pragma once
#include <string>
#include "services/model_service.h"

namespace commands {

class ModelStatusCmd {
public:
explicit ModelStatusCmd(const ModelService& model_service)
: model_service_{model_service} {};

bool IsLoaded(const std::string& host, int port,
const std::string& model_handle);

private:
ModelService model_service_;
};
} // namespace commands
} // namespace commands
9 changes: 1 addition & 8 deletions engine/commands/model_stop_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
#include "model_stop_cmd.h"
#include "config/yaml_config.h"
#include "database/models.h"
#include "httplib.h"
#include "nlohmann/json.hpp"
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"
#include "services/model_service.h"

namespace commands {

void ModelStopCmd::Exec(const std::string& host, int port,
const std::string& model_handle) {
ModelService ms;
auto res = ms.StopModel(host, port, model_handle);
auto res = model_service_.StopModel(host, port, model_handle);

if (res.has_error()) {
CLI_LOG("Error: " + res.error());
Expand Down
8 changes: 7 additions & 1 deletion engine/commands/model_stop_cmd.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#pragma once

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

namespace commands {

class ModelStopCmd {
public:
explicit ModelStopCmd(const ModelService& model_service)
: model_service_{model_service} {};

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

private:
ModelService model_service_;
};
} // namespace commands
15 changes: 11 additions & 4 deletions engine/commands/run_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "config/yaml_config.h"
#include "cortex_upd_cmd.h"
#include "database/models.h"
#include "model_start_cmd.h"
#include "model_status_cmd.h"
#include "server_start_cmd.h"
#include "utils/cli_selection_utils.h"
Expand Down Expand Up @@ -100,16 +99,24 @@ void RunCmd::Exec(bool chat_flag) {
{
if ((mc.engine.find(kLlamaRepo) == std::string::npos &&
mc.engine.find(kLlamaEngine) == std::string::npos) ||
!commands::ModelStatusCmd().IsLoaded(host_, port_, *model_id)) {
if (!ModelStartCmd().Exec(host_, port_, *model_id)) {
!commands::ModelStatusCmd(model_service_)
.IsLoaded(host_, port_, *model_id)) {

auto result = model_service_.StartModel(host_, port_, *model_id);
if (result.has_error()) {
CLI_LOG("Error: " + result.error());
return;
}
if (!result.value()) {
CLI_LOG("Error: Failed to start model");
return;
}
}
}

// Chat
if (chat_flag) {
ChatCompletionCmd().Exec(host_, port_, *model_id, mc, "");
ChatCompletionCmd(model_service_).Exec(host_, port_, *model_id, mc, "");
} else {
CLI_LOG(*model_id << " model started successfully. Use `"
<< commands::GetCortexBinary() << " chat " << *model_id
Expand Down
3 changes: 2 additions & 1 deletion engine/commands/run_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class RunCmd {
: host_{std::move(host)},
port_{port},
model_handle_{std::move(model_handle)},
model_service_{ModelService()} {};
engine_service_{EngineService(std::make_shared<DownloadService>())},
model_service_{ModelService(std::make_shared<DownloadService>())} {};

void Exec(bool chat_flag);

Expand Down
Loading
Loading