Skip to main content

AI Service

Purpose

The AI Service exposes one or more trained AI models as callable functions from JavaScript or Python scripts within a Workflow. Scripts access the service via the services pseudo-class — the same mechanism used for other services like JDBC or KVS. Each logical model name in the service maps to a dynamically generated function that can classify new data.

Prerequisites

  • One or more AI Model Resources defining the model type, input/output schema, and hyperparameters
  • One or more trained models stored in AI Storage (trained by an AI Trainer)
  • A Data Dictionary with attributes defined in the namespace configured in the service
tip

If you are not familiar with the AI workflow in layline.io, read Using Artificial Intelligence in Workflows first.

Configuration

Name & Description

Name — Name of the Asset. Spaces are not allowed.

Description — Free-text description of this service.

Required roles

If you are deploying to a Cluster with Reactive Engine Nodes that have specific Roles configured, you can restrict use of this Asset to those Nodes with matching roles. Leave empty to match all Nodes (no restriction).

AI Service Settings

Data dictionary namespace for the service parameters — The Data Dictionary namespace used to define the request and response parameters for this service. Supports macros for per-environment values.

AI Models

The AI Models table lists the models this service exposes. Each row maps a logical parameter name to an AI Model Resource and a trained model version in AI Storage.

Click + Add model to add a row:

ColumnDescription
Logical nameThe logical name for this model. This name is used as part of the function name in scripts.
ModelReference to an AI Model Resource in the Project. Select from the filtered list of AI Model Resources.
Trained modelThe path in AI Storage where the trained model is stored (e.g., models/usage-classifier). Append :<version> for a specific version (e.g., models/usage-classifier:3) or :latest for the most recent version.

AI Service Settings — data dictionary namespace and AI Models table

Behavior

Exposed functions

For each row in the AI Models table, the service dynamically generates one or more functions:

If a trained model path is set:

Function nameDescription
<LogicalName>ClassifyClassify a single record using the trained model

If an AI Model Resource is set (without a trained model path):

Function nameDescription
<LogicalName>BeginTrainingBegin a new training session
<LogicalName>LearnAdd training data to the session
<LogicalName>FinishTrainingComplete training and store the model in AI Storage

Invocation from scripts

The AI Service is accessed from JavaScript or Python scripts the same way as other services (JDBC, KVS, etc.) — via the services pseudo-class.

1. Link the service to a script processor:

In the JavaScript or Python Processor editor, add the AI Service to the processor's Service Assignments.

2. Access the service in your script:

// Access the AI Service via the services pseudo-class
const aiService = services.MyServiceName;

// Call the Classify function for a trained model
const result = await aiService.CreditScoreGermanClassify({
// Input attributes matching the AI Model Resource's input schema
call_type_ind: "VOICE",
rate_scenario_cd: "STANDARD",
primary_mcc_mnc: "26201"
});

// The result contains the predicted class label
processor.logInfo("Classification result: " + result.classLabel);

Model versioning

Each training run of the same model path creates a new version in AI Storage. Use :latest to always use the most recent version, or specify :<version-number> to pin to a known version.

Example

An AI Service exposes a trained credit scoring model for use by a script processor.

Step 1 — Prerequisites:

  • AI Model Resource: AI-Model-J48 (Weka J48, defines input attributes and class attribute)
  • Trained model in AI Storage: models/credit-score_german:latest
  • Data Dictionary namespace: CreditScore (defines the input/output attribute types)

Step 2 — Configure the AI Service:

SettingValue
NameCreditScoringService
Data dictionary namespaceCreditScore
Logical nameCreditScoreGerman
ModelAI-Model-J48
Trained modelmodels/credit-score_german:latest

This exposes the function CreditScoreGermanClassify(...) in scripts.

Step 3 — Link the service to a script processor:

In a JavaScript or Python Processor, add CreditScoringService to its Service Assignments.

Step 4 — Classify records:

export function onMessage(message) {
// Call the AI Classification function
const result = await services.CreditScoringService.CreditScoreGermanClassify({
call_type_ind: message.callType,
rate_scenario_cd: message.rateScenario,
primary_mcc_mnc: message.mccMnc
});

// Use the classification result
message.classification = result.classLabel;

stream.emit(message, OUTPUT_PORT);
}

Incremental training via BeginTraining / Learn / FinishTraining

If no trained model path is set in the AI Models table (only an AI Model Resource is configured), the service exposes three additional functions for online/incremental training. This is useful when training data arrives over time and you want to update the model without a full batch training run.

Step 1 — Configure for training:

SettingValue
NameCreditScoringService
Data dictionary namespaceCreditScore
Logical nameCreditScoreGerman
ModelAI-Model-J48
Trained model(leave empty — this enables the training functions)

This exposes the functions CreditScoreGermanBeginTraining(...), CreditScoreGermanLearn(...), and CreditScoreGermanFinishTraining(...).

Step 2 — Begin a training session:

export function onMessage(message) {
// Begin a new training session
const handle = await services.CreditScoringService.CreditScoreGermanBeginTraining({});
processor.logInfo("Training session started, handle: " + handle);
}

Step 3 — Add training data:

export function onMessage(message) {
// Add a training record to the session
await services.CreditScoringService.CreditScoreGermanLearn({
Handle: sessionHandle, // the handle returned by BeginTraining
Attribute: {
call_type_ind: message.callType,
rate_scenario_cd: message.rateScenario,
primary_mcc_mnc: message.mccMnc,
classLabel: message.actualClass // the known correct label
}
});
processor.logInfo("Training data added");
}

Step 4 — Finish training:

export function onStreamEnd() {
// Complete training and store the model in AI Storage
await services.CreditScoringService.CreditScoreGermanFinishTraining({
Handle: sessionHandle // the handle returned by BeginTraining
});
processor.logInfo("Model training complete, stored in AI Storage");
}

See Also


Can't find what you are looking for?

Please note, that the creation of the online documentation is Work-In-Progress. It is constantly being updated. should you have questions or suggestions, please don't hesitate to contact us at support@layline.io .