Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b545de

Browse files
chore: database service (menloresearch#1834)
* chore: remove services namespace * chore: db: engines to service * chore: db: file to service * chore: db: hardware to service * chore: db: models to service --------- Co-authored-by: vansangpfiev <[email protected]>
1 parent d9bdb81 commit 3b545de

31 files changed

+406
-221
lines changed

engine/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ target_link_libraries(${TARGET_NAME} PRIVATE JsonCpp::JsonCpp Drogon::Drogon Ope
157157
target_link_libraries(${TARGET_NAME} PRIVATE SQLiteCpp)
158158
target_link_libraries(${TARGET_NAME} PRIVATE eventpp::eventpp)
159159
target_link_libraries(${TARGET_NAME} PRIVATE lfreist-hwinfo::hwinfo)
160-
160+
161161
# ##############################################################################
162162

163163
if(CMAKE_CXX_STANDARD LESS 17)

engine/cli/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ add_executable(${TARGET_NAME} main.cc
8383
${CMAKE_CURRENT_SOURCE_DIR}/../services/model_service.cc
8484
${CMAKE_CURRENT_SOURCE_DIR}/../services/inference_service.cc
8585
${CMAKE_CURRENT_SOURCE_DIR}/../services/hardware_service.cc
86+
${CMAKE_CURRENT_SOURCE_DIR}/../services/database_service.cc
8687
${CMAKE_CURRENT_SOURCE_DIR}/../extensions/remote-engine/remote_engine.cc
8788
${CMAKE_CURRENT_SOURCE_DIR}/../extensions/remote-engine/template_renderer.cc
8889
${CMAKE_CURRENT_SOURCE_DIR}/utils/easywsclient.cc

engine/cli/command_line_parser.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ CommandLineParser::CommandLineParser()
4949
: app_("\nCortex.cpp CLI\n"),
5050
download_service_{std::make_shared<DownloadService>()},
5151
dylib_path_manager_{std::make_shared<cortex::DylibPathManager>()},
52-
engine_service_{std::make_shared<EngineService>(download_service_,
53-
dylib_path_manager_)} {
52+
db_service_{std::make_shared<DatabaseService>()},
53+
engine_service_{std::make_shared<EngineService>(
54+
download_service_, dylib_path_manager_, db_service_)} {
5455
supported_engines_ = engine_service_->GetSupportedEngineNames().value();
5556
}
5657

@@ -177,7 +178,7 @@ void CommandLineParser::SetupCommonCommands() {
177178
return;
178179
commands::RunCmd rc(cml_data_.config.apiServerHost,
179180
std::stoi(cml_data_.config.apiServerPort),
180-
cml_data_.model_id, engine_service_);
181+
cml_data_.model_id, db_service_, engine_service_);
181182
rc.Exec(cml_data_.run_detach, run_settings_);
182183
});
183184
}
@@ -216,9 +217,10 @@ void CommandLineParser::SetupModelCommands() {
216217
CLI_LOG(model_start_cmd->help());
217218
return;
218219
};
219-
commands::ModelStartCmd().Exec(cml_data_.config.apiServerHost,
220-
std::stoi(cml_data_.config.apiServerPort),
221-
cml_data_.model_id, run_settings_);
220+
commands::ModelStartCmd(db_service_)
221+
.Exec(cml_data_.config.apiServerHost,
222+
std::stoi(cml_data_.config.apiServerPort), cml_data_.model_id,
223+
run_settings_);
222224
});
223225

224226
auto stop_model_cmd =

engine/cli/command_line_parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class CommandLineParser {
4545
CLI::App app_;
4646
std::shared_ptr<DownloadService> download_service_;
4747
std::shared_ptr<cortex::DylibPathManager> dylib_path_manager_;
48+
std::shared_ptr<DatabaseService> db_service_;
4849
std::shared_ptr<EngineService> engine_service_;
4950
std::vector<std::string> supported_engines_;
5051

engine/cli/commands/chat_completion_cmd.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ void ChatCompletionCmd::Exec(const std::string& host, int port,
5656
const std::string& model_handle, std::string msg) {
5757
namespace fs = std::filesystem;
5858
namespace fmu = file_manager_utils;
59-
cortex::db::Models modellist_handler;
6059
config::YamlHandler yaml_handler;
6160
try {
62-
auto model_entry = modellist_handler.GetModelInfo(model_handle);
61+
auto model_entry = db_service_->GetModelInfo(model_handle);
6362
if (model_entry.has_error()) {
6463
CLI_LOG("Error: " + model_entry.error());
6564
return;

engine/cli/commands/chat_completion_cmd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
#include <string>
44
#include <vector>
55
#include "config/model_config.h"
6+
#include "services/database_service.h"
67

78
namespace commands {
89
class ChatCompletionCmd {
910
public:
11+
explicit ChatCompletionCmd(std::shared_ptr<DatabaseService> db_service)
12+
: db_service_(db_service) {}
1013
void Exec(const std::string& host, int port, const std::string& model_handle,
1114
std::string msg);
1215
void Exec(const std::string& host, int port, const std::string& model_handle,
1316
const config::ModelConfig& mc, std::string msg);
1417

1518
private:
19+
std::shared_ptr<DatabaseService> db_service_;
1620
std::vector<Json::Value> histories_;
1721
};
1822
} // namespace commands

engine/cli/commands/model_start_cmd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ bool ModelStartCmd::Exec(
1313
const std::unordered_map<std::string, std::string>& options,
1414
bool print_success_log) {
1515
std::optional<std::string> model_id =
16-
SelectLocalModel(host, port, model_handle);
16+
SelectLocalModel(host, port, model_handle, *db_service_);
1717

1818
if (!model_id.has_value()) {
1919
return false;

engine/cli/commands/model_start_cmd.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
#include <string>
44
#include <unordered_map>
55
#include "json/json.h"
6+
#include "services/database_service.h"
67

78
namespace commands {
89

910
class ModelStartCmd {
1011
public:
12+
explicit ModelStartCmd(std::shared_ptr<DatabaseService> db_service)
13+
: db_service_(db_service) {}
1114
bool Exec(const std::string& host, int port, const std::string& model_handle,
1215
const std::unordered_map<std::string, std::string>& options,
1316
bool print_success_log = true);
14-
private:
17+
18+
private:
1519
bool UpdateConfig(Json::Value& data, const std::string& key,
1620
const std::string& value);
21+
22+
private:
23+
std::shared_ptr<DatabaseService> db_service_;
1724
};
1825
} // namespace commands

engine/cli/commands/run_cmd.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
namespace commands {
1515

1616
std::optional<std::string> SelectLocalModel(std::string host, int port,
17-
const std::string& model_handle) {
17+
const std::string& model_handle,
18+
DatabaseService& db_service) {
1819
std::optional<std::string> model_id = model_handle;
19-
cortex::db::Models modellist_handler;
20-
2120
if (model_handle.empty()) {
22-
auto all_local_models = modellist_handler.LoadModelList();
21+
auto all_local_models = db_service.LoadModelList();
2322
if (all_local_models.has_error() || all_local_models.value().empty()) {
2423
CLI_LOG("No local models available!");
2524
return std::nullopt;
@@ -42,7 +41,7 @@ std::optional<std::string> SelectLocalModel(std::string host, int port,
4241
CLI_LOG("Selected: " << selection.value());
4342
}
4443
} else {
45-
auto related_models_ids = modellist_handler.FindRelatedModel(model_handle);
44+
auto related_models_ids = db_service.FindRelatedModel(model_handle);
4645
if (related_models_ids.has_error() || related_models_ids.value().empty()) {
4746
auto result = ModelPullCmd().Exec(host, port, model_handle);
4847
if (!result) {
@@ -69,19 +68,18 @@ std::optional<std::string> SelectLocalModel(std::string host, int port,
6968
void RunCmd::Exec(bool run_detach,
7069
const std::unordered_map<std::string, std::string>& options) {
7170
std::optional<std::string> model_id =
72-
SelectLocalModel(host_, port_, model_handle_);
71+
SelectLocalModel(host_, port_, model_handle_, *db_service_);
7372
if (!model_id.has_value()) {
7473
return;
7574
}
7675

77-
cortex::db::Models modellist_handler;
7876
config::YamlHandler yaml_handler;
7977
auto address = host_ + ":" + std::to_string(port_);
8078

8179
try {
8280
namespace fs = std::filesystem;
8381
namespace fmu = file_manager_utils;
84-
auto model_entry = modellist_handler.GetModelInfo(*model_id);
82+
auto model_entry = db_service_->GetModelInfo(*model_id);
8583
if (model_entry.has_error()) {
8684
CLI_LOG("Error: " + model_entry.error());
8785
return;
@@ -128,7 +126,7 @@ void RunCmd::Exec(bool run_detach,
128126
mc.engine.find(kLlamaEngine) == std::string::npos) ||
129127
!commands::ModelStatusCmd().IsLoaded(host_, port_, *model_id)) {
130128

131-
auto res = commands::ModelStartCmd()
129+
auto res = commands::ModelStartCmd(db_service_)
132130
.Exec(host_, port_, *model_id, options,
133131
false /*print_success_log*/);
134132
if (!res) {
@@ -144,7 +142,7 @@ void RunCmd::Exec(bool run_detach,
144142
<< commands::GetCortexBinary() << " run " << *model_id
145143
<< "` for interactive chat shell");
146144
} else {
147-
ChatCompletionCmd().Exec(host_, port_, *model_id, mc, "");
145+
ChatCompletionCmd(db_service_).Exec(host_, port_, *model_id, mc, "");
148146
}
149147
}
150148
} catch (const std::exception& e) {

engine/cli/commands/run_cmd.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
#include <string>
44
#include <unordered_map>
5+
#include "services/database_service.h"
56
#include "services/engine_service.h"
67

78
namespace commands {
89

910
std::optional<std::string> SelectLocalModel(std::string host, int port,
10-
const std::string& model_handle);
11+
const std::string& model_handle,
12+
DatabaseService& db_service);
1113

1214
class RunCmd {
1315
public:
1416
explicit RunCmd(std::string host, int port, std::string model_handle,
17+
std::shared_ptr<DatabaseService> db_service,
1518
std::shared_ptr<EngineService> engine_service)
1619
: host_{std::move(host)},
1720
port_{port},
1821
model_handle_{std::move(model_handle)},
22+
db_service_(db_service),
1923
engine_service_{engine_service} {};
2024

2125
void Exec(bool chat_flag,
@@ -25,6 +29,7 @@ class RunCmd {
2529
std::string host_;
2630
int port_;
2731
std::string model_handle_;
32+
std::shared_ptr<DatabaseService> db_service_;
2833
std::shared_ptr<EngineService> engine_service_;
2934
};
3035
} // namespace commands

0 commit comments

Comments
 (0)