nemoguardrails.library.hf_classifier.backends
nemoguardrails.library.hf_classifier.backends
Pluggable inference backends for HuggingFace classifier rails.
Module Contents
Classes
Functions
Data
API
Bases: typing.TypedDict
Bases: ABC
Abstract interface for HuggingFace classifier inference.
Classify a single text. Returns list of label/score detections.
Release resources (e.g. HTTP sessions). Override in subclasses.
Bases: _RemoteBackend
FMS guardrails-detectors /api/v1/text/contents backend.
Tested against: FMS guardrails-detectors API v1. Expects response shape::
[[{“detection_type”: ”…”, “score”: float}, …]]
Raises ValueError if the response is not a list-of-lists or detection
entries lack required keys.
Bases: _RemoteBackend
KServe v1 inference predict backend.
Tested against: KServe v1 predict API (/v1/models/{name}:predict).
Handles three predictions shapes:
dict: class-index → probability (--return_probabilities)int/float: argmax class index, score assumed 1.0- nested
list: token-level class indices, flattened and deduplicated
Raises ValueError if predictions key is missing or the prediction
has an unrecognised type.
Bases: ClassifierBackend
Local HF Transformers pipeline backend.
Tested against: transformers >= 4.35 pipeline API for
text-classification and token-classification tasks.
Bases: _RemoteBackend
vLLM /classify endpoint backend.
Tested against: vLLM v0.6.x /classify API. Expects response shape::
{“data”: [{“label”: ”…”, “probs”: [float, …]}]}
Raises ValueError if the response is missing required keys (data,
label), indicating an API change.
Stores httpx-compatible SSL parameters (verify, cert).
Sentinel stored in _pipelines when a pipeline fails to load at startup. Prevents silent retry — classify() raises immediately with the original error.
Sentinel stored while a pipeline is being loaded. Prevents duplicate concurrent loads — a second caller waits on the event.
Bases: ClassifierBackend
Base class for remote HTTP classifier backends.
Uses a fresh httpx client per request — no connection pool state to manage. Classifier calls are infrequent and latency-tolerant (inference >> TLS handshake), so connection reuse provides negligible benefit while introducing stale-connection complexity.
POST with one automatic retry on transient failures.
Build HTTP request headers from classifier config.
Build httpx SSL params from config parameters (cached).
Reads from config.parameters:
verify_ssl(bool, default True): set to False to skip TLS verification.ca_cert(str): path to a CA bundle file for custom/internal CAs.client_cert(str) +client_key(str): paths for mTLS client auth.
Return cached pipeline, wait on loading, or raise on error. Returns None on miss.
Get or create a cached backend instance from classifier config.