clean up kg

This commit is contained in:
Jason Liu
2023-11-17 14:50:15 -05:00
parent 37d82ae087
commit ea6de6501d
+141 -607
View File
@@ -11,24 +11,21 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction\n",
"# Introduction\n",
"\n",
"**What is a knowledge graph**\n",
"**What is a knowledge graph?**\n",
"\n",
"A knowledge graph, also known as a semantic network, represents a network of real-world entities—i.e. objects, events, situations, or concepts—and illustrates the relationship between them.\n",
"A knowledge graph, also known as a semantic network, represents real-world entities and their relationships. It consists of nodes, edges, and labels. Nodes can represent any entity, while edges define the connections between them. For example, a node representing an author like \"J.K. Rowling\" can be connected to another node representing one of her books, \"Harry Potter\", with the edge \"author of\".\n",
"\n",
"A knowledge graph primarily consists of three elements: ``nodes``, ``edges``, and ``labels``. Nodes can represent any entity, be it an object, location, or individual. Edges establish the connection or relationship between these nodes. For instance, consider a node representing a popular author, \"J.K. Rowling\", and another node representing one of her books, \"Harry Potter\". The edge between these nodes could define the relationship as \"author of\", indicating that J.K. Rowling is the author of Harry Potter.\n",
"**Applications of knowledge graphs**\n",
"\n",
"**Knowledge graph applications**\n",
"Knowledge graphs have various applications, including:\n",
"\n",
"By using automated knowledge graphs, you can split hard topics into visually appealing and easy bits, making learning less scary and more helpful.\n",
"\n",
"some of the widely used examples are:\n",
"- Search Engines: Knowledge graphs are used by search engines like Google to enhance search results with semantic-search information gathered from a wide variety of sources.\n",
"- Recommendation Systems: They are used in recommendation systems to suggest products or services based on user's behavior and preferences.\n",
"- Natural Language Processing: In NLP, knowledge graphs are used to understand and generate human language.\n",
"- Data Integration: Knowledge graphs help in integrating data from different sources by understanding the relationship between them.\n",
"- Artificial Intelligence and Machine Learning: They are used in AI and ML to provide context to data, which helps in better decision making."
"- Search Engines: They enhance search results by incorporating semantic-search information from diverse sources.\n",
"- Recommendation Systems: They suggest products or services based on user behavior and preferences.\n",
"- Natural Language Processing: They aid in understanding and generating human language.\n",
"- Data Integration: They facilitate the integration of data from different sources by identifying relationships.\n",
"- Artificial Intelligence and Machine Learning: They provide contextual information to improve decision-making."
]
},
{
@@ -54,7 +51,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
@@ -63,7 +60,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@@ -84,20 +81,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Defining the structures"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Node and Edge Classes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Node and Edge Classes\n",
"\n",
"We begin by modeling our knowledge graph with Node and Edge objects.\n",
"\n",
"Node objects represent key concepts or entities, while Edge objects signify the relationships between them."
@@ -105,23 +90,18 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"from pydantic import BaseModel, Field\n",
"from typing import List\n",
"from typing import List, Optional\n",
"\n",
"# The Node class represents key concepts or entities in our knowledge graph.\n",
"# Each node has an id, a label, and a color.\n",
"class Node(BaseModel):\n",
" id: int\n",
" label: str\n",
" color: str\n",
"\n",
"# The Edge class signifies the relationships between nodes in our knowledge graph.\n",
"# Each edge has a source node, a target node, a label, and a color.\n",
"# By default, the color of an edge is set to \"black\".\n",
"class Edge(BaseModel):\n",
" source: int\n",
" target: int\n",
@@ -133,24 +113,23 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### KnowledgeGraph Class"
"## `KnowledgeGraph` Class"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The KnowledgeGraph class integrates the nodes and edges, forming a comprehensive structure of our graph. It contains a list of nodes and a list of edges. Each node represents a key concept or entity, and each edge represents a relationship between two nodes.\n",
"The `KnowledgeGraph` class combines nodes and edges to create a comprehensive graph structure. It includes lists of nodes and edges, where each node represents a key concept or entity, and each edge represents a relationship between two nodes.\n",
"\n",
"Later you'll notice that we model this class to be match the graphviz library's graph object.\n",
"Making it easier to visualize our graph.\n",
"Later on, you'll see that we designed this class to match the graph object in the graphviz library, which makes it easier to visualize our graph.\n",
"\n",
"The `visualize_knowledge_graph` function visualizes a knowledge graph. It accepts a `KnowledgeGraph` object as input, which includes nodes and edges. The function uses the `graphviz` library to create a directed graph (`Digraph`). Each node and edge from the `KnowledgeGraph` is added to the `Digraph` with their respective attributes (id, label, color). The graph is then rendered and displayed."
"The `visualize_knowledge_graph` function is used to visualize a knowledge graph. It takes a `KnowledgeGraph` object as input, which contains nodes and edges. The function utilizes the `graphviz` library to generate a directed graph (`Digraph`). Each node and edge from the `KnowledgeGraph` is added to the `Digraph` with their respective attributes (id, label, color). Finally, the graph is rendered and displayed."
]
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
@@ -166,7 +145,7 @@
" dot = Digraph(comment=\"Knowledge Graph\")\n",
"\n",
" for node in self.nodes:\n",
" dot.node(str(node.id), node.label, color=node.color)\n",
" dot.node(name=str(node.id), label=node.label, color=node.color)\n",
" for edge in self.edges:\n",
" dot.edge(str(edge.source), str(edge.target), label=edge.label, color=edge.color)\n",
" \n",
@@ -177,20 +156,10 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generating the Knowledge Graph"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### generate_graph function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generating the Knowledge Graph\n",
"\n",
"### generate_graph function\n",
"\n",
"The ``generate_graph`` function uses OpenAI's model to create a KnowledgeGraph object from an input string.\n",
"\n",
"It requests the model to interpret the input as a detailed knowledge graph and uses the response to form the KnowledgeGraph object."
@@ -198,13 +167,13 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def generate_graph(input) -> KnowledgeGraph:\n",
" return client.chat.completions.create(\n",
" model=\"gpt-4-1106-preview\",\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" {\n",
" \"role\": \"user\",\n",
@@ -217,7 +186,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -229,179 +198,125 @@
"<!-- Generated by graphviz version 9.0.0 (20230911.1827)\n",
" -->\n",
"<!-- Pages: 1 -->\n",
"<svg width=\"691pt\" height=\"310pt\"\n",
" viewBox=\"0.00 0.00 690.86 309.50\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 305.5)\">\n",
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-305.5 686.86,-305.5 686.86,4 -4,4\"/>\n",
"<svg width=\"574pt\" height=\"398pt\"\n",
" viewBox=\"0.00 0.00 574.32 398.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 394)\">\n",
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-394 570.32,-394 570.32,4 -4,4\"/>\n",
"<!-- 1 -->\n",
"<g id=\"node1\" class=\"node\">\n",
"<title>1</title>\n",
"<ellipse fill=\"none\" stroke=\"blue\" cx=\"403.56\" cy=\"-283.5\" rx=\"70.8\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"403.56\" y=\"-278.45\" font-family=\"Times,serif\" font-size=\"14.00\">Neural Network</text>\n",
"<ellipse fill=\"none\" stroke=\"#4287f5\" cx=\"90.76\" cy=\"-372\" rx=\"90.76\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"90.76\" y=\"-366.95\" font-family=\"Times,serif\" font-size=\"14.00\">Artificial Intelligence</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g id=\"node2\" class=\"node\">\n",
"<title>2</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"193.56\" cy=\"-195\" rx=\"54.42\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"193.56\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">Input Layer</text>\n",
"<ellipse fill=\"none\" stroke=\"#4287f5\" cx=\"90.76\" cy=\"-283.5\" rx=\"79.5\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"90.76\" y=\"-278.45\" font-family=\"Times,serif\" font-size=\"14.00\">Machine Learning</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;2 -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>1&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M364.46,-268.1C348.05,-261.93 328.81,-254.54 311.56,-247.5 285.84,-237 257.27,-224.55 234.69,-214.53\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"236.39,-211.45 225.83,-210.58 233.54,-217.85 236.39,-211.45\"/>\n",
"<text text-anchor=\"middle\" x=\"334.06\" y=\"-234.2\" font-family=\"Times,serif\" font-size=\"14.00\">contains</text>\n",
"<path fill=\"none\" stroke=\"#f5b542\" d=\"M90.76,-353.91C90.76,-342.26 90.76,-326.55 90.76,-313.02\"/>\n",
"<polygon fill=\"#f5b542\" stroke=\"#f5b542\" points=\"94.26,-313.36 90.76,-303.36 87.26,-313.36 94.26,-313.36\"/>\n",
"<text text-anchor=\"middle\" x=\"126.39\" y=\"-322.7\" font-family=\"Times,serif\" font-size=\"14.00\">is a subset of</text>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g id=\"node3\" class=\"node\">\n",
"<title>3</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"331.56\" cy=\"-195\" rx=\"65.68\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"331.56\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">Hidden Layers</text>\n",
"<ellipse fill=\"none\" stroke=\"#4287f5\" cx=\"90.76\" cy=\"-195\" rx=\"66.19\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"90.76\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">Deep Learning</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;3 -->\n",
"<!-- 2&#45;&gt;3 -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>1&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M389.34,-265.41C378.98,-252.97 364.78,-235.91 353.05,-221.81\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"355.82,-219.66 346.73,-214.22 350.44,-224.14 355.82,-219.66\"/>\n",
"<text text-anchor=\"middle\" x=\"396.06\" y=\"-234.2\" font-family=\"Times,serif\" font-size=\"14.00\">contains</text>\n",
"<title>2&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"#f5b542\" d=\"M90.76,-265.41C90.76,-253.76 90.76,-238.05 90.76,-224.52\"/>\n",
"<polygon fill=\"#f5b542\" stroke=\"#f5b542\" points=\"94.26,-224.86 90.76,-214.86 87.26,-224.86 94.26,-224.86\"/>\n",
"<text text-anchor=\"middle\" x=\"126.39\" y=\"-234.2\" font-family=\"Times,serif\" font-size=\"14.00\">is a subset of</text>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g id=\"node4\" class=\"node\">\n",
"<title>4</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"60.56\" cy=\"-195\" rx=\"60.56\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"60.56\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">Output Layer</text>\n",
"<ellipse fill=\"none\" stroke=\"#4287f5\" cx=\"296.76\" cy=\"-106.5\" rx=\"70.8\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"296.76\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">Neural Network</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;4 -->\n",
"<!-- 3&#45;&gt;4 -->\n",
"<g id=\"edge3\" class=\"edge\">\n",
"<title>1&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M345.23,-272.9C309.82,-266.63 263.83,-257.74 223.56,-247.5 184.84,-237.65 141.77,-223.92 109.63,-213.11\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"110.77,-209.8 100.17,-209.91 108.52,-216.43 110.77,-209.8\"/>\n",
"<text text-anchor=\"middle\" x=\"246.06\" y=\"-234.2\" font-family=\"Times,serif\" font-size=\"14.00\">contains</text>\n",
"</g>\n",
"<!-- 6 -->\n",
"<g id=\"node6\" class=\"node\">\n",
"<title>6</title>\n",
"<ellipse fill=\"none\" stroke=\"red\" cx=\"403.56\" cy=\"-106.5\" rx=\"41.63\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"403.56\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">Weights</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;6 -->\n",
"<g id=\"edge7\" class=\"edge\">\n",
"<title>1&#45;&gt;6</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M413.24,-265.21C415.82,-259.72 418.26,-253.51 419.56,-247.5 427.94,-209.05 419.47,-163.65 412.02,-135.21\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"415.5,-134.65 409.45,-125.95 408.75,-136.52 415.5,-134.65\"/>\n",
"<text text-anchor=\"middle\" x=\"434.19\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">uses</text>\n",
"</g>\n",
"<!-- 7 -->\n",
"<g id=\"node7\" class=\"node\">\n",
"<title>7</title>\n",
"<ellipse fill=\"none\" stroke=\"red\" cx=\"655.56\" cy=\"-106.5\" rx=\"27.3\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"655.56\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">Bias</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;7 -->\n",
"<g id=\"edge8\" class=\"edge\">\n",
"<title>1&#45;&gt;7</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M457.93,-271.55C480.42,-265.94 506.37,-258.03 528.56,-247.5 586.19,-220.14 607.26,-212.79 641.56,-159 645.98,-152.07 649.02,-143.76 651.1,-135.86\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"654.48,-136.78 653.2,-126.26 647.65,-135.29 654.48,-136.78\"/>\n",
"<text text-anchor=\"middle\" x=\"639.19\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">uses</text>\n",
"</g>\n",
"<!-- 9 -->\n",
"<g id=\"node9\" class=\"node\">\n",
"<title>9</title>\n",
"<ellipse fill=\"none\" stroke=\"cyan\" cx=\"524.56\" cy=\"-195\" rx=\"44.19\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"524.56\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">Learning</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;9 -->\n",
"<g id=\"edge10\" class=\"edge\">\n",
"<title>1&#45;&gt;9</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M426.61,-266.03C445.75,-252.34 473.15,-232.75 494.11,-217.77\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"495.9,-220.8 502,-212.13 491.83,-215.1 495.9,-220.8\"/>\n",
"<text text-anchor=\"middle\" x=\"499.31\" y=\"-234.2\" font-family=\"Times,serif\" font-size=\"14.00\">performs</text>\n",
"</g>\n",
"<!-- 5 -->\n",
"<g id=\"node5\" class=\"node\">\n",
"<title>5</title>\n",
"<ellipse fill=\"none\" stroke=\"orange\" cx=\"193.56\" cy=\"-106.5\" rx=\"42.14\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"193.56\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">Neurons</text>\n",
"</g>\n",
"<!-- 2&#45;&gt;5 -->\n",
"<g id=\"edge4\" class=\"edge\">\n",
"<title>2&#45;&gt;5</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M193.56,-176.91C193.56,-165.26 193.56,-149.55 193.56,-136.02\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"197.06,-136.36 193.56,-126.36 190.06,-136.36 197.06,-136.36\"/>\n",
"<text text-anchor=\"middle\" x=\"228.81\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">composed of</text>\n",
"</g>\n",
"<!-- 3&#45;&gt;5 -->\n",
"<g id=\"edge5\" class=\"edge\">\n",
"<title>3&#45;&gt;5</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M313.39,-177.45C301.04,-166.69 284,-152.8 267.56,-142.5 256.98,-135.87 244.94,-129.66 233.69,-124.37\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"235.15,-121.19 224.59,-120.23 232.24,-127.56 235.15,-121.19\"/>\n",
"<text text-anchor=\"middle\" x=\"325.81\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">composed of</text>\n",
"</g>\n",
"<!-- 4&#45;&gt;5 -->\n",
"<g id=\"edge6\" class=\"edge\">\n",
"<title>4&#45;&gt;5</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M76.93,-177.31C88.13,-166.5 103.68,-152.59 119.06,-142.5 129.48,-135.67 141.43,-129.43 152.68,-124.18\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"154.1,-127.37 161.78,-120.08 151.23,-120.99 154.1,-127.37\"/>\n",
"<text text-anchor=\"middle\" x=\"154.81\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">composed of</text>\n",
"<title>3&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"#f5b542\" d=\"M115.72,-178.06C133.76,-167.02 159,-152.56 182.51,-142.5 199.88,-135.06 219.36,-128.49 237.24,-123.1\"/>\n",
"<polygon fill=\"#f5b542\" stroke=\"#f5b542\" points=\"238.07,-126.51 246.68,-120.33 236.1,-119.79 238.07,-126.51\"/>\n",
"<text text-anchor=\"middle\" x=\"218.39\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">is a subset of</text>\n",
"</g>\n",
"<!-- 8 -->\n",
"<g id=\"node8\" class=\"node\">\n",
"<title>8</title>\n",
"<ellipse fill=\"none\" stroke=\"purple\" cx=\"193.56\" cy=\"-18\" rx=\"85.13\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"193.56\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">Activation Function</text>\n",
"<ellipse fill=\"none\" stroke=\"#90caf9\" cx=\"224.76\" cy=\"-18\" rx=\"41.63\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"224.76\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">Weights</text>\n",
"</g>\n",
"<!-- 5&#45;&gt;8 -->\n",
"<g id=\"edge9\" class=\"edge\">\n",
"<title>5&#45;&gt;8</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M193.56,-88.41C193.56,-76.76 193.56,-61.05 193.56,-47.52\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"197.06,-47.86 193.56,-37.86 190.06,-47.86 197.06,-47.86\"/>\n",
"<text text-anchor=\"middle\" x=\"212.69\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">applies</text>\n",
"<!-- 4&#45;&gt;8 -->\n",
"<g id=\"edge7\" class=\"edge\">\n",
"<title>4&#45;&gt;8</title>\n",
"<path fill=\"none\" stroke=\"#f54242\" d=\"M282.54,-88.41C272.05,-75.81 257.61,-58.46 245.79,-44.26\"/>\n",
"<polygon fill=\"#f54242\" stroke=\"#f54242\" points=\"248.52,-42.08 239.44,-36.63 243.14,-46.56 248.52,-42.08\"/>\n",
"<text text-anchor=\"middle\" x=\"276.76\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">has</text>\n",
"</g>\n",
"<!-- 9&#45;&gt;6 -->\n",
"<g id=\"edge11\" class=\"edge\">\n",
"<title>9&#45;&gt;6</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M503.49,-178.93C484.14,-165.1 455.27,-144.46 433.53,-128.92\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"435.8,-126.24 425.63,-123.27 431.73,-131.94 435.8,-126.24\"/>\n",
"<text text-anchor=\"middle\" x=\"495.19\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">updates</text>\n",
"<!-- 9 -->\n",
"<g id=\"node9\" class=\"node\">\n",
"<title>9</title>\n",
"<ellipse fill=\"none\" stroke=\"#90caf9\" cx=\"369.76\" cy=\"-18\" rx=\"85.13\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"369.76\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">Activation Function</text>\n",
"</g>\n",
"<!-- 9&#45;&gt;7 -->\n",
"<g id=\"edge12\" class=\"edge\">\n",
"<title>9&#45;&gt;7</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M547.41,-179.23C556.81,-173.07 567.77,-165.78 577.56,-159 587.83,-151.89 589.98,-149.51 600.31,-142.5 608.69,-136.82 617.93,-130.85 626.42,-125.49\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"628.04,-128.6 634.66,-120.32 624.32,-122.67 628.04,-128.6\"/>\n",
"<text text-anchor=\"middle\" x=\"620.19\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">updates</text>\n",
"<!-- 4&#45;&gt;9 -->\n",
"<g id=\"edge8\" class=\"edge\">\n",
"<title>4&#45;&gt;9</title>\n",
"<path fill=\"none\" stroke=\"#f54242\" d=\"M311.18,-88.41C321.68,-75.97 336.08,-58.91 347.98,-44.81\"/>\n",
"<polygon fill=\"#f54242\" stroke=\"#f54242\" points=\"350.62,-47.11 354.39,-37.21 345.27,-42.6 350.62,-47.11\"/>\n",
"<text text-anchor=\"middle\" x=\"351.39\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">uses</text>\n",
"</g>\n",
"<!-- 10 -->\n",
"<g id=\"node10\" class=\"node\">\n",
"<title>10</title>\n",
"<ellipse fill=\"none\" stroke=\"cyan\" cx=\"536.56\" cy=\"-106.5\" rx=\"73.87\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"536.56\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">Backpropagation</text>\n",
"<!-- 5 -->\n",
"<g id=\"node5\" class=\"node\">\n",
"<title>5</title>\n",
"<ellipse fill=\"none\" stroke=\"#90caf9\" cx=\"229.76\" cy=\"-195\" rx=\"54.42\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"229.76\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">Input Layer</text>\n",
"</g>\n",
"<!-- 9&#45;&gt;10 -->\n",
"<g id=\"edge13\" class=\"edge\">\n",
"<title>9&#45;&gt;10</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M526.93,-176.91C528.55,-165.26 530.73,-149.55 532.61,-136.02\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"536.04,-136.74 533.95,-126.35 529.11,-135.77 536.04,-136.74\"/>\n",
"<text text-anchor=\"middle\" x=\"554.44\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">involves</text>\n",
"<!-- 5&#45;&gt;4 -->\n",
"<g id=\"edge4\" class=\"edge\">\n",
"<title>5&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"#f54242\" d=\"M242.68,-177.32C252.35,-164.84 265.75,-147.53 276.8,-133.27\"/>\n",
"<polygon fill=\"#f54242\" stroke=\"#f54242\" points=\"279.37,-135.67 282.73,-125.62 273.84,-131.38 279.37,-135.67\"/>\n",
"<text text-anchor=\"middle\" x=\"278.76\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">has</text>\n",
"</g>\n",
"<!-- 11 -->\n",
"<g id=\"node11\" class=\"node\">\n",
"<title>11</title>\n",
"<ellipse fill=\"none\" stroke=\"purple\" cx=\"536.56\" cy=\"-18\" rx=\"63.63\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"536.56\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">Loss Function</text>\n",
"<!-- 6 -->\n",
"<g id=\"node6\" class=\"node\">\n",
"<title>6</title>\n",
"<ellipse fill=\"none\" stroke=\"#90caf9\" cx=\"364.76\" cy=\"-195\" rx=\"62.1\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"364.76\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">Hidden Layer</text>\n",
"</g>\n",
"<!-- 10&#45;&gt;11 -->\n",
"<g id=\"edge14\" class=\"edge\">\n",
"<title>10&#45;&gt;11</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M536.56,-88.41C536.56,-76.76 536.56,-61.05 536.56,-47.52\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"540.06,-47.86 536.56,-37.86 533.06,-47.86 540.06,-47.86\"/>\n",
"<text text-anchor=\"middle\" x=\"548.19\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">uses</text>\n",
"<!-- 6&#45;&gt;4 -->\n",
"<g id=\"edge5\" class=\"edge\">\n",
"<title>6&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"#f54242\" d=\"M351.65,-177.32C341.84,-164.84 328.23,-147.53 317.02,-133.27\"/>\n",
"<polygon fill=\"#f54242\" stroke=\"#f54242\" points=\"319.93,-131.31 311,-125.61 314.43,-135.64 319.93,-131.31\"/>\n",
"<text text-anchor=\"middle\" x=\"345.76\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">has</text>\n",
"</g>\n",
"<!-- 7 -->\n",
"<g id=\"node7\" class=\"node\">\n",
"<title>7</title>\n",
"<ellipse fill=\"none\" stroke=\"#90caf9\" cx=\"505.76\" cy=\"-195\" rx=\"60.56\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"505.76\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">Output Layer</text>\n",
"</g>\n",
"<!-- 7&#45;&gt;4 -->\n",
"<g id=\"edge6\" class=\"edge\">\n",
"<title>7&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"#f54242\" d=\"M471.72,-179.91C436.72,-165.43 381.99,-142.77 343.07,-126.67\"/>\n",
"<polygon fill=\"#f54242\" stroke=\"#f54242\" points=\"344.52,-123.48 333.94,-122.89 341.84,-129.95 344.52,-123.48\"/>\n",
"<text text-anchor=\"middle\" x=\"428.76\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">has</text>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x1073e8ad0>"
"<graphviz.graphs.Digraph at 0x10ccdf510>"
]
},
"metadata": {},
@@ -416,46 +331,34 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Advanced Iterative Graph Generation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When dealing with extensive or segmented text inputs, processing them all at once might be challenging due to limitations in prompt length or the complexity of the content. In such scenarios, an iterative approach to building the knowledge graph proves beneficial. This method involves processing the text in smaller, manageable chunks, updating the graph with new information from each chunk."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### What are the benefits of this approach?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Scalability: This approach can handle large datasets by breaking them down into smaller, more manageable pieces.\n",
"## Advanced: Accumulating Knowledge Graphs\n",
"\n",
"- Flexibility: It allows for dynamic updates to the graph, accommodating new information as it becomes available.\n",
"When dealing with larger datasets, or knowledge that grows over time, processing them all at once can be challenging due to limitations in prompt length or the complexity of the content. In such cases, an iterative approach to building the knowledge graph can be beneficial. This method involves processing the text in smaller, manageable chunks and updating the graph with new information from each chunk.\n",
"\n",
"- Efficiency: Processing smaller chunks of text can be more efficient and less prone to errors or omissions."
"### What are the benefits of this approach?\n",
"\n",
"- Scalability: This approach can handle large datasets by breaking them down into smaller, more manageable pieces.\n",
"\n",
"- Flexibility: It allows for dynamic updates to the graph, accommodating new information as it becomes available.\n",
"\n",
"- Efficiency: Processing smaller chunks of text can be more efficient and less prone to errors or omissions."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### What's different?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Previous example laid the foundation, while this new example will adds more complexity and functionality. The Node and Edge classes have been augmented with a __hash__ method, enabling these objects to be used in sets, thereby making it easier to handle duplicates."
"### What has changed?\n",
"\n",
"The previous example provided a basic structure, while this new example introduces additional complexity and functionality. The Node and Edge classes now have a __hash__ method, allowing them to be used in sets and simplifying duplicate handling.\n",
"\n",
"The KnowledgeGraph class has been enhanced with two new methods: ``update`` and ``draw``.\n",
"\n",
"In the KnowledgeGraph class, the nodes and edges fields are now optional, offering greater flexibility.\n",
"\n",
"The ``update`` method enables the merging and removal of duplicates from two graphs.\n",
"\n",
"The ``draw`` method includes a prefix parameter, making it easier to create different graph versions during iterations."
]
},
{
@@ -482,27 +385,12 @@
" return hash((self.source, self.target, self.label))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"KnowledgeGraph Class now have ``update`` and ``draw`` methods.\n",
"\n",
"The nodes and edges fields in the KnowledgeGraph class are now optional, providing more flexibility.\n",
"\n",
"``update``: This method allows for the combination and deduplication of two graphs.\n",
"\n",
"``draw``: includes a prefix parameter, facilitating the creation of different graph versions during iterations."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"from typing import Optional\n",
"\n",
"class KnowledgeGraph(BaseModel):\n",
" # Optional list of nodes and edges in the knowledge graph\n",
" nodes: Optional[List[Node]] = Field(..., default_factory=list)\n",
@@ -530,29 +418,31 @@
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generate itrative graph"
]
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The new ``generate_graph`` function is designed to handle a list of inputs iteratively, updating the graph with each new piece of information.\n",
"### Generate iterative graphs\n",
"\n",
"If you look carefully it looks liek a very common pattern in programming, a reduce, or fold function. A simple example could be iterating over a list of find the sum of all the elements squared.\n",
"The updated `generate_graph` function is specifically designed to handle a list of inputs iteratively. It updates the graph with each new piece of information.\n",
"\n",
"Upon closer inspection, this pattern resembles a common programming technique known as a \"reduce\" or \"fold\" function. A simple example of this would be iterating over a list to find the sum of all the elements squared.\n",
"\n",
"Here's an example in Python:\n",
"\n",
"```python\n",
"cur_state = 0\n",
"for i in [1, 2, 3, 4, 5]:\n",
" c += i**2\n",
"print(c)\n",
" cur_state += i**2\n",
"print(cur_state)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
@@ -617,360 +507,9 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 9.0.0 (20230911.1827)\n",
" -->\n",
"<!-- Pages: 1 -->\n",
"<svg width=\"393pt\" height=\"133pt\"\n",
" viewBox=\"0.00 0.00 392.88 132.50\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 128.5)\">\n",
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-128.5 388.88,-128.5 388.88,4 -4,4\"/>\n",
"<!-- 1 -->\n",
"<g id=\"node1\" class=\"node\">\n",
"<title>1</title>\n",
"<ellipse fill=\"none\" stroke=\"blue\" cx=\"190.67\" cy=\"-106.5\" rx=\"31.39\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"190.67\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">Jason</text>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g id=\"node2\" class=\"node\">\n",
"<title>3</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"43.67\" cy=\"-18\" rx=\"43.67\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"43.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">physicist</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;3 -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>1&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M169.3,-92.92C145.33,-78.82 106.12,-55.74 77.87,-39.12\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"79.71,-36.14 69.32,-34.09 76.16,-42.18 79.71,-36.14\"/>\n",
"<text text-anchor=\"middle\" x=\"140.05\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">is a</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g id=\"node3\" class=\"node\">\n",
"<title>2</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"190.67\" cy=\"-18\" rx=\"85.64\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"190.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">quantum mechanics</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;2 -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>1&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M190.67,-88.41C190.67,-76.76 190.67,-61.05 190.67,-47.52\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"194.17,-47.86 190.67,-37.86 187.17,-47.86 194.17,-47.86\"/>\n",
"<text text-anchor=\"middle\" x=\"208.3\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">knows</text>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g id=\"node4\" class=\"node\">\n",
"<title>4</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"339.67\" cy=\"-18\" rx=\"45.21\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"339.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">professor</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;4 -->\n",
"<g id=\"edge3\" class=\"edge\">\n",
"<title>1&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M212.02,-93.11C236.35,-78.98 276.46,-55.7 305.23,-39\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"306.69,-42.2 313.58,-34.15 303.18,-36.14 306.69,-42.2\"/>\n",
"<text text-anchor=\"middle\" x=\"288.05\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">is a</text>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x111368990>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 9.0.0 (20230911.1827)\n",
" -->\n",
"<!-- Pages: 1 -->\n",
"<svg width=\"393pt\" height=\"133pt\"\n",
" viewBox=\"0.00 0.00 392.88 132.50\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 128.5)\">\n",
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-128.5 388.88,-128.5 388.88,4 -4,4\"/>\n",
"<!-- 1 -->\n",
"<g id=\"node1\" class=\"node\">\n",
"<title>1</title>\n",
"<ellipse fill=\"none\" stroke=\"blue\" cx=\"190.67\" cy=\"-106.5\" rx=\"31.39\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"190.67\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">Jason</text>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g id=\"node2\" class=\"node\">\n",
"<title>3</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"43.67\" cy=\"-18\" rx=\"43.67\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"43.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">physicist</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;3 -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>1&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M169.3,-92.92C145.33,-78.82 106.12,-55.74 77.87,-39.12\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"79.71,-36.14 69.32,-34.09 76.16,-42.18 79.71,-36.14\"/>\n",
"<text text-anchor=\"middle\" x=\"140.05\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">is a</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g id=\"node3\" class=\"node\">\n",
"<title>2</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"190.67\" cy=\"-18\" rx=\"85.64\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"190.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">quantum mechanics</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;2 -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>1&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M190.67,-88.41C190.67,-76.76 190.67,-61.05 190.67,-47.52\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"194.17,-47.86 190.67,-37.86 187.17,-47.86 194.17,-47.86\"/>\n",
"<text text-anchor=\"middle\" x=\"208.3\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">knows</text>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g id=\"node4\" class=\"node\">\n",
"<title>4</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"339.67\" cy=\"-18\" rx=\"45.21\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"339.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">professor</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;4 -->\n",
"<g id=\"edge3\" class=\"edge\">\n",
"<title>1&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M212.02,-93.11C236.35,-78.98 276.46,-55.7 305.23,-39\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"306.69,-42.2 313.58,-34.15 303.18,-36.14 306.69,-42.2\"/>\n",
"<text text-anchor=\"middle\" x=\"288.05\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">is a</text>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x1113b93d0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 9.0.0 (20230911.1827)\n",
" -->\n",
"<!-- Pages: 1 -->\n",
"<svg width=\"393pt\" height=\"221pt\"\n",
" viewBox=\"0.00 0.00 392.88 221.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 217)\">\n",
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-217 388.88,-217 388.88,4 -4,4\"/>\n",
"<!-- 1 -->\n",
"<g id=\"node1\" class=\"node\">\n",
"<title>1</title>\n",
"<ellipse fill=\"none\" stroke=\"blue\" cx=\"190.67\" cy=\"-106.5\" rx=\"31.39\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"190.67\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">Jason</text>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g id=\"node2\" class=\"node\">\n",
"<title>3</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"43.67\" cy=\"-18\" rx=\"43.67\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"43.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">physicist</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;3 -->\n",
"<g id=\"edge3\" class=\"edge\">\n",
"<title>1&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M169.3,-92.92C145.33,-78.82 106.12,-55.74 77.87,-39.12\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"79.71,-36.14 69.32,-34.09 76.16,-42.18 79.71,-36.14\"/>\n",
"<text text-anchor=\"middle\" x=\"140.05\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">is a</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g id=\"node5\" class=\"node\">\n",
"<title>2</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"190.67\" cy=\"-18\" rx=\"85.64\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"190.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">quantum mechanics</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;2 -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>1&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M190.67,-88.41C190.67,-76.76 190.67,-61.05 190.67,-47.52\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"194.17,-47.86 190.67,-37.86 187.17,-47.86 194.17,-47.86\"/>\n",
"<text text-anchor=\"middle\" x=\"208.3\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">knows</text>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g id=\"node6\" class=\"node\">\n",
"<title>4</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"339.67\" cy=\"-18\" rx=\"45.21\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"339.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">professor</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;4 -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>1&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M212.02,-93.11C236.35,-78.98 276.46,-55.7 305.23,-39\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"306.69,-42.2 313.58,-34.15 303.18,-36.14 306.69,-42.2\"/>\n",
"<text text-anchor=\"middle\" x=\"287.05\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">is a</text>\n",
"</g>\n",
"<!-- 5 -->\n",
"<g id=\"node3\" class=\"node\">\n",
"<title>5</title>\n",
"<ellipse fill=\"none\" stroke=\"blue\" cx=\"234.67\" cy=\"-195\" rx=\"31.9\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"234.67\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">Sarah</text>\n",
"</g>\n",
"<!-- 5&#45;&gt;1 -->\n",
"<g id=\"edge5\" class=\"edge\">\n",
"<title>5&#45;&gt;1</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M223.68,-177.95C219.89,-172.11 215.77,-165.38 212.42,-159 208.44,-151.4 204.62,-142.92 201.32,-135.06\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"204.61,-133.87 197.61,-125.93 198.13,-136.51 204.61,-133.87\"/>\n",
"<text text-anchor=\"middle\" x=\"230.3\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">knows</text>\n",
"</g>\n",
"<!-- 6 -->\n",
"<g id=\"node4\" class=\"node\">\n",
"<title>6</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"277.67\" cy=\"-106.5\" rx=\"37.53\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"277.67\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">student</text>\n",
"</g>\n",
"<!-- 5&#45;&gt;6 -->\n",
"<g id=\"edge4\" class=\"edge\">\n",
"<title>5&#45;&gt;6</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M242.97,-177.32C248.99,-165.19 257.29,-148.52 264.25,-134.5\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"267.27,-136.29 268.59,-125.78 261,-133.17 267.27,-136.29\"/>\n",
"<text text-anchor=\"middle\" x=\"297.92\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">is a student of</text>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x111350b10>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 9.0.0 (20230911.1827)\n",
" -->\n",
"<!-- Pages: 1 -->\n",
"<svg width=\"514pt\" height=\"221pt\"\n",
" viewBox=\"0.00 0.00 513.99 221.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 217)\">\n",
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-217 509.99,-217 509.99,4 -4,4\"/>\n",
"<!-- 7 -->\n",
"<g id=\"node1\" class=\"node\">\n",
"<title>7</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"91.78\" cy=\"-106.5\" rx=\"91.78\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"91.78\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">University of Toronto</text>\n",
"</g>\n",
"<!-- 8 -->\n",
"<g id=\"node2\" class=\"node\">\n",
"<title>8</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"64.78\" cy=\"-18\" rx=\"38.56\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"64.78\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">Canada</text>\n",
"</g>\n",
"<!-- 7&#45;&gt;8 -->\n",
"<g id=\"edge4\" class=\"edge\">\n",
"<title>7&#45;&gt;8</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M86.45,-88.41C82.75,-76.56 77.74,-60.52 73.48,-46.84\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"76.88,-46 70.56,-37.5 70.2,-48.09 76.88,-46\"/>\n",
"<text text-anchor=\"middle\" x=\"92.41\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">is in</text>\n",
"</g>\n",
"<!-- 1 -->\n",
"<g id=\"node3\" class=\"node\">\n",
"<title>1</title>\n",
"<ellipse fill=\"none\" stroke=\"blue\" cx=\"285.78\" cy=\"-106.5\" rx=\"31.39\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"285.78\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">Jason</text>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g id=\"node4\" class=\"node\">\n",
"<title>3</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"164.78\" cy=\"-18\" rx=\"43.67\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"164.78\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">physicist</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;3 -->\n",
"<g id=\"edge3\" class=\"edge\">\n",
"<title>1&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M266.61,-91.79C247.37,-78.04 217.44,-56.65 195.01,-40.6\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"197.3,-37.94 187.13,-34.98 193.23,-43.64 197.3,-37.94\"/>\n",
"<text text-anchor=\"middle\" x=\"246.16\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">is a</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g id=\"node7\" class=\"node\">\n",
"<title>2</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"311.78\" cy=\"-18\" rx=\"85.64\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"311.78\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">quantum mechanics</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;2 -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>1&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M290.92,-88.41C294.46,-76.64 299.24,-60.73 303.34,-47.11\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"306.61,-48.38 306.13,-37.8 299.9,-46.37 306.61,-48.38\"/>\n",
"<text text-anchor=\"middle\" x=\"319.41\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">knows</text>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g id=\"node8\" class=\"node\">\n",
"<title>4</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"460.78\" cy=\"-18\" rx=\"45.21\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"460.78\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">professor</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;4 -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>1&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M309.02,-94.01C338.09,-79.64 388.25,-54.85 422.9,-37.72\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"424.35,-40.91 431.76,-33.35 421.25,-34.64 424.35,-40.91\"/>\n",
"<text text-anchor=\"middle\" x=\"398.16\" y=\"-57.2\" font-family=\"Times,serif\" font-size=\"14.00\">is a</text>\n",
"</g>\n",
"<!-- 5 -->\n",
"<g id=\"node5\" class=\"node\">\n",
"<title>5</title>\n",
"<ellipse fill=\"none\" stroke=\"blue\" cx=\"285.78\" cy=\"-195\" rx=\"31.9\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"285.78\" y=\"-189.95\" font-family=\"Times,serif\" font-size=\"14.00\">Sarah</text>\n",
"</g>\n",
"<!-- 5&#45;&gt;7 -->\n",
"<g id=\"edge5\" class=\"edge\">\n",
"<title>5&#45;&gt;7</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M261.22,-183.15C245.62,-176.25 225.01,-167.12 206.78,-159 184.05,-148.87 158.81,-137.57 137.78,-128.15\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"139.45,-125.06 128.89,-124.16 136.58,-131.45 139.45,-125.06\"/>\n",
"<text text-anchor=\"middle\" x=\"244.28\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">is a student at</text>\n",
"</g>\n",
"<!-- 5&#45;&gt;1 -->\n",
"<g id=\"edge7\" class=\"edge\">\n",
"<title>5&#45;&gt;1</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M285.78,-176.91C285.78,-165.26 285.78,-149.55 285.78,-136.02\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"289.28,-136.36 285.78,-126.36 282.28,-136.36 289.28,-136.36\"/>\n",
"<text text-anchor=\"middle\" x=\"303.41\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">knows</text>\n",
"</g>\n",
"<!-- 6 -->\n",
"<g id=\"node6\" class=\"node\">\n",
"<title>6</title>\n",
"<ellipse fill=\"none\" stroke=\"green\" cx=\"372.78\" cy=\"-106.5\" rx=\"37.53\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"372.78\" y=\"-101.45\" font-family=\"Times,serif\" font-size=\"14.00\">student</text>\n",
"</g>\n",
"<!-- 5&#45;&gt;6 -->\n",
"<g id=\"edge6\" class=\"edge\">\n",
"<title>5&#45;&gt;6</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M302.79,-179.33C309.73,-173.19 317.77,-165.88 324.78,-159 333.55,-150.4 342.83,-140.61 350.87,-131.9\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"353.25,-134.48 357.41,-124.74 348.08,-129.76 353.25,-134.48\"/>\n",
"<text text-anchor=\"middle\" x=\"378.03\" y=\"-145.7\" font-family=\"Times,serif\" font-size=\"14.00\">is a student of</text>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x1113ce110>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"text_chunks = [\n",
" \"Jason knows a lot about quantum mechanics. He is a physicist. He is a professor\",\n",
@@ -1050,11 +589,6 @@
"\n",
"All of them will follow an idea of iteratively extracting more and more information and accumulating it some state."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {