Files
semantica/cookbook/advanced/08_Reasoning_and_Inference.ipynb
T
KaifAhmad1 7a6f1d0417 docs: add citation section and fix stale org references
Add a Cite Us section to the README with BibTeX citation info, and
align it with docs/citation.md (author/organization: Semantica, 2026).
Update LICENSE and docs/project-license.md copyright holder to
Semantica, and replace the stale Hawksight-AI GitHub org slug with
semantica-agi across READMEs, plugin manifests, cookbook notebooks,
and GitHub templates.
2026-08-24 16:07:22 +05:30

9.7 KiB

Open In Colab

Reasoning and Inference

Overview

Build knowledge graphs, define rules, perform forward/backward chaining, and generate explanations for AI reasoning using the Semantica Reasoning Module.

Documentation: API Reference

Installation

Install Semantica from PyPI:

pip install semantica
# Or with all optional dependencies:
pip install semantica[all]

Workflow: Build KG → Define Rules → Forward/Backward Chaining → Generate Explanations

In [ ]:
!pip install -qU semantica
In [ ]:
from semantica.kg import GraphBuilder
from semantica.reasoning import Reasoner, ExplanationGenerator

Step 1: Build Knowledge Graph

In [ ]:
builder = GraphBuilder()

entities = [
    {"id": "alice", "type": "Person", "name": "Alice"},
    {"id": "bob", "type": "Person", "name": "Bob"},
    {"id": "charlie", "type": "Person", "name": "Charlie"},
    {"id": "sf", "type": "Location", "name": "San Francisco"},
    {"id": "california", "type": "Location", "name": "California"},
]

relationships = [
    {"source": "alice", "target": "bob", "type": "parent_of"},
    {"source": "bob", "target": "charlie", "type": "parent_of"},
    {"source": "sf", "target": "california", "type": "located_in"},
    {"source": "alice", "target": "sf", "type": "lives_in"},
]

knowledge_graph = builder.build([{"entities": entities, "relationships": relationships}])

Step 2: Define Rules

In [ ]:
# Initialize Reasoner
reasoner = Reasoner()

# Define rules using logic syntax
rules = [
    "IF parent_of(?a, ?b) AND parent_of(?b, ?c) THEN grandparent_of(?a, ?c)",
    "IF lives_in(?x, ?y) AND located_in(?y, ?z) THEN lives_in(?x, ?z)"
]

for rule in rules:
    reasoner.add_rule(rule)

Step 3: Forward Chaining

In [ ]:
# Perform forward chaining to derive new facts
# The Reasoner can infer facts directly from the knowledge graph or a list of facts
inferred_facts = reasoner.infer_facts(knowledge_graph)

print(f"Inferred {len(inferred_facts)} new facts:")
for fact in inferred_facts:
    print(f" - {fact}")

Step 4: Backward Chaining

In [ ]:
# Define a goal to prove
goal = "grandparent_of(alice, charlie)"

# Perform backward chaining
proof = reasoner.backward_chain(goal)

if proof:
    print(f"Goal '{goal}' proven successfully!")
else:
    print(f"Could not prove goal '{goal}'.")

Step 5: Generate Explanations

In [ ]:
generator = ExplanationGenerator()

# If we have a proof from backward chaining, explain it
if proof:
    proof_explanation = generator.generate_explanation(proof)
    print("Explanation for backward chaining proof:")
    print(proof_explanation.natural_language)

Summary

Reasoning and inference workflow:

  • Knowledge Graph Built
  • Inference Rules Defined
  • Facts Loaded into Engine
  • Forward Chaining Performed
  • Backward Chaining Performed
  • Explanations Generated

Deep Dive: Reasoning Module

This section provides an in-depth guide to Semantica's reasoning capabilities. Learn rule syntax, fact formats, chaining strategies, and explanation generation with robust, reproducible examples.

What you'll practice

  • Defining rules with variables and predicates
  • Loading facts in predicate form
  • Running forward and backward chaining
  • Generating human-readable explanations
In [ ]:
from semantica.kg import GraphBuilder
from semantica.reasoning import Reasoner, ExplanationGenerator

builder = GraphBuilder()
reasoner = Reasoner()
explainer = ExplanationGenerator()

Rule Syntax

Rules use predicate logic with variables prefixed by ?.

  • Example: IF parent_of(?a, ?b) AND parent_of(?b, ?c) THEN grandparent_of(?a, ?c)
  • Variables unify across predicates in the same rule
  • Conclusions are added as new facts when conditions match
In [ ]:
entities = [
    {"id": "alice", "type": "Person", "name": "Alice"},
    {"id": "bob", "type": "Person", "name": "Bob"},
    {"id": "charlie", "type": "Person", "name": "Charlie"},
    {"id": "sf", "type": "Location", "name": "San Francisco"},
    {"id": "california", "type": "Location", "name": "California"}
]

relationships = [
    {"source": "alice", "target": "bob", "type": "parent_of"},
    {"source": "bob", "target": "charlie", "type": "parent_of"},
    {"source": "sf", "target": "california", "type": "located_in"},
    {"source": "alice", "target": "sf", "type": "lives_in"}
]

knowledge_graph = builder.build([{"entities": entities, "relationships": relationships}])
print(len(knowledge_graph.get("entities", [])))
print(len(knowledge_graph.get("relationships", [])))
In [ ]:
rules = [
    "IF parent_of(?a, ?b) AND parent_of(?b, ?c) THEN grandparent_of(?a, ?c)",
    "IF lives_in(?x, ?y) AND located_in(?y, ?z) THEN lives_in(?x, ?z)"
]
for r in rules:
    reasoner.add_rule(r)
In [ ]:
for rel in relationships:
    fact = f"{rel['type']}({rel['source']}, {rel['target']})"
    reasoner.add_fact(fact)

derived = reasoner.forward_chain()
print(len(derived))
for d in derived:
    print(d.conclusion)
In [ ]:
goals = [
    "grandparent_of(alice, charlie)",
    "lives_in(alice, california)"
]
for g in goals:
    proof = reasoner.backward_chain(g)
    print(g)
    print(bool(proof))
In [ ]:
if derived:
    exp = explainer.generate_explanation(derived[0])
    print(exp.natural_language)

goal = "grandparent_of(alice, charlie)"
proof = reasoner.backward_chain(goal)
if proof:
    pexp = explainer.generate_explanation(proof)
    print(pexp.natural_language)