@memberjunction/predictive-studio 0.0.1 → 5.44.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,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module feature-assembly/leakage-guard
|
|
3
|
+
*
|
|
4
|
+
* Leakage protection (plan §6.4). An automated feature-search agent will
|
|
5
|
+
* relentlessly exploit target leakage — a field that's a proxy for the label →
|
|
6
|
+
* AUC ~0.99 garbage. Two complementary defenses live here:
|
|
7
|
+
*
|
|
8
|
+
* 1. **Deny-list enforcement (assembly-time).** Deny-listed fields/sources are
|
|
9
|
+
* EXCLUDED from the assembled feature matrix — filtered out *before* any
|
|
10
|
+
* column is produced. This is enforced by {@link LeakageGuardEnforcer},
|
|
11
|
+
* used by the FeatureAssembly executor.
|
|
12
|
+
* 2. **Single-feature-dominance detection (post-train).** After training, if one
|
|
13
|
+
* feature's importance exceeds a threshold (e.g. >0.6), the run is flagged as
|
|
14
|
+
* suspicious → warn loudly + block promotion until a human signs off. The
|
|
15
|
+
* detection itself ({@link detectSingleFeatureDominance}) runs post-train
|
|
16
|
+
* (importance only exists after a model is fit); the executor only enforces
|
|
17
|
+
* the deny-list at assembly time.
|
|
18
|
+
*/
|
|
19
|
+
import type { LeakageGuard, FeatureImportance } from '@memberjunction/predictive-studio-core';
|
|
20
|
+
/**
|
|
21
|
+
* Assembly-time deny-list enforcer. Normalizes the guard's `DenyFields` /
|
|
22
|
+
* `DenySources` into case-insensitive sets and answers "is this field/source
|
|
23
|
+
* allowed into the matrix?".
|
|
24
|
+
*/
|
|
25
|
+
export declare class LeakageGuardEnforcer {
|
|
26
|
+
private readonly denyFields;
|
|
27
|
+
private readonly denySources;
|
|
28
|
+
/**
|
|
29
|
+
* @param guard the leakage-guard configuration (deny-list + dominance threshold)
|
|
30
|
+
*/
|
|
31
|
+
constructor(guard: LeakageGuard);
|
|
32
|
+
/**
|
|
33
|
+
* Whether a field is allowed into the feature matrix (i.e. NOT deny-listed).
|
|
34
|
+
*
|
|
35
|
+
* @param fieldName candidate column/feature name
|
|
36
|
+
*/
|
|
37
|
+
isFieldAllowed(fieldName: string): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Whether a source binding is allowed to be drawn from (i.e. NOT deny-listed).
|
|
40
|
+
*
|
|
41
|
+
* @param sourceRef the source's `Ref` (entity name / Query id / etc.)
|
|
42
|
+
*/
|
|
43
|
+
isSourceAllowed(sourceRef: string): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Partitions a list of candidate column names into allowed vs denied — handy
|
|
46
|
+
* for the executor's `select` step, which filters deny-listed columns out of
|
|
47
|
+
* the matrix before producing values.
|
|
48
|
+
*
|
|
49
|
+
* @param columns candidate column names
|
|
50
|
+
* @returns `{ allowed, denied }` partition
|
|
51
|
+
*/
|
|
52
|
+
partitionColumns(columns: string[]): {
|
|
53
|
+
allowed: string[];
|
|
54
|
+
denied: string[];
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Result of the post-training single-feature-dominance check.
|
|
59
|
+
*/
|
|
60
|
+
export interface DominanceResult {
|
|
61
|
+
/** Whether a single feature's importance exceeds the threshold. */
|
|
62
|
+
Dominant: boolean;
|
|
63
|
+
/** The most-important feature's name (when any importances were provided). */
|
|
64
|
+
TopFeature?: string;
|
|
65
|
+
/** The top feature's normalized importance share (0..1). */
|
|
66
|
+
TopShare?: number;
|
|
67
|
+
/** The threshold compared against. */
|
|
68
|
+
Threshold: number;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Post-training single-feature-dominance check (plan §6.4). Normalizes the raw
|
|
72
|
+
* importance map to shares of total importance, then flags when the top feature's
|
|
73
|
+
* share exceeds the threshold. A flagged run is suspicious (likely target
|
|
74
|
+
* leakage) → training surfaces a plain-language warning and blocks promotion
|
|
75
|
+
* until a human signs off.
|
|
76
|
+
*
|
|
77
|
+
* Importances are taken as magnitudes (`Math.abs`) so signed coefficients (e.g.
|
|
78
|
+
* logistic regression) are handled correctly. When all importances are zero (or
|
|
79
|
+
* the map is empty), nothing is flagged.
|
|
80
|
+
*
|
|
81
|
+
* @param featureImportance per-feature importance/contribution from the sidecar
|
|
82
|
+
* @param threshold dominance threshold (e.g. `0.6` from `LeakageGuard.SingleFeatureDominanceThreshold`)
|
|
83
|
+
* @returns the {@link DominanceResult}
|
|
84
|
+
*/
|
|
85
|
+
export declare function detectSingleFeatureDominance(featureImportance: FeatureImportance, threshold: number): DominanceResult;
|
|
86
|
+
//# sourceMappingURL=leakage-guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leakage-guard.d.ts","sourceRoot":"","sources":["../../src/feature-assembly/leakage-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAC;AAE9F;;;;GAIG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAc;IACzC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAc;IAE1C;;OAEG;gBACS,KAAK,EAAE,YAAY;IAK/B;;;;OAIG;IACI,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAIjD;;;;OAIG;IACI,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAIlD;;;;;;;OAOG;IACI,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG;QAAE,OAAO,EAAE,MAAM,EAAE,CAAC;QAAC,MAAM,EAAE,MAAM,EAAE,CAAA;KAAE;CAQpF;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,mEAAmE;IACnE,QAAQ,EAAE,OAAO,CAAC;IAClB,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,4DAA4D;IAC5D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,sCAAsC;IACtC,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,4BAA4B,CAAC,iBAAiB,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,GAAG,eAAe,CA6BrH"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module feature-assembly/leakage-guard
|
|
3
|
+
*
|
|
4
|
+
* Leakage protection (plan §6.4). An automated feature-search agent will
|
|
5
|
+
* relentlessly exploit target leakage — a field that's a proxy for the label →
|
|
6
|
+
* AUC ~0.99 garbage. Two complementary defenses live here:
|
|
7
|
+
*
|
|
8
|
+
* 1. **Deny-list enforcement (assembly-time).** Deny-listed fields/sources are
|
|
9
|
+
* EXCLUDED from the assembled feature matrix — filtered out *before* any
|
|
10
|
+
* column is produced. This is enforced by {@link LeakageGuardEnforcer},
|
|
11
|
+
* used by the FeatureAssembly executor.
|
|
12
|
+
* 2. **Single-feature-dominance detection (post-train).** After training, if one
|
|
13
|
+
* feature's importance exceeds a threshold (e.g. >0.6), the run is flagged as
|
|
14
|
+
* suspicious → warn loudly + block promotion until a human signs off. The
|
|
15
|
+
* detection itself ({@link detectSingleFeatureDominance}) runs post-train
|
|
16
|
+
* (importance only exists after a model is fit); the executor only enforces
|
|
17
|
+
* the deny-list at assembly time.
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Assembly-time deny-list enforcer. Normalizes the guard's `DenyFields` /
|
|
21
|
+
* `DenySources` into case-insensitive sets and answers "is this field/source
|
|
22
|
+
* allowed into the matrix?".
|
|
23
|
+
*/
|
|
24
|
+
export class LeakageGuardEnforcer {
|
|
25
|
+
/**
|
|
26
|
+
* @param guard the leakage-guard configuration (deny-list + dominance threshold)
|
|
27
|
+
*/
|
|
28
|
+
constructor(guard) {
|
|
29
|
+
this.denyFields = new Set((guard.DenyFields ?? []).map(normalize));
|
|
30
|
+
this.denySources = new Set((guard.DenySources ?? []).map(normalize));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Whether a field is allowed into the feature matrix (i.e. NOT deny-listed).
|
|
34
|
+
*
|
|
35
|
+
* @param fieldName candidate column/feature name
|
|
36
|
+
*/
|
|
37
|
+
isFieldAllowed(fieldName) {
|
|
38
|
+
return !this.denyFields.has(normalize(fieldName));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether a source binding is allowed to be drawn from (i.e. NOT deny-listed).
|
|
42
|
+
*
|
|
43
|
+
* @param sourceRef the source's `Ref` (entity name / Query id / etc.)
|
|
44
|
+
*/
|
|
45
|
+
isSourceAllowed(sourceRef) {
|
|
46
|
+
return !this.denySources.has(normalize(sourceRef));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Partitions a list of candidate column names into allowed vs denied — handy
|
|
50
|
+
* for the executor's `select` step, which filters deny-listed columns out of
|
|
51
|
+
* the matrix before producing values.
|
|
52
|
+
*
|
|
53
|
+
* @param columns candidate column names
|
|
54
|
+
* @returns `{ allowed, denied }` partition
|
|
55
|
+
*/
|
|
56
|
+
partitionColumns(columns) {
|
|
57
|
+
const allowed = [];
|
|
58
|
+
const denied = [];
|
|
59
|
+
for (const c of columns) {
|
|
60
|
+
(this.isFieldAllowed(c) ? allowed : denied).push(c);
|
|
61
|
+
}
|
|
62
|
+
return { allowed, denied };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Post-training single-feature-dominance check (plan §6.4). Normalizes the raw
|
|
67
|
+
* importance map to shares of total importance, then flags when the top feature's
|
|
68
|
+
* share exceeds the threshold. A flagged run is suspicious (likely target
|
|
69
|
+
* leakage) → training surfaces a plain-language warning and blocks promotion
|
|
70
|
+
* until a human signs off.
|
|
71
|
+
*
|
|
72
|
+
* Importances are taken as magnitudes (`Math.abs`) so signed coefficients (e.g.
|
|
73
|
+
* logistic regression) are handled correctly. When all importances are zero (or
|
|
74
|
+
* the map is empty), nothing is flagged.
|
|
75
|
+
*
|
|
76
|
+
* @param featureImportance per-feature importance/contribution from the sidecar
|
|
77
|
+
* @param threshold dominance threshold (e.g. `0.6` from `LeakageGuard.SingleFeatureDominanceThreshold`)
|
|
78
|
+
* @returns the {@link DominanceResult}
|
|
79
|
+
*/
|
|
80
|
+
export function detectSingleFeatureDominance(featureImportance, threshold) {
|
|
81
|
+
const entries = Object.entries(featureImportance ?? {});
|
|
82
|
+
if (entries.length === 0) {
|
|
83
|
+
return { Dominant: false, Threshold: threshold };
|
|
84
|
+
}
|
|
85
|
+
let total = 0;
|
|
86
|
+
let topFeature = entries[0][0];
|
|
87
|
+
let topMagnitude = -Infinity;
|
|
88
|
+
for (const [name, value] of entries) {
|
|
89
|
+
const mag = Math.abs(value);
|
|
90
|
+
total += mag;
|
|
91
|
+
if (mag > topMagnitude) {
|
|
92
|
+
topMagnitude = mag;
|
|
93
|
+
topFeature = name;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (total === 0) {
|
|
97
|
+
return { Dominant: false, TopFeature: topFeature, TopShare: 0, Threshold: threshold };
|
|
98
|
+
}
|
|
99
|
+
const topShare = topMagnitude / total;
|
|
100
|
+
return {
|
|
101
|
+
Dominant: topShare > threshold,
|
|
102
|
+
TopFeature: topFeature,
|
|
103
|
+
TopShare: topShare,
|
|
104
|
+
Threshold: threshold,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/** Lowercase + trim for case/whitespace-insensitive name matching. */
|
|
108
|
+
function normalize(name) {
|
|
109
|
+
return name.trim().toLowerCase();
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=leakage-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leakage-guard.js","sourceRoot":"","sources":["../../src/feature-assembly/leakage-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH;;;;GAIG;AACH,MAAM,OAAO,oBAAoB;IAI/B;;OAEG;IACH,YAAY,KAAmB;QAC7B,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;QACnE,IAAI,CAAC,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,WAAW,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;IACvE,CAAC;IAED;;;;OAIG;IACI,cAAc,CAAC,SAAiB;QACrC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,S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C;YACvB,YAAY,GAAG,GAAG,CAAC;YACnB,UAAU,GAAG,IAAI,CAAC;QACpB,CAAC;IACH,CAAC;IAED,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;QAChB,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;IACxF,CAAC;IAED,MAAM,QAAQ,GAAG,YAAY,GAAG,KAAK,CAAC;IACtC,OAAO;QACL,QAAQ,EAAE,QAAQ,GAAG,SAAS;QAC9B,UAAU,EAAE,UAAU;QACtB,QAAQ,EAAE,QAAQ;QAClB,SAAS,EAAE,SAAS;KACrB,CAAC;AACJ,CAAC;AAED,sEAAsE;AACtE,SAAS,SAAS,CAAC,IAAY;IAC7B,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module feature-assembly/vision-llm
|
|
3
|
+
*
|
|
4
|
+
* **Multimodal vision-LLM-as-feature** (plan §11 / §5.6, PS-MM-1) — the per-row
|
|
5
|
+
* extraction engine behind a {@link VisionLLMFeatureStep}. For each target
|
|
6
|
+
* record it builds an {@link AIPromptParams} whose user turn carries the row's
|
|
7
|
+
* image as a multimodal `image_url` content block plus the configured vision
|
|
8
|
+
* prompt, runs it through an **injected** prompt-runner seam, and parses the
|
|
9
|
+
* model's single structured output (a named category or scalar) into the feature
|
|
10
|
+
* column's value.
|
|
11
|
+
*
|
|
12
|
+
* ## Why this is exempt from the fit-once/apply-everywhere split (§6.2)
|
|
13
|
+
*
|
|
14
|
+
* Preprocessing steps (`impute`/`standardize`/`onehot`/`bin`) learn parameters
|
|
15
|
+
* from the training distribution (a mean, a vocabulary, bin edges) that MUST be
|
|
16
|
+
* frozen and replayed identically at scoring — otherwise train/serve skew. The
|
|
17
|
+
* vision-LLM step has **no fitted state**: the same prompt is applied to each
|
|
18
|
+
* row independently and statelessly, so the row's own image fully determines the
|
|
19
|
+
* output. There is nothing to fit on the training set and replay at scoring, so
|
|
20
|
+
* it produces a RAW feature column directly — exactly like `select`/`embedding`
|
|
21
|
+
* — and is wired into the executor's data-assembly path, not the preprocessing
|
|
22
|
+
* recipe. (Any downstream `onehot` of the resulting category IS a preprocessing
|
|
23
|
+
* step and goes through the normal fit/replay split.)
|
|
24
|
+
*
|
|
25
|
+
* ## As-of and leakage
|
|
26
|
+
*
|
|
27
|
+
* - **As-of**: the image is read from the row *as of* the assembly's as-of time —
|
|
28
|
+
* we use the image column value present on the record the executor resolved for
|
|
29
|
+
* that as-of date. The vision extraction never reaches forward in time; it only
|
|
30
|
+
* looks at the row's own image.
|
|
31
|
+
* - **Leakage guard**: a feature derived from the row's OWN image is allowed —
|
|
32
|
+
* it is a property of the record itself, not a proxy for the future label
|
|
33
|
+
* (the deny-list only blocks fields/sources known to leak the target). The
|
|
34
|
+
* output feature name is still passed through the guard so an operator can
|
|
35
|
+
* deny-list a specific vision feature if it proves leaky post-train.
|
|
36
|
+
*/
|
|
37
|
+
import type { ChatMessage } from '@memberjunction/ai';
|
|
38
|
+
import { AIPromptParams } from '@memberjunction/ai-core-plus';
|
|
39
|
+
import type { AIPromptRunResult } from '@memberjunction/ai-core-plus';
|
|
40
|
+
import type { VisionLLMFeatureStep, VisionLLMOutput } from '@memberjunction/predictive-studio-core';
|
|
41
|
+
import type { UserInfo } from '@memberjunction/core';
|
|
42
|
+
import type { SourceRow } from './data-access.js';
|
|
43
|
+
/**
|
|
44
|
+
* The injected prompt-runner seam. The production binding wraps
|
|
45
|
+
* `@memberjunction/ai-prompts`' `AIPromptRunner.ExecutePrompt`; unit tests supply
|
|
46
|
+
* a fake so the vision-LLM step assembles end-to-end WITHOUT a live model.
|
|
47
|
+
*
|
|
48
|
+
* The contract is deliberately narrowed to the single method the vision step
|
|
49
|
+
* needs, so the engine does not take a hard dependency on the (heavy) concrete
|
|
50
|
+
* runner — only on the real `AIPromptParams` / `AIPromptRunResult` shapes.
|
|
51
|
+
*/
|
|
52
|
+
export interface IVisionPromptRunner {
|
|
53
|
+
/**
|
|
54
|
+
* Execute a (multimodal) prompt and return its result. Mirrors
|
|
55
|
+
* `AIPromptRunner.ExecutePrompt`'s signature so the production binding is a
|
|
56
|
+
* direct passthrough.
|
|
57
|
+
*/
|
|
58
|
+
ExecutePrompt<T = unknown>(params: AIPromptParams): Promise<AIPromptRunResult<T>>;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Per-record output of a vision extraction. `value` is the parsed feature cell
|
|
62
|
+
* (or `null` when the image is missing or the output couldn't be parsed).
|
|
63
|
+
*/
|
|
64
|
+
export interface VisionExtractionResult {
|
|
65
|
+
/** The parsed feature value for the row, or `null`. */
|
|
66
|
+
value: string | number | null;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Runs a {@link VisionLLMFeatureStep} over a single record: builds the
|
|
70
|
+
* multimodal prompt params, invokes the injected runner, and parses the
|
|
71
|
+
* structured output. Stateless — construct once and reuse across rows.
|
|
72
|
+
*/
|
|
73
|
+
export declare class VisionFeatureExtractor {
|
|
74
|
+
private readonly runner;
|
|
75
|
+
private readonly contextUser?;
|
|
76
|
+
/**
|
|
77
|
+
* @param runner the injected prompt-runner seam (mocked in tests)
|
|
78
|
+
* @param contextUser request user — passed to the runner for isolation/audit
|
|
79
|
+
*/
|
|
80
|
+
constructor(runner: IVisionPromptRunner, contextUser?: UserInfo);
|
|
81
|
+
/**
|
|
82
|
+
* Extract the vision feature for one record. Returns `{ value: null }` without
|
|
83
|
+
* calling the model when the row's image column is null/missing/blank.
|
|
84
|
+
*
|
|
85
|
+
* @param step the vision-llm step spec
|
|
86
|
+
* @param record the target record (already resolved as-of its decision date)
|
|
87
|
+
* @param promptEntity the resolved prompt entity to run (caller resolves it
|
|
88
|
+
* from `step.Prompt` against `MJ: AI Prompts`); typed loosely so the engine
|
|
89
|
+
* doesn't depend on the entity class just to thread it through
|
|
90
|
+
*/
|
|
91
|
+
extract(step: VisionLLMFeatureStep, record: SourceRow, promptEntity: AIPromptParams['prompt']): Promise<VisionExtractionResult>;
|
|
92
|
+
/**
|
|
93
|
+
* Build the {@link AIPromptParams} for one row: the vision prompt as the system
|
|
94
|
+
* template plus a user turn whose content carries the image as an `image_url`
|
|
95
|
+
* multimodal content block alongside an instruction to emit the structured
|
|
96
|
+
* output.
|
|
97
|
+
*/
|
|
98
|
+
private buildPromptParams;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Read the image reference (URL or binary/data ref) from a record's image
|
|
102
|
+
* column. Returns `null` for null/undefined/blank values so the caller can skip
|
|
103
|
+
* the model call gracefully.
|
|
104
|
+
*/
|
|
105
|
+
export declare function readImageRef(record: SourceRow, imageColumn: string): string | null;
|
|
106
|
+
/**
|
|
107
|
+
* Build the user-turn {@link ChatMessage} carrying the image as an `image_url`
|
|
108
|
+
* multimodal content block plus a text block asking the model to emit the
|
|
109
|
+
* structured output named by {@link VisionLLMOutput.FeatureName}.
|
|
110
|
+
*/
|
|
111
|
+
export declare function buildVisionUserMessage(imageRef: string, output: VisionLLMOutput, inlinePrompt?: string): ChatMessage;
|
|
112
|
+
/**
|
|
113
|
+
* Compose the instruction asking the model to emit exactly the named structured
|
|
114
|
+
* output. For a closed category set, the allowed values are enumerated so the
|
|
115
|
+
* model stays in-set.
|
|
116
|
+
*/
|
|
117
|
+
export declare function buildOutputInstruction(output: VisionLLMOutput): string;
|
|
118
|
+
/**
|
|
119
|
+
* Parse the runner's result into the feature cell value per the output contract.
|
|
120
|
+
* Handles both a structured `result` object (preferred) and a raw JSON/text
|
|
121
|
+
* `rawResult` fallback. Returns `null` on failure, an unsuccessful run, or an
|
|
122
|
+
* out-of-set category.
|
|
123
|
+
*/
|
|
124
|
+
export declare function parseVisionOutput(runResult: AIPromptRunResult<unknown>, output: VisionLLMOutput): string | number | null;
|
|
125
|
+
//# sourceMappingURL=vision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Ac1B;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAQlF;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,WAAW,CAQpH;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,eAAe,GAAG,MAAM,CAStE;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,iBAAiB,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,eAAe,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CASxH"}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module feature-assembly/vision-llm
|
|
3
|
+
*
|
|
4
|
+
* **Multimodal vision-LLM-as-feature** (plan §11 / §5.6, PS-MM-1) — the per-row
|
|
5
|
+
* extraction engine behind a {@link VisionLLMFeatureStep}. For each target
|
|
6
|
+
* record it builds an {@link AIPromptParams} whose user turn carries the row's
|
|
7
|
+
* image as a multimodal `image_url` content block plus the configured vision
|
|
8
|
+
* prompt, runs it through an **injected** prompt-runner seam, and parses the
|
|
9
|
+
* model's single structured output (a named category or scalar) into the feature
|
|
10
|
+
* column's value.
|
|
11
|
+
*
|
|
12
|
+
* ## Why this is exempt from the fit-once/apply-everywhere split (§6.2)
|
|
13
|
+
*
|
|
14
|
+
* Preprocessing steps (`impute`/`standardize`/`onehot`/`bin`) learn parameters
|
|
15
|
+
* from the training distribution (a mean, a vocabulary, bin edges) that MUST be
|
|
16
|
+
* frozen and replayed identically at scoring — otherwise train/serve skew. The
|
|
17
|
+
* vision-LLM step has **no fitted state**: the same prompt is applied to each
|
|
18
|
+
* row independently and statelessly, so the row's own image fully determines the
|
|
19
|
+
* output. There is nothing to fit on the training set and replay at scoring, so
|
|
20
|
+
* it produces a RAW feature column directly — exactly like `select`/`embedding`
|
|
21
|
+
* — and is wired into the executor's data-assembly path, not the preprocessing
|
|
22
|
+
* recipe. (Any downstream `onehot` of the resulting category IS a preprocessing
|
|
23
|
+
* step and goes through the normal fit/replay split.)
|
|
24
|
+
*
|
|
25
|
+
* ## As-of and leakage
|
|
26
|
+
*
|
|
27
|
+
* - **As-of**: the image is read from the row *as of* the assembly's as-of time —
|
|
28
|
+
* we use the image column value present on the record the executor resolved for
|
|
29
|
+
* that as-of date. The vision extraction never reaches forward in time; it only
|
|
30
|
+
* looks at the row's own image.
|
|
31
|
+
* - **Leakage guard**: a feature derived from the row's OWN image is allowed —
|
|
32
|
+
* it is a property of the record itself, not a proxy for the future label
|
|
33
|
+
* (the deny-list only blocks fields/sources known to leak the target). The
|
|
34
|
+
* output feature name is still passed through the guard so an operator can
|
|
35
|
+
* deny-list a specific vision feature if it proves leaky post-train.
|
|
36
|
+
*/
|
|
37
|
+
import { AIPromptParams } from '@memberjunction/ai-core-plus';
|
|
38
|
+
/**
|
|
39
|
+
* Runs a {@link VisionLLMFeatureStep} over a single record: builds the
|
|
40
|
+
* multimodal prompt params, invokes the injected runner, and parses the
|
|
41
|
+
* structured output. Stateless — construct once and reuse across rows.
|
|
42
|
+
*/
|
|
43
|
+
export class VisionFeatureExtractor {
|
|
44
|
+
/**
|
|
45
|
+
* @param runner the injected prompt-runner seam (mocked in tests)
|
|
46
|
+
* @param contextUser request user — passed to the runner for isolation/audit
|
|
47
|
+
*/
|
|
48
|
+
constructor(runner, contextUser) {
|
|
49
|
+
this.runner = runner;
|
|
50
|
+
this.contextUser = contextUser;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Extract the vision feature for one record. Returns `{ value: null }` without
|
|
54
|
+
* calling the model when the row's image column is null/missing/blank.
|
|
55
|
+
*
|
|
56
|
+
* @param step the vision-llm step spec
|
|
57
|
+
* @param record the target record (already resolved as-of its decision date)
|
|
58
|
+
* @param promptEntity the resolved prompt entity to run (caller resolves it
|
|
59
|
+
* from `step.Prompt` against `MJ: AI Prompts`); typed loosely so the engine
|
|
60
|
+
* doesn't depend on the entity class just to thread it through
|
|
61
|
+
*/
|
|
62
|
+
async extract(step, record, promptEntity) {
|
|
63
|
+
const imageRef = readImageRef(record, step.ImageColumn);
|
|
64
|
+
if (imageRef === null) {
|
|
65
|
+
// Graceful: no image for this row → null feature, no model call.
|
|
66
|
+
return { value: null };
|
|
67
|
+
}
|
|
68
|
+
const params = this.buildPromptParams(step, imageRef, promptEntity);
|
|
69
|
+
const runResult = await this.runner.ExecutePrompt(params);
|
|
70
|
+
return { value: parseVisionOutput(runResult, step.Output) };
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Build the {@link AIPromptParams} for one row: the vision prompt as the system
|
|
74
|
+
* template plus a user turn whose content carries the image as an `image_url`
|
|
75
|
+
* multimodal content block alongside an instruction to emit the structured
|
|
76
|
+
* output.
|
|
77
|
+
*/
|
|
78
|
+
buildPromptParams(step, imageRef, promptEntity) {
|
|
79
|
+
const params = new AIPromptParams();
|
|
80
|
+
params.prompt = promptEntity;
|
|
81
|
+
params.contextUser = this.contextUser;
|
|
82
|
+
// The vision prompt template is the system turn; the image rides on a user turn.
|
|
83
|
+
params.templateMessageRole = 'system';
|
|
84
|
+
params.conversationMessages = [buildVisionUserMessage(imageRef, step.Output, step.Prompt.InlinePrompt)];
|
|
85
|
+
if (step.ModelRef) {
|
|
86
|
+
// Surface the model override to the prompt run via data (the runner's model
|
|
87
|
+
// selection honors an explicit override key); pinned into model Lineage.
|
|
88
|
+
params.data = { ...(params.data ?? {}), visionModelRef: step.ModelRef };
|
|
89
|
+
}
|
|
90
|
+
return params;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Read the image reference (URL or binary/data ref) from a record's image
|
|
95
|
+
* column. Returns `null` for null/undefined/blank values so the caller can skip
|
|
96
|
+
* the model call gracefully.
|
|
97
|
+
*/
|
|
98
|
+
export function readImageRef(record, imageColumn) {
|
|
99
|
+
const raw = record[imageColumn];
|
|
100
|
+
if (raw === null || raw === undefined) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
const str = typeof raw === 'string' ? raw : String(raw);
|
|
104
|
+
const trimmed = str.trim();
|
|
105
|
+
return trimmed.length === 0 ? null : trimmed;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Build the user-turn {@link ChatMessage} carrying the image as an `image_url`
|
|
109
|
+
* multimodal content block plus a text block asking the model to emit the
|
|
110
|
+
* structured output named by {@link VisionLLMOutput.FeatureName}.
|
|
111
|
+
*/
|
|
112
|
+
export function buildVisionUserMessage(imageRef, output, inlinePrompt) {
|
|
113
|
+
const blocks = [];
|
|
114
|
+
if (inlinePrompt && inlinePrompt.trim().length > 0) {
|
|
115
|
+
blocks.push({ type: 'text', content: inlinePrompt.trim() });
|
|
116
|
+
}
|
|
117
|
+
blocks.push({ type: 'text', content: buildOutputInstruction(output) });
|
|
118
|
+
blocks.push({ type: 'image_url', content: imageRef });
|
|
119
|
+
return { role: 'user', content: blocks };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Compose the instruction asking the model to emit exactly the named structured
|
|
123
|
+
* output. For a closed category set, the allowed values are enumerated so the
|
|
124
|
+
* model stays in-set.
|
|
125
|
+
*/
|
|
126
|
+
export function buildOutputInstruction(output) {
|
|
127
|
+
const base = `Analyze the image and respond with a JSON object containing a single key "${output.FeatureName}"`;
|
|
128
|
+
if (output.Kind === 'scalar') {
|
|
129
|
+
return `${base} whose value is a number.`;
|
|
130
|
+
}
|
|
131
|
+
if (output.AllowedCategories && output.AllowedCategories.length > 0) {
|
|
132
|
+
return `${base} whose value is exactly one of: ${output.AllowedCategories.join(', ')}.`;
|
|
133
|
+
}
|
|
134
|
+
return `${base} whose value is a short category label string.`;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Parse the runner's result into the feature cell value per the output contract.
|
|
138
|
+
* Handles both a structured `result` object (preferred) and a raw JSON/text
|
|
139
|
+
* `rawResult` fallback. Returns `null` on failure, an unsuccessful run, or an
|
|
140
|
+
* out-of-set category.
|
|
141
|
+
*/
|
|
142
|
+
export function parseVisionOutput(runResult, output) {
|
|
143
|
+
if (!runResult.success) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
const raw = extractNamedValue(runResult, output.FeatureName);
|
|
147
|
+
if (raw === null) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
return output.Kind === 'scalar' ? coerceScalar(raw) : coerceCategory(raw, output.AllowedCategories);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Pull the named output value from a prompt run result, preferring the parsed
|
|
154
|
+
* structured `result` object and falling back to parsing `rawResult` as JSON.
|
|
155
|
+
*/
|
|
156
|
+
function extractNamedValue(runResult, featureName) {
|
|
157
|
+
const fromStructured = readKey(runResult.result, featureName);
|
|
158
|
+
if (fromStructured !== undefined) {
|
|
159
|
+
return fromStructured ?? null;
|
|
160
|
+
}
|
|
161
|
+
const parsed = tryParseJSON(runResult.rawResult);
|
|
162
|
+
const fromRaw = readKey(parsed, featureName);
|
|
163
|
+
return fromRaw === undefined ? null : (fromRaw ?? null);
|
|
164
|
+
}
|
|
165
|
+
/** Read a string-keyed property from an unknown value when it's a plain object. */
|
|
166
|
+
function readKey(value, key) {
|
|
167
|
+
if (value !== null && typeof value === 'object' && key in value) {
|
|
168
|
+
return value[key];
|
|
169
|
+
}
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
/** Parse a string as JSON, returning `null` on any failure or non-string input. */
|
|
173
|
+
function tryParseJSON(raw) {
|
|
174
|
+
if (typeof raw !== 'string' || raw.trim().length === 0) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
return JSON.parse(raw);
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/** Coerce a raw value into a finite number, or `null` when it isn't numeric. */
|
|
185
|
+
function coerceScalar(raw) {
|
|
186
|
+
if (typeof raw === 'number') {
|
|
187
|
+
return Number.isFinite(raw) ? raw : null;
|
|
188
|
+
}
|
|
189
|
+
if (typeof raw === 'string') {
|
|
190
|
+
const n = Number(raw.trim());
|
|
191
|
+
return Number.isFinite(n) ? n : null;
|
|
192
|
+
}
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Coerce a raw value into a category label string, enforcing the closed set when
|
|
197
|
+
* one is configured (out-of-set → `null` so an unexpected label never enters the
|
|
198
|
+
* matrix). Matching is case-insensitive but returns the canonical configured value.
|
|
199
|
+
*/
|
|
200
|
+
function coerceCategory(raw, allowed) {
|
|
201
|
+
const label = typeof raw === 'string' ? raw.trim() : String(raw).trim();
|
|
202
|
+
if (label.length === 0) {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
if (!allowed || allowed.length === 0) {
|
|
206
|
+
return label;
|
|
207
|
+
}
|
|
208
|
+
const match = allowed.find((a) => a.toLowerCase() === label.toLowerCase());
|
|
209
|
+
return match ?? null;
|
|
210
|
+
}
|
|
211
|
+
//# sourceMappingURL=vision-llm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vision-llm.js","sourceRoot":"","sources":["../../src/feature-assembly/vision-llm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAkC9D;;;;GAIG;AACH,MAAM,OAAO,sBAAsB;IACjC;;;OAGG;IACH,YACmB,MAA2B,EAC3B,WAAsB;QADtB,WAAM,GAAN,MAAM,CAAqB;QAC3B,gBAAW,GAAX,WAAW,CAAW;IACtC,CAAC;IAEJ;;;;;;;;;OASG;IACI,KAAK,CAAC,OAAO,CAClB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