-
Notifications
You must be signed in to change notification settings - Fork 14
fix: improved small-model support #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 18 commits
f5b8b39
0a4df44
669d189
fbaa6ba
121c8e2
bbdb6dc
42992e5
f978fbd
2235b1e
eb209ad
792c9da
5b32f2e
94cb8e9
260ee41
87ae1eb
32e40a1
2a5c585
cf19ca3
19efe6e
4bbfc8d
5a49381
b2420de
bc67c1a
38aac1a
e98e7f2
097577a
c23c2e1
174266b
16bff86
ca08fb2
8060c4c
9d8674b
dc0bc84
576de6a
5fa19b7
329d488
5e55aea
5e1cd08
4a54b85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,269 @@ | ||||||||||||||||||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||||||||||||||||||
| # SPDX-License-Identifier: Apache-2.0 | ||||||||||||||||||
| """Server-side reconstruction of the strict EntityDispositionSchema from the | ||||||||||||||||||
| loose wire-contract SimpleDispositionResult + the per-entity context columns. | ||||||||||||||||||
|
|
||||||||||||||||||
| The disposition_analyzer LLM now emits a minimal `SimpleDispositionResult` | ||||||||||||||||||
| (8 optional/loose fields per item). This module rebuilds the strict form | ||||||||||||||||||
| deterministically: pair each simple item with its entity context (by id, | ||||||||||||||||||
| with entity_label/value echoes as belt-and-braces), derive needs_protection, | ||||||||||||||||||
| and template protection_reason when the model did not provide one. | ||||||||||||||||||
|
|
||||||||||||||||||
| No LLM calls; no I/O. Pure python for the reconstruction column. | ||||||||||||||||||
| """ | ||||||||||||||||||
|
|
||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||
|
|
||||||||||||||||||
| import logging | ||||||||||||||||||
|
|
||||||||||||||||||
| from anonymizer.engine.schemas.rewrite import ( | ||||||||||||||||||
| EntityDispositionSchema, | ||||||||||||||||||
| SensitivityDispositionSchema, | ||||||||||||||||||
| SimpleDispositionItem, | ||||||||||||||||||
| SimpleDispositionResult, | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| # --------------------------------------------------------------------------- | ||||||||||||||||||
| # Derivation helpers | ||||||||||||||||||
| # --------------------------------------------------------------------------- | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def derive_needs_protection(method: str) -> bool: | ||||||||||||||||||
| """Tautological with EntityDispositionSchema._validate_protection_consistency. | ||||||||||||||||||
|
|
||||||||||||||||||
| If the model picks any method other than leave_as_is, the entity needs | ||||||||||||||||||
| protection; otherwise it does not. Deriving this instead of asking the | ||||||||||||||||||
| LLM for it eliminates the consistency-rule drift (class K). | ||||||||||||||||||
| """ | ||||||||||||||||||
| return (method or "").strip() != "leave_as_is" | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| # (category, method) -> template text (without leading sensitivity prefix). | ||||||||||||||||||
| # Sensitivity fills a prefix ("high-risk ...", "moderate-risk ...", ""). | ||||||||||||||||||
| _REASON_TEMPLATES: dict[tuple[str, str], str] = { | ||||||||||||||||||
| ("direct_identifier", "replace"): "direct identifier — replaced with a contextual surrogate", | ||||||||||||||||||
| ("direct_identifier", "remove"): "direct identifier — removed to prevent re-identification", | ||||||||||||||||||
| ("direct_identifier", "generalize"): "direct identifier — generalized to reduce re-identification", | ||||||||||||||||||
| ("direct_identifier", "suppress_inference"): "direct identifier — suppressed to prevent inference", | ||||||||||||||||||
| ("quasi_identifier", "generalize"): "quasi-identifier — generalized to reduce re-identification risk", | ||||||||||||||||||
| ("quasi_identifier", "replace"): "quasi-identifier — replaced with a plausible surrogate", | ||||||||||||||||||
| ("quasi_identifier", "remove"): "quasi-identifier — removed due to re-identification risk", | ||||||||||||||||||
| ("quasi_identifier", "suppress_inference"): "quasi-identifier — suppressed to prevent inference", | ||||||||||||||||||
| ("sensitive_attribute", "remove"): "sensitive attribute — removed to prevent disclosure harm", | ||||||||||||||||||
| ("sensitive_attribute", "generalize"): "sensitive attribute — generalized to reduce harm", | ||||||||||||||||||
| ("sensitive_attribute", "suppress_inference"): "sensitive attribute — suppressed to prevent disclosure", | ||||||||||||||||||
| ("sensitive_attribute", "replace"): "sensitive attribute — replaced with a less harmful value", | ||||||||||||||||||
| ("latent_identifier", "suppress_inference"): "latent inference — suppressed to prevent deduction", | ||||||||||||||||||
| ("latent_identifier", "remove"): "latent identifier — removed to prevent inference", | ||||||||||||||||||
| ("latent_identifier", "generalize"): "latent identifier — generalized to reduce inference", | ||||||||||||||||||
| ("latent_identifier", "replace"): "latent identifier — replaced with a less specific surrogate", | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| _SENSITIVITY_PREFIX = {"low": "", "medium": "moderate-risk ", "high": "high-risk "} | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def template_protection_reason(category: str, method: str, sensitivity: str) -> str: | ||||||||||||||||||
| """Build a reason string guaranteed ≥10 chars (EntityDispositionSchema min_length). | ||||||||||||||||||
|
|
||||||||||||||||||
| Used when the LLM omits or emits a too-short protection_reason. Strong | ||||||||||||||||||
| models that provide their own document-specific reason have theirs | ||||||||||||||||||
| kept verbatim by the reconstructor. | ||||||||||||||||||
| """ | ||||||||||||||||||
| method = (method or "").strip() | ||||||||||||||||||
| category = (category or "").strip() | ||||||||||||||||||
| sensitivity = (sensitivity or "").strip().lower() | ||||||||||||||||||
|
|
||||||||||||||||||
| if method == "leave_as_is": | ||||||||||||||||||
| cat_label = category.replace("_", " ") if category else "entity" | ||||||||||||||||||
| return f"Low-risk {cat_label}; retained as-is for utility." | ||||||||||||||||||
|
|
||||||||||||||||||
| base = _REASON_TEMPLATES.get((category, method)) | ||||||||||||||||||
| if base is None: | ||||||||||||||||||
| cat_label = category.replace("_", " ") if category else "entity" | ||||||||||||||||||
| method_label = method or "an appropriate method" | ||||||||||||||||||
| base = f"{cat_label} — protected via {method_label}" | ||||||||||||||||||
|
|
||||||||||||||||||
| prefix = _SENSITIVITY_PREFIX.get(sensitivity, "") | ||||||||||||||||||
| reason = (prefix + base).strip() | ||||||||||||||||||
| # Capitalize first letter; template shapes already make this ≥10 chars. | ||||||||||||||||||
| return reason[:1].upper() + reason[1:] if reason else "Protection applied per policy." | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| # --------------------------------------------------------------------------- | ||||||||||||||||||
| # Entity-context flattening | ||||||||||||||||||
| # --------------------------------------------------------------------------- | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _coerce_entity_list(raw: object) -> list[dict]: | ||||||||||||||||||
| """DataDesigner hands context columns to custom generators in several | ||||||||||||||||||
| shapes: a pydantic-dump dict with a keyed list, a raw list, a JSON- | ||||||||||||||||||
| encoded string, or None. Normalize to a plain list of dicts. | ||||||||||||||||||
| """ | ||||||||||||||||||
| import json | ||||||||||||||||||
| if raw is None: | ||||||||||||||||||
| return [] | ||||||||||||||||||
| if isinstance(raw, str): | ||||||||||||||||||
| raw = raw.strip() | ||||||||||||||||||
| if not raw: | ||||||||||||||||||
| return [] | ||||||||||||||||||
| try: | ||||||||||||||||||
| raw = json.loads(raw) | ||||||||||||||||||
| except Exception: | ||||||||||||||||||
| return [] | ||||||||||||||||||
| if isinstance(raw, dict): | ||||||||||||||||||
| # pydantic dump of a wrapper schema like EntitiesByValueSchema or | ||||||||||||||||||
| # LatentEntitiesSchema — the inner list lives under one of these keys. | ||||||||||||||||||
| for key in ("entities_by_value", "latent_entities", "entities", "items"): | ||||||||||||||||||
| if key in raw and isinstance(raw[key], list): | ||||||||||||||||||
| raw = raw[key] | ||||||||||||||||||
| break | ||||||||||||||||||
| else: | ||||||||||||||||||
| return [] | ||||||||||||||||||
| if not isinstance(raw, list): | ||||||||||||||||||
| return [] | ||||||||||||||||||
| out: list[dict] = [] | ||||||||||||||||||
| for item in raw: | ||||||||||||||||||
| if isinstance(item, dict): | ||||||||||||||||||
| out.append(item) | ||||||||||||||||||
| elif isinstance(item, str): | ||||||||||||||||||
| # JSON-string-per-item (rare but seen). | ||||||||||||||||||
| try: | ||||||||||||||||||
| parsed = json.loads(item) | ||||||||||||||||||
| if isinstance(parsed, dict): | ||||||||||||||||||
| out.append(parsed) | ||||||||||||||||||
| except Exception: | ||||||||||||||||||
| continue | ||||||||||||||||||
| return out | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _flatten_context( | ||||||||||||||||||
| entities_by_value: object, | ||||||||||||||||||
| latent_entities: object, | ||||||||||||||||||
| ) -> list[dict]: | ||||||||||||||||||
| """Produce a flat, ordered list of {source, entity_label, entity_value}. | ||||||||||||||||||
|
|
||||||||||||||||||
| Order matches how the disposition prompt enumerates entities: | ||||||||||||||||||
| tagged entries from entities_by_value (one per (value, label) pair) | ||||||||||||||||||
| followed by latent entries. The returned list index+1 is the expected id. | ||||||||||||||||||
| """ | ||||||||||||||||||
| flat: list[dict] = [] | ||||||||||||||||||
| for ev in _coerce_entity_list(entities_by_value): | ||||||||||||||||||
| value = ev.get("value", "") | ||||||||||||||||||
| labels = ev.get("labels") or [] | ||||||||||||||||||
| if not labels: | ||||||||||||||||||
| flat.append({"source": "tagged", "entity_label": "", "entity_value": value}) | ||||||||||||||||||
| continue | ||||||||||||||||||
| for label in labels: | ||||||||||||||||||
| flat.append({"source": "tagged", "entity_label": label, "entity_value": value}) | ||||||||||||||||||
| for le in _coerce_entity_list(latent_entities): | ||||||||||||||||||
| flat.append({ | ||||||||||||||||||
| "source": "latent", | ||||||||||||||||||
| "entity_label": le.get("label", ""), | ||||||||||||||||||
| "entity_value": le.get("value", ""), | ||||||||||||||||||
| }) | ||||||||||||||||||
| return flat | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| # --------------------------------------------------------------------------- | ||||||||||||||||||
| # Reconstruction | ||||||||||||||||||
| # --------------------------------------------------------------------------- | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def reconstruct_full_disposition( | ||||||||||||||||||
| simple: SimpleDispositionResult, | ||||||||||||||||||
| entities_by_value: object = None, | ||||||||||||||||||
| latent_entities: object = None, | ||||||||||||||||||
| ) -> SensitivityDispositionSchema: | ||||||||||||||||||
| """Build the strict disposition from the loose LLM output + context columns. | ||||||||||||||||||
|
|
||||||||||||||||||
| For each SimpleDispositionItem: | ||||||||||||||||||
| - prefer the model-echoed source/entity_label/entity_value; fall back | ||||||||||||||||||
| to the id-indexed context lookup if the echo is missing or empty. | ||||||||||||||||||
| - derive needs_protection from method. | ||||||||||||||||||
| - keep the LLM protection_reason if it stripped to ≥10 chars, else | ||||||||||||||||||
| template one from (category, method, sensitivity). | ||||||||||||||||||
|
|
||||||||||||||||||
| Orphan simple items (id outside the context range AND no usable echoes) | ||||||||||||||||||
| are skipped with a warning — better to return a smaller valid schema | ||||||||||||||||||
| than to drop the whole record. | ||||||||||||||||||
| Duplicate ids are de-duplicated (first occurrence wins). | ||||||||||||||||||
| """ | ||||||||||||||||||
| context = _flatten_context(entities_by_value, latent_entities) | ||||||||||||||||||
| seen_ids: set[int] = set() | ||||||||||||||||||
| full_items: list[EntityDispositionSchema] = [] | ||||||||||||||||||
|
|
||||||||||||||||||
| for item in simple.sensitivity_disposition: | ||||||||||||||||||
| if item.id in seen_ids: | ||||||||||||||||||
| logger.warning( | ||||||||||||||||||
| "reconstruct_full_disposition: duplicate id=%s in simple output; keeping first occurrence", | ||||||||||||||||||
| item.id, | ||||||||||||||||||
| ) | ||||||||||||||||||
| continue | ||||||||||||||||||
| seen_ids.add(item.id) | ||||||||||||||||||
|
|
||||||||||||||||||
| # Resolve (source, entity_label, entity_value). Context is the | ||||||||||||||||||
| # AUTHORITATIVE source when the id falls in range — small models | ||||||||||||||||||
| # (gemma4-e2b) routinely echo garbage in these fields (e.g. the | ||||||||||||||||||
| # entity_label in the source slot), so trusting the echo there | ||||||||||||||||||
| # corrupts the strict schema. Fall back to the LLM echo only when | ||||||||||||||||||
| # there is no context entry for this id (orphan). | ||||||||||||||||||
| idx = item.id - 1 | ||||||||||||||||||
| if 0 <= idx < len(context): | ||||||||||||||||||
| ctx = context[idx] | ||||||||||||||||||
| src = ctx["source"] | ||||||||||||||||||
| lbl = ctx["entity_label"] | ||||||||||||||||||
| val = ctx["entity_value"] | ||||||||||||||||||
| else: | ||||||||||||||||||
| # Orphan path: id has no context entry. The LLM echoes are the | ||||||||||||||||||
| # only source of truth, but they may be drifted (gemma4-e4b | ||||||||||||||||||
| # observed emitting prompt section names in source). Validate | ||||||||||||||||||
| # the source enum and skip the item if both source and labels | ||||||||||||||||||
| # are unusable — a skipped orphan is better than a ValidationError | ||||||||||||||||||
| # that drops the whole record. | ||||||||||||||||||
| echoed_src = (item.source or "").strip().lower() | ||||||||||||||||||
| src = echoed_src if echoed_src in {"tagged", "latent"} else "" | ||||||||||||||||||
| lbl = item.entity_label or "" | ||||||||||||||||||
| val = item.entity_value or "" | ||||||||||||||||||
|
|
||||||||||||||||||
| if not src or not lbl or not val: | ||||||||||||||||||
| logger.warning( | ||||||||||||||||||
| "reconstruct_full_disposition: orphan simple item id=%s " | ||||||||||||||||||
| "(missing or drifted source/label/value, out of context range); skipping", | ||||||||||||||||||
| item.id, | ||||||||||||||||||
| ) | ||||||||||||||||||
| continue | ||||||||||||||||||
|
|
||||||||||||||||||
| # Derive derived fields. | ||||||||||||||||||
| method = (item.protection_method_suggestion or "").strip() or "leave_as_is" | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the small model omits the method, this silently derives Suggested fix — pessimistic default based on category/sensitivity:
Suggested change
Add a test for |
||||||||||||||||||
| needs = derive_needs_protection(method) | ||||||||||||||||||
|
|
||||||||||||||||||
| # Keep LLM reason if usable, else template. | ||||||||||||||||||
| raw_reason = (item.protection_reason or "").strip() | ||||||||||||||||||
| reason = raw_reason if len(raw_reason) >= 10 else template_protection_reason( | ||||||||||||||||||
| item.category or "", method, item.sensitivity or "" | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| # Default empty LLM-drift slots to sane values so the strict schema | ||||||||||||||||||
| # doesn't reject the row. category/sensitivity are enums at the | ||||||||||||||||||
| # internal layer; empty strings would fail. | ||||||||||||||||||
| category = (item.category or "").strip() or "quasi_identifier" | ||||||||||||||||||
| sensitivity = (item.sensitivity or "").strip().lower() or "medium" | ||||||||||||||||||
|
|
||||||||||||||||||
| full_items.append( | ||||||||||||||||||
| EntityDispositionSchema( | ||||||||||||||||||
| id=item.id, | ||||||||||||||||||
| source=src, | ||||||||||||||||||
| category=category, # strict schema coerces via its before-validator | ||||||||||||||||||
| sensitivity=sensitivity, | ||||||||||||||||||
| entity_label=lbl, | ||||||||||||||||||
| entity_value=val, | ||||||||||||||||||
| needs_protection=needs, | ||||||||||||||||||
| protection_method_suggestion=method, | ||||||||||||||||||
| protection_reason=reason, | ||||||||||||||||||
| ) | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| return SensitivityDispositionSchema(sensitivity_disposition=full_items) | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
endswith("_identifiers")branch can never be reached for any of the intended inputs. The precedingendswith("s")check already handles"direct_identifiers","quasi_identifiers", and"latent_identifiers"— their[:-1]forms are all in_VALID_CATEGORIES, so that check returns first. The_identifiersbranch is only reached whennormalized[:-1]is not in_VALID_CATEGORIES, in which case it returns an invalid category string that would cause aValidationErrordownstream sinceEntityDispositionSchema.categoryhas no before-validator. In practice unreachable for expected model output, but the dead code is misleading and could cause silent failures for hypothetical edge-case inputs.