/
modelsRestApiClient.ts
429 lines (405 loc) · 14.5 KB
/
modelsRestApiClient.ts
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import ModelsApiClient from './modelsApiClient';
import mysql from 'mysql';
import { AdjustOptions, TrainingOptions } from './trainingOptions';
import SqlApiClient from '../sql/sqlApiClient';
import {
Model,
ModelDescribeAccuracy,
ModelFeatureDescription,
ModelPrediction,
ModelRow,
} from './model';
import { BatchQueryOptions, QueryOptions } from './queryOptions';
import { MindsDbError } from '../errors';
/** Implementation of ModelsApiClient that goes through the REST API */
export default class ModelsRestApiClient extends ModelsApiClient {
/** SQL API client to send all SQL query requests. */
sqlClient: SqlApiClient;
/**
*
* @param {SqlApiClient} sqlClient - SQL API client to send all SQL query requests.
*/
constructor(sqlClient: SqlApiClient) {
super();
this.sqlClient = sqlClient;
}
private makeRetrainClause(name: string, project: string): string {
return `RETRAIN ${mysql.escapeId(project)}.${mysql.escapeId(name)}`;
}
private makeTrainingCreateClause(name: string, project: string): string {
return `CREATE MODEL ${mysql.escapeId(project)}.${mysql.escapeId(name)}`;
}
private makeTrainingFromClause(options: TrainingOptions): string {
const integration = options['integration'];
if (integration) {
return `FROM ${mysql.escapeId(integration)}`;
}
return '';
}
private makeTrainingSelectClause(
options: TrainingOptions | AdjustOptions
): string {
const select = options['select'];
if (select) {
return `(${select})`;
}
return '';
}
private makeTrainingPredictClause(targetColumn: string): string {
return `PREDICT ${mysql.escapeId(targetColumn)}`;
}
private makeTrainingOrderByClause(trainingOptions: TrainingOptions): string {
const orderByColumn = trainingOptions['orderBy'];
if (!orderByColumn) {
return '';
}
return `ORDER BY ${mysql.escapeId(orderByColumn)}`;
}
private makeTrainingGroupByClause(trainingOptions: TrainingOptions): string {
const groupByColumn = trainingOptions['groupBy'];
if (!groupByColumn) {
return '';
}
return `GROUP BY ${mysql.escapeId(groupByColumn)}`;
}
private makeTrainingWindowHorizonClause(
trainingOptions: TrainingOptions
): string {
const window = trainingOptions['window'];
const horizon = trainingOptions['horizon'];
if (!window || !horizon) {
return '';
}
return `WINDOW ${mysql.escape(window)}\nHORIZON ${mysql.escape(horizon)}`;
}
private makeWhereClause(where: string | Array<string>) {
let whereClause = '';
if (Array.isArray(where)) {
if (where.length === 0) {
return whereClause;
}
whereClause = `WHERE ${where[0]}\n`;
if (where.length === 1) {
return whereClause;
}
whereClause += where
.slice(1)
.map(
(o) =>
// Escaping WHERE conditions is quite tricky. We should
// come up with a better solution to indicate WHERE conditions
// when querying so we aren't passing a raw string.
`AND ${o}`
)
.join('\n');
} else {
whereClause = `WHERE ${where}`;
}
return whereClause;
}
private makeTrainingUsingClause(
options: AdjustOptions | TrainingOptions
): string {
const using = options['using'];
if (!using) {
return '';
}
const allParams = [];
for (const param in using) {
let paramVal = using[param];
if (Array.isArray(paramVal) || typeof paramVal === 'object') {
// MindsDB SQL allows param values to be objects and arrays, not just primitives.
paramVal = JSON.stringify(paramVal);
} else {
paramVal = mysql.escape(paramVal);
}
allParams.push(`${mysql.escapeId(param)} = ${paramVal}`);
}
return `USING\n${allParams.join(',\n')}`;
}
/**
* Gets a model by name and project.
* @param {string} name - Name of the model.
* @param {string} project - Project the model belongs to.
* @param {number} version - The version of the model.
* @returns {Promise<Model | undefined>} - The matching model, or undefined if none exists.
*/
override async getModel(
name: string,
project: string,
version?: number
): Promise<Model | undefined> {
const selectQuery = `SELECT * FROM ${mysql.escapeId(project)}.models${
version ? '_versions' : ''
} WHERE name = ${mysql.escape(name)}${
version ? ` and version = ${mysql.escape(version)}` : ''
}`;
const sqlQueryResult = await this.sqlClient.runQuery(selectQuery);
if (sqlQueryResult.rows?.length === 0) {
return undefined;
}
return Model.fromModelRow(sqlQueryResult.rows[0] as ModelRow, this);
}
/**
* Gets all models associated with a project.
* @param {string} project - Project the models belong to.
* @returns {Promise<Array<Model>>} - All models for the given project.
*/
override async getAllModels(project: string): Promise<Array<Model>> {
const selectQuery = `SELECT * FROM ${mysql.escapeId(project)}.models`;
const sqlQueryResult = await this.sqlClient.runQuery(selectQuery);
return sqlQueryResult.rows.map((modelRow) =>
Model.fromModelRow(modelRow as ModelRow, this)
);
}
/**
* Describes the features of this model.
* @param {string} name - Name of the model.
* @param {string} project - Project the model belongs to.
* @returns {Array<ModelFeatureDescription>} - All feature descriptions of the model. Empty if the model doesn't exist.
*/
override async describeModel(
name: string,
project: string
): Promise<Array<ModelFeatureDescription>> {
const describeQuery = `DESCRIBE ${mysql.escapeId(project)}.${mysql.escapeId(
name
)}.features`;
const sqlQueryResult = await this.sqlClient.runQuery(describeQuery);
if (sqlQueryResult.rows.length === 0) {
return [];
}
return sqlQueryResult.rows as Array<ModelFeatureDescription>;
}
/**
* Describes the features of this model.
* @param {string} name - Name of the model.
* @param {string} project - Project the model belongs to.
* @param {string} unique_id - Optional unique id to filter the accuracy by.
* @returns {Array<ModelDescribeAccuracy>} - All feature descriptions of the model. Empty if the model doesn't exist.
*/
override async describeAccuracyModel(
name: string,
project: string,
unique_id?: string
): Promise<Array<ModelDescribeAccuracy>> {
const describeQuery = `DESCRIBE ${mysql.escapeId(project)}.${mysql.escapeId(
name
)}.accuracy${unique_id ? `.${mysql.escapeId(unique_id)}` : ''}`;
const sqlQueryResult = await this.sqlClient.runQuery(describeQuery);
if (sqlQueryResult.rows.length === 0) {
return [];
}
return sqlQueryResult.rows as Array<ModelDescribeAccuracy>;
}
/**
* Deletes this model.
* @param {string} name - Name of the model.
* @param {string} project - Project the model belongs to.
* @throws {MindsDB} - Something went wrong deleting the model.
*
*/
override async deleteModel(name: string, project: string): Promise<void> {
const deleteQuery = `DROP MODEL ${mysql.escapeId(project)}.${mysql.escapeId(
name
)}`;
const sqlQueryResult = await this.sqlClient.runQuery(deleteQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}
/**
* Queries this model for a single prediction. For batch predictions, use batchQuery.
* @param {string} name - Name of the model.
* @param {string} targetColumn - Column that the model predicts.
* @param {string} project - Project the model belongs to.
* @param {QueryOptions} options - Options to use when querying the model.
* @returns {Promise<ModelPrediction>} - The prediction result.
* @throws {MindsDbError} - Something went wrong querying the model.
*/
override async queryModel(
name: string,
version: number,
targetColumn: string,
project: string,
options: QueryOptions
): Promise<ModelPrediction> {
const selectClause = `SELECT * FROM ${mysql.escapeId(
project
)}.${mysql.escapeId(name)}.${version}`;
const whereClause = this.makeWhereClause(options['where'] || []);
const selectQuery = [selectClause, whereClause].join('\n');
const sqlQueryResult = await this.sqlClient.runQuery(selectQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
const predictionRow = sqlQueryResult.rows[0];
const prediction = {
value: predictionRow[targetColumn],
explain: predictionRow[`${targetColumn}_explain`],
data: predictionRow,
};
return prediction;
}
/**
* Queries this model for a batch prediction by joining with another data source.
* @param {string} name - Name of the model.
* @param {string} targetColumn - Column that the model predicts.
* @param {string} project - Project the model belongs to.
* @param {BatchQueryOptions} options - Options to use when querying the model.
* @returns {Promise<Array<ModelPrediction>>} - All prediction results from the batch query.
* @throws {MindsDbError} - Something went wrong querying the model.
*/
override async batchQueryModel(
name: string,
version: number,
targetColumn: string,
project: string,
options: BatchQueryOptions
): Promise<Array<ModelPrediction>> {
const selectClause = `SELECT m.${mysql.escapeId(
targetColumn
)} AS predicted, t.*, m.*`;
const joinId = options['join'];
const fromClause = `FROM ${mysql.escapeId(joinId)} AS t`;
const joinClause = `JOIN ${mysql.escapeId(project)}.${mysql.escapeId(
name
)}.${version} AS m`;
const whereClause = this.makeWhereClause(options['where'] || []);
const limitClause = options['limit']
? `LIMIT ${mysql.escape(options['limit'])}`
: '';
const selectQuery = [
selectClause,
fromClause,
joinClause,
whereClause,
limitClause,
].join('\n');
const sqlQueryResult = await this.sqlClient.runQuery(selectQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
return sqlQueryResult.rows.map((r) => ({
value: r['predicted'],
explain: r[`${targetColumn}_explain`],
data: r,
}));
}
/**
* Trains this model with the given options.
* @param {string} name - Name of the model.
* @param {string} targetColumn - Column for the model to predict.
* @param {string} project - Project the model belongs to.
* @param {TrainingOptions} options - Options to use when training the model.
* @throws {MindsDbError} - Something went wrong querying the model.
*/
override async trainModel(
name: string,
targetColumn: string,
project: string,
trainingOptions: TrainingOptions
): Promise<Model> {
const createClause = this.makeTrainingCreateClause(name, project);
const fromClause = this.makeTrainingFromClause(trainingOptions);
const selectClause = this.makeTrainingSelectClause(trainingOptions);
const predictClause = this.makeTrainingPredictClause(targetColumn);
const orderByClause = this.makeTrainingOrderByClause(trainingOptions);
const groupByClause = this.makeTrainingGroupByClause(trainingOptions);
const windowHorizonClause =
this.makeTrainingWindowHorizonClause(trainingOptions);
const usingClause = this.makeTrainingUsingClause(trainingOptions);
const query = [
createClause,
fromClause,
selectClause,
predictClause,
groupByClause,
orderByClause,
windowHorizonClause,
usingClause,
]
.filter((s) => s)
.join('\n');
const sqlQueryResult = await this.sqlClient.runQuery(query);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
return new Model(
this,
name,
project,
targetColumn,
'generating',
'up_to_date',
1
);
}
/**
* Rerains this model with the given options.
* @param {string} name - Name of the model.
* @param {string} targetColumn - Column for the model to predict.
* @param {string} project - Project the model belongs to.
* @param {string} [integration] - Integration name for the training data (e.g. mindsdb).
* @param {TrainingOptions} [options] - Options to use when retraining the model.
* @throws {MindsDbError} - Something went wrong querying the model.
*/
override async retrainModel(
name: string,
targetColumn: string,
project: string,
trainingOptions?: TrainingOptions
): Promise<void> {
const retrainClause = this.makeRetrainClause(name, project);
let query = retrainClause;
if (trainingOptions) {
const fromClause = this.makeTrainingFromClause(trainingOptions);
const selectClause = this.makeTrainingSelectClause(trainingOptions);
const predictClause = this.makeTrainingPredictClause(targetColumn);
const orderByClause = this.makeTrainingOrderByClause(trainingOptions);
const groupByClause = this.makeTrainingGroupByClause(trainingOptions);
const windowHorizonClause =
this.makeTrainingWindowHorizonClause(trainingOptions);
const usingClause = this.makeTrainingUsingClause(trainingOptions);
query = [
retrainClause,
fromClause,
selectClause,
predictClause,
groupByClause,
orderByClause,
windowHorizonClause,
usingClause,
]
.filter((s) => s)
.join('\n');
}
const sqlQueryResult = await this.sqlClient.runQuery(query);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}
/**
* Partially adjusts this model with the given options.
* @param {string} name - Name of the model.
* @param {string} project - Project the model belongs to.
* @param {AdjustOptions} options - Options to use when adjusting the model.
* @throws {MindsDbError} - Something went wrong querying the model.
*/
override async adjustModel(
name: string,
project: string,
adjustOptions: AdjustOptions
): Promise<void> {
const adjustClause = `ADJUST ${mysql.escapeId(project)}.${mysql.escapeId(
name
)} FROM ${mysql.escapeId(adjustOptions['integration'])}`;
const selectClause = this.makeTrainingSelectClause(adjustOptions);
const usingClause = this.makeTrainingUsingClause(adjustOptions);
const query = [adjustClause, selectClause, usingClause].join('\n');
const sqlQueryResult = await this.sqlClient.runQuery(query);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}
}