nemoguardrails.library.gliner.actions

View as Markdown

PII detection using GLiNER.

Module Contents

Functions

NameDescription
_mask_text_with_entitiesMask detected entities in text with their labels.
_resolve_api_keyResolve the GLiNER API key from the configured env var, logging a warning if the
detect_pii_mappingMapping for detect_pii.
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_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._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.detect_pii_mapping(
result: bool
) -> bool

Mapping for detect_pii.

Since the function returns True when PII is detected, we block if result is True.

nemoguardrails.library.gliner.actions.gliner_detect_pii(
source: str,
text: str,
config: nemoguardrails.RailsConfig,
kwargs = {}
)
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:

True if PII is detected, False 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
)
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:

The altered text with PII masked.

Raises:

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