@memberjunction/predictive-studio 0.0.1 → 5.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +101 -28
- package/dist/actions/base-predictive-studio.action.d.ts +63 -0
- package/dist/actions/base-predictive-studio.action.d.ts.map +1 -0
- package/dist/actions/base-predictive-studio.action.js +125 -0
- package/dist/actions/base-predictive-studio.action.js.map +1 -0
- package/dist/actions/index.d.ts +38 -0
- package/dist/actions/index.d.ts.map +1 -0
- package/dist/actions/index.js +49 -0
- package/dist/actions/index.js.map +1 -0
- package/dist/actions/model-scoring-action-generator.d.ts +108 -0
- package/dist/actions/model-scoring-action-generator.d.ts.map +1 -0
- package/dist/actions/model-scoring-action-generator.js +316 -0
- package/dist/actions/model-scoring-action-generator.js.map +1 -0
- package/dist/actions/promote-model.action.d.ts +102 -0
- package/dist/actions/promote-model.action.d.ts.map +1 -0
- package/dist/actions/promote-model.action.js +132 -0
- package/dist/actions/promote-model.action.js.map +1 -0
- package/dist/actions/promote-model.gate.d.ts +77 -0
- package/dist/actions/promote-model.gate.d.ts.map +1 -0
- package/dist/actions/promote-model.gate.js +205 -0
- package/dist/actions/promote-model.gate.js.map +1 -0
- package/dist/actions/run-experiment.action.d.ts +60 -0
- package/dist/actions/run-experiment.action.d.ts.map +1 -0
- package/dist/actions/run-experiment.action.js +134 -0
- package/dist/actions/run-experiment.action.js.map +1 -0
- package/dist/actions/run-experiment.deps.d.ts +47 -0
- package/dist/actions/run-experiment.deps.d.ts.map +1 -0
- package/dist/actions/run-experiment.deps.js +71 -0
- package/dist/actions/run-experiment.deps.js.map +1 -0
- package/dist/actions/schedule-model-scoring.action.d.ts +71 -0
- package/dist/actions/schedule-model-scoring.action.d.ts.map +1 -0
- package/dist/actions/schedule-model-scoring.action.js +148 -0
- package/dist/actions/schedule-model-scoring.action.js.map +1 -0
- package/dist/actions/score-record-set.action.d.ts +133 -0
- package/dist/actions/score-record-set.action.d.ts.map +1 -0
- package/dist/actions/score-record-set.action.js +136 -0
- package/dist/actions/score-record-set.action.js.map +1 -0
- package/dist/actions/score-record-set.runner.d.ts +192 -0
- package/dist/actions/score-record-set.runner.d.ts.map +1 -0
- package/dist/actions/score-record-set.runner.js +416 -0
- package/dist/actions/score-record-set.runner.js.map +1 -0
- package/dist/actions/train-model.action.d.ts +57 -0
- package/dist/actions/train-model.action.d.ts.map +1 -0
- package/dist/actions/train-model.action.js +111 -0
- package/dist/actions/train-model.action.js.map +1 -0
- package/dist/agent/index.d.ts +14 -0
- package/dist/agent/index.d.ts.map +1 -0
- package/dist/agent/index.js +14 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent/model-dev-orchestrator-agent.d.ts +42 -0
- package/dist/agent/model-dev-orchestrator-agent.d.ts.map +1 -0
- package/dist/agent/model-dev-orchestrator-agent.js +76 -0
- package/dist/agent/model-dev-orchestrator-agent.js.map +1 -0
- package/dist/agent/modeling-plan-to-pipeline.d.ts +49 -0
- package/dist/agent/modeling-plan-to-pipeline.d.ts.map +1 -0
- package/dist/agent/modeling-plan-to-pipeline.js +101 -0
- package/dist/agent/modeling-plan-to-pipeline.js.map +1 -0
- package/dist/agent/pipeline-builder-agent.d.ts +58 -0
- package/dist/agent/pipeline-builder-agent.d.ts.map +1 -0
- package/dist/agent/pipeline-builder-agent.js +101 -0
- package/dist/agent/pipeline-builder-agent.js.map +1 -0
- package/dist/agent/pipeline-builder.d.ts +67 -0
- package/dist/agent/pipeline-builder.d.ts.map +1 -0
- package/dist/agent/pipeline-builder.js +114 -0
- package/dist/agent/pipeline-builder.js.map +1 -0
- package/dist/experiment/concurrency.d.ts +41 -0
- package/dist/experiment/concurrency.d.ts.map +1 -0
- package/dist/experiment/concurrency.js +54 -0
- package/dist/experiment/concurrency.js.map +1 -0
- package/dist/experiment/experiment-orchestrator.d.ts +136 -0
- package/dist/experiment/experiment-orchestrator.d.ts.map +1 -0
- package/dist/experiment/experiment-orchestrator.js +408 -0
- package/dist/experiment/experiment-orchestrator.js.map +1 -0
- package/dist/experiment/index.d.ts +17 -0
- package/dist/experiment/index.d.ts.map +1 -0
- package/dist/experiment/index.js +17 -0
- package/dist/experiment/index.js.map +1 -0
- package/dist/experiment/leaderboard.d.ts +50 -0
- package/dist/experiment/leaderboard.d.ts.map +1 -0
- package/dist/experiment/leaderboard.js +91 -0
- package/dist/experiment/leaderboard.js.map +1 -0
- package/dist/experiment/seams.d.ts +95 -0
- package/dist/experiment/seams.d.ts.map +1 -0
- package/dist/experiment/seams.js +126 -0
- package/dist/experiment/seams.js.map +1 -0
- package/dist/experiment/types.d.ts +228 -0
- package/dist/experiment/types.d.ts.map +1 -0
- package/dist/experiment/types.js +19 -0
- package/dist/experiment/types.js.map +1 -0
- package/dist/experiment/wave-strategist.d.ts +31 -0
- package/dist/experiment/wave-strategist.d.ts.map +1 -0
- package/dist/experiment/wave-strategist.js +50 -0
- package/dist/experiment/wave-strategist.js.map +1 -0
- package/dist/feature-assembly/as-of.d.ts +74 -0
- package/dist/feature-assembly/as-of.d.ts.map +1 -0
- package/dist/feature-assembly/as-of.js +136 -0
- package/dist/feature-assembly/as-of.js.map +1 -0
- package/dist/feature-assembly/data-access.d.ts +114 -0
- package/dist/feature-assembly/data-access.d.ts.map +1 -0
- package/dist/feature-assembly/data-access.js +68 -0
- package/dist/feature-assembly/data-access.js.map +1 -0
- package/dist/feature-assembly/feature-assembly-executor.d.ts +273 -0
- package/dist/feature-assembly/feature-assembly-executor.d.ts.map +1 -0
- package/dist/feature-assembly/feature-assembly-executor.js +521 -0
- package/dist/feature-assembly/feature-assembly-executor.js.map +1 -0
- package/dist/feature-assembly/index.d.ts +13 -0
- package/dist/feature-assembly/index.d.ts.map +1 -0
- package/dist/feature-assembly/index.js +13 -0
- package/dist/feature-assembly/index.js.map +1 -0
- package/dist/feature-assembly/leakage-guard.d.ts +86 -0
- package/dist/feature-assembly/leakage-guard.d.ts.map +1 -0
- package/dist/feature-assembly/leakage-guard.js +111 -0
- package/dist/feature-assembly/leakage-guard.js.map +1 -0
- package/dist/feature-assembly/vision-llm.d.ts +125 -0
- package/dist/feature-assembly/vision-llm.d.ts.map +1 -0
- package/dist/feature-assembly/vision-llm.js +211 -0
- package/dist/feature-assembly/vision-llm.js.map +1 -0
- package/dist/feature-pipelines/feature-pipeline-engine.d.ts +159 -0
- package/dist/feature-pipelines/feature-pipeline-engine.d.ts.map +1 -0
- package/dist/feature-pipelines/feature-pipeline-engine.js +298 -0
- package/dist/feature-pipelines/feature-pipeline-engine.js.map +1 -0
- package/dist/feature-pipelines/index.d.ts +13 -0
- package/dist/feature-pipelines/index.d.ts.map +1 -0
- package/dist/feature-pipelines/index.js +13 -0
- package/dist/feature-pipelines/index.js.map +1 -0
- package/dist/feature-pipelines/types.d.ts +72 -0
- package/dist/feature-pipelines/types.d.ts.map +1 -0
- package/dist/feature-pipelines/types.js +17 -0
- package/dist/feature-pipelines/types.js.map +1 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +80 -0
- package/dist/index.js.map +1 -0
- package/dist/maintenance/index.d.ts +19 -0
- package/dist/maintenance/index.d.ts.map +1 -0
- package/dist/maintenance/index.js +19 -0
- package/dist/maintenance/index.js.map +1 -0
- package/dist/maintenance/maintenance-engine.d.ts +152 -0
- package/dist/maintenance/maintenance-engine.d.ts.map +1 -0
- package/dist/maintenance/maintenance-engine.js +412 -0
- package/dist/maintenance/maintenance-engine.js.map +1 -0
- package/dist/maintenance/metrics.d.ts +37 -0
- package/dist/maintenance/metrics.d.ts.map +1 -0
- package/dist/maintenance/metrics.js +85 -0
- package/dist/maintenance/metrics.js.map +1 -0
- package/dist/maintenance/seams.d.ts +85 -0
- package/dist/maintenance/seams.d.ts.map +1 -0
- package/dist/maintenance/seams.js +166 -0
- package/dist/maintenance/seams.js.map +1 -0
- package/dist/maintenance/types.d.ts +318 -0
- package/dist/maintenance/types.d.ts.map +1 -0
- package/dist/maintenance/types.js +27 -0
- package/dist/maintenance/types.js.map +1 -0
- package/dist/operations/__tests__/helpers.d.ts +30 -0
- package/dist/operations/__tests__/helpers.d.ts.map +1 -0
- package/dist/operations/__tests__/helpers.js +44 -0
- package/dist/operations/__tests__/helpers.js.map +1 -0
- package/dist/operations/control-experiment-session.operation.d.ts +33 -0
- package/dist/operations/control-experiment-session.operation.d.ts.map +1 -0
- package/dist/operations/control-experiment-session.operation.js +71 -0
- package/dist/operations/control-experiment-session.operation.js.map +1 -0
- package/dist/operations/create-scoring-process.operation.d.ts +30 -0
- package/dist/operations/create-scoring-process.operation.d.ts.map +1 -0
- package/dist/operations/create-scoring-process.operation.js +65 -0
- package/dist/operations/create-scoring-process.operation.js.map +1 -0
- package/dist/operations/delegation.d.ts +113 -0
- package/dist/operations/delegation.d.ts.map +1 -0
- package/dist/operations/delegation.js +139 -0
- package/dist/operations/delegation.js.map +1 -0
- package/dist/operations/index.d.ts +40 -0
- package/dist/operations/index.d.ts.map +1 -0
- package/dist/operations/index.js +55 -0
- package/dist/operations/index.js.map +1 -0
- package/dist/operations/promote-model.operation.d.ts +57 -0
- package/dist/operations/promote-model.operation.d.ts.map +1 -0
- package/dist/operations/promote-model.operation.js +113 -0
- package/dist/operations/promote-model.operation.js.map +1 -0
- package/dist/operations/run-feature-pipeline.operation.d.ts +56 -0
- package/dist/operations/run-feature-pipeline.operation.d.ts.map +1 -0
- package/dist/operations/run-feature-pipeline.operation.js +94 -0
- package/dist/operations/run-feature-pipeline.operation.js.map +1 -0
- package/dist/operations/score-record-set.operation.d.ts +47 -0
- package/dist/operations/score-record-set.operation.d.ts.map +1 -0
- package/dist/operations/score-record-set.operation.js +113 -0
- package/dist/operations/score-record-set.operation.js.map +1 -0
- package/dist/operations/start-experiment-session.operation.d.ts +66 -0
- package/dist/operations/start-experiment-session.operation.d.ts.map +1 -0
- package/dist/operations/start-experiment-session.operation.js +132 -0
- package/dist/operations/start-experiment-session.operation.js.map +1 -0
- package/dist/operations/train-model.operation.d.ts +50 -0
- package/dist/operations/train-model.operation.d.ts.map +1 -0
- package/dist/operations/train-model.operation.js +114 -0
- package/dist/operations/train-model.operation.js.map +1 -0
- package/dist/scheduling/index.d.ts +13 -0
- package/dist/scheduling/index.d.ts.map +1 -0
- package/dist/scheduling/index.js +13 -0
- package/dist/scheduling/index.js.map +1 -0
- package/dist/scheduling/scheduled-model-scoring.d.ts +169 -0
- package/dist/scheduling/scheduled-model-scoring.d.ts.map +1 -0
- package/dist/scheduling/scheduled-model-scoring.js +189 -0
- package/dist/scheduling/scheduled-model-scoring.js.map +1 -0
- package/dist/scoring/artifact-loader.d.ts +65 -0
- package/dist/scoring/artifact-loader.d.ts.map +1 -0
- package/dist/scoring/artifact-loader.js +97 -0
- package/dist/scoring/artifact-loader.js.map +1 -0
- package/dist/scoring/index.d.ts +26 -0
- package/dist/scoring/index.d.ts.map +1 -0
- package/dist/scoring/index.js +26 -0
- package/dist/scoring/index.js.map +1 -0
- package/dist/scoring/ml-model-inference-processor.d.ts +154 -0
- package/dist/scoring/ml-model-inference-processor.d.ts.map +1 -0
- package/dist/scoring/ml-model-inference-processor.js +320 -0
- package/dist/scoring/ml-model-inference-processor.js.map +1 -0
- package/dist/scoring/ml-model-score-enricher.d.ts +80 -0
- package/dist/scoring/ml-model-score-enricher.d.ts.map +1 -0
- package/dist/scoring/ml-model-score-enricher.js +139 -0
- package/dist/scoring/ml-model-score-enricher.js.map +1 -0
- package/dist/scoring/register.d.ts +81 -0
- package/dist/scoring/register.d.ts.map +1 -0
- package/dist/scoring/register.js +105 -0
- package/dist/scoring/register.js.map +1 -0
- package/dist/scoring/scoring-binding.d.ts +72 -0
- package/dist/scoring/scoring-binding.d.ts.map +1 -0
- package/dist/scoring/scoring-binding.js +76 -0
- package/dist/scoring/scoring-binding.js.map +1 -0
- package/dist/scoring/scoring-process-shared.d.ts +105 -0
- package/dist/scoring/scoring-process-shared.d.ts.map +1 -0
- package/dist/scoring/scoring-process-shared.js +141 -0
- package/dist/scoring/scoring-process-shared.js.map +1 -0
- package/dist/scoring/scoring-process.d.ts +128 -0
- package/dist/scoring/scoring-process.d.ts.map +1 -0
- package/dist/scoring/scoring-process.js +146 -0
- package/dist/scoring/scoring-process.js.map +1 -0
- package/dist/scoring/seams.d.ts +40 -0
- package/dist/scoring/seams.d.ts.map +1 -0
- package/dist/scoring/seams.js +66 -0
- package/dist/scoring/seams.js.map +1 -0
- package/dist/scoring/startup-register.d.ts +49 -0
- package/dist/scoring/startup-register.d.ts.map +1 -0
- package/dist/scoring/startup-register.js +72 -0
- package/dist/scoring/startup-register.js.map +1 -0
- package/dist/scoring/types.d.ts +134 -0
- package/dist/scoring/types.d.ts.map +1 -0
- package/dist/scoring/types.js +26 -0
- package/dist/scoring/types.js.map +1 -0
- package/dist/training/artifact-store.d.ts +124 -0
- package/dist/training/artifact-store.d.ts.map +1 -0
- package/dist/training/artifact-store.js +163 -0
- package/dist/training/artifact-store.js.map +1 -0
- package/dist/training/index.d.ts +13 -0
- package/dist/training/index.d.ts.map +1 -0
- package/dist/training/index.js +13 -0
- package/dist/training/index.js.map +1 -0
- package/dist/training/seams.d.ts +57 -0
- package/dist/training/seams.d.ts.map +1 -0
- package/dist/training/seams.js +103 -0
- package/dist/training/seams.js.map +1 -0
- package/dist/training/training-engine.d.ts +96 -0
- package/dist/training/training-engine.d.ts.map +1 -0
- package/dist/training/training-engine.js +300 -0
- package/dist/training/training-engine.js.map +1 -0
- package/dist/training/types.d.ts +159 -0
- package/dist/training/types.d.ts.map +1 -0
- package/dist/training/types.js +16 -0
- package/dist/training/types.js.map +1 -0
- package/package.json +44 -7
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module scoring/ml-model-inference-processor
|
|
3
|
+
*
|
|
4
|
+
* The **MLModelInferenceProcessor** — Predictive Studio's **scoring** work type
|
|
5
|
+
* (plan §10). It is a Record Set Processing processor (implements
|
|
6
|
+
* `IRecordProcessor` from `@memberjunction/record-set-processor-base`, the same
|
|
7
|
+
* contract as the `InferProcessor` / `ActionRecordProcessor` /
|
|
8
|
+
* `AgentRecordProcessor` siblings) so it composes onto the existing batching /
|
|
9
|
+
* concurrency / rate-limit / budget / pause-resume / audit substrate.
|
|
10
|
+
*
|
|
11
|
+
* ## Per-record flow (single + batch)
|
|
12
|
+
* 1. **Resolve + warm-load the model** ({@link MLModelInferenceProcessor.ensureModel}).
|
|
13
|
+
* The `MJ: ML Models` row (its `ArtifactFileID`, `FittedPreprocessing`,
|
|
14
|
+
* `FeatureSchema`, `ProblemType`) + its pipeline assembly config are loaded
|
|
15
|
+
* **once per processor run** and reused across every record (the Python
|
|
16
|
+
* sidecar also warm-caches by model id — §3.1). The serialized artifact is
|
|
17
|
+
* fetched once via the injected {@link IArtifactLoader}.
|
|
18
|
+
* 2. **Assemble features** via the SAME {@link FeatureAssemblyExecutor} as
|
|
19
|
+
* training, in the **on-demand / transform-only** context, using the model's
|
|
20
|
+
* FROZEN `FeatureSchema` + `FittedPreprocessing` (never re-fit — the anti-skew
|
|
21
|
+
* guarantee, §6.2). The matrix shape matches what the model expects.
|
|
22
|
+
* 3. **Call the sidecar `/predict`** with the artifact + frozen preprocessing +
|
|
23
|
+
* feature schema + the assembled row(s) → predictions (score + class).
|
|
24
|
+
* 4. **Produce the per-record `RecordResult`** carrying the prediction.
|
|
25
|
+
* **Ephemeral by default**; when the Record Process has an `OutputMapping`,
|
|
26
|
+
* `RecordProcessExecutor` wraps this processor in the shared `WriteBackProcessor`
|
|
27
|
+
* (exactly as it does for the Action/Agent/Infer siblings) so the score lands
|
|
28
|
+
* as a sortable/filterable column or child record. The write-back is NOT done
|
|
29
|
+
* here — keeping it in the wrapper is how all work types share one declarative
|
|
30
|
+
* write-back.
|
|
31
|
+
*
|
|
32
|
+
* ## Registration (work-type key)
|
|
33
|
+
* Registered on the MJGlobal ClassFactory via
|
|
34
|
+
* `@RegisterClass(MLModelInferenceProcessor, 'ML Model')` (alias `'MLModelInference'`
|
|
35
|
+
* via {@link registerMLInferenceWorkType}). This establishes the clear work-type
|
|
36
|
+
* key and a static code path the bundler cannot tree-shake — WITHOUT modifying
|
|
37
|
+
* the `record-set-processor` substrate (its base package must not depend on
|
|
38
|
+
* Predictive Studio). A future `RecordProcessExecutor` extension resolves this
|
|
39
|
+
* processor dynamically by work-type key via {@link resolveMLInferenceProcessor}.
|
|
40
|
+
*/
|
|
41
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
42
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
43
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
44
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
45
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
46
|
+
};
|
|
47
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
48
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
49
|
+
};
|
|
50
|
+
import { RegisterClass } from '@memberjunction/global';
|
|
51
|
+
import { LogError } from '@memberjunction/core';
|
|
52
|
+
import { FeatureAssemblyExecutor } from '../feature-assembly/index.js';
|
|
53
|
+
/** Stable work-type keys this processor is registered under on the ClassFactory. */
|
|
54
|
+
export const ML_INFERENCE_WORK_TYPE = 'ML Model';
|
|
55
|
+
/** Alias work-type key (no spaces) for callers that prefer an identifier-style key. */
|
|
56
|
+
export const ML_INFERENCE_WORK_TYPE_ALIAS = 'MLModelInference';
|
|
57
|
+
/**
|
|
58
|
+
* Record Set Processing **scoring** processor. Stateless across records except
|
|
59
|
+
* for a warm, lazily-loaded model cache (artifact + frozen contract) shared
|
|
60
|
+
* across a batch. Construct one per Record Process run.
|
|
61
|
+
*/
|
|
62
|
+
let MLModelInferenceProcessor = class MLModelInferenceProcessor {
|
|
63
|
+
/**
|
|
64
|
+
* @param options model id + injected seams (+ optional pk field / dated sources)
|
|
65
|
+
* @param assembler optional FeatureAssembly executor override (tests may inject one)
|
|
66
|
+
*/
|
|
67
|
+
constructor(options, assembler) {
|
|
68
|
+
/** Warm model cache — loaded once on first record, reused across the batch. */
|
|
69
|
+
this.loadedModel = null;
|
|
70
|
+
/** Memoized in-flight load so concurrent records don't double-load the artifact. */
|
|
71
|
+
this.loadPromise = null;
|
|
72
|
+
this.modelId = options.modelId;
|
|
73
|
+
this.deps = options.deps;
|
|
74
|
+
this.primaryKeyField = options.primaryKeyField ?? 'ID';
|
|
75
|
+
this.datedSources = options.datedSources;
|
|
76
|
+
this.assembler = assembler ?? new FeatureAssemblyExecutor();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Score one record. Warm-loads the model on first call, assembles the single
|
|
80
|
+
* record's features (transform-only), calls the sidecar, and returns the
|
|
81
|
+
* prediction as the record's `ResultPayload`. Write-back, if configured, is
|
|
82
|
+
* applied by the `WriteBackProcessor` wrapper — not here.
|
|
83
|
+
*
|
|
84
|
+
* @param record the record to score
|
|
85
|
+
* @param context the per-record execution context (user + provider)
|
|
86
|
+
*/
|
|
87
|
+
async ProcessRecord(record, context) {
|
|
88
|
+
try {
|
|
89
|
+
const model = await this.ensureModel(context);
|
|
90
|
+
const row = this.recordToPlain(record);
|
|
91
|
+
const assembly = await this.assembleFeatures(model, [row], context);
|
|
92
|
+
const response = await this.callSidecar(model, assembly);
|
|
93
|
+
const prediction = response.predictions[0];
|
|
94
|
+
if (!prediction) {
|
|
95
|
+
return { Status: 'Failed', ErrorMessage: 'Sidecar returned no prediction for the record' };
|
|
96
|
+
}
|
|
97
|
+
return { Status: 'Succeeded', ResultPayload: this.toPayload(model, prediction) };
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
101
|
+
LogError(`MLModelInferenceProcessor: scoring failed for record '${record.RecordID}': ${message}`);
|
|
102
|
+
return { Status: 'Failed', ErrorMessage: message };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Batch-score many records in one sidecar round-trip. The warm model is loaded
|
|
107
|
+
* once; features for all rows are assembled together (transform-only), then a
|
|
108
|
+
* single `/predict` carries every row. Returns one {@link RecordResult} per
|
|
109
|
+
* input record, positionally aligned. Useful for bulk / materialization paths;
|
|
110
|
+
* the per-record `WriteBackProcessor` wrapper still owns write-back when the
|
|
111
|
+
* engine drives records individually.
|
|
112
|
+
*
|
|
113
|
+
* @param records the records to score (each must carry/resolve a row of data)
|
|
114
|
+
* @param context the execution context (user + provider)
|
|
115
|
+
*/
|
|
116
|
+
async ProcessBatch(records, context) {
|
|
117
|
+
if (records.length === 0) {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
const model = await this.ensureModel(context);
|
|
122
|
+
const rows = records.map((r) => this.recordToPlain(r));
|
|
123
|
+
const assembly = await this.assembleFeatures(model, rows, context);
|
|
124
|
+
const response = await this.callSidecar(model, assembly);
|
|
125
|
+
return records.map((_r, i) => {
|
|
126
|
+
const prediction = response.predictions[i];
|
|
127
|
+
return prediction
|
|
128
|
+
? { Status: 'Succeeded', ResultPayload: this.toPayload(model, prediction) }
|
|
129
|
+
: { Status: 'Failed', ErrorMessage: 'Sidecar returned no prediction for the record' };
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
134
|
+
LogError(`MLModelInferenceProcessor: batch scoring failed: ${message}`);
|
|
135
|
+
return records.map(() => ({ Status: 'Failed', ErrorMessage: message }));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// region: warm model load -----------------------------------------------------
|
|
139
|
+
/**
|
|
140
|
+
* Load the model + artifact ONCE and cache it for the run (§3.1 warm cache).
|
|
141
|
+
* Concurrent records share a single in-flight load via {@link loadPromise} so
|
|
142
|
+
* the artifact is fetched exactly once even under bounded concurrency.
|
|
143
|
+
*/
|
|
144
|
+
async ensureModel(context) {
|
|
145
|
+
if (this.loadedModel) {
|
|
146
|
+
return this.loadedModel;
|
|
147
|
+
}
|
|
148
|
+
if (!this.loadPromise) {
|
|
149
|
+
this.loadPromise = this.loadModel(context);
|
|
150
|
+
}
|
|
151
|
+
this.loadedModel = await this.loadPromise;
|
|
152
|
+
return this.loadedModel;
|
|
153
|
+
}
|
|
154
|
+
/** Resolve the `MJ: ML Models` row + its frozen contract + the artifact bytes. */
|
|
155
|
+
async loadModel(context) {
|
|
156
|
+
const model = await this.deps.modelLoader.loadModel(this.modelId, context.contextUser, context.provider);
|
|
157
|
+
if (!model) {
|
|
158
|
+
throw new Error(`MLModelInferenceProcessor: ML Model '${this.modelId}' not found`);
|
|
159
|
+
}
|
|
160
|
+
if (!model.ArtifactFileID) {
|
|
161
|
+
throw new Error(`MLModelInferenceProcessor: ML Model '${this.modelId}' has no ArtifactFileID`);
|
|
162
|
+
}
|
|
163
|
+
const bytes = await this.deps.artifactLoader.load(model.ArtifactFileID, context.contextUser);
|
|
164
|
+
if (!bytes) {
|
|
165
|
+
throw new Error(`MLModelInferenceProcessor: artifact '${model.ArtifactFileID}' not found for model '${this.modelId}'`);
|
|
166
|
+
}
|
|
167
|
+
return this.buildLoadedModel(model, bytes);
|
|
168
|
+
}
|
|
169
|
+
/** Assemble the frozen inference contract from a loaded model row + artifact bytes. */
|
|
170
|
+
buildLoadedModel(model, bytes) {
|
|
171
|
+
const pipeline = this.resolvePipelineConfig(model);
|
|
172
|
+
return {
|
|
173
|
+
modelId: model.ID,
|
|
174
|
+
targetEntityName: pipeline.targetEntityName,
|
|
175
|
+
artifactB64: encodeArtifact(bytes),
|
|
176
|
+
fittedPreprocessing: parseJson(model.FittedPreprocessing, {}),
|
|
177
|
+
featureSchema: parseJson(model.FeatureSchema, []),
|
|
178
|
+
problemType: model.ProblemType ?? 'classification',
|
|
179
|
+
targetVariable: model.TargetVariable,
|
|
180
|
+
sourceBindings: pipeline.sourceBindings,
|
|
181
|
+
featureSteps: pipeline.featureSteps,
|
|
182
|
+
asOf: pipeline.asOf,
|
|
183
|
+
leakageGuard: pipeline.leakageGuard,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Resolve the assembly config (sources / steps / as-of / leakage / target
|
|
188
|
+
* entity) the model's features were built from. Read off the loaded model's
|
|
189
|
+
* `Pipeline` relationship when present; otherwise fall back to the model's own
|
|
190
|
+
* frozen lineage so scoring stays self-contained even without the pipeline row.
|
|
191
|
+
*/
|
|
192
|
+
resolvePipelineConfig(model) {
|
|
193
|
+
const lineage = parseJson(model.Lineage, {});
|
|
194
|
+
return {
|
|
195
|
+
// The target entity the model scores is the training-unit entity from lineage.
|
|
196
|
+
targetEntityName: typeof lineage.targetEntityName === 'string' ? lineage.targetEntityName : (model.Pipeline ?? ''),
|
|
197
|
+
sourceBindings: Array.isArray(lineage.sourceBindings) ? lineage.sourceBindings : [],
|
|
198
|
+
featureSteps: isFeatureStepGraph(lineage.featureSteps) ? lineage.featureSteps : { Steps: [] },
|
|
199
|
+
asOf: isAsOfStrategy(lineage.asOfStrategy) ? lineage.asOfStrategy : { Mode: 'none' },
|
|
200
|
+
// Scoring never re-fits or re-evaluates leakage — a permissive guard is fine here;
|
|
201
|
+
// the frozen FeatureSchema is the contract that bounds which columns are produced.
|
|
202
|
+
leakageGuard: { DenyFields: [], SingleFeatureDominanceThreshold: 1 },
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
// region: feature assembly (transform-only) -----------------------------------
|
|
206
|
+
/**
|
|
207
|
+
* Assemble the raw feature matrix for the given rows via the shared
|
|
208
|
+
* FeatureAssembly executor in the **on-demand / transform-only** context (§6.1).
|
|
209
|
+
* The model's frozen schema is the contract; preprocessing is NOT applied here
|
|
210
|
+
* (the sidecar applies the frozen `FittedPreprocessing`).
|
|
211
|
+
*/
|
|
212
|
+
async assembleFeatures(model, rows, context) {
|
|
213
|
+
return this.assembler.assemble({
|
|
214
|
+
targetEntityName: model.targetEntityName,
|
|
215
|
+
records: rows,
|
|
216
|
+
sources: model.sourceBindings,
|
|
217
|
+
steps: model.featureSteps,
|
|
218
|
+
asOf: model.asOf,
|
|
219
|
+
leakageGuard: model.leakageGuard,
|
|
220
|
+
datedSources: this.datedSources,
|
|
221
|
+
primaryKeyField: this.primaryKeyField,
|
|
222
|
+
// No targetVariable at score time — the label is what we're predicting.
|
|
223
|
+
context: 'on-demand',
|
|
224
|
+
contextUser: context.contextUser,
|
|
225
|
+
provider: context.provider,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
// region: sidecar predict -----------------------------------------------------
|
|
229
|
+
/**
|
|
230
|
+
* Build the `/predict` request from the model's frozen contract + the assembled
|
|
231
|
+
* matrix and call the sidecar. The matrix's row-arrays are mapped to the
|
|
232
|
+
* feature-name → value objects `/predict` expects, using the MODEL's frozen
|
|
233
|
+
* feature schema order (the inference input contract).
|
|
234
|
+
*/
|
|
235
|
+
async callSidecar(model, assembly) {
|
|
236
|
+
const req = {
|
|
237
|
+
artifact_b64: model.artifactB64,
|
|
238
|
+
fitted_preprocessing: model.fittedPreprocessing,
|
|
239
|
+
feature_schema: model.featureSchema,
|
|
240
|
+
rows: matrixToFeatureRows(assembly.matrix, model.featureSchema),
|
|
241
|
+
};
|
|
242
|
+
return this.deps.sidecar.predict(req);
|
|
243
|
+
}
|
|
244
|
+
// region: result shaping ------------------------------------------------------
|
|
245
|
+
/** Shape a single sidecar prediction into the record result payload. */
|
|
246
|
+
toPayload(model, prediction) {
|
|
247
|
+
return {
|
|
248
|
+
modelId: model.modelId,
|
|
249
|
+
target: model.targetVariable,
|
|
250
|
+
problemType: model.problemType,
|
|
251
|
+
score: prediction.score,
|
|
252
|
+
class: prediction.class,
|
|
253
|
+
// Stamp when the prediction was produced so write-back can persist a "last scored at"
|
|
254
|
+
// column via OutputMapping (`{ "<YourColumn>": "$.scoredAt" }`). ISO-8601 UTC.
|
|
255
|
+
scoredAt: new Date().toISOString(),
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
// region: record helpers ------------------------------------------------------
|
|
259
|
+
/** Extract a plain field map from the record (BaseEntity → GetAll(), plain object → as-is). */
|
|
260
|
+
recordToPlain(record) {
|
|
261
|
+
const r = record.Record;
|
|
262
|
+
if (r && typeof r.GetAll === 'function') {
|
|
263
|
+
return r.GetAll();
|
|
264
|
+
}
|
|
265
|
+
if (r && typeof r === 'object') {
|
|
266
|
+
return r;
|
|
267
|
+
}
|
|
268
|
+
// No preloaded row — surface the ids so assembly can at least key the record.
|
|
269
|
+
return { [this.primaryKeyField]: record.RecordID };
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
MLModelInferenceProcessor = __decorate([
|
|
273
|
+
RegisterClass(MLModelInferenceProcessor, ML_INFERENCE_WORK_TYPE),
|
|
274
|
+
__metadata("design:paramtypes", [Object, FeatureAssemblyExecutor])
|
|
275
|
+
], MLModelInferenceProcessor);
|
|
276
|
+
export { MLModelInferenceProcessor };
|
|
277
|
+
/**
|
|
278
|
+
* Map a row-major {@link MatrixData} into the feature-name → value objects the
|
|
279
|
+
* sidecar `/predict` expects, in the model's FROZEN feature-schema order. Columns
|
|
280
|
+
* present in the matrix but not in the schema are dropped; schema columns missing
|
|
281
|
+
* from the matrix are emitted as `null` (the sidecar imputes via fitted params).
|
|
282
|
+
*/
|
|
283
|
+
export function matrixToFeatureRows(matrix, featureSchema) {
|
|
284
|
+
const colIndex = new Map();
|
|
285
|
+
matrix.columns.forEach((c, i) => colIndex.set(c, i));
|
|
286
|
+
const schemaNames = featureSchema.map((f) => f.Name);
|
|
287
|
+
return matrix.rows.map((row) => {
|
|
288
|
+
const obj = {};
|
|
289
|
+
for (const name of schemaNames) {
|
|
290
|
+
const idx = colIndex.get(name);
|
|
291
|
+
obj[name] = idx != null ? row[idx] : null;
|
|
292
|
+
}
|
|
293
|
+
return obj;
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
/** Parse a possibly-null JSON column, falling back to a default on null/parse error. */
|
|
297
|
+
function parseJson(raw, fallback) {
|
|
298
|
+
if (raw == null || raw.trim().length === 0) {
|
|
299
|
+
return fallback;
|
|
300
|
+
}
|
|
301
|
+
try {
|
|
302
|
+
return JSON.parse(raw);
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
return fallback;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/** Encode raw artifact bytes to base64 for the sidecar `PredictRequest`. */
|
|
309
|
+
function encodeArtifact(bytes) {
|
|
310
|
+
return Buffer.from(bytes).toString('base64');
|
|
311
|
+
}
|
|
312
|
+
/** Narrow an unknown lineage value to a {@link FeatureStepGraph}. */
|
|
313
|
+
function isFeatureStepGraph(value) {
|
|
314
|
+
return !!value && typeof value === 'object' && Array.isArray(value.Steps);
|
|
315
|
+
}
|
|
316
|
+
/** Narrow an unknown lineage value to an {@link AsOfStrategy}. */
|
|
317
|
+
function isAsOfStrategy(value) {
|
|
318
|
+
return !!value && typeof value === 'object' && typeof value.Mode === 'string';
|
|
319
|
+
}
|
|
320
|
+
//# sourceMappingURL=ml-model-inference-processor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ml-model-inference-processor.js","sourceRoot":"","sources":["../../src/scoring/ml-model-inference-processor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;;;;;;;;;;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAwBhD,OAAO,EAAE,uBAAuB,EAAoD,MAAM,qBAAqB,CAAC;AAQhH,oFAAoF;AACpF,MAAM,CAAC,MAAM,sBAAsB,GAAG,UAAU,CAAC;AACjD,uFAAuF;AACvF,MAAM,CAAC,MAAM,4BAA4B,GAAG,kBAAkB,CAAC;AAE/D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AC;IAAC,MAAM,CAAC;QACP,OAAO,QAAQ,CAAC;IAClB,CAAC;AACH,CAAC;AAED,4EAA4E;AAC5E,SAAS,cAAc,CAAC,KAAiB;IACvC,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAC/C,CAAC;AAED,qEAAqE;AACrE,SAAS,kBAAkB,CAAC,KAAc;IACxC,OAAO,CAAC,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAE,KAA6B,CAAC,KAAK,CAAC,CAAC;AACrG,CAAC;AAED,kEAAkE;AAClE,SAAS,cAAc,CAAC,KAAc;IACpC,OAAO,CAAC,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAQ,KAA4B,CAAC,IAAI,KAAK,QAAQ,CAAC;AACxG,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module scoring/ml-model-score-enricher
|
|
3
|
+
*
|
|
4
|
+
* **PS2-3 — scored-query enrichment.** A {@link QueryResultEnricherBase} that scores a
|
|
5
|
+
* RunQuery result set with a trained ML model and appends the prediction as a new
|
|
6
|
+
* column on each row. It is the Predictive-Studio half of the decoupled enrichment
|
|
7
|
+
* seam: MJCore's `RunQuery` resolves an enricher by key (`@RegisterClass`) at runtime
|
|
8
|
+
* and never depends on Predictive Studio (mirrors how the `'ML Model'` Record Set
|
|
9
|
+
* Processing work type stays decoupled via the registry).
|
|
10
|
+
*
|
|
11
|
+
* The model determines the target entity (the processor assembles features against the
|
|
12
|
+
* model's `targetEntityName`), so the enrichment only needs each row's PRIMARY KEY — it
|
|
13
|
+
* passes those keys as the scorer's `records` scope, scores WITHOUT write-back so the
|
|
14
|
+
* predictions are ephemeral, then joins them back onto the rows by record id.
|
|
15
|
+
*
|
|
16
|
+
* Resilience: the enricher degrades gracefully (logs + returns the input rows unchanged)
|
|
17
|
+
* for recoverable conditions — missing config, no primary keys — and RunQuery additionally
|
|
18
|
+
* guards the call, so a scoring problem can never break the underlying query.
|
|
19
|
+
*/
|
|
20
|
+
import { QueryResultEnricherBase, type IMetadataProvider, type QueryInfo, type UserInfo } from '@memberjunction/core';
|
|
21
|
+
import type { EphemeralPrediction, IScoreRecordSetRunner } from '../actions/score-record-set.action.js';
|
|
22
|
+
/** The ClassFactory key the enricher registers under — the `EnricherKey` callers pass on `RunQueryParams.Enrichment`. */
|
|
23
|
+
export declare const ML_MODEL_SCORE_ENRICHER_KEY = "ML Model Score";
|
|
24
|
+
/**
|
|
25
|
+
* The shape of `RunQueryEnrichment.Config` this enricher reads. Owned here (not by
|
|
26
|
+
* MJCore), so RunQuery stays generic.
|
|
27
|
+
*/
|
|
28
|
+
export interface MLModelScoreEnricherConfig {
|
|
29
|
+
/** The `MJ: ML Models` id to score each row with. */
|
|
30
|
+
modelId: string;
|
|
31
|
+
/** The result column the prediction is written into (added to each scored row). */
|
|
32
|
+
outputField: string;
|
|
33
|
+
/** The result column holding each row's primary key (joined to the model's records). Defaults to `ID`. */
|
|
34
|
+
primaryKeyField?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Which prediction value to write: the numeric `score` (default — e.g. a renewal
|
|
37
|
+
* probability) or the classification `class` label. Falls back to `score` when
|
|
38
|
+
* `class` is requested but absent.
|
|
39
|
+
*/
|
|
40
|
+
valueKind?: 'score' | 'class';
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Scores RunQuery result rows with a trained ML model, appending the prediction column.
|
|
44
|
+
* Registered on the MJGlobal ClassFactory so MJCore's `RunQuery` can resolve it by key
|
|
45
|
+
* without a static dependency.
|
|
46
|
+
*/
|
|
47
|
+
export declare class MLModelScoreEnricher extends QueryResultEnricherBase {
|
|
48
|
+
/** @inheritdoc */
|
|
49
|
+
EnrichResults(opts: {
|
|
50
|
+
rows: Record<string, unknown>[];
|
|
51
|
+
config: Record<string, unknown>;
|
|
52
|
+
query?: QueryInfo;
|
|
53
|
+
contextUser?: UserInfo;
|
|
54
|
+
provider?: IMetadataProvider;
|
|
55
|
+
}): Promise<Record<string, unknown>[]>;
|
|
56
|
+
/**
|
|
57
|
+
* The scoring runner to delegate to. Overridable so unit tests inject a fake with no
|
|
58
|
+
* live DB / sidecar (mirrors the Score action / operation `createRunner` seam). The
|
|
59
|
+
* production default wires the model loader / sidecar / artifact loader.
|
|
60
|
+
*/
|
|
61
|
+
protected createRunner(): IScoreRecordSetRunner;
|
|
62
|
+
/**
|
|
63
|
+
* Validate + narrow the free-form config to {@link MLModelScoreEnricherConfig}, or
|
|
64
|
+
* `null` (with a logged reason) when the required `modelId` / `outputField` are absent.
|
|
65
|
+
*/
|
|
66
|
+
protected parseConfig(config: Record<string, unknown>): MLModelScoreEnricherConfig | null;
|
|
67
|
+
/** Distinct, defined primary-key values from the rows (as strings — the scorer's record ids). */
|
|
68
|
+
protected collectPrimaryKeys(rows: Record<string, unknown>[], pkField: string): string[];
|
|
69
|
+
/** A single row's primary-key value as a string, or `undefined` when absent/null. */
|
|
70
|
+
protected rowKey(row: Record<string, unknown>, pkField: string): string | undefined;
|
|
71
|
+
/** Index ephemeral predictions by their `recordId` for an O(1) join back onto rows. */
|
|
72
|
+
protected indexPredictionsById(predictions: EphemeralPrediction[] | undefined): Map<string, EphemeralPrediction>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Tree-shaking anchor — referencing the class through this function creates a static
|
|
76
|
+
* code path the bundler keeps, so the `@RegisterClass` side-effect survives. Called from
|
|
77
|
+
* the scoring barrel so the enricher is registered when the package's module graph loads.
|
|
78
|
+
*/
|
|
79
|
+
export declare function LoadMLModelScoreEnricher(): void;
|
|
80
|
+
//# sourceMappingURL=ml-model-score-enricher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ml-model-score-enricher.d.ts","sourceRoot":"","sources":["../../src/scoring/ml-model-score-enricher.ts"],"names":[],"mappings":"AAAA;;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G,MAAM,EAAE;IAWxF,qFAAqF;IACrF,SAAS,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAKnF,uFAAuF;IACvF,SAAS,CAAC,oBAAoB,CAAC,WAAW,EAAE,mBAAmB,EAAE,GAAG,SAAS,GAAG,GAAG,CAAC,MAAM,EAAE,mBAAmB,CAAC;CASjH;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,IAAI,IAAI,CAE/C"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module scoring/ml-model-score-enricher
|
|
3
|
+
*
|
|
4
|
+
* **PS2-3 — scored-query enrichment.** A {@link QueryResultEnricherBase} that scores a
|
|
5
|
+
* RunQuery result set with a trained ML model and appends the prediction as a new
|
|
6
|
+
* column on each row. It is the Predictive-Studio half of the decoupled enrichment
|
|
7
|
+
* seam: MJCore's `RunQuery` resolves an enricher by key (`@RegisterClass`) at runtime
|
|
8
|
+
* and never depends on Predictive Studio (mirrors how the `'ML Model'` Record Set
|
|
9
|
+
* Processing work type stays decoupled via the registry).
|
|
10
|
+
*
|
|
11
|
+
* The model determines the target entity (the processor assembles features against the
|
|
12
|
+
* model's `targetEntityName`), so the enrichment only needs each row's PRIMARY KEY — it
|
|
13
|
+
* passes those keys as the scorer's `records` scope, scores WITHOUT write-back so the
|
|
14
|
+
* predictions are ephemeral, then joins them back onto the rows by record id.
|
|
15
|
+
*
|
|
16
|
+
* Resilience: the enricher degrades gracefully (logs + returns the input rows unchanged)
|
|
17
|
+
* for recoverable conditions — missing config, no primary keys — and RunQuery additionally
|
|
18
|
+
* guards the call, so a scoring problem can never break the underlying query.
|
|
19
|
+
*/
|
|
20
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
21
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
22
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
23
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
24
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
25
|
+
};
|
|
26
|
+
import { RegisterClass } from '@memberjunction/global';
|
|
27
|
+
import { LogError, QueryResultEnricherBase } from '@memberjunction/core';
|
|
28
|
+
import { buildScoreRecordSetRunner } from '../operations/delegation.js';
|
|
29
|
+
/** The ClassFactory key the enricher registers under — the `EnricherKey` callers pass on `RunQueryParams.Enrichment`. */
|
|
30
|
+
export const ML_MODEL_SCORE_ENRICHER_KEY = 'ML Model Score';
|
|
31
|
+
/**
|
|
32
|
+
* Scores RunQuery result rows with a trained ML model, appending the prediction column.
|
|
33
|
+
* Registered on the MJGlobal ClassFactory so MJCore's `RunQuery` can resolve it by key
|
|
34
|
+
* without a static dependency.
|
|
35
|
+
*/
|
|
36
|
+
let MLModelScoreEnricher = class MLModelScoreEnricher extends QueryResultEnricherBase {
|
|
37
|
+
/** @inheritdoc */
|
|
38
|
+
async EnrichResults(opts) {
|
|
39
|
+
const { rows, contextUser, provider } = opts;
|
|
40
|
+
if (!Array.isArray(rows) || rows.length === 0) {
|
|
41
|
+
return rows;
|
|
42
|
+
}
|
|
43
|
+
const config = this.parseConfig(opts.config);
|
|
44
|
+
if (!config) {
|
|
45
|
+
// Recoverable: bad/missing config — leave the rows as they are.
|
|
46
|
+
return rows;
|
|
47
|
+
}
|
|
48
|
+
const pkField = config.primaryKeyField ?? 'ID';
|
|
49
|
+
const ids = this.collectPrimaryKeys(rows, pkField);
|
|
50
|
+
if (ids.length === 0) {
|
|
51
|
+
LogError(`MLModelScoreEnricher: no '${pkField}' primary keys in the result rows; returning rows un-enriched`);
|
|
52
|
+
return rows;
|
|
53
|
+
}
|
|
54
|
+
// Score WITHOUT write-back → ephemeral predictions. The model supplies the target
|
|
55
|
+
// entity, so bare primary keys are a sufficient `records` scope.
|
|
56
|
+
const result = await this.createRunner().run({
|
|
57
|
+
modelId: config.modelId,
|
|
58
|
+
scope: { records: ids },
|
|
59
|
+
contextUser,
|
|
60
|
+
provider,
|
|
61
|
+
});
|
|
62
|
+
const predictionById = this.indexPredictionsById(result.predictions);
|
|
63
|
+
if (predictionById.size === 0) {
|
|
64
|
+
return rows;
|
|
65
|
+
}
|
|
66
|
+
const useClass = config.valueKind === 'class';
|
|
67
|
+
for (const row of rows) {
|
|
68
|
+
const id = this.rowKey(row, pkField);
|
|
69
|
+
const prediction = id != null ? predictionById.get(id) : undefined;
|
|
70
|
+
if (prediction) {
|
|
71
|
+
row[config.outputField] = useClass && prediction.class != null ? prediction.class : prediction.score;
|
|
72
|
+
}
|
|
73
|
+
// Rows with no matching prediction are intentionally left untouched (no column added).
|
|
74
|
+
}
|
|
75
|
+
return rows;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The scoring runner to delegate to. Overridable so unit tests inject a fake with no
|
|
79
|
+
* live DB / sidecar (mirrors the Score action / operation `createRunner` seam). The
|
|
80
|
+
* production default wires the model loader / sidecar / artifact loader.
|
|
81
|
+
*/
|
|
82
|
+
createRunner() {
|
|
83
|
+
return buildScoreRecordSetRunner();
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Validate + narrow the free-form config to {@link MLModelScoreEnricherConfig}, or
|
|
87
|
+
* `null` (with a logged reason) when the required `modelId` / `outputField` are absent.
|
|
88
|
+
*/
|
|
89
|
+
parseConfig(config) {
|
|
90
|
+
const modelId = typeof config?.modelId === 'string' ? config.modelId : undefined;
|
|
91
|
+
const outputField = typeof config?.outputField === 'string' ? config.outputField : undefined;
|
|
92
|
+
if (!modelId || !outputField) {
|
|
93
|
+
LogError(`MLModelScoreEnricher: config requires string 'modelId' and 'outputField'; got ${JSON.stringify(config)}`);
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
const primaryKeyField = typeof config.primaryKeyField === 'string' ? config.primaryKeyField : undefined;
|
|
97
|
+
const valueKind = config.valueKind === 'class' ? 'class' : 'score';
|
|
98
|
+
return { modelId, outputField, primaryKeyField, valueKind };
|
|
99
|
+
}
|
|
100
|
+
/** Distinct, defined primary-key values from the rows (as strings — the scorer's record ids). */
|
|
101
|
+
collectPrimaryKeys(rows, pkField) {
|
|
102
|
+
const seen = new Set();
|
|
103
|
+
for (const row of rows) {
|
|
104
|
+
const key = this.rowKey(row, pkField);
|
|
105
|
+
if (key != null) {
|
|
106
|
+
seen.add(key);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return [...seen];
|
|
110
|
+
}
|
|
111
|
+
/** A single row's primary-key value as a string, or `undefined` when absent/null. */
|
|
112
|
+
rowKey(row, pkField) {
|
|
113
|
+
const value = row?.[pkField];
|
|
114
|
+
return value == null ? undefined : String(value);
|
|
115
|
+
}
|
|
116
|
+
/** Index ephemeral predictions by their `recordId` for an O(1) join back onto rows. */
|
|
117
|
+
indexPredictionsById(predictions) {
|
|
118
|
+
const map = new Map();
|
|
119
|
+
for (const prediction of predictions ?? []) {
|
|
120
|
+
if (prediction.recordId != null) {
|
|
121
|
+
map.set(String(prediction.recordId), prediction);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return map;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
MLModelScoreEnricher = __decorate([
|
|
128
|
+
RegisterClass(QueryResultEnricherBase, ML_MODEL_SCORE_ENRICHER_KEY)
|
|
129
|
+
], MLModelScoreEnricher);
|
|
130
|
+
export { MLModelScoreEnricher };
|
|
131
|
+
/**
|
|
132
|
+
* Tree-shaking anchor — referencing the class through this function creates a static
|
|
133
|
+
* code path the bundler keeps, so the `@RegisterClass` side-effect survives. Called from
|
|
134
|
+
* the scoring barrel so the enricher is registered when the package's module graph loads.
|
|
135
|
+
*/
|
|
136
|
+
export function LoadMLModelScoreEnricher() {
|
|
137
|
+
void MLModelScoreEnricher;
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=ml-model-score-enricher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ml-model-score-enricher.js","sourceRoot":"","sources":["../../src/scoring/ml-model-score-enricher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;;;;;;;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,uBAAuB,EAAyD,MAAM,sBAAsB,CAAC;AAOhI,OAAO,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAErE,yHAAyH;AACzH,MAAM,CAAC,MAAM,2BAA2B,GAAG,gBAAgB,CAAC;AAqB5D;;;;GAIG;AAEI,IAAM,oBAAoB,GAA1B,MAAM,oBAAqB,SAAQ,uBAAuB;IAC/D,kBAAkB;IACX,KAAK,CAAC,aAAa,CAAC,IAM1B;QACC,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC;QAC7C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9C,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC7C,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,gEAAgE;YAChE,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,OAAO,GAAG,MAAM,CAAC,eAAe,IAAI,IAAI,CAAC;QAC/C,MAAM,GAAG,GAAG,IAAI,CAAC,kBAAkB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACnD,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACrB,QAAQ,CAAC,6BAA6B,OAAO,+D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CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACnD,CAAC;IAED,uFAAuF;IAC7E,oBAAoB,CAAC,WAA8C;QAC3E,MAAM,GAAG,GAAG,IAAI,GAAG,EAA+B,CAAC;QACnD,KAAK,MAAM,UAAU,IAAI,WAAW,IAAI,EAAE,EAAE,CAAC;YAC3C,IAAI,UAAU,CAAC,QAAQ,IAAI,IAAI,EAAE,CAAC;gBAChC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,UAAU,CAAC,CAAC;YACnD,CAAC;QACH,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;CACF,CAAA;AA1GY,oBAAoB;IADhC,aAAa,CAAC,uBAAuB,EAAE,2BAA2B,CAAC;GACvD,oBAAoB,CA0GhC;;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB;IACtC,KAAK,oBAAoB,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module scoring/register
|
|
3
|
+
*
|
|
4
|
+
* Work-type registration + dynamic resolution for the scoring processor, WITHOUT
|
|
5
|
+
* modifying the `record-set-processor` substrate (its base package must not
|
|
6
|
+
* depend on Predictive Studio).
|
|
7
|
+
*
|
|
8
|
+
* ## How scoring plugs into Record Set Processing
|
|
9
|
+
*
|
|
10
|
+
* The substrate's `RecordProcessExecutor.buildProcessor()` builds the built-in
|
|
11
|
+
* work types (`FieldRules` / `Action` / `Agent` / `Infer`) directly. To add the
|
|
12
|
+
* `ML Model` work type without editing that package, the
|
|
13
|
+
* {@link MLModelInferenceProcessor} registers itself on the MJGlobal ClassFactory
|
|
14
|
+
* via `@RegisterClass` (mirroring how the substrate's own Remote Operations
|
|
15
|
+
* register, e.g. `@RegisterClass(BaseRemotableOperation, 'RecordProcess.RunNow')`).
|
|
16
|
+
*
|
|
17
|
+
* A thin {@link resolveMLInferenceProcessor} pulls the processor back out by
|
|
18
|
+
* work-type key. The supervisor wires this into the executor seam (a small
|
|
19
|
+
* extension/override of `buildProcessor`) — no change to the base substrate is
|
|
20
|
+
* required, so the package stays Predictive-Studio-free.
|
|
21
|
+
*/
|
|
22
|
+
import { type IRecordProcessor } from '@memberjunction/record-set-processor-base';
|
|
23
|
+
import type { MLInferenceDeps, MLModelInferenceProcessorOptions } from './types.js';
|
|
24
|
+
import type { DatedSourceSpec } from '../feature-assembly/index.js';
|
|
25
|
+
/**
|
|
26
|
+
* The set of work-type keys the scoring processor is registered under. A
|
|
27
|
+
* `RecordProcessExecutor` extension checks membership to decide whether to
|
|
28
|
+
* delegate a Record Process's `WorkType` to the ML scorer.
|
|
29
|
+
*/
|
|
30
|
+
export declare const ML_INFERENCE_WORK_TYPES: readonly string[];
|
|
31
|
+
/**
|
|
32
|
+
* Whether a Record Process `WorkType` value targets the ML scoring processor.
|
|
33
|
+
*
|
|
34
|
+
* @param workType the `MJ: Record Processes.WorkType` value
|
|
35
|
+
*/
|
|
36
|
+
export declare function isMLInferenceWorkType(workType: string | null | undefined): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Resolve an {@link MLModelInferenceProcessor} for a work-type key via the
|
|
39
|
+
* MJGlobal ClassFactory, constructed with the supplied options. Returns `null`
|
|
40
|
+
* when the key is not an ML-inference work type (so callers can fall through to
|
|
41
|
+
* the substrate's built-in processors).
|
|
42
|
+
*
|
|
43
|
+
* @param workType the Record Process `WorkType` value (`'ML Model'` / `'MLModelInference'`)
|
|
44
|
+
* @param options the model id + injected seams to construct the processor with
|
|
45
|
+
*/
|
|
46
|
+
export declare function resolveMLInferenceProcessor(workType: string | null | undefined, options: MLModelInferenceProcessorOptions): IRecordProcessor | null;
|
|
47
|
+
/**
|
|
48
|
+
* The shape a `MJ: Record Processes.Configuration` JSON carries for an `'ML Model'` work type — the
|
|
49
|
+
* per-Record-Process scoring inputs the factory reads to construct the {@link MLModelInferenceProcessor}.
|
|
50
|
+
* The injected runtime seams ({@link MLInferenceDeps}) are supplied at registration time (closed over by
|
|
51
|
+
* the factory), NOT in this config, so the config stays declarative + storable on the Record Process row.
|
|
52
|
+
*/
|
|
53
|
+
export interface MLScoringConfiguration {
|
|
54
|
+
/** The `MJ: ML Models` id this Record Process scores with. */
|
|
55
|
+
modelId: string;
|
|
56
|
+
/** Primary-key field on the target entity (defaults to `ID`). */
|
|
57
|
+
primaryKeyField?: string;
|
|
58
|
+
/** Optional dated sources supplying point-in-time ("as-of") features. */
|
|
59
|
+
datedSources?: DatedSourceSpec[];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Register the {@link MLModelInferenceProcessor} into the Record Set Processing
|
|
63
|
+
* {@link RecordProcessorRegistry} for the `'ML Model'` work type (and the `'MLModelInference'` alias),
|
|
64
|
+
* so `RecordProcessExecutor.buildProcessor()` resolves it automatically — WITHOUT the substrate
|
|
65
|
+
* depending on Predictive Studio. The registered factory closes over the injected {@link MLInferenceDeps}
|
|
66
|
+
* (model loader / artifact loader / sidecar) and reads the per-run {@link MLScoringConfiguration}
|
|
67
|
+
* (the `modelId` etc.) off the Record Process's `Configuration`.
|
|
68
|
+
*
|
|
69
|
+
* Idempotent — calling more than once simply re-registers (last-wins). Call from the engine's
|
|
70
|
+
* startup/bootstrap with the production deps.
|
|
71
|
+
*
|
|
72
|
+
* @param deps the injected runtime seams the ML scorer needs (model loader, artifact loader, sidecar)
|
|
73
|
+
*/
|
|
74
|
+
export declare function registerMLScoringProcessor(deps: MLInferenceDeps): void;
|
|
75
|
+
/**
|
|
76
|
+
* Tree-shaking anchor — call from a server bootstrap to guarantee the
|
|
77
|
+
* `@RegisterClass` registration is not eliminated by the bundler. Referencing the
|
|
78
|
+
* class through this function creates a static code path the bundler keeps.
|
|
79
|
+
*/
|
|
80
|
+
export declare function LoadMLModelInferenceProcessor(): void;
|
|
81
|
+
//# sourceMappingURL=register.d.ts.map
|