This blogpost introduces the event-driven predictions addon and is the third of my series of event-driven with Kogito posts, after the event-driven decisions and event-driven rules addons.
It is available since Kogito v1.12.0 and its behavior resembles what the previous two addons already do for decisions and rules.
Key concepts
The new addon enables the evaluation of PMML models in an event-driven fashion, so that it can be used as part of an event processing pipeline.
Like the other addons, it comes in two flavours: Quarkus and Spring Boot, and, in order to use it, the developer only needs to include the correct version as dependency of his Kogito app and configure it. The Kogito code-generation and framework specific CDI are then leveraged to do the wiring.
The execution is triggered upon receiving an event containing the input parameters in a specified Kafka topic. The result is then sent to a Kafka output topic (which may be the same). Both input and output events are formatted as CloudEvents.
It is implemented to behave like the REST endpoints: the model executor can be configured via a dedicated flag to either receive only the main output parameter(s) or the full PMML result in the output event.
Event structure
Input event
A model evaluation is triggered by a specific event called PredictionRequest
.
Here is the list of the supported field, including the optional ones:
Field | Purpose | Mandatory | Default |
---|---|---|---|
data | Input parameters | yes | |
id | CloudEvent ID | yes | |
kogitopmmlfullresult | Boolean flag to enable/disable receiving full PMML result as output | no | false |
kogitopmmlmodelname | Name of PMML model to evaluate | yes | |
source | CloudEvent source | yes | |
specversion | Must be equal to 1.0 as mandated by CloudEvent specification | yes | |
subject | If specified, the engine will put the same value as subject of the output event. Its usage is up to the caller (e.g. as correlation ID). | no | null |
type | Must be equal to PredictionRequest | yes |
Example of PredictionRequest event
{
"specversion": "1.0",
"id": "a89b61a2-5644-487a-8a86-144855c5dce8",
"source": "SomeEventSource",
"type": "PredictionRequest",
"subject": "TheSubject",
"kogitopmmlmodelname": "PredicatesMining",
"data": {
"residenceState": "AP",
"validLicense": true,
"occupation": "ASTRONAUT",
"categoricalY": "classA",
"categoricalX": "red",
"variable": 6.6,
"age": 25.0
}
}
Output events
If the request is evaluated successfully, the system returns two different types of output events depending on the value of the kogitopmmlfullresult
flag:
PredictionResponse
if only the main output parameters are returnedPredictionResponseFull
if the full PMML result is returned
The results are always in the data
field.
Example of PredictionResponse event
{
"specversion": "1.0",
"id": "d54ace84-6788-46b6-a359-b308f8b21778",
"source": "PredicatesMining",
"type": "PredictionResponse",
"subject": "TheSubject",
"kogitopmmlmodelname": "PredicatesMining",
"data": {
"categoricalResult": 1.381666666666666
}
}
Example of PredictionResponseFull event
{
"specversion": "1.0",
"id": "d54ace84-6788-46b6-a359-b308f8b21778",
"source": "PredicatesMining",
"type": "PredictionResponseFull",
"subject": "TheSubject",
"kogitopmmlmodelname": "PredicatesMining",
"data": {
"segmentIndex": 0,
"resultCode": "OK",
"resultObjectName": "categoricalResult",
"resultVariables": {
"categoricalResult": 1.381666666666666
}
}
}
Error events
If, for some reason, the request event is malformed or contains wrong information so that the evaluation can’t be triggered, a PredictionResponseError
is sent as output.
In this case the data
field contains a string that specifies the error type:
Error Type | Meaning |
BAD_REQUEST | Malformed input event (e.g. when some mandatory fields are missing) |
MODEL_NOT_FOUND | The specified PMML model can’t be found in the current service |
Examples
The Kogito Examples repository contains two examples, one for Quarkus and one for Spring Boot, that you can use as a starting point to practice with this addon.
They also contain tests for every possible variation in the structure of the input/output events supported by the addon in the src/test/resources/events
subfolder.
Conclusion
If you liked this article and are interested in the evolution of Kogito, stay tuned for more news!
Thanks for reading.