forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.cc
More file actions
336 lines (309 loc) · 11.4 KB
/
models.cc
File metadata and controls
336 lines (309 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "models.h"
#include <algorithm>
#include <sstream>
#include "database.h"
#include "utils/logging_utils.h"
#include "utils/result.hpp"
#include "utils/scope_exit.h"
namespace cortex::db {
Models::Models() : db_(cortex::db::Database::GetInstance().db()) {}
Models::~Models() {}
std::string Models::StatusToString(ModelStatus status) const {
switch (status) {
case ModelStatus::Remote:
return "remote";
case ModelStatus::Downloaded:
return "downloaded";
case ModelStatus::Downloadable:
return "downloadable";
}
return "unknown";
}
Models::Models(SQLite::Database& db) : db_(db) {}
ModelStatus Models::StringToStatus(const std::string& status_str) const {
if (status_str == "remote") {
return ModelStatus::Remote;
} else if (status_str == "downloaded" || status_str.empty()) {
return ModelStatus::Downloaded;
} else if (status_str == "downloadable") {
return ModelStatus::Downloadable;
}
throw std::invalid_argument("Invalid status string");
}
cpp::result<std::vector<ModelEntry>, std::string> Models::LoadModelList()
const {
try {
db_.exec("BEGIN TRANSACTION;");
cortex::utils::ScopeExit se([this] { db_.exec("COMMIT;"); });
return LoadModelListNoLock();
} catch (const std::exception& e) {
CTL_WRN(e.what());
return cpp::fail(e.what());
}
}
bool Models::IsUnique(const std::vector<ModelEntry>& entries,
const std::string& model_id) const {
return std::none_of(
entries.begin(), entries.end(),
[&](const ModelEntry& entry) { return entry.model == model_id; });
}
cpp::result<std::vector<ModelEntry>, std::string> Models::LoadModelListNoLock()
const {
try {
std::vector<ModelEntry> entries;
SQLite::Statement query(
db_,
"SELECT model_id, author_repo_id, branch_name, "
"path_to_model_yaml, model_alias, model_format, "
"model_source, status, engine, metadata FROM models");
while (query.executeStep()) {
ModelEntry entry;
entry.model = query.getColumn(0).getString();
entry.author_repo_id = query.getColumn(1).getString();
entry.branch_name = query.getColumn(2).getString();
entry.path_to_model_yaml = query.getColumn(3).getString();
entry.model_alias = query.getColumn(4).getString();
entry.model_format = query.getColumn(5).getString();
entry.model_source = query.getColumn(6).getString();
entry.status = StringToStatus(query.getColumn(7).getString());
entry.engine = query.getColumn(8).getString();
entry.metadata = query.getColumn(9).getString();
entries.push_back(entry);
}
return entries;
} catch (const std::exception& e) {
CTL_WRN(e.what());
return cpp::fail(e.what());
}
}
cpp::result<ModelEntry, std::string> Models::GetModelInfo(
const std::string& identifier) const {
try {
SQLite::Statement query(
db_,
"SELECT model_id, author_repo_id, branch_name, "
"path_to_model_yaml, model_alias, model_format, "
"model_source, status, engine, metadata FROM models "
"WHERE model_id = ?");
query.bind(1, identifier);
if (query.executeStep()) {
ModelEntry entry;
entry.model = query.getColumn(0).getString();
entry.author_repo_id = query.getColumn(1).getString();
entry.branch_name = query.getColumn(2).getString();
entry.path_to_model_yaml = query.getColumn(3).getString();
entry.model_alias = query.getColumn(4).getString();
entry.model_format = query.getColumn(5).getString();
entry.model_source = query.getColumn(6).getString();
entry.status = StringToStatus(query.getColumn(7).getString());
entry.engine = query.getColumn(8).getString();
entry.metadata = query.getColumn(9).getString();
return entry;
} else {
return cpp::fail("Model not found: " + identifier);
}
} catch (const std::exception& e) {
return cpp::fail(e.what());
}
}
void Models::PrintModelInfo(const ModelEntry& entry) const {
LOG_INFO << "Model ID: " << entry.model;
LOG_INFO << "Author/Repo ID: " << entry.author_repo_id;
LOG_INFO << "Branch Name: " << entry.branch_name;
LOG_INFO << "Path to model.yaml: " << entry.path_to_model_yaml;
LOG_INFO << "Model Alias: " << entry.model_alias;
LOG_INFO << "Model Format: " << entry.model_format;
LOG_INFO << "Model Source: " << entry.model_source;
LOG_INFO << "Status: " << StatusToString(entry.status);
LOG_INFO << "Engine: " << entry.engine;
LOG_INFO << "Metadata: " << entry.metadata;
}
cpp::result<bool, std::string> Models::AddModelEntry(ModelEntry new_entry) {
try {
db_.exec("BEGIN TRANSACTION;");
cortex::utils::ScopeExit se([this] { db_.exec("COMMIT;"); });
auto model_list = LoadModelListNoLock();
if (model_list.has_error()) {
CTL_WRN(model_list.error());
return cpp::fail(model_list.error());
}
if (IsUnique(model_list.value(), new_entry.model)) {
SQLite::Statement insert(
db_,
"INSERT INTO models (model_id, author_repo_id, branch_name, "
"path_to_model_yaml, model_alias, model_format, model_source, "
"status, engine, metadata) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
insert.bind(1, new_entry.model);
insert.bind(2, new_entry.author_repo_id);
insert.bind(3, new_entry.branch_name);
insert.bind(4, new_entry.path_to_model_yaml);
insert.bind(5, new_entry.model_alias);
insert.bind(6, new_entry.model_format);
insert.bind(7, new_entry.model_source);
insert.bind(8, StatusToString(new_entry.status));
insert.bind(9, new_entry.engine);
insert.bind(10, new_entry.metadata);
insert.exec();
return true;
}
return false; // Entry not added due to non-uniqueness
} catch (const std::exception& e) {
CTL_WRN(e.what());
return cpp::fail(e.what());
}
}
cpp::result<bool, std::string> Models::UpdateModelEntry(
const std::string& identifier, const ModelEntry& updated_entry) {
if (!HasModel(identifier)) {
return cpp::fail("Model not found: " + identifier);
}
try {
SQLite::Statement upd(
db_,
"UPDATE models SET author_repo_id = ?, branch_name = ?, "
"path_to_model_yaml = ?, model_format = ?, model_source = ?, status = "
"?, engine = ?, metadata = ? WHERE model_id = ?");
upd.bind(1, updated_entry.author_repo_id);
upd.bind(2, updated_entry.branch_name);
upd.bind(3, updated_entry.path_to_model_yaml);
upd.bind(4, updated_entry.model_format);
upd.bind(5, updated_entry.model_source);
upd.bind(6, StatusToString(updated_entry.status));
upd.bind(7, updated_entry.engine);
upd.bind(8, updated_entry.metadata);
upd.bind(9, identifier);
return upd.exec() == 1;
} catch (const std::exception& e) {
return cpp::fail(e.what());
}
}
cpp::result<bool, std::string> Models::DeleteModelEntry(
const std::string& identifier) {
try {
// delete only if its there
if (!HasModel(identifier)) {
return true;
}
SQLite::Statement del(db_, "DELETE from models WHERE model_id = ?");
del.bind(1, identifier);
return del.exec() == 1;
} catch (const std::exception& e) {
return cpp::fail(e.what());
}
}
cpp::result<bool, std::string> Models::DeleteModelEntryWithOrg(
const std::string& src) {
try {
SQLite::Statement del(db_,
"DELETE from models WHERE model_source LIKE ? AND "
"status = \"downloadable\"");
del.bind(1, src + "%");
return del.exec() == 1;
} catch (const std::exception& e) {
return cpp::fail(e.what());
}
}
cpp::result<bool, std::string> Models::DeleteModelEntryWithRepo(
const std::string& src) {
try {
SQLite::Statement del(db_,
"DELETE from models WHERE model_source = ? AND "
"status = \"downloadable\"");
del.bind(1, src);
return del.exec() == 1;
} catch (const std::exception& e) {
return cpp::fail(e.what());
}
}
cpp::result<std::vector<std::string>, std::string> Models::FindRelatedModel(
const std::string& identifier) const {
try {
std::vector<std::string> related_models;
SQLite::Statement query(db_,
"SELECT model_id FROM models WHERE model_id LIKE ? "
"AND status = \"downloaded\"");
query.bind(1, "%" + identifier + "%");
while (query.executeStep()) {
related_models.push_back(query.getColumn(0).getString());
}
return related_models;
} catch (const std::exception& e) {
return cpp::fail(e.what());
}
}
bool Models::HasModel(const std::string& identifier) const {
try {
SQLite::Statement query(db_,
"SELECT COUNT(*) FROM models WHERE model_id = ?");
query.bind(1, identifier);
if (query.executeStep()) {
return query.getColumn(0).getInt() > 0;
}
return false;
} catch (const std::exception& e) {
CTL_WRN(e.what());
return false;
}
}
cpp::result<std::vector<ModelEntry>, std::string> Models::GetModels(
const std::string& model_src) const {
try {
std::vector<ModelEntry> res;
SQLite::Statement query(db_,
"SELECT model_id, author_repo_id, branch_name, "
"path_to_model_yaml, model_alias, model_format, "
"model_source, status, engine, metadata FROM "
"models WHERE model_source = "
"? AND status = \"downloadable\"");
query.bind(1, model_src);
while (query.executeStep()) {
ModelEntry entry;
entry.model = query.getColumn(0).getString();
entry.author_repo_id = query.getColumn(1).getString();
entry.branch_name = query.getColumn(2).getString();
entry.path_to_model_yaml = query.getColumn(3).getString();
entry.model_alias = query.getColumn(4).getString();
entry.model_format = query.getColumn(5).getString();
entry.model_source = query.getColumn(6).getString();
entry.status = StringToStatus(query.getColumn(7).getString());
entry.engine = query.getColumn(8).getString();
entry.metadata = query.getColumn(9).getString();
res.push_back(entry);
}
return res;
} catch (const std::exception& e) {
return cpp::fail(e.what());
}
}
cpp::result<std::vector<ModelEntry>, std::string> Models::GetModelSources()
const {
try {
std::vector<ModelEntry> res;
SQLite::Statement query(
db_,
"SELECT model_id, author_repo_id, branch_name, "
"path_to_model_yaml, model_alias, model_format, "
"model_source, status, engine, metadata FROM models "
"WHERE model_source != \"\" AND model_source != \"imported\" AND "
"(status = \"downloaded\" OR status = "
"\"downloadable\")");
while (query.executeStep()) {
ModelEntry entry;
entry.model = query.getColumn(0).getString();
entry.author_repo_id = query.getColumn(1).getString();
entry.branch_name = query.getColumn(2).getString();
entry.path_to_model_yaml = query.getColumn(3).getString();
entry.model_alias = query.getColumn(4).getString();
entry.model_format = query.getColumn(5).getString();
entry.model_source = query.getColumn(6).getString();
entry.status = StringToStatus(query.getColumn(7).getString());
entry.engine = query.getColumn(8).getString();
entry.metadata = query.getColumn(9).getString();
res.push_back(entry);
}
return res;
} catch (const std::exception& e) {
return cpp::fail(e.what());
}
}
} // namespace cortex::db