Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 08b8841

Browse files
authored
feat: sqlite database implementation (#1336)
* feat: sqlite * chore: unit tests * fix: unit tests * refactor: db * fix: remove mutex * fix: rm file * fix: test * fix: test * refactor: LoadModelList * refactor: more * fix: transaction * fix: format * fix: make alias unique * fix: remove ModelStatus * fix: models
1 parent 6502797 commit 08b8841

22 files changed

+657
-525
lines changed

engine/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ find_package(unofficial-minizip CONFIG REQUIRED)
7777
find_package(LibArchive REQUIRED)
7878
find_package(tabulate CONFIG REQUIRED)
7979
find_package(CURL REQUIRED)
80+
find_package(SQLiteCpp REQUIRED)
8081

8182
add_executable(${TARGET_NAME} main.cc
8283
${CMAKE_CURRENT_SOURCE_DIR}/utils/cpuid/cpu_info.cc
8384
${CMAKE_CURRENT_SOURCE_DIR}/utils/file_logger.cc
84-
${CMAKE_CURRENT_SOURCE_DIR}/utils/modellist_utils.cc
8585
)
8686

8787
target_link_libraries(${TARGET_NAME} PRIVATE httplib::httplib)
@@ -93,6 +93,7 @@ target_link_libraries(${TARGET_NAME} PRIVATE tabulate::tabulate)
9393
target_link_libraries(${TARGET_NAME} PRIVATE CURL::libcurl)
9494
target_link_libraries(${TARGET_NAME} PRIVATE JsonCpp::JsonCpp Drogon::Drogon OpenSSL::SSL OpenSSL::Crypto yaml-cpp::yaml-cpp
9595
${CMAKE_THREAD_LIBS_INIT})
96+
target_link_libraries(${TARGET_NAME} PRIVATE SQLiteCpp)
9697

9798
# ##############################################################################
9899

@@ -114,7 +115,8 @@ aux_source_directory(models MODEL_SRC)
114115
aux_source_directory(cortex-common CORTEX_COMMON)
115116
aux_source_directory(config CONFIG_SRC)
116117
aux_source_directory(commands COMMANDS_SRC)
117-
118+
aux_source_directory(database DB_SRC)
119+
118120
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} )
119121

120-
target_sources(${TARGET_NAME} PRIVATE ${COMMANDS_SRC} ${CONFIG_SRC} ${CTL_SRC} ${COMMON_SRC} ${SERVICES_SRC})
122+
target_sources(${TARGET_NAME} PRIVATE ${COMMANDS_SRC} ${CONFIG_SRC} ${CTL_SRC} ${COMMON_SRC} ${SERVICES_SRC} ${DB_SRC})

engine/commands/chat_cmd.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
#include "httplib.h"
33

44
#include "cortex_upd_cmd.h"
5+
#include "database/models.h"
56
#include "model_status_cmd.h"
67
#include "server_start_cmd.h"
78
#include "trantor/utils/Logger.h"
89
#include "utils/logging_utils.h"
9-
#include "utils/modellist_utils.h"
1010

1111
namespace commands {
1212
namespace {
@@ -39,11 +39,15 @@ struct ChunkParser {
3939

4040
void ChatCmd::Exec(const std::string& host, int port,
4141
const std::string& model_handle, std::string msg) {
42-
modellist_utils::ModelListUtils modellist_handler;
42+
cortex::db::Models modellist_handler;
4343
config::YamlHandler yaml_handler;
4444
try {
4545
auto model_entry = modellist_handler.GetModelInfo(model_handle);
46-
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
46+
if (model_entry.has_error()) {
47+
CLI_LOG("Error: " + model_entry.error());
48+
return;
49+
}
50+
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
4751
auto mc = yaml_handler.GetModelConfig();
4852
Exec(host, port, mc, std::move(msg));
4953
} catch (const std::exception& e) {

engine/commands/model_alias_cmd.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include "model_alias_cmd.h"
2-
#include "utils/modellist_utils.h"
2+
#include "database/models.h"
33

44
namespace commands {
55

66
void ModelAliasCmd::Exec(const std::string& model_handle,
77
const std::string& model_alias) {
8-
modellist_utils::ModelListUtils modellist_handler;
8+
cortex::db::Models modellist_handler;
99
try {
1010
if (modellist_handler.UpdateModelAlias(model_handle, model_alias)) {
1111
CLI_LOG("Successfully set model alias '" + model_alias +

engine/commands/model_get_cmd.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55
#include <vector>
66
#include "cmd_info.h"
77
#include "config/yaml_config.h"
8+
#include "database/models.h"
89
#include "utils/file_manager_utils.h"
910
#include "utils/logging_utils.h"
10-
#include "utils/modellist_utils.h"
1111

1212
namespace commands {
1313

1414
void ModelGetCmd::Exec(const std::string& model_handle) {
15-
modellist_utils::ModelListUtils modellist_handler;
15+
cortex::db::Models modellist_handler;
1616
config::YamlHandler yaml_handler;
1717
try {
1818
auto model_entry = modellist_handler.GetModelInfo(model_handle);
19-
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
19+
if (model_entry.has_error()) {
20+
CLI_LOG("Error: " + model_entry.error());
21+
return;
22+
}
23+
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
2024
auto model_config = yaml_handler.GetModelConfig();
2125

2226
std::cout << model_config.ToString() << std::endl;

engine/commands/model_import_cmd.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#include <vector>
44
#include "config/gguf_parser.h"
55
#include "config/yaml_config.h"
6+
#include "database/models.h"
67
#include "utils/file_manager_utils.h"
78
#include "utils/logging_utils.h"
8-
#include "utils/modellist_utils.h"
99

1010
namespace commands {
1111

@@ -16,15 +16,15 @@ ModelImportCmd::ModelImportCmd(std::string model_handle, std::string model_path)
1616
void ModelImportCmd::Exec() {
1717
config::GGUFHandler gguf_handler;
1818
config::YamlHandler yaml_handler;
19-
modellist_utils::ModelListUtils modellist_utils_obj;
19+
cortex::db::Models modellist_utils_obj;
2020

2121
std::string model_yaml_path = (file_manager_utils::GetModelsContainerPath() /
2222
std::filesystem::path("imported") /
2323
std::filesystem::path(model_handle_ + ".yml"))
2424
.string();
25-
modellist_utils::ModelEntry model_entry{
25+
cortex::db::ModelEntry model_entry{
2626
model_handle_, "local", "imported",
27-
model_yaml_path, model_handle_, modellist_utils::ModelStatus::READY};
27+
model_yaml_path, model_handle_};
2828
try {
2929
std::filesystem::create_directories(
3030
std::filesystem::path(model_yaml_path).parent_path());
@@ -34,7 +34,7 @@ void ModelImportCmd::Exec() {
3434
model_config.model = model_handle_;
3535
yaml_handler.UpdateModelConfig(model_config);
3636

37-
if (modellist_utils_obj.AddModelEntry(model_entry)) {
37+
if (modellist_utils_obj.AddModelEntry(model_entry).value()) {
3838
yaml_handler.WriteYamlFile(model_yaml_path);
3939
CLI_LOG("Model is imported successfully!");
4040
} else {

engine/commands/model_list_cmd.cc

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
#include <tabulate/table.hpp>
44
#include <vector>
55
#include "config/yaml_config.h"
6+
#include "database/models.h"
67
#include "utils/file_manager_utils.h"
78
#include "utils/logging_utils.h"
8-
#include "utils/modellist_utils.h"
99

1010
namespace commands {
1111

1212
void ModelListCmd::Exec() {
1313
auto models_path = file_manager_utils::GetModelsContainerPath();
14-
modellist_utils::ModelListUtils modellist_handler;
14+
cortex::db::Models modellist_handler;
1515
config::YamlHandler yaml_handler;
1616
tabulate::Table table;
1717

@@ -20,24 +20,24 @@ void ModelListCmd::Exec() {
2020
int count = 0;
2121
// Iterate through directory
2222

23-
try {
24-
auto list_entry = modellist_handler.LoadModelList();
25-
for (const auto& model_entry : list_entry) {
26-
// auto model_entry = modellist_handler.GetModelInfo(model_handle);
27-
try {
28-
count += 1;
29-
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
30-
auto model_config = yaml_handler.GetModelConfig();
31-
table.add_row({std::to_string(count), model_entry.model_id,
32-
model_entry.model_alias, model_config.engine,
33-
model_config.version});
34-
yaml_handler.Reset();
35-
} catch (const std::exception& e) {
36-
CTL_ERR("Fail to get list model information: " + std::string(e.what()));
37-
}
23+
auto list_entry = modellist_handler.LoadModelList();
24+
if (list_entry.has_error()) {
25+
CTL_ERR("Fail to get list model information: " << list_entry.error());
26+
return;
27+
}
28+
for (const auto& model_entry : list_entry.value()) {
29+
// auto model_entry = modellist_handler.GetModelInfo(model_handle);
30+
try {
31+
count += 1;
32+
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
33+
auto model_config = yaml_handler.GetModelConfig();
34+
table.add_row({std::to_string(count), model_entry.model_id,
35+
model_entry.model_alias, model_config.engine,
36+
model_config.version});
37+
yaml_handler.Reset();
38+
} catch (const std::exception& e) {
39+
CTL_ERR("Fail to get list model information: " + std::string(e.what()));
3840
}
39-
} catch (const std::exception& e) {
40-
CTL_ERR("Fail to get list model information: " + std::string(e.what()));
4141
}
4242

4343
for (int i = 0; i < 5; i++) {

engine/commands/model_start_cmd.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
#include "model_start_cmd.h"
22
#include "cortex_upd_cmd.h"
3+
#include "database/models.h"
34
#include "httplib.h"
45
#include "model_status_cmd.h"
56
#include "nlohmann/json.hpp"
67
#include "server_start_cmd.h"
78
#include "trantor/utils/Logger.h"
89
#include "utils/file_manager_utils.h"
910
#include "utils/logging_utils.h"
10-
#include "utils/modellist_utils.h"
1111

1212
namespace commands {
1313
bool ModelStartCmd::Exec(const std::string& host, int port,
1414
const std::string& model_handle) {
1515

16-
modellist_utils::ModelListUtils modellist_handler;
16+
cortex::db::Models modellist_handler;
1717
config::YamlHandler yaml_handler;
1818
try {
1919
auto model_entry = modellist_handler.GetModelInfo(model_handle);
20-
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
20+
if (model_entry.has_error()) {
21+
CLI_LOG("Error: " + model_entry.error());
22+
return false;
23+
}
24+
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
2125
auto mc = yaml_handler.GetModelConfig();
2226
return Exec(host, port, mc);
2327
} catch (const std::exception& e) {

engine/commands/model_status_cmd.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
#include "model_status_cmd.h"
22
#include "config/yaml_config.h"
3+
#include "database/models.h"
34
#include "httplib.h"
45
#include "nlohmann/json.hpp"
56
#include "utils/logging_utils.h"
6-
#include "utils/modellist_utils.h"
77

88
namespace commands {
99
bool ModelStatusCmd::IsLoaded(const std::string& host, int port,
1010
const std::string& model_handle) {
11-
modellist_utils::ModelListUtils modellist_handler;
11+
cortex::db::Models modellist_handler;
1212
config::YamlHandler yaml_handler;
1313
try {
1414
auto model_entry = modellist_handler.GetModelInfo(model_handle);
15-
yaml_handler.ModelConfigFromFile(model_entry.path_to_model_yaml);
15+
if (model_entry.has_error()) {
16+
CLI_LOG("Error: " + model_entry.error());
17+
return false;
18+
}
19+
yaml_handler.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
1620
auto mc = yaml_handler.GetModelConfig();
1721
return IsLoaded(host, port, mc);
1822
} catch (const std::exception& e) {

engine/commands/model_upd_cmd.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ void ModelUpdCmd::Exec(
1111
const std::unordered_map<std::string, std::string>& options) {
1212
try {
1313
auto model_entry = model_list_utils_.GetModelInfo(model_handle_);
14-
yaml_handler_.ModelConfigFromFile(model_entry.path_to_model_yaml);
14+
if (model_entry.has_error()) {
15+
CLI_LOG("Error: " + model_entry.error());
16+
return;
17+
}
18+
yaml_handler_.ModelConfigFromFile(model_entry.value().path_to_model_yaml);
1519
model_config_ = yaml_handler_.GetModelConfig();
1620

1721
for (const auto& [key, value] : options) {
@@ -21,7 +25,7 @@ void ModelUpdCmd::Exec(
2125
}
2226

2327
yaml_handler_.UpdateModelConfig(model_config_);
24-
yaml_handler_.WriteYamlFile(model_entry.path_to_model_yaml);
28+
yaml_handler_.WriteYamlFile(model_entry.value().path_to_model_yaml);
2529
CLI_LOG("Successfully updated model ID '" + model_handle_ + "'!");
2630
} catch (const std::exception& e) {
2731
CLI_LOG("Failed to update model with model ID '" + model_handle_ +

engine/commands/model_upd_cmd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <unordered_map>
66
#include <vector>
77
#include "config/model_config.h"
8-
#include "utils/modellist_utils.h"
98
#include "config/yaml_config.h"
9+
#include "database/models.h"
1010
namespace commands {
1111
class ModelUpdCmd {
1212
public:
@@ -17,7 +17,7 @@ class ModelUpdCmd {
1717
std::string model_handle_;
1818
config::ModelConfig model_config_;
1919
config::YamlHandler yaml_handler_;
20-
modellist_utils::ModelListUtils model_list_utils_;
20+
cortex::db::Models model_list_utils_;
2121

2222
void UpdateConfig(const std::string& key, const std::string& value);
2323
void UpdateVectorField(const std::string& key, const std::string& value);

0 commit comments

Comments
 (0)