7

Event-driven predictions with Kogito

 2 years ago
source link: https://blog.kie.org/2022/01/event-driven-predictions-with-kogito.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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:

FieldPurposeMandatoryDefaultdataInput parametersyesidCloudEvent IDyeskogitopmmlfullresultBoolean flag to enable/disable receiving full PMML result as outputnofalsekogitopmmlmodelnameName of PMML model to evaluateyessourceCloudEvent sourceyesspecversionMust be equal to 1.0 as mandated by CloudEvent specificationyessubjectIf 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).nonulltypeMust be equal to PredictionRequestyes

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 returned
  • PredictionResponseFull 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 TypeMeaningBAD_REQUESTMalformed input event (e.g. when some mandatory fields are missing)MODEL_NOT_FOUNDThe 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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK