mixle.reason.ontology module¶
Ontology objects for typed constraints on knowledge graphs.
A knowledge graph without a schema will happily assert (paris, employs, france). An
Ontology is the typed contract that rules such triples out structurally: a class hierarchy,
relation signatures (employs: Organization -> Person), per-relation axioms (functional, symmetric,
asymmetric, irreflexive), and disjoint-class declarations. Ontology.check_triple() names every
violation of one assertion; Ontology.check_graph() audits a whole triple set, including the
cross-triple axioms (a functional relation asserted with two different tails).
The same contract turns a fitted KG embedding into an ontology-constrained
distribution: OntologyConstrainedKG wraps a
KnowledgeGraphDistribution and masks the tail posterior to
range-conforming entities, renormalizing – so the model literally cannot place probability on a triple
the ontology forbids. Constrained extraction or decoding can apply the same
mask before accepting generated triples.
Everything is symbolic and dependency-free: entities/relations are strings, entity types are supplied
as a {entity: class} map (the entity-linking output). Violations are named, never silent.
- class Ontology(classes=<factory>, relations=<factory>, axioms=<factory>, disjoint=<factory>)[source]
Bases:
objectA typed schema over knowledge: class hierarchy + relation signatures + axioms + disjointness.
- Parameters:
- add_class(name, parent=None)[source]
- add_relation(name, domain, range_, *axioms)[source]
- is_a(cls, ancestor)[source]
Whether
clsisancestoror a descendant of it (walks the parent chain).
- check_triple(h, r, t, types)[source]
Every named violation of
(h, r, t)given entitytypes({} means unconstrained).
- check_graph(triples, types)[source]
Audit a triple set: per-triple violations plus the cross-triple axioms (functional/asymmetric).
Returns
{consistent, n_triples, violations: [{triple, problems}]}– every problem named.
- class OntologyConstrainedKG(kg, ontology, *, entities, relations, types)[source]
Bases:
objectA fitted KG embedding, typed by an ontology: probability mass only on schema-consistent triples.
Wraps a
KnowledgeGraphDistribution(entities and relations as integer indices) together with the symbolic ontology and the index<->name maps. The tail posterior is masked to entities whose class conforms to the relation’s range and renormalized, so completion can never propose an ontology-violating tail –Graph(ontology)as a distribution.- Parameters:
- tail_posterior(head, relation)[source]
p(tail | head, relation)over ONLY the range-conforming entities (renormalized).
- class ConstrainedDecode(facts, rejected, below_floor, n_samples)[source]
Bases:
objectThe result of ontology-constrained LLM decoding: what survived, what the schema rejected, and why.
- Parameters:
- n_samples: int
- constrained_decode(llm, prompt, ontology, types, *, n=None, floor=0.5, calibrator=None)[source]
Ontology-constrained decoding (D2): an LLM may only assert schema-consistent, confident facts.
Samples
llm(aGraphLLM)ntimes, masks every sampled graph throughOntology.filter_triples()(violating triples are REJECTED with named reasons – the typed-grammar mask applied post-parse), then marginalizes the constrained graphs into aGraphDistributionand keeps only facts whose edge marginal clearsfloor– the calibrated confidence floor (pass a fittedcalibratorfromfit_fact_calibrator()to apply the floor on CALIBRATED truth probability rather than the raw marginal). Consistent-but-underconfident facts are reported as withheld, never silently dropped: the decode says what it refused to assert and why.