Skip to content

Commit 1f80ff7

Browse files
committed
refactor: import cortex.cpp code
1 parent 9d2b02d commit 1f80ff7

21 files changed

Lines changed: 1653 additions & 13 deletions

engine/CMakeLists.txt

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,13 @@ else()
163163
endif()
164164

165165
aux_source_directory(controllers CTL_SRC)
166+
aux_source_directory(services SERVICES_SRC)
166167
aux_source_directory(common COMMON_SRC)
167168
aux_source_directory(models MODEL_SRC)
168169
aux_source_directory(cortex-common CORTEX_COMMON)
169-
# aux_source_directory(filters FILTER_SRC) aux_source_directory(plugins
170-
# PLUGIN_SRC)
171-
172-
# drogon_create_views(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/views
173-
# ${CMAKE_CURRENT_BINARY_DIR}) use the following line to create views with
174-
# namespaces. drogon_create_views(${PROJECT_NAME}
175-
# ${CMAKE_CURRENT_SOURCE_DIR}/views ${CMAKE_CURRENT_BINARY_DIR} TRUE)
170+
aux_source_directory(config CONFIG_SRC)
171+
aux_source_directory(commands COMMANDS_SRC)
176172

177173
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} )
178-
# ${CMAKE_CURRENT_SOURCE_DIR}/models)
179-
target_sources(${PROJECT_NAME} PRIVATE ${CTL_SRC} ${COMMON_SRC})
180-
# ${FILTER_SRC} ${PLUGIN_SRC} ${MODEL_SRC})
181-
# ##############################################################################
182-
# uncomment the following line for dynamically loading views set_property(TARGET
183-
# ${PROJECT_NAME} PROPERTY ENABLE_EXPORTS ON)
174+
175+
target_sources(${PROJECT_NAME} PRIVATE ${COMMANDS_SRC} ${CONFIG_SRC} ${CTL_SRC} ${COMMON_SRC} ${SERVICES_SRC})

engine/commands/model_pull_cmd.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "model_pull_cmd.h"
2+
#include <utility>
3+
#include "services/download_service.h"
4+
#include "trantor/utils/Logger.h"
5+
#include "utils/cortexso_parser.h"
6+
7+
namespace commands {
8+
ModelPullCmd::ModelPullCmd(std::string modelHandle)
9+
: modelHandle_(std::move(modelHandle)) {}
10+
11+
void ModelPullCmd::Exec() {
12+
auto downloadTask = cortexso_parser::getDownloadTask(modelHandle_);
13+
if (downloadTask.has_value()) {
14+
DownloadService downloadService;
15+
downloadService.AddDownloadTask(downloadTask.value());
16+
std::cout << "Download finished" << std::endl;
17+
} else {
18+
std::cout << "Model not found" << std::endl;
19+
}
20+
}
21+
22+
}; // namespace commands

engine/commands/model_pull_cmd.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace commands {
6+
7+
class ModelPullCmd {
8+
public:
9+
ModelPullCmd(std::string modelHandle);
10+
void Exec();
11+
12+
private:
13+
std::string modelHandle_;
14+
};
15+
} // namespace commands

engine/commands/start_model_cmd.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "start_model_cmd.h"
2+
#include "httplib.h"
3+
#include "nlohmann/json.hpp"
4+
#include "trantor/utils/Logger.h"
5+
6+
namespace commands {
7+
StartModelCmd::StartModelCmd(std::string host, int port,
8+
const config::ModelConfig& mc)
9+
: host_(std::move(host)), port_(port), mc_(mc) {}
10+
11+
void StartModelCmd::Exec() {
12+
httplib::Client cli(host_ + ":" + std::to_string(port_));
13+
nlohmann::json json_data;
14+
if (mc_.files.size() > 0) {
15+
// TODO(sang) support multiple files
16+
json_data["model_path"] = mc_.files[0];
17+
} else {
18+
LOG_WARN << "model_path is empty";
19+
return;
20+
}
21+
json_data["model"] = mc_.name;
22+
json_data["system_prompt"] = mc_.system_template;
23+
json_data["user_prompt"] = mc_.user_template;
24+
json_data["ai_prompt"] = mc_.ai_template;
25+
json_data["ctx_len"] = mc_.ctx_len;
26+
json_data["stop"] = mc_.stop;
27+
json_data["engine"] = mc_.engine;
28+
29+
auto data_str = json_data.dump();
30+
31+
auto res = cli.Post("/inferences/server/loadmodel", httplib::Headers(),
32+
data_str.data(), data_str.size(), "application/json");
33+
if (res) {
34+
if (res->status == httplib::StatusCode::OK_200) {
35+
LOG_INFO << res->body;
36+
}
37+
} else {
38+
auto err = res.error();
39+
LOG_WARN << "HTTP error: " << httplib::to_string(err);
40+
}
41+
}
42+
43+
}; // namespace commands

engine/commands/start_model_cmd.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include <string>
3+
#include <optional>
4+
#include "config/model_config.h"
5+
6+
namespace commands {
7+
8+
class StartModelCmd{
9+
public:
10+
StartModelCmd(std::string host, int port, const config::ModelConfig& mc);
11+
void Exec();
12+
13+
private:
14+
std::string host_;
15+
int port_;
16+
const config::ModelConfig& mc_;
17+
};
18+
} // namespace commands

engine/commands/stop_model_cmd.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "stop_model_cmd.h"
2+
#include "httplib.h"
3+
#include "nlohmann/json.hpp"
4+
#include "trantor/utils/Logger.h"
5+
6+
namespace commands {
7+
StopModelCmd::StopModelCmd(std::string host, int port,
8+
const config::ModelConfig& mc)
9+
: host_(std::move(host)), port_(port), mc_(mc) {}
10+
11+
void StopModelCmd::Exec() {
12+
httplib::Client cli(host_ + ":" + std::to_string(port_));
13+
nlohmann::json json_data;
14+
json_data["model"] = mc_.name;
15+
json_data["engine"] = mc_.engine;
16+
17+
auto data_str = json_data.dump();
18+
19+
auto res = cli.Post("/inferences/server/unloadmodel", httplib::Headers(),
20+
data_str.data(), data_str.size(), "application/json");
21+
if (res) {
22+
if (res->status == httplib::StatusCode::OK_200) {
23+
LOG_INFO << res->body;
24+
}
25+
} else {
26+
auto err = res.error();
27+
LOG_WARN << "HTTP error: " << httplib::to_string(err);
28+
}
29+
}
30+
31+
}; // namespace commands

engine/commands/stop_model_cmd.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include <string>
3+
#include <optional>
4+
#include "config/model_config.h"
5+
6+
namespace commands {
7+
8+
class StopModelCmd{
9+
public:
10+
StopModelCmd(std::string host, int port, const config::ModelConfig& mc);
11+
void Exec();
12+
13+
private:
14+
std::string host_;
15+
int port_;
16+
const config::ModelConfig& mc_;
17+
};
18+
} // namespace commands

engine/commands/stop_server_cmd.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "stop_server_cmd.h"
2+
#include "httplib.h"
3+
#include "trantor/utils/Logger.h"
4+
5+
namespace commands {
6+
StopServerCmd::StopServerCmd(std::string host, int port)
7+
: host_(std::move(host)), port_(port) {}
8+
9+
void StopServerCmd::Exec() {
10+
httplib::Client cli(host_ + ":" + std::to_string(port_));
11+
auto res = cli.Delete("/processManager/destroy");
12+
if (res) {
13+
LOG_INFO << res->body;
14+
} else {
15+
auto err = res.error();
16+
LOG_WARN << "HTTP error: " << httplib::to_string(err);
17+
}
18+
}
19+
20+
}; // namespace commands

engine/commands/stop_server_cmd.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
#include <string>
3+
4+
namespace commands {
5+
6+
class StopServerCmd{
7+
public:
8+
StopServerCmd(std::string host, int port);
9+
void Exec();
10+
11+
private:
12+
std::string host_;
13+
int port_;
14+
};
15+
} // namespace commands

0 commit comments

Comments
 (0)