Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix: update notebook 09 with correct API usage and imports

KaifAhmad1d987abd7a9f7
diff --git a/cookbook/advanced/08_Reasoning_and_Inference.ipynb b/cookbook/advanced/08_Reasoning_and_Inference.ipynb
index add6d98b..8883d16f 100644
--- a/cookbook/advanced/08_Reasoning_and_Inference.ipynb
+++ b/cookbook/advanced/08_Reasoning_and_Inference.ipynb
@@ -25,21 +25,37 @@
"pip install semantica[all]\n",
"```\n",
"\n",
- "## Workflow: Build KG \u2192 Define Rules \u2192 Forward/Backward Chaining \u2192 Generate Explanations\n"
+ "## Workflow: Build KG → Define Rules → Forward/Backward Chaining → Generate Explanations\n"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 2,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "WARNING: Ignoring invalid distribution ~gno (C:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages)\n",
+ "WARNING: Ignoring invalid distribution ~lotly (C:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages)\n",
+ "WARNING: Ignoring invalid distribution ~ython-socketio (C:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages)\n",
+ "WARNING: Ignoring invalid distribution ~gno (C:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages)\n",
+ "WARNING: Ignoring invalid distribution ~lotly (C:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages)\n",
+ "WARNING: Ignoring invalid distribution ~ython-socketio (C:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages)\n",
+ "WARNING: Ignoring invalid distribution ~gno (C:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages)\n",
+ "WARNING: Ignoring invalid distribution ~lotly (C:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages)\n",
+ "WARNING: Ignoring invalid distribution ~ython-socketio (C:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages)\n"
+ ]
+ }
+ ],
"source": [
- "!pip install semantica\n"
+ "!pip install -qU semantica\n"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@@ -56,9 +72,22 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 4,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "<div style='font-family: monospace;'><h4>🧠 Semantica - 📊 Current Progress</h4><table style='width: 100%; border-collapse: collapse;'><tr><th>Status</th><th>Action</th><th>Module</th><th>Submodule</th><th>File</th><th>Time</th></tr><tr><td>✅</td><td>Semantica is building</td><td>🧠 kg</td><td>GraphBuilder</td><td>-</td><td>0.31s</td></tr><tr><td>🔄</td><td>Semantica is building</td><td>🧠 kg</td><td>EntityResolver</td><td>-</td><td>2.16s</td></tr><tr><td>✅</td><td>Semantica is deduplicating</td><td>🔄 deduplication</td><td>DuplicateDetector</td><td>-</td><td>0.02s</td></tr><tr><td>✅</td><td>Semantica is deduplicating</td><td>🔄 deduplication</td><td>SimilarityCalculator</td><td>-</td><td>0.01s</td></tr><tr><td>✅</td><td>Semantica is deduplicating</td><td>🔄 deduplication</td><td>EntityMerger</td><td>-</td><td>0.05s</td></tr><tr><td>✅</td><td>Semantica is deduplicating</td><td>🔄 deduplication</td><td>MergeStrategyManager</td><td>-</td><td>0.01s</td></tr><tr><td>✅</td><td>Semantica is resolving</td><td>⚠️ conflicts</td><td>ConflictDetector</td><td>-</td><td>0.00s</td></tr><tr><td>✅</td><td>Semantica is reasoning</td><td>🤔 reasoning</td><td>RuleManager</td><td>-</td><td>0.01s</td></tr><tr><td>✅</td><td>Semantica is reasoning</td><td>🤔 reasoning</td><td>InferenceEngine</td><td>-</td><td>0.00s</td></tr><tr><td>✅</td><td>Semantica is reasoning</td><td>🤔 reasoning</td><td>ExplanationGenerator</td><td>-</td><td>0.01s</td></tr></table></div>"
+ ],
+ "text/plain": [
+ "<IPython.core.display.HTML object>"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
"source": [
"builder = GraphBuilder()\n",
"\n",
@@ -89,7 +118,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
@@ -115,9 +144,19 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 6,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Inferred 2 new facts:\n",
+ " - grandparent_of(alice, charlie) (Rule: Rule 1)\n",
+ " - lives_in(alice, california) (Rule: Rule 2)\n"
+ ]
+ }
+ ],
"source": [
"# Load facts from relationships into the engine\n",
"for rel in relationships:\n",
@@ -142,9 +181,17 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 7,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Goal 'grandparent_of(alice, charlie)' proven successfully!\n"
+ ]
+ }
+ ],
"source": [
"# Define a goal to prove\n",
"goal = \"grandparent_of(alice, charlie)\"\n",
@@ -167,9 +214,21 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 8,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Explanation for first inferred fact:\n",
+ "Given the premises: parent_of(alice, bob), parent_of(bob, charlie), we conclude: grandparent_of(alice, charlie) using rule 'Rule 1'.\n",
+ "\n",
+ "Explanation for backward chaining proof:\n",
+ "Given the premises: , we conclude: grandparent_of(alice, charlie).\n"
+ ]
+ }
+ ],
"source": [
"generator = ExplanationGenerator()\n",
"\n",
@@ -220,7 +279,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@@ -247,9 +306,18 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 10,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "4\n",
+ "4\n"
+ ]
+ }
+ ],
"source": [
"entities = [\n",
" {\"id\": \"alice\", \"type\": \"Person\", \"name\": \"Alice\"},\n",
@@ -273,7 +341,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
@@ -287,9 +355,19 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 12,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "2\n",
+ "grandparent_of(alice, charlie)\n",
+ "lives_in(alice, california)\n"
+ ]
+ }
+ ],
"source": [
"for rel in relationships:\n",
" fact = f\"{rel['type']}({rel['source']}, {rel['target']})\"\n",
@@ -303,9 +381,20 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 13,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "grandparent_of(alice, charlie)\n",
+ "True\n",
+ "lives_in(alice, california)\n",
+ "True\n"
+ ]
+ }
+ ],
"source": [
"goals = [\n",
" \"grandparent_of(alice, charlie)\",\n",
@@ -319,9 +408,18 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 14,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Given the premises: parent_of(alice, bob), parent_of(bob, charlie), we conclude: grandparent_of(alice, charlie) using rule 'Rule 1'.\n",
+ "Given the premises: , we conclude: grandparent_of(alice, charlie).\n"
+ ]
+ }
+ ],
"source": [
"if derived:\n",
" exp = explainer.generate_explanation(derived[0])\n",
@@ -336,10 +434,24 @@
}
],
"metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
"language_info": {
- "name": "python"
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
-}
\ No newline at end of file
+}
diff --git a/cookbook/advanced/09_Semantic_Layer_Construction.ipynb b/cookbook/advanced/09_Semantic_Layer_Construction.ipynb
index a392c350..6344cca8 100644
--- a/cookbook/advanced/09_Semantic_Layer_Construction.ipynb
+++ b/cookbook/advanced/09_Semantic_Layer_Construction.ipynb
@@ -164,7 +164,9 @@
"outputs": [],
"source": [
"exporter = RDFExporter()\n",
- "exporter.export(knowledge_graph, ontology, \"semantic_layer.rdf\")\n"
+ "# Export Knowledge Graph\n",
+ "exporter.export(knowledge_graph, \"knowledge_graph.ttl\", format=\"turtle\")\n",
+ "print(\"Exported knowledge graph to knowledge_graph.ttl\")\n"
]
},
{