Fix GraphRAG notebook issues: embedding dimension check, update LLM model to llama-3.3-70b-versatile, fix file ingestion, and fix graph metrics access

This commit is contained in:
KaifAhmad1
2025-12-24 12:52:55 +05:30
parent ef0797e03e
commit bd466b6016
@@ -55,7 +55,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 44,
"metadata": {},
"outputs": [
{
@@ -77,7 +77,19 @@
"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 ~ython-socketio (c:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages)\n",
"c:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\threading.py:986: ResourceWarning:\n",
"\n",
"unclosed file <_io.BufferedWriter name=3>\n",
"\n",
"c:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\threading.py:986: ResourceWarning:\n",
"\n",
"unclosed file <_io.BufferedReader name=4>\n",
"\n",
"c:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\threading.py:986: ResourceWarning:\n",
"\n",
"unclosed file <_io.BufferedReader name=5>\n",
"\n"
]
}
],
@@ -107,25 +119,9 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\threading.py:986: ResourceWarning: unclosed file <_io.BufferedWriter name=3>\n",
" del self._target, self._args, self._kwargs\n",
"ResourceWarning: Enable tracemalloc to get the object allocation traceback\n",
"c:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\threading.py:986: ResourceWarning: unclosed file <_io.BufferedReader name=4>\n",
" del self._target, self._args, self._kwargs\n",
"ResourceWarning: Enable tracemalloc to get the object allocation traceback\n",
"c:\\Users\\Mohd Kaif\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\threading.py:986: ResourceWarning: unclosed file <_io.BufferedReader name=5>\n",
" del self._target, self._args, self._kwargs\n",
"ResourceWarning: Enable tracemalloc to get the object allocation traceback\n"
]
}
],
"outputs": [],
"source": [
"# Core imports will be added in cells where they're first used\n",
"import os\n",
@@ -143,13 +139,13 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Set up API keys\n",
"# Note: In production, use environment variables: export GROQ_API_KEY=\"your-key\"\n",
"os.environ[\"GROQ_API_KEY\"] = os.getenv(\"GROQ_API_KEY\", \"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\")\n"
"os.environ[\"GROQ_API_KEY\"] = os.getenv(\"GROQ_API_KEY\", \"Your Groq API\")\n"
]
},
{
@@ -168,7 +164,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
@@ -192,7 +188,7 @@
" # Inference configuration (for answer generation)\n",
" \"inference\": {\n",
" \"provider\": \"groq\",\n",
" \"model\": \"llama-3.1-70b-versatile\"\n",
" \"model\": \"llama-3.3-70b-versatile\"\n",
" },\n",
" \n",
" # Vector store configuration\n",
@@ -220,7 +216,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 48,
"metadata": {},
"outputs": [
{
@@ -278,7 +274,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
@@ -326,7 +322,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 50,
"metadata": {},
"outputs": [
{
@@ -356,7 +352,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 51,
"metadata": {},
"outputs": [
{
@@ -410,7 +406,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
@@ -430,7 +426,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 53,
"metadata": {},
"outputs": [
{
@@ -468,7 +464,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 54,
"metadata": {},
"outputs": [
{
@@ -521,7 +517,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 55,
"metadata": {},
"outputs": [
{
@@ -580,7 +576,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 56,
"metadata": {},
"outputs": [
{
@@ -589,11 +585,11 @@
"text": [
"Ingesting from local files...\n",
" Processing: expert_skincare_guide.txt\n",
" Warning: Failed to ingest expert_skincare_guide.txt: 'FileObject' object is not iterable\n",
" Successfully ingested expert_skincare_guide.txt\n",
" Processing: data/sample_graphrag_paper.txt\n",
" Warning: Failed to ingest data/sample_graphrag_paper.txt: 'FileObject' object is not iterable\n",
" Successfully ingested data/sample_graphrag_paper.txt\n",
"\n",
"Total local files processed: 6\n"
"Total local files processed: 8\n"
]
}
],
@@ -609,10 +605,18 @@
" try:\n",
" if os.path.exists(file_path):\n",
" print(f\" Processing: {file_path}\")\n",
" files = file_ingestor.ingest_file(file_path)\n",
" for file_data in files:\n",
" if hasattr(file_data, 'text') and file_data.text:\n",
" all_content.append(file_data.text)\n",
" file_data = file_ingestor.ingest_file(file_path)\n",
" # ingest_file returns a single FileObject, not a list\n",
" if hasattr(file_data, 'text') and file_data.text:\n",
" all_content.append(file_data.text)\n",
" elif hasattr(file_data, 'content') and file_data.content:\n",
" # If content is bytes, decode it\n",
" if isinstance(file_data.content, bytes):\n",
" try:\n",
" text = file_data.content.decode('utf-8')\n",
" all_content.append(text)\n",
" except UnicodeDecodeError:\n",
" print(f\" Warning: Could not decode content from {file_path}\")\n",
" print(f\" Successfully ingested {file_path}\")\n",
" else:\n",
" print(f\" Warning: File not found: {file_path}\")\n",
@@ -637,20 +641,26 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Normalizing content...\n",
"Normalizing content...\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Phase 1 Complete. Ingested 6 documents from multiple sources.\n",
"Phase 1 Complete. Ingested 8 documents from multiple sources.\n",
" - Feed items: 2 feeds processed\n",
" - Web pages: 1 URLs processed\n",
" - Local files: 2 files processed\n",
" - Total normalized documents: 6\n"
" - Total normalized documents: 8\n"
]
}
],
@@ -696,7 +706,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
@@ -715,7 +725,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 59,
"metadata": {},
"outputs": [
{
@@ -752,7 +762,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 60,
"metadata": {},
"outputs": [
{
@@ -766,10 +776,12 @@
" Document 4: 17 chunks created\n",
" Document 5: 26 chunks created\n",
" Document 6: 24 chunks created\n",
" Document 7: 1 chunks created\n",
" Document 8: 2 chunks created\n",
"\n",
"Phase 2 Complete. Generated 73 semantic chunks.\n",
" - Average chunk size: 2071 characters\n",
" - Total chunks: 73\n"
"Phase 2 Complete. Generated 76 semantic chunks.\n",
" - Average chunk size: 2008 characters\n",
" - Total chunks: 76\n"
]
}
],
@@ -814,7 +826,7 @@
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
@@ -839,7 +851,7 @@
},
{
"cell_type": "code",
"execution_count": 54,
"execution_count": 62,
"metadata": {},
"outputs": [
{
@@ -909,7 +921,7 @@
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 63,
"metadata": {},
"outputs": [
{
@@ -948,7 +960,7 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 64,
"metadata": {},
"outputs": [
{
@@ -960,11 +972,12 @@
"\n",
"Chunk 1/10:\n",
" Extracting entities...\n",
" Found 0 entities\n",
" Skipping relationship extraction (no entities found)\n",
" Found 3 entities\n",
" Extracting relationships...\n",
" Found 2 relationships\n",
" Extracting triplets...\n",
"DEBUG: Entity map keys: ['href=\"https://makeupandbeautyblog.com', 'monday', '2/', 'beauty blog', '893', 'december-15-2025-610x763.jpg', '5px;max', '100%', 'https://makeupandbeautyblog.com/wp-content/uploads/2025/12/mbb-monday-poll-december-15-2025-768x960.jpg 768w', 'https://makeupandbeautyblog.com/wp-content/uploads/2025/12/mbb-monday-poll-december-15-2025.jpg 1000w']\n",
" Found 0 triplets\n",
"DEBUG: Entity map keys: ['makeup and beauty blog', 'monday poll', 'december 15, 2025']\n",
" Found 9 triplets\n",
" Detecting events...\n",
" Found 0 events\n",
"\n",
@@ -972,127 +985,98 @@
" Extracting entities...\n",
" Found 8 entities\n",
" Extracting relationships...\n",
" Found 0 relationships\n",
" Found 3 relationships\n",
" Extracting triplets...\n",
"DEBUG: Entity map keys: ['jo malone', 'laneige', 'ulta', 'trader joe', 'monday', '2007', 'makeup and beauty blog', 'makeup and beauty blog | makeup reviews, swatches and how-to makeup']\n",
" Found 27 triplets\n",
"DEBUG: Entity map keys: ['jo malone', 'laneige', 'ulta', 'trader joe', 'monday', '2007', 'makeup and beauty blog', 'makeup and beauty blog monday poll, vol. 893']\n",
" Found 198 triplets\n",
" Detecting events...\n",
" Found 1 events\n",
"\n",
"Chunk 3/10:\n",
" Extracting entities...\n",
" Found 7 entities\n",
" Found 5 entities\n",
" Extracting relationships...\n",
" Found 9 relationships\n",
" Found 4 relationships\n",
" Extracting triplets...\n",
"DEBUG: Entity map keys: ['makeup and beauty blog', 'monday poll', 'vol. 892', 'monday morning', '2007']\n",
" Found 7 triplets\n",
"DEBUG: Entity map keys: ['makeup and beauty blog', 'monday poll', 'makeup and beauty blog monday poll', 'vol. 892', '2007']\n",
" Found 5 triplets\n",
" Detecting events...\n",
" Found 0 events\n",
"\n",
"Chunk 4/10:\n",
" Extracting entities...\n",
" Found 10 entities\n",
" Found 5 entities\n",
" Extracting relationships...\n",
" Found 9 relationships\n",
" Found 2 relationships\n",
" Extracting triplets...\n",
"DEBUG: Entity map keys: ['holiday shopping', 'mbb', 'makeup and beauty blog', 'makeup reviews, swatches and how-to makeup', 'makeup and beauty blog monday poll, vol. 892', 'makeupandbeautyblog.com']\n",
" Found 10 triplets\n",
"DEBUG: Entity map keys: ['holiday', 'mbb', 'makeup and beauty blog']\n",
" Found 9 triplets\n",
" Detecting events...\n",
" Found 3 events\n",
"\n",
"Chunk 5/10:\n",
" Extracting entities...\n",
" Found 9 entities\n",
" Found 7 entities\n",
" Extracting relationships...\n",
" Found 10 relationships\n",
" Found 3 relationships\n",
" Extracting triplets...\n",
"DEBUG: Entity map keys: ['makeup and beauty blog', 'monday poll', 'vol. 891', 'hawaii', 'lanikai', '2024', '2025', 'makeup and beauty blog monday poll', '2007']\n",
" Found 14 triplets\n",
"DEBUG: Entity map keys: ['makeup and beauty blog', 'monday poll', 'vol. 891', 'hawaii', 'lanikai', '2024', '2007']\n",
" Found 18 triplets\n",
" Detecting events...\n",
" Found 0 events\n",
"\n",
"Chunk 6/10:\n",
" Extracting entities...\n",
" Found 9 entities\n",
" Found 4 entities\n",
" Extracting relationships...\n",
" Found 0 relationships\n",
" Found 1 relationships\n",
" Extracting triplets...\n",
"DEBUG: Entity map keys: ['my upper lip', 'a friend of mine', 'cavallo point', 'sausalito', 'rosie', 'marnie', 'makeup and beauty blog', 'makeup reviews, swatches and how-to makeup']\n",
" Found 9 triplets\n",
"DEBUG: Entity map keys: ['cavallo point', 'sausalito', 'rosie', 'marnie']\n",
" Found 11 triplets\n",
" Detecting events...\n",
" Found 3 events\n",
"\n",
"Chunk 7/10:\n",
" Extracting entities...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"LLM entity extraction failed: Failed to parse JSON from Groq response: Failed to parse extracted JSON: Expecting ',' delimiter: line 280 column 70 (char 19586)\n",
"Raw snippet: [\n",
" {\"text\": \"I\", \"label\": \"PERSON\", \"start\": 0, \"...\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" Found 0 entities\n",
" Skipping relationship extraction (no entities found)\n",
" Extracting entities...\n",
" Found 207 entities\n",
" Extracting relationships...\n",
" Found 0 relationships\n",
" Extracting triplets...\n",
"DEBUG: Entity map keys: ['the treatment options and products to control rosacea</h2>', 'over 35 years', '#', '10px', 'rosacea skincare', 'li><a', '4', 'href=\"https://drbaileyskincare.com', 'therapy skincare kit', 'step rosacea skincare', 'calming zinc', 'the rosacea therapy kit).</li>', 'li aria', 'li aria-level=\"2\">alternatively', 'sodium sulfacetamide sulfur', 'gentle foaming facial cleanser</a>', 'li aria-level=\"2\">in', 'height=\"340', 'alt=\"extremely gentle foaming facial cleanser']\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"LLM triplet extraction failed: Failed to parse JSON from Groq response: Failed to parse extracted JSON: Expecting ',' delimiter: line 218 column 104 (char 27090)\n",
"Raw snippet: [\n",
" {\"subject\": \"rosacea\", \"predicate\": \"has_treat...\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" Found 0 triplets\n",
"DEBUG: Entity map keys: ['rosacea', 'dr. bailey', \"dr. bailey's skincare\"]\n",
" Found 25 triplets\n",
" Detecting events...\n",
" Found 0 events\n",
"\n",
"Chunk 8/10:\n",
" Extracting entities...\n",
" Found 8 entities\n",
" Found 9 entities\n",
" Extracting relationships...\n",
" Found 7 relationships\n",
" Found 5 relationships\n",
" Extracting triplets...\n",
"DEBUG: Entity map keys: ['dr. bailey', 'green tea antioxidant skin therapy', 'rosacea therapy skin care kit', 'benzoyl peroxide cream', \"dr. bailey's skincare\"]\n",
"DEBUG: Entity map keys: ['dr. bailey', 'green tea antioxidant skin therapy', 'rosacea therapy skin care kit', 'rosacea treatment routine', 'benzoyl peroxide cream', \"dr. bailey's skincare\"]\n",
" Found 7 triplets\n",
" Detecting events...\n",
" Found 0 events\n",
"\n",
"Chunk 9/10:\n",
" Extracting entities...\n",
" Found 8 entities\n",
" Found 7 entities\n",
" Extracting relationships...\n",
" Found 10 relationships\n",
" Extracting triplets...\n",
"DEBUG: Entity map keys: ['rosacea', 'facial dandruff', 'calming zinc soap', 'green tea antioxidant skin therapy', 'clotrimazole', \"dr. bailey's skincare\", 'rosacea medical treatments: creams, topical & oral medications, and procedures', 'dr. bailey']\n",
" Found 13 triplets\n",
"DEBUG: Entity map keys: ['facial dandruff', 'rosacea', 'calming zinc soap', 'green tea antioxidant skin therapy', 'clotrimazole', \"dr. bailey's skincare\", 'dr. bailey']\n",
" Found 12 triplets\n",
" Detecting events...\n",
" Found 0 events\n",
"\n",
"Chunk 10/10:\n",
" Extracting entities...\n",
" Found 10 entities\n",
" Found 6 entities\n",
" Extracting relationships...\n",
" Found 9 relationships\n",
" Found 5 relationships\n",
" Extracting triplets...\n",
"DEBUG: Entity map keys: ['dr. bailey', 'daily moisturizing face cream', \"dr. bailey's skincare\", 'omega enriched face booster oil', 'sea buckthorn', 'castor seed oil']\n",
" Found 8 triplets\n",
"DEBUG: Entity map keys: ['hydrate', 'rosacea', 'daily moisturizing face cream', 'omega enriched face booster oil', 'sea buckthorn', 'castor seed oil']\n",
" Found 11 triplets\n",
" Detecting events...\n",
" Found 0 events\n",
"\n"
@@ -1226,7 +1210,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 65,
"metadata": {},
"outputs": [
{
@@ -1236,25 +1220,25 @@
"============================================================\n",
"Phase 3 Complete - Extraction Summary\n",
"============================================================\n",
"Entities extracted: 69\n",
"Relationships extracted: 54\n",
"Triplets extracted: 95\n",
"Entities extracted: 261\n",
"Relationships extracted: 35\n",
"Triplets extracted: 305\n",
"Events detected: 7\n",
"============================================================\n",
"\n",
"Sample entities:\n",
" - Makeup and Beauty Blog (ORG)\n",
" - Monday Poll (EVENT)\n",
" - December 15, 2025 (DATE)\n",
" - Jo Malone (ORG)\n",
" - Laneige (ORG)\n",
" - Ulta (ORG)\n",
" - Trader Joe (ORG)\n",
" - Monday (DATE)\n",
"\n",
"Sample relationships:\n",
" - Makeup and Beauty Blog --[publishes]--> Monday Poll\n",
" - Makeup and Beauty Blog --[posts]--> Monday Poll\n",
" - Makeup and Beauty Blog --[has been posting]--> Monday Poll\n",
" - Monday Poll --[has been posted]--> Makeup and Beauty Blog\n",
" - Monday Poll --[is published on]--> Makeup and Beauty Blog\n"
" - Entity(text='Makeup and Beauty Blog', label='ORG', start_char=0, end_char=20, confidence=1.0, metadata={'provider': 'groq', 'model': 'llama-3.1-8b-instant', 'extraction_method': 'llm'}) --[hosts]--> Entity(text='Monday Poll', label='EVENT', start_char=20, end_char=32, confidence=1.0, metadata={'provider': 'groq', 'model': 'llama-3.1-8b-instant', 'extraction_method': 'llm'})\n",
" - Entity(text='Monday Poll', label='EVENT', start_char=20, end_char=32, confidence=1.0, metadata={'provider': 'groq', 'model': 'llama-3.1-8b-instant', 'extraction_method': 'llm'}) --[occurs on]--> Entity(text='December 15, 2025', label='DATE', start_char=53, end_char=71, confidence=1.0, metadata={'provider': 'groq', 'model': 'llama-3.1-8b-instant', 'extraction_method': 'llm'})\n",
" - Entity(text='Makeup and Beauty Blog', label='ORG', start_char=187, end_char=207, confidence=1, metadata={'provider': 'groq', 'model': 'llama-3.1-8b-instant', 'extraction_method': 'llm'}) --[publishes]--> Entity(text='Makeup and Beauty Blog Monday Poll, Vol. 893', label='EVENT', start_char=208, end_char=238, confidence=1, metadata={'provider': 'groq', 'model': 'llama-3.1-8b-instant', 'extraction_method': 'llm'})\n",
" - Entity(text='Makeup and Beauty Blog', label='ORG', start_char=187, end_char=207, confidence=1, metadata={'provider': 'groq', 'model': 'llama-3.1-8b-instant', 'extraction_method': 'llm'}) --[has]--> Entity(text='Monday', label='DATE', start_char=35, end_char=43, confidence=1, metadata={'provider': 'groq', 'model': 'llama-3.1-8b-instant', 'extraction_method': 'llm'})\n",
" - Entity(text='Makeup and Beauty Blog', label='ORG', start_char=187, end_char=207, confidence=1, metadata={'provider': 'groq', 'model': 'llama-3.1-8b-instant', 'extraction_method': 'llm'}) --[has]--> Entity(text='2007', label='DATE', start_char=57, end_char=65, confidence=1, metadata={'provider': 'groq', 'model': 'llama-3.1-8b-instant', 'extraction_method': 'llm'})\n"
]
}
],
@@ -1314,7 +1298,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 66,
"metadata": {},
"outputs": [
{
@@ -1324,8 +1308,8 @@
"Building knowledge graph...\n",
"Initial graph statistics:\n",
" - Entities: 20\n",
" - Relationships: 54\n",
" - Metadata: {'num_entities': 20, 'num_relationships': 54, 'temporal_enabled': False, 'timestamp': '2025-12-23T23:19:22.955279', 'entity_resolution_applied': True}\n"
" - Relationships: 35\n",
" - Metadata: {'num_entities': 20, 'num_relationships': 35, 'temporal_enabled': False, 'timestamp': '2025-12-24T12:46:41.535755', 'entity_resolution_applied': True}\n"
]
}
],
@@ -1360,7 +1344,7 @@
},
{
"cell_type": "code",
"execution_count": 63,
"execution_count": 67,
"metadata": {},
"outputs": [
{
@@ -1369,7 +1353,13 @@
"text": [
"Resolving entities (deduplication)...\n",
" Method: Semantic similarity matching\n",
" Threshold: 0.85 (85% similarity)\n",
" Threshold: 0.85 (85% similarity)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Entity resolution complete:\n",
" - Original entities: 20\n",
@@ -1417,7 +1407,7 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 68,
"metadata": {},
"outputs": [
{
@@ -1427,11 +1417,11 @@
"Analyzing graph structure...\n",
"\n",
"Graph structure metrics:\n",
" - Graph density: 0.0000\n",
" - Graph density: 0.0900\n",
" - Connected components: 0\n",
" - Average degree: 0.00\n",
" - Total nodes: 0\n",
" - Total edges: 0\n"
" - Average degree: 2.20\n",
" - Total nodes: 25\n",
" - Total edges: 27\n"
]
}
],
@@ -1446,12 +1436,16 @@
"# Perform comprehensive graph analysis\n",
"analysis = analyzer.analyze_graph(kg_final)\n",
"\n",
"# Extract metrics from nested structure\n",
"metrics = analysis.get('metrics', {})\n",
"connectivity = analysis.get('connectivity', {})\n",
"\n",
"print(f\"\\nGraph structure metrics:\")\n",
"print(f\" - Graph density: {analysis.get('density', 0):.4f}\")\n",
"print(f\" - Connected components: {analysis.get('connected_components', 0)}\")\n",
"print(f\" - Average degree: {analysis.get('average_degree', 0):.2f}\")\n",
"print(f\" - Total nodes: {analysis.get('num_nodes', 0)}\")\n",
"print(f\" - Total edges: {analysis.get('num_edges', 0)}\")\n"
"print(f\" - Graph density: {metrics.get('density', 0):.4f}\")\n",
"print(f\" - Connected components: {connectivity.get('connected_components', 0)}\")\n",
"print(f\" - Average degree: {metrics.get('avg_degree', 0):.2f}\")\n",
"print(f\" - Total nodes: {metrics.get('num_nodes', 0)}\")\n",
"print(f\" - Total edges: {metrics.get('num_edges', 0)}\")\n"
]
},
{
@@ -1468,7 +1462,7 @@
},
{
"cell_type": "code",
"execution_count": 65,
"execution_count": 69,
"metadata": {},
"outputs": [
{
@@ -1478,11 +1472,11 @@
"Calculating centrality measures...\n",
"\n",
"Top 5 entities by degree centrality:\n",
" 1. Makeup and Beauty Blog: 0.3571\n",
" 2. Dr. Bailey's Skincare: 0.3214\n",
" 3. Dr. Bailey: 0.2143\n",
" 4. Green Tea Antioxidant Skin Therapy: 0.1786\n",
" 5. Omega Enriched Face Booster Oil: 0.1429\n"
" 1. Makeup and Beauty Blog: 0.4167\n",
" 2. Green Tea Antioxidant Skin Therapy: 0.2083\n",
" 3. Dr. Bailey: 0.1667\n",
" 4. Calming Zinc Soap: 0.1667\n",
" 5. Rosacea Therapy Skin Care Kit: 0.1250\n"
]
}
],
@@ -1524,7 +1518,7 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": 70,
"metadata": {},
"outputs": [
{
@@ -1535,13 +1529,13 @@
" Method: Louvain algorithm (greedy modularity optimization)\n",
"\n",
"Community detection results:\n",
" - Total communities found: 6\n",
" - Total communities found: 4\n",
" - Community 1: 10 entities\n",
" Sample entities: 2025, Makeup and Beauty Blog Monday Poll, Makeup and Beauty Blog\n",
" Sample entities: 2007, Makeup and Beauty Blog Monday Poll, Monday Poll\n",
" - Community 2: 8 entities\n",
" Sample entities: Benzoyl Peroxide Cream, Omega Enriched Face Booster Oil, Rosacea Medical Treatments: Creams, Topical & Oral Medications, and Procedures\n",
" Sample entities: Dr. Bailey, rosacea, Facial dandruff\n",
" - Community 3: 5 entities\n",
" Sample entities: Rosacea Therapy Skin Care Kit, Green Tea Antioxidant Skin Therapy, rosacea\n"
" Sample entities: Omega Enriched Face Booster Oil, sea buckthorn, Hydrate\n"
]
}
],
@@ -1586,7 +1580,7 @@
},
{
"cell_type": "code",
"execution_count": 67,
"execution_count": 71,
"metadata": {},
"outputs": [
{
@@ -1598,10 +1592,10 @@
"============================================================\n",
"Final graph contains:\n",
" - Entities: 20\n",
" - Relationships: 54\n",
" - Graph density: 0.0000\n",
" - Relationships: 35\n",
" - Graph density: 0.0900\n",
" - Connected components: 0\n",
" - Communities: 6\n",
" - Communities: 4\n",
"============================================================\n"
]
}
@@ -1610,11 +1604,15 @@
"print(\"=\" * 60)\n",
"print(\"Phase 4 Complete - Knowledge Graph Summary\")\n",
"print(\"=\" * 60)\n",
"# Extract metrics from nested structure (if not already extracted)\n",
"metrics = analysis.get('metrics', {})\n",
"connectivity = analysis.get('connectivity', {})\n",
"\n",
"print(f\"Final graph contains:\")\n",
"print(f\" - Entities: {len(kg_final['entities'])}\")\n",
"print(f\" - Relationships: {len(kg_final.get('relationships', []))}\")\n",
"print(f\" - Graph density: {analysis.get('density', 0):.4f}\")\n",
"print(f\" - Connected components: {analysis.get('connected_components', 0)}\")\n",
"print(f\" - Graph density: {metrics.get('density', 0):.4f}\")\n",
"print(f\" - Connected components: {connectivity.get('connected_components', 0)}\")\n",
"print(f\" - Communities: {len(communities) if communities else 0}\")\n",
"print(\"=\" * 60)\n"
]
@@ -1644,7 +1642,7 @@
},
{
"cell_type": "code",
"execution_count": 68,
"execution_count": 72,
"metadata": {},
"outputs": [
{
@@ -1652,7 +1650,7 @@
"output_type": "stream",
"text": [
"Preparing texts for embedding...\n",
" - Total chunks to embed: 73\n",
" - Total chunks to embed: 76\n",
" - Embedding model: all-MiniLM-L6-v2\n",
" - Expected dimension: 384\n"
]
@@ -1670,9 +1668,16 @@
},
{
"cell_type": "code",
"execution_count": 69,
"execution_count": 73,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Generating embeddings...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
@@ -1684,20 +1689,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Generating embeddings...\n",
"Embeddings generated successfully:\n",
" - Total embeddings: 73\n"
]
},
{
"ename": "ValueError",
"evalue": "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[69], line 7\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEmbeddings generated successfully:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m - Total embeddings: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(embeddings)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m - Embedding dimension: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(embeddings[\u001b[38;5;241m0\u001b[39m])\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mif\u001b[39;00m\u001b[38;5;250m \u001b[39membeddings\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01melse\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;241m0\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[1;31mValueError\u001b[0m: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
" - Total embeddings: 76\n",
" - Embedding dimension: 128\n"
]
}
],
@@ -1708,7 +1702,7 @@
"\n",
"print(f\"Embeddings generated successfully:\")\n",
"print(f\" - Total embeddings: {len(embeddings)}\")\n",
"print(f\" - Embedding dimension: {len(embeddings[0]) if embeddings else 0}\")\n"
"print(f\" - Embedding dimension: {embeddings.shape[1] if len(embeddings) > 0 else 0}\")\n"
]
},
{
@@ -1722,7 +1716,7 @@
},
{
"cell_type": "code",
"execution_count": 70,
"execution_count": 74,
"metadata": {},
"outputs": [
{
@@ -1730,7 +1724,7 @@
"output_type": "stream",
"text": [
"Preparing metadata...\n",
" - Metadata entries created: 73\n"
" - Metadata entries created: 76\n"
]
}
],
@@ -1761,7 +1755,7 @@
},
{
"cell_type": "code",
"execution_count": 71,
"execution_count": 75,
"metadata": {},
"outputs": [
{
@@ -1771,19 +1765,10 @@
"Storing vectors in vector store...\n",
"\n",
"Phase 5 Complete. Vector store populated successfully.\n",
" - Vectors stored: 73\n",
" - Vector store backend: FAISS\n"
]
},
{
"ename": "ValueError",
"evalue": "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[71], line 8\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m - Vectors stored: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(embeddings)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m - Vector store backend: FAISS\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m - Embedding dimension: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(embeddings[\u001b[38;5;241m0\u001b[39m])\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mif\u001b[39;00m\u001b[38;5;250m \u001b[39membeddings\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01melse\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;241m0\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m - Ready for similarity search\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[1;31mValueError\u001b[0m: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
" - Vectors stored: 76\n",
" - Vector store backend: FAISS\n",
" - Embedding dimension: 128\n",
" - Ready for similarity search\n"
]
}
],
@@ -1795,7 +1780,7 @@
"print(f\"\\nPhase 5 Complete. Vector store populated successfully.\")\n",
"print(f\" - Vectors stored: {len(embeddings)}\")\n",
"print(f\" - Vector store backend: FAISS\")\n",
"print(f\" - Embedding dimension: {len(embeddings[0]) if embeddings else 0}\")\n",
"print(f\" - Embedding dimension: {embeddings.shape[1] if len(embeddings) > 0 else 0}\")\n",
"print(f\" - Ready for similarity search\")\n"
]
},
@@ -1824,7 +1809,7 @@
},
{
"cell_type": "code",
"execution_count": 72,
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
@@ -1848,7 +1833,7 @@
},
{
"cell_type": "code",
"execution_count": 73,
"execution_count": 77,
"metadata": {},
"outputs": [
{
@@ -1894,7 +1879,7 @@
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": 78,
"metadata": {},
"outputs": [
{
@@ -1903,18 +1888,18 @@
"text": [
"LLM provider initialized.\n",
" - Provider: Groq\n",
" - Model: llama-3.1-70b-versatile\n",
" - Model: llama-3.3-70b-versatile\n",
" - Ready for answer generation\n"
]
}
],
"source": [
"# Initialize LLM provider for answer generation\n",
"llm_provider = create_provider(\"groq\", model=\"llama-3.1-70b-versatile\")\n",
"llm_provider = create_provider(\"groq\", model=\"llama-3.3-70b-versatile\")\n",
"\n",
"print(\"LLM provider initialized.\")\n",
"print(f\" - Provider: Groq\")\n",
"print(f\" - Model: llama-3.1-70b-versatile\")\n",
"print(f\" - Model: llama-3.3-70b-versatile\")\n",
"print(f\" - Ready for answer generation\")\n"
]
},
@@ -1929,7 +1914,7 @@
},
{
"cell_type": "code",
"execution_count": 75,
"execution_count": 79,
"metadata": {},
"outputs": [
{
@@ -1960,7 +1945,7 @@
},
{
"cell_type": "code",
"execution_count": 76,
"execution_count": 80,
"metadata": {},
"outputs": [
{
@@ -1996,7 +1981,7 @@
},
{
"cell_type": "code",
"execution_count": 77,
"execution_count": 81,
"metadata": {},
"outputs": [
{
@@ -2045,7 +2030,7 @@
},
{
"cell_type": "code",
"execution_count": 78,
"execution_count": 82,
"metadata": {},
"outputs": [
{
@@ -2106,7 +2091,7 @@
},
{
"cell_type": "code",
"execution_count": 79,
"execution_count": 83,
"metadata": {},
"outputs": [
{
@@ -2117,11 +2102,37 @@
"GENERATING FINAL ANSWER\n",
"======================================================================\n",
"Using LLM to synthesize answer from retrieved context...\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"To prevent irritation when using Retinol, it's essential to combine it with ingredients that synergize well and help mitigate potential side effects such as redness, dryness, and sensitivity. Here are some key ingredients known to work harmoniously with Retinol:\n",
"\n",
"Warning: LLM generation failed: Error code: 400 - {'error': {'message': 'The model `llama-3.1-70b-versatile` has been decommissioned and is no longer supported. Please refer to https://console.groq.com/docs/deprecations for a recommendation on which model to use instead.', 'type': 'invalid_request_error', 'code': 'model_decommissioned'}}\n",
"1. **Hyaluronic Acid**: This natural humectant is renowned for its ability to retain moisture in the skin. When used alongside Retinol, Hyaluronic Acid helps to counterbalance the drying effects that Retinol can sometimes cause, ensuring the skin remains hydrated and comfortable.\n",
"\n",
"However, we successfully retrieved relevant context using GraphRAG!\n",
"The context above can be used to answer the query manually.\n"
"2. **Niacinamide**: A form of Vitamin B3, Niacinamide is celebrated for its skin-restoring abilities. It can improve the skin's barrier function, reduce inflammation, and enhance the skin's natural ceramide production. When paired with Retinol, Niacinamide can help reduce the risk of irritation and support the skin's overall health and resilience.\n",
"\n",
"3. **Ceramides**: These are vital components of the skin's barrier, helping to lock in moisture and protect the skin from environmental stressors. Combining Ceramides with Retinol can help repair and restore the skin's barrier, minimizing the potential for irritation and dryness associated with Retinol use.\n",
"\n",
"4. **Glycerin**: Similar to Hyaluronic Acid, Glycerin is a humectant that attracts and retains moisture in the skin. Its inclusion in a Retinol regimen can help maintain the skin's moisture balance, reducing the likelihood of dryness and irritation.\n",
"\n",
"5. **Squalane**: This naturally occurring lipid is highly compatible with the skin and serves as an excellent moisturizer. Squalane can help protect the skin from water loss, reducing the potential for Retinol-induced dryness and irritation, and leaving the skin feeling soft and supple.\n",
"\n",
"6. **Peptides**: Short chains of amino acids, Peptides can stimulate collagen production and improve skin elasticity. When used in conjunction with Retinol, Peptides may help in reducing the appearance of fine lines and wrinkles while supporting the skin's natural repair processes, potentially minimizing irritation.\n",
"\n",
"7. **Vitamin E**: Known for its antioxidant properties, Vitamin E can help protect the skin from oxidative stress and damage caused by free radicals. Combining Vitamin E with Retinol may enhance the skin's tolerance to Retinol by reducing oxidative stress, thereby potentially lowering the risk of irritation.\n",
"\n",
"8. **Aloe Vera**: Aloe Vera is widely recognized for its soothing and calming effects on the skin. It can help mitigate irritation and redness, making it a beneficial ingredient to use alongside Retinol, especially for sensitive skin types.\n",
"\n",
"When incorporating these ingredients into your skincare routine with Retinol, it's crucial to follow a few guidelines:\n",
"- Start with a lower concentration of Retinol and gradually increase as your skin becomes more tolerant.\n",
"- Apply moisturizing products containing these synergistic ingredients generously to help buffer the skin against potential irritation.\n",
"- Be patient, as it may take some time for your skin to adjust to Retinol, even with the use of soothing ingredients.\n",
"\n",
"Remember, everyone's skin is different, so it's essential to monitor your skin's response to Retinol and adjust your skincare regimen accordingly. If irritation persists, consider consulting with a dermatologist for personalized advice.\n"
]
}
],
@@ -2187,9 +2198,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 84,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Visualizing knowledge graph...\n",
" - Layout: Spring (force-directed)\n",
" - Title: GraphRAG Knowledge Graph\n",
"Graph visualization complete.\n"
]
}
],
"source": [
"from semantica.visualization import KGVisualizer\n",
"import matplotlib.pyplot as plt\n",
@@ -2228,9 +2250,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 85,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Exporting knowledge graph...\n",
" Warning: JSON export error: 'GraphExporter' object has no attribute 'export_json'\n",
" Warning: GraphML export error: 'GraphExporter' object has no attribute 'export_graphml'\n",
"\n",
"Phase 7 Complete. Graph visualized and exported.\n"
]
}
],
"source": [
"from semantica.export import GraphExporter\n",
"\n",