mixle.substrate.interop module¶
External model interop for reasoner delegation.
ExternalModel wraps a generate callable from an external model,
agent, hosted LLM, or remote tool. It can estimate semantic uncertainty by
sampling multiple answers and clustering them through an equivalence function.
The returned answer is always one of the assessed samples, never a separate,
unassessed call (MXR-080-0270) – the verdict genuinely describes the text the
caller actually receives.
external_action() adapts the wrapper into a reasoner
Action. When the external model is not
CONFIDENT – genuinely uncertain, or never calibrated at all – the action
contributes no evidence, allowing the reasoner to continue or abstain.
- class Confidence(*values)[source]
Bases:
StrEnumThe three-state verdict on an external answer’s semantic-entropy assessment (MXR-080-0270) – a closed vocabulary, never a bare boolean: a bare
confident=Truecannot distinguish “assessed against a calibrated cutoff and found consistent” from “no calibration policy was ever configured, so there was nothing to compare the entropy against” – exactly the fail-open overclaim this finding fixes (mirrorsFreshnessState).- CONFIDENT: a calibration policy (
calibration_promptsor an explicitmax_entropy) is configured, and the returned answer’s own measured semantic entropy is at or below the cutoff.
- UNCERTAIN: a calibration policy is configured, but the measured semantic entropy exceeds the
cutoff – the sampled batch (which the returned answer is itself drawn from) disagrees with itself more than the calibrated policy tolerates.
- UNCALIBRATED: no calibration policy is configured at all – there is no cutoff to compare the
entropy against, so the verdict fails closed rather than silently treating a missing cutoff as “anything finite is confident”.
- CONFIDENT: a calibration policy (
- class ExternalAnswer(prompt, answer, entropy, state)[source]
Bases:
objectAn external model’s answer plus its self-measured uncertainty (semantic entropy).
answeris itself one of the samples measured forentropy(MXR-080-0270), drawn from the same batch rather than a separate, unassessed call – sostategenuinely describes the returned text’s own membership in the measured cluster.- property confident: bool
TrueiffstateisConfidence.CONFIDENT.Never itself the source of truth –
stateis (MXR-080-0270): bothConfidence.UNCERTAINandConfidence.UNCALIBRATEDreadFalsehere, so an uncalibrated model can no longer read as confident just because its entropy happened to be finite.- Type:
Read-only convenience view
- property calibrated: bool
TrueunlessstateisConfidence.UNCALIBRATED.
- class ExternalModel(generate, *, calibration_prompts=None, equivalent=None, max_entropy=None, alpha=0.1, samples=8)[source]
Bases:
objectAn external
generatecallable wrapped so each answer carries semantic-entropy UQ.- Parameters:
generate (Callable[[Any], Any]) –
prompt -> answer(an external agent / LLM / remote tool). Called multiple times per query to measure how much its meaning varies (the uncertainty signal).calibration_prompts (Any) – example prompts whose (1-alpha) semantic-entropy quantile becomes the “too uncertain” cutoff – a calibration policy. Required unless
max_entropyis given directly: with neither (or with calibration prompts too few/degenerate to yield a finite quantile), no calibration policy exists at all, and every answer’sstateisConfidence.UNCALIBRATED(MXR-080-0270) – never silently treated as confident just because its entropy happened to be finite.equivalent (Callable[[Any, Any], bool] | None) –
(a, b) -> boolmeaning-equivalence for clustering samples (default: exact match).max_entropy (float | None) – an explicit uncertainty cutoff, overriding the calibrated one. Must be finite and non-negative – entropy is never negative, so a non-finite or negative cutoff is always a caller error, not a valid (if strict) policy.
samples (int) – how many draws to sample per
answer()call. The first draw becomes the returned answer; entropy is measured over the same batch it came from (MXR-080-0270).alpha (float)
- property max_entropy: float | None
Semantic-entropy cutoff used to decide confidence, or
Noneif no calibration policy is configured (MXR-080-0270).Nonewhenever neithercalibration_promptsnor an explicitmax_entropyproduced a finite cutoff – callers must fail closed onNone, never treat a missing cutoff as “anything finite is confident” the way an unguardedentropy <= infsilently would.
- answer(prompt)[source]
Sample the external model; the first draw is the returned answer (MXR-080-0270).
Every draw – the one returned and every other sample used to measure entropy – comes from the same batch, so the confidence verdict genuinely describes the returned text’s own semantic position in the cluster, rather than a separately generated call that was never actually compared to anything. Fails closed to
Confidence.UNCALIBRATEDwhen no calibration policy (calibration_promptsor an explicitmax_entropy) is configured, however low the measured entropy happens to be.- Parameters:
prompt (Any)
- Return type:
ExternalAnswer
- external_action(model, *, name='external', cost=8.0, description='', trust_uncertain=False)[source]
A reasoner delegate action backed by a UQ-wrapped external model (see module docstring).
By default (
trust_uncertain=False) the action contributes evidence only when the external model’s answer isConfidence.CONFIDENT; anUNCERTAINorUNCALIBRATEDanswer yields no fragment, so the reasoner treats it as no answer rather than a guess (MXR-080-0270: a never-calibrated model is withheld exactly like a genuinely self-contradicting one – both are “we cannot vouch for this”, not “confident”). The fragment carries the model’s entropy and verdict so the trace records how sure the external source was, and why. Cost defaults high – external calls are the escalation of last resort.