Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix(split): populate entities/relations in chunk metadata and reload modules in notebook
KaifAhmad1b5c91a90a35a
diff --git a/cookbook/introduction/11_Chunking_and_Splitting.ipynb b/cookbook/introduction/11_Chunking_and_Splitting.ipynbindex c1cd0deb..41775247 100644--- a/cookbook/introduction/11_Chunking_and_Splitting.ipynb+++ b/cookbook/introduction/11_Chunking_and_Splitting.ipynb@@ -55,11 +55,27 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 1,"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 -q semantica\n"]},{@@ -81,9 +97,72 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 3,"metadata": {},- "outputs": [],+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Split into 8 chunks using recursive method\n",+ "\n",+ "================================================================================\n",+ "\n",+ "Chunk 1:\n",+ " Length: 181 characters\n",+ " Start: 0, End: 184\n",+ " Text: Apple Inc. is a technology company founded by Steve Jobs, Steve Wozniak, and Ronald Wayne \n",+ "in Cupert...\n",+ "\n",+ "Chunk 2:\n",+ " Length: 144 characters\n",+ " Start: 134, End: 281\n",+ " Text: The company's current CEO is Tim Cook, who took \n",+ "over from Steve Jobs in August 2011. Apple is headq...\n",+ "\n",+ "Chunk 3:\n",+ " Length: 150 characters\n",+ " Start: 231, End: 383\n",+ " Text: eadquartered at One Apple Park Way in Cupertino.\n",+ "\n",+ "Apple develops and sells consumer electronics, com...\n",+ "\n",+ "Chunk 4:\n",+ " Length: 151 characters\n",+ " Start: 333, End: 486\n",+ " Text: ter software, and online services. The company's \n",+ "hardware products include the iPhone smartphone, t...\n",+ "\n",+ "Chunk 5:\n",+ " Length: 176 characters\n",+ " Start: 436, End: 614\n",+ " Text: iPad tablet computer, the Mac personal computer, \n",+ "the iPod portable media player, the Apple Watch sm...\n",+ "\n",+ "Chunk 6:\n",+ " Length: 152 characters\n",+ " Start: 564, End: 718\n",+ " Text: al media player, and the \n",+ "HomePod smart speaker.\n",+ "\n",+ "Apple's software includes the macOS and iOS operat...\n",+ "\n",+ "Chunk 7:\n",+ " Length: 150 characters\n",+ " Start: 668, End: 820\n",+ " Text: systems, the iTunes media player, the Safari web \n",+ "browser, and the iLife and iWork creativity and pr...\n",+ "\n",+ "Chunk 8:\n",+ " Length: 126 characters\n",+ " Start: 770, End: 896\n",+ " Text: uctivity suites. Its online services include the \n",+ "iTunes Store, the iOS App Store and Mac App Store,...\n",+ "\n",+ "================================================================================\n"+ ]+ }+ ],"source": ["from semantica.split import TextSplitter\n","\n",@@ -118,7 +197,7 @@"for i, chunk in enumerate(chunks, 1):\n"," print(f\"\\nChunk {i}:\")\n"," print(f\" Length: {len(chunk.text)} characters\")\n",- " print(f\" Start: {chunk.start}, End: {chunk.end}\")\n",+ " print(f\" Start: {chunk.start_index}, End: {chunk.end_index}\")\n"," print(f\" Text: {chunk.text[:100]}...\")\n","\n","print(\"\\n\" + \"=\" * 80)"@@ -144,9 +223,39 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 4,"metadata": {},- "outputs": [],+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Comparing Standard Splitting Methods:\n",+ "\n",+ "================================================================================\n",+ "\n",+ "Method: RECURSIVE\n",+ "----------------------------------------\n",+ " Chunks created: 8\n",+ " Avg chunk size: 154 chars\n",+ " First chunk: Apple Inc. is a technology company founded by Steve Jobs, Steve Wozniak, and Ron...\n",+ "\n",+ "Method: SENTENCE\n",+ "----------------------------------------\n",+ " Chunks created: 6\n",+ " Avg chunk size: 148 chars\n",+ " First chunk: Apple Inc. is a technology company founded by Steve Jobs, Steve Wozniak, and Ron...\n",+ "\n",+ "Method: PARAGRAPH\n",+ "----------------------------------------\n",+ " Chunks created: 3\n",+ " Avg chunk size: 297 chars\n",+ " First chunk: Apple Inc. is a technology company founded by Steve Jobs, Steve Wozniak, and Ron...\n",+ "\n",+ "================================================================================\n"+ ]+ }+ ],"source": ["# Compare different methods\n","methods = [\"recursive\", \"sentence\", \"paragraph\"]\n",@@ -189,9 +298,136 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 5,"metadata": {},- "outputs": [],+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Token-Based Splitting Results:\n",+ "\n",+ "================================================================================\n",+ "\n",+ "Chunk 1:\n",+ " Tokens: 100\n",+ " Characters: 461\n",+ " Ratio: 4.61 chars/token\n",+ "\n",+ "Chunk 2:\n",+ " Tokens: 100\n",+ " Characters: 501\n",+ " Ratio: 5.01 chars/token\n",+ "\n",+ "Chunk 3:\n",+ " Tokens: 31\n",+ " Characters: 150\n",+ " Ratio: 4.84 chars/token\n",+ "\n",+ "Chunk 4:\n",+ " Tokens: 20\n",+ " Characters: 78\n",+ " Ratio: 3.90 chars/token\n",+ "\n",+ "Chunk 5:\n",+ " Tokens: 19\n",+ " Characters: 76\n",+ " Ratio: 4.00 chars/token\n",+ "\n",+ "Chunk 6:\n",+ " Tokens: 18\n",+ " Characters: 74\n",+ " Ratio: 4.11 chars/token\n",+ "\n",+ "Chunk 7:\n",+ " Tokens: 17\n",+ " Characters: 70\n",+ " Ratio: 4.12 chars/token\n",+ "\n",+ "Chunk 8:\n",+ " Tokens: 16\n",+ " Characters: 64\n",+ " Ratio: 4.00 chars/token\n",+ "\n",+ "Chunk 9:\n",+ " Tokens: 15\n",+ " Characters: 63\n",+ " Ratio: 4.20 chars/token\n",+ "\n",+ "Chunk 10:\n",+ " Tokens: 14\n",+ " Characters: 59\n",+ " Ratio: 4.21 chars/token\n",+ "\n",+ "Chunk 11:\n",+ " Tokens: 13\n",+ " Characters: 55\n",+ " Ratio: 4.23 chars/token\n",+ "\n",+ "Chunk 12:\n",+ " Tokens: 12\n",+ " Characters: 51\n",+ " Ratio: 4.25 chars/token\n",+ "\n",+ "Chunk 13:\n",+ " Tokens: 11\n",+ " Characters: 45\n",+ " Ratio: 4.09 chars/token\n",+ "\n",+ "Chunk 14:\n",+ " Tokens: 10\n",+ " Characters: 41\n",+ " Ratio: 4.10 chars/token\n",+ "\n",+ "Chunk 15:\n",+ " Tokens: 9\n",+ " Characters: 37\n",+ " Ratio: 4.11 chars/token\n",+ "\n",+ "Chunk 16:\n",+ " Tokens: 8\n",+ " Characters: 33\n",+ " Ratio: 4.12 chars/token\n",+ "\n",+ "Chunk 17:\n",+ " Tokens: 7\n",+ " Characters: 27\n",+ " Ratio: 3.86 chars/token\n",+ "\n",+ "Chunk 18:\n",+ " Tokens: 6\n",+ " Characters: 26\n",+ " Ratio: 4.33 chars/token\n",+ "\n",+ "Chunk 19:\n",+ " Tokens: 5\n",+ " Characters: 20\n",+ " Ratio: 4.00 chars/token\n",+ "\n",+ "Chunk 20:\n",+ " Tokens: 4\n",+ " Characters: 14\n",+ " Ratio: 3.50 chars/token\n",+ "\n",+ "Chunk 21:\n",+ " Tokens: 3\n",+ " Characters: 13\n",+ " Ratio: 4.33 chars/token\n",+ "\n",+ "Chunk 22:\n",+ " Tokens: 2\n",+ " Characters: 9\n",+ " Ratio: 4.50 chars/token\n",+ "\n",+ "Chunk 23:\n",+ " Tokens: 1\n",+ " Characters: 2\n",+ " Ratio: 2.00 chars/token\n",+ "\n",+ "================================================================================\n"+ ]+ }+ ],"source": ["from semantica.split import split_by_tokens\n","\n",@@ -235,9 +471,69 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 6,"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 splitting</td><td>βοΈ split</td><td>SemanticChunker</td><td>-</td><td>0.19s</td></tr><tr><td>β </td><td>Semantica is splitting</td><td>βοΈ split</td><td>EntityAwareChunker</td><td>-</td><td>4.08s</td></tr><tr><td>β </td><td>Semantica is extracting</td><td>π― semantic_extract</td><td>NERExtractor</td><td>-</td><td>1.90s</td></tr></table></div>"+ ],+ "text/plain": [+ "<IPython.core.display.HTML object>"+ ]+ },+ "metadata": {},+ "output_type": "display_data"+ },+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Semantic Chunking Results:\n",+ "\n",+ "================================================================================\n",+ "\n",+ "Chunk 1:\n",+ " Length: 133 chars\n",+ " Coherence: N/A\n",+ " Text: Apple Inc. is a technology company founded by Steve Jobs, Steve Wozniak, and Ronald Wayne \n",+ "in Cupert...\n",+ "\n",+ "Chunk 2:\n",+ " Length: 194 chars\n",+ " Coherence: N/A\n",+ " Text: Wayne \n",+ "in Cupertino, California on April 1, 1976. The company's current CEO is Tim Cook, who took \n",+ "o...\n",+ "\n",+ "Chunk 3:\n",+ " Length: 136 chars\n",+ " Coherence: N/A\n",+ " Text: headquartered at One Apple Park Way in Cupertino. Apple develops and sells consumer electronics, com...\n",+ "\n",+ "Chunk 4:\n",+ " Length: 295 chars\n",+ " Coherence: N/A\n",+ " Text: ectronics, computer software, and online services. The company's \n",+ "hardware products include the iPho...\n",+ "\n",+ "Chunk 5:\n",+ " Length: 223 chars\n",+ " Coherence: N/A\n",+ " Text: ital media player, and the \n",+ "HomePod smart speaker. Apple's software includes the macOS and iOS opera...\n",+ "\n",+ "Chunk 6:\n",+ " Length: 159 chars\n",+ " Coherence: N/A\n",+ " Text: Life and iWork creativity and productivity suites. Its online services include the \n",+ "iTunes Store, th...\n",+ "\n",+ "================================================================================\n"+ ]+ }+ ],"source": ["from semantica.split import SemanticChunker\n","\n",@@ -281,9 +577,25 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 8,"metadata": {},- "outputs": [],+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Entity-Aware Chunking Results:\n",+ "\n",+ "================================================================================\n",+ "\n",+ "Chunk 1:\n",+ " Length: 892 chars\n",+ " Entities: 0\n",+ "\n",+ "================================================================================\n"+ ]+ }+ ],"source": ["from semantica.split import EntityAwareChunker\n","\n",@@ -307,6 +619,7 @@" print(f\" Entities: {len(entities)}\")\n"," \n"," if entities:\n",+ " # Handle both Entity objects and dicts\n"," entity_texts = [e.get('text', e.get('entity', '')) if isinstance(e, dict) else str(e) for e in entities[:3]]\n"," print(f\" Sample entities: {entity_texts}\")\n","\n",@@ -854,7 +1167,7 @@"name": "python","nbconvert_exporter": "python","pygments_lexer": "ipython3",- "version": "3.10.0"+ "version": "3.11.9"}},"nbformat": 4,diff --git a/semantica/split/methods.py b/semantica/split/methods.pyindex a464b022..033382c7 100644--- a/semantica/split/methods.py+++ b/semantica/split/methods.py@@ -950,6 +950,11 @@ def split_entity_aware(if text_pos <= e.start_char < text_end]),+ "entities": [+ e+ for e in entities+ if text_pos <= e.start_char < text_end+ ],},))@@ -981,6 +986,9 @@ def split_entity_aware("entity_count": len([e for e in entities if text_pos <= e.start_char < text_end]),+ "entities": [+ e for e in entities if text_pos <= e.start_char < text_end+ ],},))@@ -1086,6 +1094,16 @@ def split_relation_aware(if text_pos <= r.subject.start_char < text_end]),+ "relationships": [+ r+ for r in relations+ if text_pos <= r.subject.start_char < text_end+ ],+ "triplets": [+ r+ for r in relations+ if text_pos <= r.subject.start_char < text_end+ ],},))@@ -1118,9 +1136,19 @@ def split_relation_aware([rfor r in relations- if text_pos <= r.subject_start < text_end+ if text_pos <= r.subject.start_char < text_end]),+ "relationships": [+ r+ for r in relations+ if text_pos <= r.subject.start_char < text_end+ ],+ "triplets": [+ r+ for r in relations+ if text_pos <= r.subject.start_char < text_end+ ],},))