PredictiveModel class abstract interface Query Enterprise Edition
Interface for a machine learning model that can be used in a Query.
This feature is only available in the Enterprise Edition.
Before using a predictive model in a query, it must be registered using Prediction.registerModel. The model can be unregistered using Prediction.unregisterModel.
To use a predictive model in a query, use the query builder
Function_.prediction function or the SQL++ PREDICTION
function.
Example
The following example shows how to create and use a predictive model that converts a string to uppercase.
class UppercaseModel implements PredictiveModel {
@override
Dictionary? predict(Dictionary input) =>
MutableDictionary({'out': input.string('in')!.toUpperCase()});
}
Before using the model in a query, it must be registered:
Prediction.registerModel('uppercase', UppercaseModel());
Query Builder
final users = db.createCollection('users');
await users.saveDocument(MutableDocument({'name': 'Alice'}));
final query = const QueryBuilder()
.select(
SelectResult.expression(
Function_.prediction(
'uppercase',
Expression.dictionary({'in': Expression.property('name')}),
).property('out'),
).as('prediction'),
)
.from(DataSource.collection(users));
final resultSet = await query.execute();
final results = await resultSet.allPlainMapResults();
// results: [{"prediction": {"out": "ALICE"}}]
SQL++
final users = db.createCollection('users');
await users.saveDocument(MutableDocument({'name': 'Alice'}));
final query = await db.createQuery(
'''
SELECT PREDICTION(uppercase, {"in": name}, "out") AS prediction
FROM users
''',
);
final resultSet = await query.execute();
final results = await resultSet.allPlainMapResults();
// results: [{"prediction": "ALICE"}]
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
predict(
Dictionary input) → Dictionary? -
Invokes the model with the given
input
dictionary and returns the prediction result. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited