mixle.substrate.kg_rag module

KG-RAG – typed retrieval over a knowledge graph, with an entity-linking leaf (D3).

Text retrieval finds passages; a knowledge graph answers with facts. link_entities() is the entity-linking leaf: it maps a question’s tokens onto the KG’s entity inventory (longest-name-first, so “new york city” links before “york”). retrieve_triples() returns the facts about the linked entities – filtered through an Ontology when one is given, so a schema-violating triple in a dirty store is never served as evidence. kg_action() packages that as a reasoner Action, so investigate() / the Reasoner can buy typed evidence: the fragment for (ada, lives_in, paris) reads ada lives_in paris, citable and checkable against the graph rather than parsed back out of prose.

link_entities(question, entities)[source]

The entity-linking leaf: which KG entities does the question mention?

Matches each entity’s normalized name as a token subsequence of the question, longest name first so multi-word entities win over their substrings. Returns the linked entities in match order.

Parameters:
  • question (str)

  • entities (Any)

Return type:

list[str]

retrieve_triples(triples, question, *, ontology=None, types=None, k=8)[source]

Typed KG retrieval: link the question’s entities, return the (schema-valid) facts about them.

Returns {entities, facts, rejected}facts are the triples touching a linked entity (head or tail), at most k; when an ontology (+ entity types) is supplied, schema-violating triples are excluded and reported under rejected with named reasons, so a dirty store cannot inject a type-invalid fact as evidence.

Parameters:
Return type:

dict[str, Any]

kg_action(triples, *, ontology=None, types=None, name='kg', cost=1.0, description='', k=8)[source]

A reasoner RETRIEVE action over a knowledge graph (typed facts, not passages).

Contributes one fragment per fact (head relation tail); nothing links -> no evidence, so the reasoner falls through honestly instead of forcing a match. Relevance comes from the action’s description plus the KG’s own entity inventory (queries naming a known entity score).

Parameters:
Return type:

Any