topicjev.entail¶
Zero-shot classification backends. The Zero-shot Classification guide explains how to choose a backend and lists the options that each one reads from its keyword arguments.
Base classes¶
Entailment
¶
Entailment(
mname: str | None,
*,
batch_size: int = 16,
max_tokens: int = 512,
multi_lbl: bool = False,
decoys: list[str] | None = None,
probes: list[str] | None = None,
temperature: float = 1.0,
threshold: float | None = None,
**kwargs,
)
Bases: ModelBackend
Base class for entailment and zero-shot classification backends.
Orchestrates pairing, probe calibration, temperature scaling, normalization, and threshold gating across heterogeneous model backends.
Source code in topicjev/entail/base.py
entail
¶
Score documents against labels and return classification results.
Source code in topicjev/entail/base.py
LocalEntailment
¶
Bases: Entailment
Entailment backend that loads local PyTorch models onto hardware devices.
Source code in topicjev/entail/base.py
RemoteEntailment
¶
RemoteEntailment(
mname: str | None,
*,
batch_size: int = 16,
max_tokens: int = 512,
multi_lbl: bool = False,
decoys: list[str] | None = None,
probes: list[str] | None = None,
temperature: float = 1.0,
threshold: float | None = None,
**kwargs,
)
Bases: Entailment
Entailment backend for hosted remote API services without local GPU requirements.
Source code in topicjev/entail/base.py
Local backends¶
XEncoderEntail
¶
Bases: LocalEntailment
Zero-shot classification using Natural Language Inference (NLI) cross-encoders.
Pairs documents as premises and hypothesis templates ('The text discusses {label}').
Source code in topicjev/entail/xencoder.py
entail_indices
property
¶
Resolve indices for entailment, contradiction, and neutral output labels.
load_model
¶
Load cross-encoder classification model and tokenizer.
Source code in topicjev/entail/xencoder.py
prep_pairs
¶
prep_pairs(
prompts: list[str], lbls: list[str]
) -> list[Pair]
Format documents and hypothesis labels into cross-encoder premise-hypothesis pairs.
Source code in topicjev/entail/xencoder.py
batch_score
¶
batch_score(pairs: list[Pair]) -> list[float]
Compute cross-encoder classification logits.
Source code in topicjev/entail/xencoder.py
Seq2SeqEntail
¶
Bases: LocalEntailment
Scores candidate labels by their average per-token log-likelihood under teacher-forcing.
Source code in topicjev/entail/seq2seq.py
load_model
¶
Load encoder-decoder seq2seq model and tokenizer.
Source code in topicjev/entail/seq2seq.py
prep_pairs
¶
prep_pairs(
prompts: list[str], lbls: list[str]
) -> list[Pair]
Format input prompts and candidate target labels into teacher-forced pairs.
Source code in topicjev/entail/seq2seq.py
batch_score
¶
batch_score(pairs: list[Pair]) -> list[float]
Compute mean target token log-probabilities under teacher forcing.
Source code in topicjev/entail/seq2seq.py
GoalExEntail
¶
Bases: YesNoLogitMixin, Seq2SeqEntail
Evaluates candidate labels as independent binary property verification questions.
Scores the logit difference between 'Yes' and 'No' tokens at the initial decoder step of an encoder-decoder seq2seq model in a single forward pass without autoregressive generation.
Inspired by the GoalEx approach (Wang, Shang, and Zhong, 2023)_.
.. _(Wang, Shang, and Zhong, 2023): https://arxiv.org/abs/2305.13749
Source code in topicjev/entail/goalex.py
prep_pairs
¶
prep_pairs(
prompts: list[str], lbls: list[str]
) -> list[Pair]
Format input prompts and labels into GoalEx question pairs.
Source code in topicjev/entail/goalex.py
batch_score
¶
batch_score(pairs: list[Pair]) -> list[float]
Score pairs by extracting yes/no logits at initial decoder token position.
Source code in topicjev/entail/goalex.py
CausalEntail
¶
Bases: CausalLMBackend, GoalExEntail
Evaluates binary property verification from the last token of a causal LM forward pass.
Requires left-padding so position index -1 corresponds to the terminal prompt token. Inherits model loading and chat templating from CausalLMBackend.
Source code in topicjev/entail/causal.py
prep_pairs
¶
prep_pairs(
prompts: list[str], lbls: list[str]
) -> list[Pair]
Format input prompts and labels into left-padded scoring pairs.
Source code in topicjev/entail/causal.py
batch_score
¶
batch_score(pairs: list[Pair]) -> list[float]
Compute yes/no logit difference at terminal token position.
Source code in topicjev/entail/causal.py
Laya and TypeSafe¶
LayaEntail
¶
Bases: JevEntail
Zero-shot classification via local Laya agent models.
Source code in topicjev/entail/jevlike.py
load_model
¶
Load local Laya model on detected hardware device.
batch_score
¶
batch_score(pairs: list[Pair]) -> list[list[float]]
Score candidate choices in local batches, returning log-probabilities.
Source code in topicjev/entail/jevlike.py
JevEntail
¶
Bases: RemoteEntailment
Zero-shot classification via hosted TypeSafe System-One decision endpoints.
Source code in topicjev/entail/jevlike.py
load_model
¶
prep_pairs
¶
prep_pairs(
prompts: list[str], lbls: list[str]
) -> list[Pair]
Construct question criteria schema and input prompt pairs.
Source code in topicjev/entail/jevlike.py
batch_score
¶
batch_score(pairs: list[Pair]) -> list[list[float]]
Score candidate choices via remote endpoint calls, returning log-probabilities.
Source code in topicjev/entail/jevlike.py
Data classes¶
Pair
dataclass
¶
A single (input, optional target) pair sent to a scoring model.
EntailResults
dataclass
¶
EntailResults(
lbls: list[str],
multi_lbl: bool = False,
decoys: list[str] = list(),
threshold: float | None = None,
)
Aggregates batch probabilities into top-1 classification predictions.
Applies threshold gating to assign a document to either its top predicted label or falls back to 'Other'.
compute_results
¶
Map per-label probability distributions to classification result dictionaries.