gheim 0.1.4 → 0.1.6

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.
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Calibrated detector — fixes the v1 model's "single-category mode"
3
+ * bug at inference time by subtracting a constant `oBias` from the O
4
+ * class logit before argmax.
5
+ *
6
+ * Empirical justification (see eval/calibration_sweep.json): on a
7
+ * 237-case multi-entity pathology suite the v1 model emits literally
8
+ * zero non-O tokens for "Müller" / "März" when an IBAN follows them —
9
+ * but the per-token softmax shows person at 47% vs O at 51%. A small
10
+ * additive bias on O flips those marginal cases without inducing false
11
+ * positives on PII-free text.
12
+ *
13
+ * Sweet spot: oBias=0.5:
14
+ * - pathology full-coverage 81% → 94.5% (+13.5pp)
15
+ * - test_v1 strict-F1 0.872 → 0.868 (−0.4pp)
16
+ * - ~0.05 extra FP per chunk on real text
17
+ *
18
+ * `defaultDetector()` returns this with oBias=0.5, so users get the
19
+ * better behavior automatically.
20
+ */
21
+ import type { Detector, Span } from "./base.ts";
22
+ import type { LocalDetectorLoadEvent } from "./local.ts";
23
+ /** Constructor options for {@link CalibratedDetector}. */
24
+ export interface CalibratedDetectorOptions {
25
+ /** HuggingFace model id (must be a token-classification head). */
26
+ model?: string;
27
+ /**
28
+ * Constant subtracted from the O-class logit before argmax. Default
29
+ * `0.5` is the Pareto-clean point per the full-scale bias sweep
30
+ * (eval/calibration_sweep.json):
31
+ *
32
+ * - oBias=0.0 uncalibrated (matches the raw model)
33
+ * - oBias=0.5 recovers ~95% of multi-entity pathology cases for
34
+ * ~0.4pp test_v1 strict-F1 cost
35
+ * - oBias=1.0 more aggressive: 96.6% pathology, 1.1pp test_v1 cost
36
+ * - oBias=1.5+ trades too much precision; not recommended
37
+ */
38
+ oBias?: number;
39
+ /** Backend selector. See {@link LocalDetectorOptions.device}. */
40
+ device?: string;
41
+ /** Quantization dtype: "fp32" | "fp16" | "q8" | "q4" | "auto". */
42
+ dtype?: string;
43
+ /** Sliding-window size in model tokens (default 510). */
44
+ chunkTokens?: number;
45
+ /** Overlap between consecutive windows (default 32). */
46
+ chunkOverlapTokens?: number;
47
+ /** Strip leading/trailing whitespace from each emitted span. Default true. */
48
+ trimWhitespace?: boolean;
49
+ /** Optional progress callback during model load (mirrors LocalDetector). */
50
+ onProgress?: (event: LocalDetectorLoadEvent) => void;
51
+ }
52
+ /** CalibratedDetector — local model + O-logit bias. */
53
+ export declare class CalibratedDetector implements Detector {
54
+ readonly model: string;
55
+ readonly oBias: number;
56
+ readonly device: string;
57
+ readonly dtype: string;
58
+ readonly chunkTokens: number;
59
+ readonly chunkOverlapTokens: number;
60
+ readonly trimWhitespace: boolean;
61
+ /** Backend actually used after `load()` resolves; `null` before. */
62
+ actualDevice: string | null;
63
+ private _model;
64
+ private _tokenizer;
65
+ private _id2label;
66
+ private _oId;
67
+ private _loadPromise;
68
+ private readonly _onProgress?;
69
+ constructor(opts?: CalibratedDetectorOptions);
70
+ load(): Promise<void>;
71
+ detect(text: string, opts?: {
72
+ model?: string;
73
+ }): Promise<Span[]>;
74
+ private _detectChunked;
75
+ private _detectWindow;
76
+ private _emitProgress;
77
+ }
78
+ //# sourceMappingURL=calibrated.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"calibrated.d.ts","sourceRoot":"","sources":["../../../src/detectors/calibrated.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAMzD,0DAA0D;AAC1D,MAAM,WAAW,yBAAyB;IACxC,kEAAkE;IAClE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;;;;;;;OAUG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,iEAAiE;IACjE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kEAAk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  export type { LocalDetectorOptions, LocalDetectorLoadEvent } from "./local.ts";
6
6
  export { CompositeDetector } from "./composite.ts";
7
7
  export type { CompositeDetectorOptions } from "./composite.ts";
8
+ export { CalibratedDetector } from "./calibrated.ts";
9
+ export type { CalibratedDetectorOptions } from "./calibrated.ts";
8
10
  import type { Detector } from "./base.ts";
9
- /** Pick a reasonable default: remote if `GHEIM_API_KEY` is set in env, else local. */
11
+ /** Pick a reasonable default: remote when `GHEIM_API_KEY` is set in env,
12
+ * else a calibrated local model.
13
+ *
14
+ * The calibrated default (`oBias=0.5`) recovers ~95% of the
15
+ * multi-entity-letter cases that the raw v1 model fails on, with
16
+ * ~0.4pp test_v1 strict-F1 cost. See eval/calibration_sweep.json. */
10
17
  export declare function defaultDetector(): Detector;
11
18
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/detectors/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EACL,gBAAgB,EAChB,aAAa,IAAI,oBAAoB,EACrC,cAAc,GACf,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,YAAY,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAC/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,YAAY,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAE/D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAI1C,sFAAsF;AACtF,wBAAgB,eAAe,IAAI,QAAQ,CAI1C"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/detectors/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EACL,gBAAgB,EAChB,aAAa,IAAI,oBAAoB,EACrC,cAAc,GACf,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,YAAY,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAC/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,YAAY,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,YAAY,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AAEjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAI1C;;;;;sEAKsE;AACtE,wBAAgB,eAAe,IAAI,QAAQ,CAI1C"}
@@ -13,11 +13,11 @@ export { LABEL_TO_TAG, SENTINEL_RE, isPossibleSentinelPrefix, labelTag, parseSen
13
13
  export type { Sentinel } from "./core/sentinels.ts";
14
14
  export { StreamDeanonymizer } from "./core/stream.ts";
15
15
  export type { Detector, Span } from "./detectors/base.ts";
16
- export { DEFAULT_BASE_URL, REMOTE_DEFAULT_MODEL, RemoteDetector, LocalDetector, CompositeDetector, defaultDetector, } from "./detectors/index.ts";
17
- export type { RemoteDetectorOptions, LocalDetectorOptions, LocalDetectorLoadEvent, CompositeDetectorOptions, } from "./detectors/index.ts";
16
+ export { DEFAULT_BASE_URL, REMOTE_DEFAULT_MODEL, RemoteDetector, LocalDetector, CompositeDetector, CalibratedDetector, defaultDetector, } from "./detectors/index.ts";
17
+ export type { RemoteDetectorOptions, LocalDetectorOptions, LocalDetectorLoadEvent, CompositeDetectorOptions, CalibratedDetectorOptions, } from "./detectors/index.ts";
18
18
  export { anonymizeMessages, anonymizeText, deanonymizeStream, deanonymizeText, } from "./plain.ts";
19
19
  export type { ChatMessage } from "./plain.ts";
20
- export declare const VERSION = "0.1.4";
20
+ export declare const VERSION = "0.1.6";
21
21
  export { mergeAdjacent } from "./core/session.ts";
22
22
  export type { MergedSpan } from "./core/session.ts";
23
23
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,YAAY,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EACL,YAAY,EACZ,WAAW,EACX,wBAAwB,EACxB,QAAQ,EACR,aAAa,EACb,cAAc,GACf,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,YAAY,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,cAAc,EACd,aAAa,EACb,iBAAiB,EACjB,eAAe,GAChB,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EACV,qBAAqB,EACrB,oBAAoB,EACpB,sBAAsB,EACtB,wBAAwB,GACzB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,iBAAiB,EACjB,aAAa,EACb,iBAAiB,EACjB,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAK9C,eAAO,MAAM,OAAO,UAAU,CAAC;AAE/B,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,YAAY,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,YAAY,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EACL,YAAY,EACZ,WAAW,EACX,wBAAwB,EACxB,QAAQ,EACR,aAAa,EACb,cAAc,GACf,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,YAAY,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,cAAc,EACd,aAAa,EACb,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,GAChB,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EACV,qBAAqB,EACrB,oBAAoB,EACpB,sBAAsB,EACtB,wBAAwB,EACxB,yBAAyB,GAC1B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,iBAAiB,EACjB,aAAa,EACb,iBAAiB,EACjB,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAK9C,eAAO,MAAM,OAAO,UAAU,CAAC;AAE/B,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,YAAY,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gheim",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "PII round-trip for LLM APIs: anonymize before the request, de-anonymize the stream on the way back.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Joel Barmettler <joel.barmettler@gmail.com>",