@euconform/core 1.0.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/.turbo/turbo-build.log +4 -0
- package/LICENSE +21 -0
- package/dist/fairness-metrics.d.ts +65 -0
- package/dist/fairness-metrics.d.ts.map +1 -0
- package/dist/fairness-metrics.js +394 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/inference/browser-inference.d.ts +60 -0
- package/dist/inference/browser-inference.d.ts.map +1 -0
- package/dist/inference/browser-inference.js +221 -0
- package/dist/inference/capability-detection.d.ts +183 -0
- package/dist/inference/capability-detection.d.ts.map +1 -0
- package/dist/inference/capability-detection.js +552 -0
- package/dist/inference/index.d.ts +43 -0
- package/dist/inference/index.d.ts.map +1 -0
- package/dist/inference/index.js +97 -0
- package/dist/inference/inference-factory.d.ts +73 -0
- package/dist/inference/inference-factory.d.ts.map +1 -0
- package/dist/inference/inference-factory.js +175 -0
- package/dist/inference/ollama-client.d.ts +114 -0
- package/dist/inference/ollama-client.d.ts.map +1 -0
- package/dist/inference/ollama-client.js +386 -0
- package/dist/legal-checks/annex-iv-report.d.ts +131 -0
- package/dist/legal-checks/annex-iv-report.d.ts.map +1 -0
- package/dist/legal-checks/annex-iv-report.js +62 -0
- package/dist/legal-checks/bias-metrics.d.ts +162 -0
- package/dist/legal-checks/bias-metrics.d.ts.map +1 -0
- package/dist/legal-checks/bias-metrics.js +408 -0
- package/dist/legal-checks/data-governance.d.ts +30 -0
- package/dist/legal-checks/data-governance.d.ts.map +1 -0
- package/dist/legal-checks/data-governance.js +99 -0
- package/dist/legal-checks/human-oversight-logging.d.ts +27 -0
- package/dist/legal-checks/human-oversight-logging.d.ts.map +1 -0
- package/dist/legal-checks/human-oversight-logging.js +106 -0
- package/dist/legal-checks/index.d.ts +13 -0
- package/dist/legal-checks/index.d.ts.map +1 -0
- package/dist/legal-checks/index.js +6 -0
- package/dist/legal-checks/risk-classifier.d.ts +35 -0
- package/dist/legal-checks/risk-classifier.d.ts.map +1 -0
- package/dist/legal-checks/risk-classifier.js +234 -0
- package/dist/legal-checks/robustness-transparency.d.ts +45 -0
- package/dist/legal-checks/robustness-transparency.d.ts.map +1 -0
- package/dist/legal-checks/robustness-transparency.js +111 -0
- package/dist/metrics/crows-pairs-bias.d.ts +41 -0
- package/dist/metrics/crows-pairs-bias.d.ts.map +1 -0
- package/dist/metrics/crows-pairs-bias.js +172 -0
- package/dist/metrics/index.d.ts +6 -0
- package/dist/metrics/index.d.ts.map +1 -0
- package/dist/metrics/index.js +5 -0
- package/dist/metrics/stereotype-bias.d.ts +89 -0
- package/dist/metrics/stereotype-bias.d.ts.map +1 -0
- package/dist/metrics/stereotype-bias.js +157 -0
- package/dist/risk-engine.d.ts +23 -0
- package/dist/risk-engine.d.ts.map +1 -0
- package/dist/risk-engine.js +451 -0
- package/dist/types.d.ts +131 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/package.json +28 -0
- package/src/fairness-metrics.ts +498 -0
- package/src/index.ts +150 -0
- package/src/inference/browser-inference.ts +258 -0
- package/src/inference/capability-detection.ts +714 -0
- package/src/inference/index.ts +156 -0
- package/src/inference/inference-factory.ts +222 -0
- package/src/inference/ollama-client.ts +514 -0
- package/src/inference/transformers.d.ts +62 -0
- package/src/legal-checks/annex-iv-report.ts +157 -0
- package/src/legal-checks/bias-metrics.ts +590 -0
- package/src/legal-checks/data-governance.ts +148 -0
- package/src/legal-checks/human-oversight-logging.ts +135 -0
- package/src/legal-checks/index.ts +55 -0
- package/src/legal-checks/risk-classifier.ts +285 -0
- package/src/legal-checks/robustness-transparency.ts +162 -0
- package/src/metrics/crows-pairs-bias.ts +245 -0
- package/src/metrics/index.ts +22 -0
- package/src/metrics/stereotype-bias.ts +267 -0
- package/src/risk-engine.ts +525 -0
- package/src/types.ts +154 -0
- package/tests/annex-iv-report.test.ts +323 -0
- package/tests/crows-pairs-bias.test.ts +342 -0
- package/tests/fairness-metrics.test.ts +187 -0
- package/tests/integration/bias-calculation.integration.test.ts +688 -0
- package/tests/legal-checks.test.ts +53 -0
- package/tests/ollama-client.test.ts +427 -0
- package/tests/risk-engine.test.ts +149 -0
- package/tsconfig.json +11 -0
- package/vitest.config.ts +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Benedikt Hiepler
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fairness Metrics for AI Bias Detection
|
|
3
|
+
* Based on EU AI Act requirements for non-discrimination
|
|
4
|
+
*/
|
|
5
|
+
import type { BrowserInference } from "./inference/browser-inference";
|
|
6
|
+
import { type InferenceResult } from "./inference/inference-factory";
|
|
7
|
+
import type { OllamaClient } from "./inference/ollama-client";
|
|
8
|
+
import type { BiasTestConfig, BiasTestResult, FairnessMetric, PredictionData } from "./types";
|
|
9
|
+
/**
|
|
10
|
+
* Calculate Disparate Impact Ratio
|
|
11
|
+
* Measures whether protected groups receive favorable outcomes at similar rates
|
|
12
|
+
*
|
|
13
|
+
* Formula: P(positive | unprivileged) / P(positive | privileged)
|
|
14
|
+
* Threshold: > 0.8 (80% rule from US EEOC guidelines, also applicable in EU context)
|
|
15
|
+
*/
|
|
16
|
+
export declare function disparateImpact(data: PredictionData[]): FairnessMetric;
|
|
17
|
+
/**
|
|
18
|
+
* Calculate Statistical Parity Difference
|
|
19
|
+
* Measures the difference in positive outcome rates between groups
|
|
20
|
+
*
|
|
21
|
+
* Threshold: |difference| < 0.1
|
|
22
|
+
*/
|
|
23
|
+
export declare function statisticalParityDifference(data: PredictionData[]): FairnessMetric;
|
|
24
|
+
/**
|
|
25
|
+
* Calculate Equalized Odds Difference
|
|
26
|
+
* Measures whether TPR and FPR are equal across groups
|
|
27
|
+
* Requires labels to be present
|
|
28
|
+
*
|
|
29
|
+
* Threshold: |difference| < 0.1 for both TPR and FPR
|
|
30
|
+
*/
|
|
31
|
+
export declare function equalizedOdds(data: PredictionData[]): FairnessMetric;
|
|
32
|
+
/**
|
|
33
|
+
* Calculate Predictive Parity (Precision equality across groups)
|
|
34
|
+
* Measures whether precision is equal across groups
|
|
35
|
+
*/
|
|
36
|
+
export declare function predictiveParity(data: PredictionData[]): FairnessMetric;
|
|
37
|
+
/**
|
|
38
|
+
* Run all fairness metrics on prediction data
|
|
39
|
+
*/
|
|
40
|
+
export declare function runFairnessAnalysis(data: PredictionData[], modelId: string, protectedAttribute: string): BiasTestResult;
|
|
41
|
+
/**
|
|
42
|
+
* Run CrowS-Pairs bias analysis using the specified inference client
|
|
43
|
+
* This is the preferred method for bias detection as it uses the CrowS-Pairs methodology
|
|
44
|
+
*/
|
|
45
|
+
export declare function runCrowsPairsBiasAnalysis(config: BiasTestConfig, inferenceClient: BrowserInference | OllamaClient): Promise<BiasTestResult>;
|
|
46
|
+
/**
|
|
47
|
+
* Run CrowS-Pairs bias analysis using InferenceFactory with automatic method detection
|
|
48
|
+
* This version provides method metadata and automatic best method selection
|
|
49
|
+
*/
|
|
50
|
+
export declare function runCrowsPairsBiasAnalysisWithFactory(config: BiasTestConfig, statusCallback?: (status: string) => void): Promise<InferenceResult<BiasTestResult>>;
|
|
51
|
+
/**
|
|
52
|
+
* Run comprehensive bias analysis including both traditional metrics and CrowS-Pairs
|
|
53
|
+
* Combines prediction-based fairness metrics with CrowS-Pairs stereotypical bias detection
|
|
54
|
+
*/
|
|
55
|
+
export declare function runComprehensiveBiasAnalysis(predictionData: PredictionData[], crowsPairsConfig: BiasTestConfig, inferenceClient: BrowserInference | OllamaClient, protectedAttribute: string): Promise<BiasTestResult>;
|
|
56
|
+
/**
|
|
57
|
+
* Run comprehensive bias analysis using InferenceFactory with automatic method detection
|
|
58
|
+
* Combines prediction-based fairness metrics with CrowS-Pairs stereotypical bias detection
|
|
59
|
+
*/
|
|
60
|
+
export declare function runComprehensiveBiasAnalysisWithFactory(predictionData: PredictionData[], crowsPairsConfig: BiasTestConfig, protectedAttribute: string, statusCallback?: (status: string) => void): Promise<InferenceResult<BiasTestResult>>;
|
|
61
|
+
/**
|
|
62
|
+
* Generate simulated loan prediction data for demo purposes
|
|
63
|
+
*/
|
|
64
|
+
export declare function generateLoanDataset(size?: number): PredictionData[];
|
|
65
|
+
//# sourceMappingURL=fairness-metrics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fairness-metrics.d.ts","sourceRoot":"","sources":["../src/fairness-metrics.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAoB,KAAK,eAAe,EAAE,MAAM,+BAA+B,CAAC;AACvF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAE9D,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9F;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,cAAc,EAAE,GAAG,cAAc,CA0BtE;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAAC,IAAI,EAAE,cAAc,EAAE,GAAG,cAAc,CAsBlF;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,cAAc,EAAE,GAAG,cAAc,CAkDpE;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,cAAc,EAAE,GAAG,cAAc,CAmCvE;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,cAAc,EAAE,EACtB,OAAO,EAAE,MAAM,EACf,kBAAkB,EAAE,MAAM,GACzB,cAAc,CAoBhB;AAED;;;GAGG;AACH,wBAAsB,yBAAyB,CAC7C,MAAM,EAAE,cAAc,EACtB,eAAe,EAAE,gBAAgB,GAAG,YAAY,GAC/C,OAAO,CAAC,cAAc,CAAC,CA+BzB;AAED;;;GAGG;AACH,wBAAsB,oCAAoC,CACxD,MAAM,EAAE,cAAc,EACtB,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GACxC,OAAO,CAAC,eAAe,CAAC,cAAc,CAAC,CAAC,CAgD1C;AAED;;;GAGG;AACH,wBAAsB,4BAA4B,CAChD,cAAc,EAAE,cAAc,EAAE,EAChC,gBAAgB,EAAE,cAAc,EAChC,eAAe,EAAE,gBAAgB,GAAG,YAAY,EAChD,kBAAkB,EAAE,MAAM,GACzB,OAAO,CAAC,cAAc,CAAC,CAqDzB;AAED;;;GAGG;AACH,wBAAsB,uCAAuC,CAC3D,cAAc,EAAE,cAAc,EAAE,EAChC,gBAAgB,EAAE,cAAc,EAChC,kBAAkB,EAAE,MAAM,EAC1B,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GACxC,OAAO,CAAC,eAAe,CAAC,cAAc,CAAC,CAAC,CAkF1C;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,SAAO,GAAG,cAAc,EAAE,CAmBjE"}
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fairness Metrics for AI Bias Detection
|
|
3
|
+
* Based on EU AI Act requirements for non-discrimination
|
|
4
|
+
*/
|
|
5
|
+
import { InferenceFactory } from "./inference/inference-factory";
|
|
6
|
+
import { calculateCrowsPairsBias } from "./metrics/crows-pairs-bias";
|
|
7
|
+
/**
|
|
8
|
+
* Calculate Disparate Impact Ratio
|
|
9
|
+
* Measures whether protected groups receive favorable outcomes at similar rates
|
|
10
|
+
*
|
|
11
|
+
* Formula: P(positive | unprivileged) / P(positive | privileged)
|
|
12
|
+
* Threshold: > 0.8 (80% rule from US EEOC guidelines, also applicable in EU context)
|
|
13
|
+
*/
|
|
14
|
+
export function disparateImpact(data) {
|
|
15
|
+
const groups = groupByProtectedAttribute(data);
|
|
16
|
+
const groupRates = new Map();
|
|
17
|
+
for (const [group, predictions] of groups) {
|
|
18
|
+
const positiveCount = predictions.filter((p) => p.prediction === 1 || p.prediction === true).length;
|
|
19
|
+
const rate = positiveCount / predictions.length;
|
|
20
|
+
groupRates.set(group, rate);
|
|
21
|
+
}
|
|
22
|
+
const rates = Array.from(groupRates.values());
|
|
23
|
+
const minRate = Math.min(...rates);
|
|
24
|
+
const maxRate = Math.max(...rates);
|
|
25
|
+
const ratio = maxRate === 0 ? 1 : minRate / maxRate;
|
|
26
|
+
const threshold = 0.8;
|
|
27
|
+
return {
|
|
28
|
+
name: "Disparate Impact Ratio",
|
|
29
|
+
value: Number(ratio.toFixed(4)),
|
|
30
|
+
threshold,
|
|
31
|
+
passed: ratio >= threshold,
|
|
32
|
+
description: `Ratio of positive outcome rates between groups: ${(ratio * 100).toFixed(1)}%. Should be ≥80%.`,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Calculate Statistical Parity Difference
|
|
37
|
+
* Measures the difference in positive outcome rates between groups
|
|
38
|
+
*
|
|
39
|
+
* Threshold: |difference| < 0.1
|
|
40
|
+
*/
|
|
41
|
+
export function statisticalParityDifference(data) {
|
|
42
|
+
const groups = groupByProtectedAttribute(data);
|
|
43
|
+
const groupRates = new Map();
|
|
44
|
+
for (const [group, predictions] of groups) {
|
|
45
|
+
const positiveCount = predictions.filter((p) => p.prediction === 1 || p.prediction === true).length;
|
|
46
|
+
groupRates.set(group, positiveCount / predictions.length);
|
|
47
|
+
}
|
|
48
|
+
const rates = Array.from(groupRates.values());
|
|
49
|
+
const maxDiff = Math.max(...rates) - Math.min(...rates);
|
|
50
|
+
const threshold = 0.1;
|
|
51
|
+
return {
|
|
52
|
+
name: "Statistical Parity Difference",
|
|
53
|
+
value: Number(maxDiff.toFixed(4)),
|
|
54
|
+
threshold,
|
|
55
|
+
passed: maxDiff < threshold,
|
|
56
|
+
description: `Maximum difference in positive rates: ${(maxDiff * 100).toFixed(1)}%. Should be <10%.`,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Calculate Equalized Odds Difference
|
|
61
|
+
* Measures whether TPR and FPR are equal across groups
|
|
62
|
+
* Requires labels to be present
|
|
63
|
+
*
|
|
64
|
+
* Threshold: |difference| < 0.1 for both TPR and FPR
|
|
65
|
+
*/
|
|
66
|
+
export function equalizedOdds(data) {
|
|
67
|
+
const dataWithLabels = data.filter((d) => d.label !== undefined);
|
|
68
|
+
if (dataWithLabels.length === 0) {
|
|
69
|
+
return {
|
|
70
|
+
name: "Equalized Odds",
|
|
71
|
+
value: 0,
|
|
72
|
+
threshold: 0.1,
|
|
73
|
+
passed: false,
|
|
74
|
+
description: "Cannot calculate - labels required",
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
const groups = groupByProtectedAttribute(dataWithLabels);
|
|
78
|
+
const tprByGroup = new Map();
|
|
79
|
+
const fprByGroup = new Map();
|
|
80
|
+
for (const [group, predictions] of groups) {
|
|
81
|
+
const positiveLabels = predictions.filter((p) => p.label === 1 || p.label === true);
|
|
82
|
+
const negativeLabels = predictions.filter((p) => p.label === 0 || p.label === false);
|
|
83
|
+
// True Positive Rate
|
|
84
|
+
const truePositives = positiveLabels.filter((p) => p.prediction === 1 || p.prediction === true).length;
|
|
85
|
+
const tpr = positiveLabels.length > 0 ? truePositives / positiveLabels.length : 0;
|
|
86
|
+
tprByGroup.set(group, tpr);
|
|
87
|
+
// False Positive Rate
|
|
88
|
+
const falsePositives = negativeLabels.filter((p) => p.prediction === 1 || p.prediction === true).length;
|
|
89
|
+
const fpr = negativeLabels.length > 0 ? falsePositives / negativeLabels.length : 0;
|
|
90
|
+
fprByGroup.set(group, fpr);
|
|
91
|
+
}
|
|
92
|
+
const tprValues = Array.from(tprByGroup.values());
|
|
93
|
+
const fprValues = Array.from(fprByGroup.values());
|
|
94
|
+
const tprDiff = Math.max(...tprValues) - Math.min(...tprValues);
|
|
95
|
+
const fprDiff = Math.max(...fprValues) - Math.min(...fprValues);
|
|
96
|
+
const maxDiff = Math.max(tprDiff, fprDiff);
|
|
97
|
+
const threshold = 0.1;
|
|
98
|
+
return {
|
|
99
|
+
name: "Equalized Odds",
|
|
100
|
+
value: Number(maxDiff.toFixed(4)),
|
|
101
|
+
threshold,
|
|
102
|
+
passed: maxDiff < threshold,
|
|
103
|
+
description: `Max TPR/FPR difference: ${(maxDiff * 100).toFixed(1)}%. Should be <10%.`,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Calculate Predictive Parity (Precision equality across groups)
|
|
108
|
+
* Measures whether precision is equal across groups
|
|
109
|
+
*/
|
|
110
|
+
export function predictiveParity(data) {
|
|
111
|
+
const dataWithLabels = data.filter((d) => d.label !== undefined);
|
|
112
|
+
if (dataWithLabels.length === 0) {
|
|
113
|
+
return {
|
|
114
|
+
name: "Predictive Parity",
|
|
115
|
+
value: 0,
|
|
116
|
+
threshold: 0.1,
|
|
117
|
+
passed: false,
|
|
118
|
+
description: "Cannot calculate - labels required",
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const groups = groupByProtectedAttribute(dataWithLabels);
|
|
122
|
+
const precisionByGroup = new Map();
|
|
123
|
+
for (const [group, predictions] of groups) {
|
|
124
|
+
const predictedPositive = predictions.filter((p) => p.prediction === 1 || p.prediction === true);
|
|
125
|
+
const truePositives = predictedPositive.filter((p) => p.label === 1 || p.label === true).length;
|
|
126
|
+
const precision = predictedPositive.length > 0 ? truePositives / predictedPositive.length : 0;
|
|
127
|
+
precisionByGroup.set(group, precision);
|
|
128
|
+
}
|
|
129
|
+
const precisions = Array.from(precisionByGroup.values());
|
|
130
|
+
const maxDiff = Math.max(...precisions) - Math.min(...precisions);
|
|
131
|
+
const threshold = 0.1;
|
|
132
|
+
return {
|
|
133
|
+
name: "Predictive Parity",
|
|
134
|
+
value: Number(maxDiff.toFixed(4)),
|
|
135
|
+
threshold,
|
|
136
|
+
passed: maxDiff < threshold,
|
|
137
|
+
description: `Max precision difference: ${(maxDiff * 100).toFixed(1)}%. Should be <10%.`,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Run all fairness metrics on prediction data
|
|
142
|
+
*/
|
|
143
|
+
export function runFairnessAnalysis(data, modelId, protectedAttribute) {
|
|
144
|
+
const metrics = [
|
|
145
|
+
disparateImpact(data),
|
|
146
|
+
statisticalParityDifference(data),
|
|
147
|
+
equalizedOdds(data),
|
|
148
|
+
predictiveParity(data),
|
|
149
|
+
];
|
|
150
|
+
const overallPassed = metrics.every((m) => m.passed);
|
|
151
|
+
return {
|
|
152
|
+
modelId,
|
|
153
|
+
timestamp: new Date().toISOString(),
|
|
154
|
+
method: "logprobs_fallback_latency", // Default for traditional metrics
|
|
155
|
+
engine: "browser", // Default engine
|
|
156
|
+
protectedAttribute,
|
|
157
|
+
metrics,
|
|
158
|
+
overallPassed,
|
|
159
|
+
samplesAnalyzed: data.length,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Run CrowS-Pairs bias analysis using the specified inference client
|
|
164
|
+
* This is the preferred method for bias detection as it uses the CrowS-Pairs methodology
|
|
165
|
+
*/
|
|
166
|
+
export async function runCrowsPairsBiasAnalysis(config, inferenceClient) {
|
|
167
|
+
try {
|
|
168
|
+
const crowsPairsResult = await calculateCrowsPairsBias(config, inferenceClient);
|
|
169
|
+
// Determine if bias test passed based on score threshold
|
|
170
|
+
// A score close to 0 indicates less bias, positive scores indicate stereotypical bias
|
|
171
|
+
const biasThreshold = 0.1; // Configurable threshold for acceptable bias
|
|
172
|
+
const overallPassed = Math.abs(crowsPairsResult.score) < biasThreshold;
|
|
173
|
+
return {
|
|
174
|
+
modelId: config.model,
|
|
175
|
+
timestamp: new Date().toISOString(),
|
|
176
|
+
method: crowsPairsResult.method,
|
|
177
|
+
engine: config.engine,
|
|
178
|
+
crowsPairsResult,
|
|
179
|
+
overallPassed,
|
|
180
|
+
samplesAnalyzed: crowsPairsResult.pairsAnalyzed,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
console.error("CrowS-Pairs bias analysis failed:", error);
|
|
185
|
+
// Return failed result with error information
|
|
186
|
+
return {
|
|
187
|
+
modelId: config.model,
|
|
188
|
+
timestamp: new Date().toISOString(),
|
|
189
|
+
method: "logprobs_fallback_latency", // Default fallback
|
|
190
|
+
engine: config.engine,
|
|
191
|
+
overallPassed: false,
|
|
192
|
+
samplesAnalyzed: 0,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Run CrowS-Pairs bias analysis using InferenceFactory with automatic method detection
|
|
198
|
+
* This version provides method metadata and automatic best method selection
|
|
199
|
+
*/
|
|
200
|
+
export async function runCrowsPairsBiasAnalysisWithFactory(config, statusCallback) {
|
|
201
|
+
try {
|
|
202
|
+
const factory = new InferenceFactory(statusCallback);
|
|
203
|
+
const result = await factory.createBiasCalculation(config);
|
|
204
|
+
// Determine if bias test passed based on score threshold
|
|
205
|
+
const biasThreshold = 0.1; // Configurable threshold for acceptable bias
|
|
206
|
+
const overallPassed = Math.abs(result.data.score) < biasThreshold;
|
|
207
|
+
const biasTestResult = {
|
|
208
|
+
modelId: config.model,
|
|
209
|
+
timestamp: new Date().toISOString(),
|
|
210
|
+
method: result.method,
|
|
211
|
+
engine: result.engine,
|
|
212
|
+
crowsPairsResult: result.data,
|
|
213
|
+
overallPassed,
|
|
214
|
+
samplesAnalyzed: result.data.pairsAnalyzed,
|
|
215
|
+
};
|
|
216
|
+
// Clean up factory resources
|
|
217
|
+
factory.dispose();
|
|
218
|
+
return {
|
|
219
|
+
data: biasTestResult,
|
|
220
|
+
method: result.method,
|
|
221
|
+
engine: result.engine,
|
|
222
|
+
model: result.model,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
console.error("CrowS-Pairs bias analysis with factory failed:", error);
|
|
227
|
+
// Return failed result with error information
|
|
228
|
+
const failedResult = {
|
|
229
|
+
modelId: config.model,
|
|
230
|
+
timestamp: new Date().toISOString(),
|
|
231
|
+
method: "logprobs_fallback_latency", // Default fallback
|
|
232
|
+
engine: config.engine,
|
|
233
|
+
overallPassed: false,
|
|
234
|
+
samplesAnalyzed: 0,
|
|
235
|
+
};
|
|
236
|
+
return {
|
|
237
|
+
data: failedResult,
|
|
238
|
+
method: "logprobs_fallback_latency",
|
|
239
|
+
engine: config.engine,
|
|
240
|
+
model: config.model,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Run comprehensive bias analysis including both traditional metrics and CrowS-Pairs
|
|
246
|
+
* Combines prediction-based fairness metrics with CrowS-Pairs stereotypical bias detection
|
|
247
|
+
*/
|
|
248
|
+
export async function runComprehensiveBiasAnalysis(predictionData, crowsPairsConfig, inferenceClient, protectedAttribute) {
|
|
249
|
+
try {
|
|
250
|
+
// Run CrowS-Pairs analysis (primary method)
|
|
251
|
+
const crowsPairsResult = await calculateCrowsPairsBias(crowsPairsConfig, inferenceClient);
|
|
252
|
+
// Run traditional fairness metrics if prediction data is available
|
|
253
|
+
let traditionalMetrics = [];
|
|
254
|
+
if (predictionData && predictionData.length > 0) {
|
|
255
|
+
const traditionalResult = runFairnessAnalysis(predictionData, crowsPairsConfig.model, protectedAttribute);
|
|
256
|
+
traditionalMetrics = traditionalResult.metrics || [];
|
|
257
|
+
}
|
|
258
|
+
// Determine overall pass/fail based on CrowS-Pairs result and traditional metrics
|
|
259
|
+
const biasThreshold = 0.1;
|
|
260
|
+
const crowsPairsPassed = Math.abs(crowsPairsResult.score) < biasThreshold;
|
|
261
|
+
const traditionalPassed = traditionalMetrics.length === 0 || traditionalMetrics.every((m) => m.passed);
|
|
262
|
+
const overallPassed = crowsPairsPassed && traditionalPassed;
|
|
263
|
+
return {
|
|
264
|
+
modelId: crowsPairsConfig.model,
|
|
265
|
+
timestamp: new Date().toISOString(),
|
|
266
|
+
method: crowsPairsResult.method,
|
|
267
|
+
engine: crowsPairsConfig.engine,
|
|
268
|
+
protectedAttribute,
|
|
269
|
+
crowsPairsResult,
|
|
270
|
+
metrics: traditionalMetrics.length > 0 ? traditionalMetrics : undefined,
|
|
271
|
+
overallPassed,
|
|
272
|
+
samplesAnalyzed: crowsPairsResult.pairsAnalyzed + (predictionData?.length || 0),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
catch (error) {
|
|
276
|
+
console.error("Comprehensive bias analysis failed:", error);
|
|
277
|
+
// Fallback to traditional metrics only if CrowS-Pairs fails
|
|
278
|
+
if (predictionData && predictionData.length > 0) {
|
|
279
|
+
return runFairnessAnalysis(predictionData, crowsPairsConfig.model, protectedAttribute);
|
|
280
|
+
}
|
|
281
|
+
// Return failed result if both methods fail
|
|
282
|
+
return {
|
|
283
|
+
modelId: crowsPairsConfig.model,
|
|
284
|
+
timestamp: new Date().toISOString(),
|
|
285
|
+
method: "logprobs_fallback_latency",
|
|
286
|
+
engine: crowsPairsConfig.engine,
|
|
287
|
+
protectedAttribute,
|
|
288
|
+
overallPassed: false,
|
|
289
|
+
samplesAnalyzed: 0,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Run comprehensive bias analysis using InferenceFactory with automatic method detection
|
|
295
|
+
* Combines prediction-based fairness metrics with CrowS-Pairs stereotypical bias detection
|
|
296
|
+
*/
|
|
297
|
+
export async function runComprehensiveBiasAnalysisWithFactory(predictionData, crowsPairsConfig, protectedAttribute, statusCallback) {
|
|
298
|
+
try {
|
|
299
|
+
const factory = new InferenceFactory(statusCallback);
|
|
300
|
+
// Run CrowS-Pairs analysis (primary method) with automatic method detection
|
|
301
|
+
const crowsPairsResult = await factory.createBiasCalculation(crowsPairsConfig);
|
|
302
|
+
// Run traditional fairness metrics if prediction data is available
|
|
303
|
+
let traditionalMetrics = [];
|
|
304
|
+
if (predictionData && predictionData.length > 0) {
|
|
305
|
+
const traditionalResult = runFairnessAnalysis(predictionData, crowsPairsConfig.model, protectedAttribute);
|
|
306
|
+
traditionalMetrics = traditionalResult.metrics || [];
|
|
307
|
+
}
|
|
308
|
+
// Determine overall pass/fail based on CrowS-Pairs result and traditional metrics
|
|
309
|
+
const biasThreshold = 0.1;
|
|
310
|
+
const crowsPairsPassed = Math.abs(crowsPairsResult.data.score) < biasThreshold;
|
|
311
|
+
const traditionalPassed = traditionalMetrics.length === 0 || traditionalMetrics.every((m) => m.passed);
|
|
312
|
+
const overallPassed = crowsPairsPassed && traditionalPassed;
|
|
313
|
+
const biasTestResult = {
|
|
314
|
+
modelId: crowsPairsConfig.model,
|
|
315
|
+
timestamp: new Date().toISOString(),
|
|
316
|
+
method: crowsPairsResult.method,
|
|
317
|
+
engine: crowsPairsResult.engine,
|
|
318
|
+
protectedAttribute,
|
|
319
|
+
crowsPairsResult: crowsPairsResult.data,
|
|
320
|
+
metrics: traditionalMetrics.length > 0 ? traditionalMetrics : undefined,
|
|
321
|
+
overallPassed,
|
|
322
|
+
samplesAnalyzed: crowsPairsResult.data.pairsAnalyzed + (predictionData?.length || 0),
|
|
323
|
+
};
|
|
324
|
+
// Clean up factory resources
|
|
325
|
+
factory.dispose();
|
|
326
|
+
return {
|
|
327
|
+
data: biasTestResult,
|
|
328
|
+
method: crowsPairsResult.method,
|
|
329
|
+
engine: crowsPairsResult.engine,
|
|
330
|
+
model: crowsPairsResult.model,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
catch (error) {
|
|
334
|
+
console.error("Comprehensive bias analysis with factory failed:", error);
|
|
335
|
+
// Fallback to traditional metrics only if CrowS-Pairs fails
|
|
336
|
+
if (predictionData && predictionData.length > 0) {
|
|
337
|
+
const traditionalResult = runFairnessAnalysis(predictionData, crowsPairsConfig.model, protectedAttribute);
|
|
338
|
+
return {
|
|
339
|
+
data: traditionalResult,
|
|
340
|
+
method: "logprobs_fallback_latency",
|
|
341
|
+
engine: crowsPairsConfig.engine,
|
|
342
|
+
model: crowsPairsConfig.model,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
// Return failed result if both methods fail
|
|
346
|
+
const failedResult = {
|
|
347
|
+
modelId: crowsPairsConfig.model,
|
|
348
|
+
timestamp: new Date().toISOString(),
|
|
349
|
+
method: "logprobs_fallback_latency",
|
|
350
|
+
engine: crowsPairsConfig.engine,
|
|
351
|
+
protectedAttribute,
|
|
352
|
+
overallPassed: false,
|
|
353
|
+
samplesAnalyzed: 0,
|
|
354
|
+
};
|
|
355
|
+
return {
|
|
356
|
+
data: failedResult,
|
|
357
|
+
method: "logprobs_fallback_latency",
|
|
358
|
+
engine: crowsPairsConfig.engine,
|
|
359
|
+
model: crowsPairsConfig.model,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Generate simulated loan prediction data for demo purposes
|
|
365
|
+
*/
|
|
366
|
+
export function generateLoanDataset(size = 1000) {
|
|
367
|
+
const data = [];
|
|
368
|
+
const groups = ["male", "female", "non-binary"];
|
|
369
|
+
for (let i = 0; i < size; i++) {
|
|
370
|
+
const group = groups[Math.floor(Math.random() * groups.length)];
|
|
371
|
+
// Simulate slight bias against non-male groups
|
|
372
|
+
const biasedThreshold = group === "male" ? 0.65 : 0.55;
|
|
373
|
+
const prediction = Math.random() < biasedThreshold ? 1 : 0;
|
|
374
|
+
const label = Math.random() < 0.6 ? 1 : 0;
|
|
375
|
+
data.push({
|
|
376
|
+
prediction,
|
|
377
|
+
label,
|
|
378
|
+
protectedGroup: group,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
return data;
|
|
382
|
+
}
|
|
383
|
+
// Helper functions
|
|
384
|
+
function groupByProtectedAttribute(data) {
|
|
385
|
+
const groups = new Map();
|
|
386
|
+
for (const item of data) {
|
|
387
|
+
const group = item.protectedGroup;
|
|
388
|
+
if (!groups.has(group)) {
|
|
389
|
+
groups.set(group, []);
|
|
390
|
+
}
|
|
391
|
+
groups.get(group)?.push(item);
|
|
392
|
+
}
|
|
393
|
+
return groups;
|
|
394
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @euconform/core - EU AI Act Compliance Core Library
|
|
3
|
+
*
|
|
4
|
+
* Risk classification engine and fairness metrics for AI systems
|
|
5
|
+
*/
|
|
6
|
+
export type { AnnexIVReport, BiasTestResult, CrowsPairsEntry, FairnessMetric, HighRiskCategory, PredictionData, QuizAnswer, QuizOption, QuizQuestion, RiskAssessment, RiskFlag, RiskLevel, BiasCalculationMethod, BiasTestConfig, CrowsPairsBiasResult, ModelCapabilityCache, } from "./types";
|
|
7
|
+
export { ANNEX_III_QUESTIONS, PROHIBITED_AI_SCREENING_QUESTIONS, classifyAnnexIIIRisk, getAnnexIIIQuestions, getProhibitedAIScreeningQuestions, AI_ACT_SOURCES, METRIC_SOURCES, createPerspectiveApiToxicityScorer, createTransformersRegardScorer, createTransformersToxicityScorer, runDataGovernanceChecklist, defaultTextPerturbations, runTextPerturbationRobustnessTest, getHumanOversightAndLoggingTemplate, buildAnnexIVReportV1, counterfactualFairness, demographicParityDifference, equalizedOddsDifference, runArticle10NumericBiasSuite, runCrowsPairsLogProbTest, runRegardTest, runToxicityTest, } from "./legal-checks";
|
|
8
|
+
export type { AnnexIIIAnswer, AnnexIIIRiskResult, Citation, CounterfactualPair, CrowsPairsLogProbOptions, CrowsPairsLogProbResult, ChecklistStatus, DataGovernanceChecklistResult, RobustnessResult, RobustnessTestCase, Perturbation, PredictTextFn, HumanOversightRecommendation, LoggingTemplate, AnnexIVStructuredReportV1, GroupTextSample, RegardResult, RegardScorer, SensitiveAttribute, ToxicityResult, ToxicityScorer, } from "./legal-checks";
|
|
9
|
+
export { classifyRisk, getComplianceGuidance, getQuizQuestions, RISK_QUIZ_QUESTIONS, } from "./risk-engine";
|
|
10
|
+
export { disparateImpact, equalizedOdds, generateLoanDataset, predictiveParity, runFairnessAnalysis, statisticalParityDifference, runCrowsPairsBiasAnalysis, runComprehensiveBiasAnalysis, runCrowsPairsBiasAnalysisWithFactory, runComprehensiveBiasAnalysisWithFactory, } from "./fairness-metrics";
|
|
11
|
+
export { detectOllama, listOllamaModels, OllamaClient, BrowserInference, checkWebGPU, BROWSER_MODELS, detectCapabilities, createInferenceClient, createAutoInferenceClient, createCapabilityDetectionService, DefaultCapabilityDetectionService, EnhancedCapabilityDetectionService, type OllamaModel, type BrowserModel, type InferenceEngine, type InferenceCapabilities, type InferenceClient, type CapabilityDetectionService, type ModelCapability, type ModelSelectionState, type CapabilityCacheEntry, type DetectionProgress, type UserPreferences, type EnhancedModelSelectionState, type CapabilityDetectionStatus, } from "./inference";
|
|
12
|
+
export { InferenceFactory, createInferenceFactory, detectBestMethodForConfig, type InferenceResult, } from "./inference/inference-factory";
|
|
13
|
+
export { testPair, aggregateByCategory, runStereotypeBiasTest, quickBiasCheck, type StereotypePair, type BiasType, type PairTestResult, type CategoryResult, type StereotypeBiasResult, calculateCrowsPairsBias, calculateLogProbBias, calculateLatencyBias, validateCrowsPairsDataset, } from "./metrics";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,YAAY,EACV,aAAa,EACb,cAAc,EACd,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,UAAU,EACV,UAAU,EACV,YAAY,EACZ,cAAc,EACd,QAAQ,EACR,SAAS,EACT,qBAAqB,EACrB,cAAc,EACd,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,SAAS,CAAC;AAGjB,OAAO,EACL,mBAAmB,EACnB,iCAAiC,EACjC,oBAAoB,EACpB,oBAAoB,EACpB,iCAAiC,EACjC,cAAc,EACd,cAAc,EACd,kCAAkC,EAClC,8BAA8B,EAC9B,gCAAgC,EAChC,0BAA0B,EAC1B,wBAAwB,EACxB,iCAAiC,EACjC,mCAAmC,EACnC,oBAAoB,EACpB,sBAAsB,EACtB,2BAA2B,EAC3B,uBAAuB,EACvB,4BAA4B,EAC5B,wBAAwB,EACxB,aAAa,EACb,eAAe,GAChB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EACV,cAAc,EACd,kBAAkB,EAClB,QAAQ,EACR,kBAAkB,EAClB,wBAAwB,EACxB,uBAAuB,EACvB,eAAe,EACf,6BAA6B,EAC7B,gBAAgB,EAChB,kBAAkB,EAClB,YAAY,EACZ,aAAa,EACb,4BAA4B,EAC5B,eAAe,EACf,yBAAyB,EACzB,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,cAAc,EACd,cAAc,GACf,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EACL,YAAY,EACZ,qBAAqB,EACrB,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,eAAe,CAAC;AAGvB,OAAO,EACL,eAAe,EACf,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EAChB,mBAAmB,EACnB,2BAA2B,EAC3B,yBAAyB,EACzB,4BAA4B,EAC5B,oCAAoC,EACpC,uCAAuC,GACxC,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,YAAY,EACZ,gBAAgB,EAChB,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EACrB,yBAAyB,EACzB,gCAAgC,EAChC,iCAAiC,EACjC,kCAAkC,EAClC,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,qBAAqB,EAC1B,KAAK,eAAe,EACpB,KAAK,0BAA0B,EAC/B,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,2BAA2B,EAChC,KAAK,yBAAyB,GAC/B,MAAM,aAAa,CAAC;AAGrB,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,yBAAyB,EACzB,KAAK,eAAe,GACrB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,EACL,QAAQ,EACR,mBAAmB,EACnB,qBAAqB,EACrB,cAAc,EACd,KAAK,cAAc,EACnB,KAAK,QAAQ,EACb,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,oBAAoB,EACzB,uBAAuB,EACvB,oBAAoB,EACpB,oBAAoB,EACpB,yBAAyB,GAC1B,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @euconform/core - EU AI Act Compliance Core Library
|
|
3
|
+
*
|
|
4
|
+
* Risk classification engine and fairness metrics for AI systems
|
|
5
|
+
*/
|
|
6
|
+
// Legal checks (EU AI Act mappings, non-legal-advice)
|
|
7
|
+
export { ANNEX_III_QUESTIONS, PROHIBITED_AI_SCREENING_QUESTIONS, classifyAnnexIIIRisk, getAnnexIIIQuestions, getProhibitedAIScreeningQuestions, AI_ACT_SOURCES, METRIC_SOURCES, createPerspectiveApiToxicityScorer, createTransformersRegardScorer, createTransformersToxicityScorer, runDataGovernanceChecklist, defaultTextPerturbations, runTextPerturbationRobustnessTest, getHumanOversightAndLoggingTemplate, buildAnnexIVReportV1, counterfactualFairness, demographicParityDifference, equalizedOddsDifference, runArticle10NumericBiasSuite, runCrowsPairsLogProbTest, runRegardTest, runToxicityTest, } from "./legal-checks";
|
|
8
|
+
// Risk Engine
|
|
9
|
+
export { classifyRisk, getComplianceGuidance, getQuizQuestions, RISK_QUIZ_QUESTIONS, } from "./risk-engine";
|
|
10
|
+
// Fairness Metrics
|
|
11
|
+
export { disparateImpact, equalizedOdds, generateLoanDataset, predictiveParity, runFairnessAnalysis, statisticalParityDifference, runCrowsPairsBiasAnalysis, runComprehensiveBiasAnalysis, runCrowsPairsBiasAnalysisWithFactory, runComprehensiveBiasAnalysisWithFactory, } from "./fairness-metrics";
|
|
12
|
+
// Inference Layer
|
|
13
|
+
export { detectOllama, listOllamaModels, OllamaClient, BrowserInference, checkWebGPU, BROWSER_MODELS, detectCapabilities, createInferenceClient, createAutoInferenceClient, createCapabilityDetectionService, DefaultCapabilityDetectionService, EnhancedCapabilityDetectionService, } from "./inference";
|
|
14
|
+
// Inference Factory
|
|
15
|
+
export { InferenceFactory, createInferenceFactory, detectBestMethodForConfig, } from "./inference/inference-factory";
|
|
16
|
+
// Stereotype Bias Metrics
|
|
17
|
+
export { testPair, aggregateByCategory, runStereotypeBiasTest, quickBiasCheck, calculateCrowsPairsBias, calculateLogProbBias, calculateLatencyBias, validateCrowsPairsDataset, } from "./metrics";
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-based inference using transformers.js
|
|
3
|
+
* For small models (≤3B params) that can run via WebGPU
|
|
4
|
+
*
|
|
5
|
+
* STRICT MODE: Real inference only. No demo data.
|
|
6
|
+
*/
|
|
7
|
+
export interface BrowserModel {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
size: string;
|
|
11
|
+
supported: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare const BROWSER_MODELS: BrowserModel[];
|
|
14
|
+
/**
|
|
15
|
+
* Check if WebGPU is available in the browser
|
|
16
|
+
*/
|
|
17
|
+
export declare function checkWebGPU(): Promise<boolean>;
|
|
18
|
+
/**
|
|
19
|
+
* Check if transformers.js is available
|
|
20
|
+
*/
|
|
21
|
+
export declare function checkTransformersAvailable(): Promise<boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Browser inference engine using transformers.js
|
|
24
|
+
* STRICT MODE: Throws error if package not installed.
|
|
25
|
+
*/
|
|
26
|
+
export declare class BrowserInference {
|
|
27
|
+
readonly modelId: string;
|
|
28
|
+
private model;
|
|
29
|
+
private tokenizer;
|
|
30
|
+
private pipeline;
|
|
31
|
+
private loadingPromise;
|
|
32
|
+
private loadError;
|
|
33
|
+
private statusCallback;
|
|
34
|
+
constructor(modelId?: string, statusCallback?: ((status: string) => void) | null);
|
|
35
|
+
/**
|
|
36
|
+
* Check if browser inference is available
|
|
37
|
+
*/
|
|
38
|
+
isAvailable(): Promise<boolean>;
|
|
39
|
+
/**
|
|
40
|
+
* Load the model (lazy loading)
|
|
41
|
+
* Handles concurrency with loadingPromise
|
|
42
|
+
*/
|
|
43
|
+
load(): Promise<void>;
|
|
44
|
+
private performLoad;
|
|
45
|
+
/**
|
|
46
|
+
* Generate text
|
|
47
|
+
*/
|
|
48
|
+
generate(prompt: string, maxTokens?: number): Promise<string>;
|
|
49
|
+
/**
|
|
50
|
+
* Calculate Log-Probability for a given text
|
|
51
|
+
* This is the "Gold Standard" for bias testing.
|
|
52
|
+
* We calculate the loss (negative log likelihood) of the text.
|
|
53
|
+
*/
|
|
54
|
+
getLogProb(text: string): Promise<number>;
|
|
55
|
+
/**
|
|
56
|
+
* Get list of supported browser models
|
|
57
|
+
*/
|
|
58
|
+
static getSupportedModels(): BrowserModel[];
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=browser-inference.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-inference.d.ts","sourceRoot":"","sources":["../../src/inference/browser-inference.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,OAAO,CAAC;CACpB;AAGD,eAAO,MAAM,cAAc,EAAE,YAAY,EAyBxC,CAAC;AAEF;;GAEG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC,CAcpD;AAED;;GAEG;AACH,wBAAsB,0BAA0B,IAAI,OAAO,CAAC,OAAO,CAAC,CAQnE;AAED;;;GAGG;AACH,qBAAa,gBAAgB;IAC3B,SAAgB,OAAO,EAAE,MAAM,CAAC;IAChC,OAAO,CAAC,KAAK,CAAiB;IAC9B,OAAO,CAAC,SAAS,CAAiB;IAClC,OAAO,CAAC,QAAQ,CAAiB;IACjC,OAAO,CAAC,cAAc,CAA8B;IACpD,OAAO,CAAC,SAAS,CAAsB;IACvC,OAAO,CAAC,cAAc,CAA2C;gBAG/D,OAAO,SAAsB,EAC7B,cAAc,GAAE,CAAC,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC,GAAG,IAAW;IAM1D;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAKrC;;;OAGG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;YAYb,WAAW;IAqCzB;;OAEG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,SAAK,GAAG,OAAO,CAAC,MAAM,CAAC;IAiB/D;;;;OAIG;IACG,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAmE/C;;OAEG;IACH,MAAM,CAAC,kBAAkB,IAAI,YAAY,EAAE;CAG5C"}
|