nemoguardrails.library.gliner.actions

View as Markdown

PII detection using GLiNER.

Module Contents

Functions

NameDescription
_mask_pii_outcome-
_mask_text_with_entitiesMask detected entities in text with their labels.
_pii_detection_outcome-
_resolve_api_keyResolve the GLiNER API key from the configured env var, logging a warning if the
gliner_detect_piiChecks whether the provided text contains any PII using GLiNER.
gliner_mask_piiMasks any detected PII in the provided text using GLiNER.

Data

log

API

nemoguardrails.library.gliner.actions._mask_pii_outcome(
source: str,
original_text: str,
masked_text: str
) -> nemoguardrails.actions.rail_outcome.RailOutcome
nemoguardrails.library.gliner.actions._mask_text_with_entities(
text: str,
entities: typing.List[dict]
) -> str

Mask detected entities in text with their labels.

Parameters:

text
str

Original text

entities
List[dict]

List of entity dictionaries with ‘value’, ‘suggested_label’, ‘start_position’, ‘end_position’ keys

Returns: str

Text with entities replaced by [LABEL] placeholders

nemoguardrails.library.gliner.actions._pii_detection_outcome(
has_pii: bool
) -> nemoguardrails.actions.rail_outcome.RailOutcome
nemoguardrails.library.gliner.actions._resolve_api_key(
gliner_config: nemoguardrails.rails.llm.config.GLiNERDetection
) -> typing.Optional[str]

Resolve the GLiNER API key from the configured env var, logging a warning if the env var is named but not set in the environment.

nemoguardrails.library.gliner.actions.gliner_detect_pii(
source: str,
text: str,
config: nemoguardrails.RailsConfig,
http_client: nemoguardrails.http.HTTPClient | None = None,
kwargs = {}
) -> nemoguardrails.actions.rail_outcome.RailOutcome
async

Checks whether the provided text contains any PII using GLiNER.

Parameters:

source
str

The source for the text, i.e. “input”, “output”, “retrieval”.

text
str

The text to check.

config
RailsConfig

The rails configuration object.

Returns: RailOutcome

RailOutcome.block() if PII is detected, RailOutcome.allow() otherwise.

Raises:

  • ValueError: If the response is invalid or source is not valid.
nemoguardrails.library.gliner.actions.gliner_mask_pii(
source: str,
text: str,
config: nemoguardrails.RailsConfig,
http_client: nemoguardrails.http.HTTPClient | None = None
) -> nemoguardrails.actions.rail_outcome.RailOutcome
async

Masks any detected PII in the provided text using GLiNER.

Parameters:

source
str

The source for the text, i.e. “input”, “output”, “retrieval”.

text
str

The text to check.

config
RailsConfig

The rails configuration object.

Returns: RailOutcome

RailOutcome.transform() with the altered text if it changed, RailOutcome.allow() otherwise.

Raises:

  • ValueError: If the response is invalid or source is not valid.
nemoguardrails.library.gliner.actions.log = logging.getLogger(__name__)