{"id":10918,"date":"2026-03-25T21:48:03","date_gmt":"2026-03-25T21:48:03","guid":{"rendered":"https:\/\/www.aihello.com\/resources\/?p=10918---81d93d36-f26e-4a70-b080-58a48fb095a1"},"modified":"2026-09-22T17:51:31","modified_gmt":"2026-09-22T17:51:31","slug":"creating-a-knowledge-graph-without-llms","status":"publish","type":"post","link":"https:\/\/www.aihello.com\/resources\/blog\/creating-a-knowledge-graph-without-llms\/","title":{"rendered":"Creating a Knowledge Graph Without LLMs"},"content":{"rendered":"<div class=\"wp-block-group ah-reading-guide\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\"><style>\n.ah-reading-guide{max-width:820px;margin:0 auto;color:#26364a;font-size:18px;line-height:1.8;counter-reset:ah-section}.ah-reading-guide *{box-sizing:border-box}.ah-reading-guide p{margin:0 0 22px;line-height:1.8}.ah-reading-guide strong{color:#142d48}.ah-reading-guide h2{position:relative;margin:56px 0 24px;padding-top:24px;border-top:1px solid #dce7ef;font-size:29px;line-height:1.35;letter-spacing:-.4px;color:#142d48}.ah-reading-guide h2.ah-numbered{padding-left:54px;counter-increment:ah-section}.ah-reading-guide h2.ah-numbered:before{content:counter(ah-section,decimal-leading-zero);position:absolute;left:0;top:26px;display:flex;align-items:center;justify-content:center;width:38px;height:38px;background:#eaf4fc;color:#216faf;border-radius:10px;font-size:15px;font-weight:700;letter-spacing:0}.ah-reading-guide h3{position:relative;margin:34px 0 16px;font-size:23px;line-height:1.4;color:#163f65}.ah-reading-guide h4{position:relative;margin:28px 0 14px;font-size:20px;line-height:1.45;color:#163f65}.ah-reading-guide .ah-lead{margin:0 0 28px;padding:24px 28px;border-left:4px solid #2479ba;border-radius:0 12px 12px 0;background:#eef6fc;font-size:20px;line-height:1.75}.ah-reading-guide .ah-callout,.ah-reading-guide .ah-formula{margin:28px 0;padding:24px 26px;border:1px solid #d7e5f1;border-left:4px solid #2479ba;border-radius:0 12px 12px 0;background:#f5f9fd}.ah-reading-guide .ah-formula{font-size:20px;font-weight:600;font-variant-numeric:tabular-nums;line-height:1.9;overflow-wrap:anywhere}.ah-reading-guide ul,.ah-reading-guide ol{margin:24px 0 30px;padding-left:28px}.ah-reading-guide li{margin:0 0 12px;padding-left:7px;line-height:1.75}.ah-reading-guide li::marker{color:#2479ba;font-weight:700}.ah-reading-guide ol.ah-steps{list-style:none;counter-reset:ah-step;padding:0;display:grid;gap:14px}.ah-reading-guide ol.ah-steps>li{position:relative;counter-increment:ah-step;margin:0;padding:22px 24px 22px 64px;border:1px solid #dbe7f1;border-radius:12px;background:#f6fafd}.ah-reading-guide ol.ah-steps>li:before{content:counter(ah-step);position:absolute;top:23px;left:20px;display:flex;align-items:center;justify-content:center;width:28px;height:28px;border-radius:50%;background:#2378b9;color:white;font-size:14px;font-weight:700}.ah-reading-guide ul.ah-cards{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:14px;padding:0;list-style:none}.ah-reading-guide ul.ah-cards>li{margin:0;padding:20px 22px;border:1px solid #dbe8f2;background:#f7fbfe;border-radius:12px;font-size:17px;line-height:1.7}.ah-reading-guide ul.ah-cards>li:last-child:nth-child(odd){grid-column:1\/-1}.ah-reading-guide .ah-cards>li>strong:first-child{display:block;margin-bottom:8px;color:#19598d}.ah-reading-guide .ah-step-panel{margin:22px 0;padding:24px 28px;border-left:4px solid #2479ba;border-radius:0 12px 12px 0;background:#f5f9fd}.ah-reading-guide .ah-step-panel h3{margin-top:0}.ah-reading-guide .ah-step-panel p:last-child{margin-bottom:0}.ah-reading-guide figure{max-width:100%;margin:30px 0}.ah-reading-guide figure.wp-block-image{padding:7px;border:1px solid #e0e8ef;border-radius:12px;background:white;box-shadow:0 5px 18px rgba(20,45,72,.06)}.ah-reading-guide img{max-width:100%;height:auto}.ah-reading-guide figure.wp-block-image img{display:block;border-radius:6px}.ah-reading-guide figcaption{font-size:14px;line-height:1.5;color:#587086;margin-top:10px}.ah-reading-guide figure.wp-block-table{overflow-x:auto;border:1px solid #d8e5ef;border-radius:10px}.ah-reading-guide table{width:100%;border-collapse:collapse;font-size:16px;line-height:1.6}.ah-reading-guide th,.ah-reading-guide td{padding:14px 16px;border:1px solid #d8e5ef;text-align:left;vertical-align:top}.ah-reading-guide th{background:#eaf4fc;color:#163f65;font-weight:700}.ah-reading-guide tbody tr:nth-child(even){background:#f6f9fc}.ah-reading-guide blockquote{margin:30px 0;padding:22px 26px;border-left:4px solid #2479ba;background:#f3f8fc;border-radius:0 10px 10px 0}.ah-reading-guide #ez-toc-container{width:100%;margin:32px 0 42px;padding:22px 24px;border:1px solid #cbdfee;border-top:4px solid #2479ba;border-radius:12px;background:#f7fbfe;box-shadow:none}.ah-reading-guide #ez-toc-container .ez-toc-title{margin:0;font-size:20px;font-weight:700;color:#163f65;line-height:1.5}.ah-reading-guide #ez-toc-container .ez-toc-title-container{margin-bottom:14px}.ah-reading-guide #ez-toc-container ul.ez-toc-list-level-1{display:block;column-count:2;column-gap:26px;padding:0;margin:0}.ah-reading-guide #ez-toc-container ul.ez-toc-list-level-1>li{break-inside:avoid;padding:11px 0;margin:0;border-bottom:1px solid #dce7ef;line-height:1.5;font-size:16px}.ah-reading-guide #ez-toc-container ul ul{margin:7px 0 0;padding-left:18px}.ah-reading-guide #ez-toc-container li li{padding:0;margin:5px 0;font-size:14px}.ah-reading-guide #ez-toc-container a{color:#19598d;text-decoration:none}.ah-reading-guide #ez-toc-container a:hover{text-decoration:underline}.ah-reading-guide #ez-toc-container a:focus-visible{outline:3px solid #2479ba;outline-offset:4px;border-radius:3px}.ah-reading-guide :is(h2,h3,h4)>.ez-toc-section{position:absolute;top:-100px;left:0}.ah-reading-guide :is(h2,h3,h4)>.ez-toc-section-end{position:absolute;top:0;left:0}.ah-reading-guide [id]{scroll-margin-top:100px}.ah-reading-guide .gspb_container{min-width:0}@media(max-width:700px){.ah-reading-guide{font-size:17px}.ah-reading-guide h2{font-size:25px;margin-top:44px}.ah-reading-guide h2.ah-numbered{padding-left:43px}.ah-reading-guide h2.ah-numbered:before{width:30px;height:30px;font-size:13px}.ah-reading-guide h3{font-size:21px}.ah-reading-guide ul.ah-cards{grid-template-columns:1fr}.ah-reading-guide .ah-lead,.ah-reading-guide .ah-callout,.ah-reading-guide .ah-formula,.ah-reading-guide .ah-step-panel{padding:20px}.ah-reading-guide .ah-lead,.ah-reading-guide .ah-formula{font-size:18px}.ah-reading-guide #ez-toc-container{padding:18px}.ah-reading-guide #ez-toc-container ul.ez-toc-list-level-1{column-count:1}}@media(prefers-reduced-motion:reduce){.ah-reading-guide [data-aos]{opacity:1!important;visibility:visible!important;transform:none!important;transition:none!important;animation:none!important}}\n\n.ah-reading-guide .ah-process-heading{padding-left:48px;min-height:36px}.ah-reading-guide .ah-process-heading:before{position:absolute;left:0;top:0;width:34px;height:34px;display:flex;align-items:center;justify-content:center;border-radius:9px;background:#eaf4fc;color:#216faf;font-size:15px;font-weight:700}.ah-reading-guide .ah-process-1:before{content:\"01\"}.ah-reading-guide .ah-process-2:before{content:\"02\"}.ah-reading-guide .ah-process-3:before{content:\"03\"}.ah-reading-guide .ah-process-4:before{content:\"04\"}.ah-reading-guide .ah-process-5:before{content:\"05\"}.ah-reading-guide .ah-process-6:before{content:\"06\"}.ah-reading-guide .ah-process-7:before{content:\"07\"}.ah-reading-guide .ah-process-8:before{content:\"08\"}.ah-reading-guide .ah-process-9:before{content:\"09\"}\n.ah-reading-guide .ah-definition{padding:20px 24px;margin:14px 0;border:1px solid #dbe7f1;border-radius:12px;background:#f8fbfe}.ah-reading-guide .ah-definition strong{color:#163f65}\n.ah-reading-guide .ah-long-form-list>li{margin-bottom:28px}.ah-reading-guide .ah-long-form-list>li>strong:first-child{display:block;font-size:22px;line-height:1.4;margin-bottom:16px}.ah-reading-guide .ah-long-form-list li img{display:block;margin:22px auto}.ah-reading-guide .ah-formula-comparison{font-variant-numeric:tabular-nums}\n.ah-reading-guide h5,.ah-reading-guide h6{font-size:18px;line-height:1.5;color:#163f65;margin:26px 0 14px;font-weight:700}\n.ah-reading-guide h2.ah-instruction-heading{font-size:21px;margin:24px 0 18px;padding-top:18px}.ah-reading-guide h2.ah-instruction-heading:before{top:18px}\n.ah-reading-guide ul.ah-stacked-cards{list-style:none;padding:0}.ah-reading-guide .ah-stacked-cards>li{margin:14px 0;padding:20px 24px;border:1px solid #dbe7f1;border-radius:12px;background:#f8fbfe}\n.ah-reading-guide .ah-minor-label{font-weight:700;color:#1e4e64;font-size:18px;margin:20px 0 8px;}\n.ah-reading-guide h3.ah-do,.ah-reading-guide h3.ah-dont{font-size:21px;padding:18px 22px;border-radius:0 10px 10px 0}.ah-reading-guide h3.ah-do{border-left:4px solid #20877e;background:#f0faf8}.ah-reading-guide h3.ah-dont{border-left:4px solid #ae6340;background:#fff7f0}\n.ah-reading-guide p.ah-fact-row{margin:6px 0;padding:12px 18px;background:#f4f8fc;border-left:3px solid #c7dcf0;border-radius:0 8px 8px 0;font-size:17px;line-height:1.65}.ah-reading-guide p.ah-cta a{display:inline-block;padding:12px 20px;border-radius:9px;background:#135faa;color:#fff;text-decoration:none;font-weight:700}.ah-reading-guide p.ah-cta a:hover{background:#104d89}.ah-reading-guide .ah-agency-summary td:first-child{width:38%;color:#184b75}\n.ah-reading-guide details.ah-original-index{padding:16px 20px;border:1px solid #dce7f0;border-radius:10px;margin:24px 0;background:#f8fafc}.ah-reading-guide details.ah-original-index summary{cursor:pointer;font-size:18px;font-weight:700;color:#244f74}.ah-reading-guide details.ah-original-index>ol{margin-top:18px}\n.ah-reading-guide ul.ah-cycle-cards{grid-template-columns:repeat(3,minmax(0,1fr));gap:22px}.ah-reading-guide ul.ah-cycle-cards>li{position:relative;text-align:center;display:flex;align-items:center;justify-content:center;min-height:110px;font-weight:700;color:#20567d}.ah-reading-guide ul.ah-cycle-cards>li:last-child:nth-child(odd){grid-column:auto}.ah-reading-guide ul.ah-cycle-cards>li:not(:last-child)::after{content:\"\u203a\";position:absolute;right:-19px;top:50%;transform:translateY(-50%);font-size:28px;color:#3785b4}@media(max-width:700px){.ah-reading-guide ul.ah-cycle-cards{grid-template-columns:1fr}.ah-reading-guide ul.ah-cycle-cards>li:not(:last-child)::after{right:50%;top:auto;bottom:-25px;transform:rotate(90deg)}}\n.ah-reading-guide .ah-process-10::before{content:\"10\"}\n.ah-reading-guide pre.wp-block-code{background:#122238;color:#eff6ff;border:1px solid #29415f;border-radius:12px;padding:22px 24px;margin:24px 0;overflow-x:auto;font-size:15px;line-height:1.7;white-space:pre;tab-size:4}.ah-reading-guide pre.wp-block-code code{background:transparent;color:inherit;padding:0;white-space:pre;font-family:Consolas,Monaco,monospace}.ah-reading-guide p code{background:#edf3fa;color:#204d78;border-radius:4px;padding:2px 5px}\n.ah-reading-guide ol.ah-steps.ah-start-2{counter-reset:ah-step 1}\n.ah-reading-guide ol.ah-steps.ah-start-3{counter-reset:ah-step 2}\n.ah-reading-guide ol.ah-steps.ah-start-4{counter-reset:ah-step 3}\n.ah-reading-guide ol.ah-steps.ah-start-5{counter-reset:ah-step 4}\n.ah-reading-guide ol.ah-steps.ah-start-6{counter-reset:ah-step 5}\n.ah-reading-guide ol.ah-steps.ah-start-7{counter-reset:ah-step 6}\n.ah-reading-guide ol.ah-steps.ah-start-8{counter-reset:ah-step 7}\n.ah-reading-guide ol.ah-steps.ah-start-9{counter-reset:ah-step 8}\n.ah-reading-guide ol.ah-steps.ah-start-10{counter-reset:ah-step 9}\n.ah-reading-guide ol.ah-steps.ah-start-11{counter-reset:ah-step 10}\n.ah-reading-guide ol.ah-steps.ah-start-12{counter-reset:ah-step 11}\n.ah-reading-guide #ez-toc-container nav{max-height:460px;overflow-y:auto;padding-right:8px}.ah-reading-guide figure.ah-comparison-wide{max-height:650px;overflow:auto;border:1px solid #dae4ee;border-radius:12px}.ah-reading-guide .ah-comparison-wide table{min-width:760px;font-size:15px}.ah-reading-guide .ah-comparison-wide thead th{position:sticky;top:0;background:#183f65;color:#fff;z-index:1}.ah-reading-guide figure.ah-agency-summary tbody th{width:30%;background:#edf4fa;color:#244764;font-weight:700}\n.ah-reading-guide #ez-toc-container a.ez-toc-link::before{content:none!important;display:none!important}\n.ah-reading-guide .ah-comparison-wide thead th,.ah-reading-guide .ah-comparison-wide thead th *{color:#fff!important}\n.ah-reading-guide .ah-equation{display:block;margin:16px 0;padding:16px 20px;border-left:4px solid #2378b9;background:#edf4fa;border-radius:8px;font-size:20px;font-variant-numeric:tabular-nums;overflow-wrap:anywhere}\n.ah-reading-guide .ah-feature-comparison thead th:nth-child(1){width:18%}.ah-reading-guide .ah-feature-comparison thead th:nth-child(2){width:10%}.ah-reading-guide .ah-feature-comparison thead th:nth-child(3){width:50%}.ah-reading-guide .ah-feature-comparison thead th:nth-child(4){width:22%}\n.ah-reading-guide.ah-compact-toc #ez-toc-container ul.ez-toc-list>li>ul>li>ul{display:none}.ah-reading-guide h4.ah-do{color:#276044!important}.ah-reading-guide h4.ah-dont{color:#855218!important}.ah-reading-guide p.ah-tool-bullet{margin:.45em 0!important;padding:.6em 1em;border-radius:8px;background:#f4f7fa;line-height:1.65}\n.ah-reading-guide .ah-glossary-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:20px;margin:30px 0}.ah-reading-guide .ah-glossary-entry{background:#f7f9fc;border:1px solid #dce5ef;border-radius:12px;padding:22px;height:100%;box-sizing:border-box}.ah-reading-guide .ah-glossary-entry h2{font-size:21px!important;margin:0 0 14px!important;line-height:1.4!important;border:0;padding:0}.ah-reading-guide .ah-glossary-entry p{font-size:16px!important;line-height:1.7!important;margin:0!important}@media(max-width:650px){.ah-reading-guide .ah-glossary-grid{grid-template-columns:1fr}}\n.ah-reading-guide .ah-glossary-grid{display:block}.ah-reading-guide .ah-glossary-grid>.wp-block-group__inner-container{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:20px;align-items:stretch}.ah-reading-guide .ah-glossary-entry{height:auto!important;margin:0!important;min-width:0}.ah-reading-guide .ah-glossary-grid>.wp-block-group__inner-container>.wp-block-greenshift-blocks-container{display:flex;min-width:0}.ah-reading-guide .ah-glossary-grid>.wp-block-group__inner-container>.wp-block-greenshift-blocks-container>.ah-glossary-entry{flex:1}@media(max-width:650px){.ah-reading-guide .ah-glossary-grid>.wp-block-group__inner-container{grid-template-columns:1fr}}\n.ah-reading-guide ol.ah-steps.ah-feature-grid{grid-template-columns:repeat(2,minmax(0,1fr))}@media(max-width:700px){.ah-reading-guide ol.ah-steps.ah-feature-grid{grid-template-columns:1fr}}<\/style><noscript><style>.ah-reading-guide [data-aos]{opacity:1!important;visibility:visible!important;transform:none!important}<\/style><\/noscript><h2 id=\"knowledge-graph\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Knowledge_Graph\"><\/span>Knowledge Graph<span class=\"ez-toc-section-end\"><\/span><\/h2><p class=\"ah-lead wp-block-paragraph\">Knowledge graphs transform complex information into interconnected networks of entities and relationships, mirroring how human brains connect concepts. Their growing adoption is linked to their enhancement of AI systems, particularly in LLMs and <a  data-ilj-link-preview=\"true\"  data-featured-image=\"\"  data-excerpt=\"Harness the power of Retrieval Augmented Generation (RAG) to create an intelligent document interaction system using FastAPI, Haystack, ChromaDB, and Crawl4AI. Table of Contents Problem statement Prerequisites Project components Setting Up the Environment Building the FastAPI Server Building the Haystack RAG pipeline Testing the APIs Conclusion Optional: Adding Asynchronous Processing with Celery [ez-toc initial_view=&quot;show&quot;] Problem&hellip;\" href=\"https:\/\/www.aihello.com\/resources\/blog\/building-a-rag-pipeline-with-fastapi-haystack-and-chromadb-for-urls-in-python\/\">RAG<\/a> pipelines.<\/p><figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"632\" title=\"how to make money advertising for amazon | what does ctr stand for in advertising | how to rank a keyword\" src=\"https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/image-33-1024x632.png\" alt=\"Knowledge graph pipiline\" class=\"wp-image-10919\" srcset=\"https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/image-33-1024x632.png 1024w, https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/image-33-300x185.png 300w, https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/image-33-768x474.png 768w, https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/image-33-1250x771.png 1250w, https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/image-33.png 1400w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><p class=\"wp-block-paragraph\" id=\"a176\">In RAG applications, knowledge graphs surpass traditional document retrieval by providing granular, relationship-rich information. Their ability to map cross-document connections creates a unified semantic layer, enabling more contextually aware retrieval and improved AI responses.<\/p><p class=\"wp-block-paragraph\" id=\"f497\">The typical knowledge graph construction involves document chunking, followed by LLM-based entity and relationship extraction from each chunk. This process identifies key entities and establishes connections between them, creating a structured knowledge representation.<\/p><div class=\"wp-block-greenshift-blocks-container gspb_container gspb_container-gsbp-f10918-5\" id=\"gspb_container-id-gsbp-f10918-5\" data-aos=\"fade\" data-aos-easing=\"ease\" data-aos-duration=\"500\" data-aos-once=\"true\"><p class=\"ah-callout wp-block-paragraph\" id=\"858f\">While LLM-based knowledge graphs offer high accuracy, there are scenarios where a simpler, more cost-effective approach is needed. Using libraries like&nbsp;<strong>spaCy<\/strong>, we can create knowledge graphs without relying on LLMs, making it accessible for projects with limited <a  data-ilj-link-preview=\"true\"  data-featured-image=\"https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2023\/05\/top-12-quartile-alternatives-competitors-for-2025-300x150.jpg\"  data-excerpt=\"Quartile PPC not quite right? Find your perfect match! Explore 12 top Quartile alternatives for Amazon sellers, comparing features, pricing, and more.\" href=\"https:\/\/www.aihello.com\/resources\/blog\/quartile-alternatives\/\">resources<\/a> or when quick prototyping is required. Though the accuracy might be lower, this approach provides a practical solution for organizations looking to experiment with knowledge graphs without the computational and financial overhead of <a  data-ilj-link-preview=\"true\"  data-featured-image=\"\"  data-excerpt=\"Understanding tokenization: Learn why this NLP process is crucial for language models (LLMs). Discover how text is converted for AI and explore different tokenizer types.\" href=\"https:\/\/www.aihello.com\/resources\/blog\/tokenization-and-its-application\/\">LLM<\/a> implementation.<\/p><\/div><h2 id=\"pipeline\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Pipeline\"><\/span>Pipeline<span class=\"ez-toc-section-end\"><\/span><\/h2><h3 id=\"initial-setup-and-spacy-loading\" class=\"wp-block-heading ah-process-heading ah-process-1\"><span class=\"ez-toc-section\" id=\"Initial_Setup_and_SpaCy_Loading\"><\/span><strong>Initial Setup and SpaCy Loading<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3><div class=\"wp-block-greenshift-blocks-container gspb_container gspb_container-gsbp-f10918-8\" id=\"gspb_container-id-gsbp-f10918-8\" data-aos=\"fade\" data-aos-easing=\"ease\" data-aos-duration=\"500\" data-aos-once=\"true\"><p class=\"wp-block-paragraph\">First, we\u2019ll import the necessary libraries and load SpaCy\u2019s large English model, which includes word vectors for better language understanding. This sets up our foundation for natural language processing tasks.<\/p><\/div><pre class=\"wp-block-code\"><code>import spacyfrom typing import List, Dict, Tuple, Setfrom collections import defaultdictfrom rapidfuzz import fuzz# Load the large English model which includes word vectors<a  data-ilj-link-preview=\"true\"  data-featured-image=\"\"  data-excerpt=\"Understanding tokenization: Learn why this NLP process is crucial for language models (LLMs). Discover how text is converted for AI and explore different tokenizer types.\" href=\"https:\/\/www.aihello.com\/resources\/blog\/tokenization-and-its-application\/\">nlp<\/a> = spacy.load(\"en_core_web_lg\")# Example usagetext = \"\"\"Bilbo Baggins celebrates his birthday and leaves the Ring to Frodo, his heir.Gandalf suspects it is a Ring of Power and counsels Frodo to take it away.\"\"\"# Process the text with SpaCydoc = nlp(text)<\/code><\/pre><h3 id=\"entity-extraction\" class=\"wp-block-heading ah-process-heading ah-process-2\"><span class=\"ez-toc-section\" id=\"Entity_Extraction\"><\/span><strong>Entity Extraction<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3><p class=\"wp-block-paragraph\" id=\"6565\">Here we\u2019ll identify and extract named entities from the text, ensuring we capture unique entities while preserving their original context and type information.<\/p><pre class=\"wp-block-code\"><code>def extract_entities(doc) -&gt; List&#091;Dict]:    \"\"\"Extract and categorize named entities from a SpaCy doc.\"\"\"    entities = &#091;]    unique_entities = set()        for ent in doc.ents:        if ent.text not in unique_entities:            entity_dict = {                'text': ent.text,                'type': ent.label_,                'start': ent.start_char,                'end': ent.end_char            }            entities.append(entity_dict)            unique_entities.add(ent.text)        return entities, unique_entities# Example usageentities, unique_entities = extract_entities(doc)<\/code><\/pre><h3 id=\"basic-relation-extraction\" class=\"wp-block-heading ah-process-heading ah-process-3\"><span class=\"ez-toc-section\" id=\"Basic_Relation_Extraction\"><\/span><strong>Basic Relation Extraction<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3><p class=\"wp-block-paragraph\">We\u2019ll extract subject-verb-object relationships from the text, capturing how entities interact with each other through actions and connections.<\/p><pre class=\"wp-block-code\"><code>def extract_basic_relations(doc) -&gt; List&#091;Dict]:    \"\"\"Extract subject-verb-object relations from a SpaCy doc.\"\"\"    relations = &#091;]        for token in doc:        if token.pos_ == \"VERB\":            # Find subjects            subjects = &#091;]            for subj in token.lefts:                if subj.dep_ in (\"nsubj\", \"nsubjpass\"):                    subjects.append(subj)                    # Include conjunctions (e.g., \"Sam and Frodo\")                    subjects.extend(&#091;child for child in subj.children                                   if child.dep_ == \"conj\"])                        # Find objects            objects = &#091;]            for obj in token.rights:                if obj.dep_ == \"dobj\":                    objects.append(obj)                    # Include conjunctions                    objects.extend(&#091;child for child in obj.children                                  if child.dep_ == \"conj\"])                elif obj.dep_ == \"prep\":  # Handle prepositional objects                    objects.extend(&#091;child for child in obj.children                                  if child.dep_ == \"pobj\"])                        # Create relations for each subject-object pair            for subj in subjects:                for obj in objects:                    relation = {                        'subject': subj.text,                        'relation': token.lemma_,                        'object': obj.text,                        'sentence': doc&#091;token.sent.start:token.sent.end].text                    }                    relations.append(relation)        return relations# Example usagebasic_relations = extract_basic_relations(doc)<\/code><\/pre><h3 id=\"pronoun-resolution\" class=\"wp-block-heading ah-process-heading ah-process-4\"><span class=\"ez-toc-section\" id=\"Pronoun_Resolution\"><\/span><strong>Pronoun Resolution<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3><p class=\"wp-block-paragraph\">To improve the quality of our knowledge graph, we\u2019ll implement a simple pronoun resolution system that maps pronouns to their most likely antecedent entities.<\/p><pre class=\"wp-block-code\"><code>def resolve_pronouns(doc, entity_positions: List&#091;Tuple]) -&gt; Dict&#091;str, str]:    \"\"\"Create a mapping of pronouns to their likely antecedents.\"\"\"    pronouns = {\"them\", \"it\", \"they\", \"who\", \"he\", \"she\", \"him\", \"his\", \"her\", \"their\"}    pronoun_map = {}        for token in doc:        if token.text.lower() in pronouns:            # Find the nearest preceding entity            previous_entities = &#091;ent for ent in entity_positions                                if ent&#091;1] &lt; token.idx]            if previous_entities:                # Use the most recent entity as the antecedent                pronoun_map&#091;token.text] = previous_entities&#091;-1]&#091;0]        return pronoun_map# Example usageentity_positions = &#091;(ent.text, ent.start_char, ent.end_char)                    for ent in doc.ents]pronoun_map = resolve_pronouns(doc, entity_positions)<\/code><\/pre><h3 id=\"fuzzy-entity-matching\" class=\"wp-block-heading ah-process-heading ah-process-5\"><span class=\"ez-toc-section\" id=\"Fuzzy_Entity_Matching\"><\/span><strong>Fuzzy Entity Matching<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3><p class=\"wp-block-paragraph\">To handle variations in entity names, we\u2019ll implement fuzzy matching that can identify similar entities and standardize their references.<\/p><pre class=\"wp-block-code\"><code>def fuzzy_match_entity(text: str, entities: set, threshold: float = 0.9) -&gt; str:    \"\"\"Match text to the most similar entity using fuzzy matching.\"\"\"    best_match = None    best_ratio = 0    text_lower = text.lower()        for entity in entities:        entity_lower = entity.lower()                # Try exact word matching first        if text_lower in entity_lower.split() or entity_lower in text_lower.split():            if len(text) &gt; 3:  # Avoid matching very short words                return entity                # Try partial string matching        if text_lower in entity_lower or entity_lower in text_lower:            if len(text) &gt; 3:                return entity                # Calculate token sort ratio for word order differences        ratio = fuzz.token_sort_ratio(text_lower, entity_lower) \/ 100.0                # If needed, try partial ratio for substring matches        if ratio &lt; threshold:            ratio = fuzz.partial_ratio(text_lower, entity_lower) \/ 100.0                    if ratio &gt; threshold and ratio &gt; best_ratio:            best_match = entity            best_ratio = ratio        return best_match if best_match else text# Example usagetest_text = \"Baggin\"  # Misspelled namematched_entity = fuzzy_match_entity(test_text, unique_entities)<\/code><\/pre><h3 id=\"extracting-relations-from-text\" class=\"wp-block-heading ah-process-heading ah-process-6\"><span class=\"ez-toc-section\" id=\"Extracting_Relations_From_Text\"><\/span><strong>Extracting Relations From Text<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3><div class=\"wp-block-greenshift-blocks-container gspb_container gspb_container-gsbp-f10918-23\" id=\"gspb_container-id-gsbp-f10918-23\" data-aos=\"fade\" data-aos-easing=\"ease\" data-aos-duration=\"500\" data-aos-once=\"true\"><p class=\"wp-block-paragraph\">Finally, we\u2019ll combine all our components into a single pipeline that processes text and generates a complete knowledge graph representation.<\/p><\/div><pre class=\"wp-block-code\"><code>def process_text_pipeline(text: str) -&gt; Dict:    \"\"\"Process text through the complete pipeline.\"\"\"    # Step 1: Process with SpaCy    doc = nlp(text)        # Step 2: Extract entities    entities, unique_entities = extract_entities(doc)        # Step 3: Get entity positions for pronoun resolution    entity_positions = &#091;(ent&#091;'text'], ent&#091;'start'], ent&#091;'end'])                        for ent in entities]        # Step 4: Create pronoun mapping    pronoun_map = resolve_pronouns(doc, entity_positions)        # Step 5: Extract basic relations    relations = extract_basic_relations(doc)        # Step 6: Resolve pronouns and fuzzy match entities in relations    resolved_relations = &#091;]    for rel in relations:        resolved_rel = rel.copy()                # Resolve subject        if rel&#091;'subject'].lower() in pronoun_map:            resolved_rel&#091;'subject'] = pronoun_map&#091;rel&#091;'subject'].lower()]        else:            resolved_rel&#091;'subject'] = fuzzy_match_entity(                rel&#091;'subject'], unique_entities            )                # Resolve object        if rel&#091;'object'].lower() in pronoun_map:            resolved_rel&#091;'object'] = pronoun_map&#091;rel&#091;'object'].lower()]        else:            resolved_rel&#091;'object'] = fuzzy_match_entity(                rel&#091;'object'], unique_entities            )                resolved_relations.append(resolved_rel)        return {        'entities': entities,        'relations': resolved_relations,        'pronoun_map': pronoun_map    }# Example usage of the complete pipelinetext = \"\"\"Bilbo Baggins celebrates his birthday and leaves the Ring to Frodo, his heir. Gandalf suspects it is a Ring of Power and counsels Frodo to take it away.\"\"\"result = process_text_pipeline(text)<\/code><\/pre><h3 id=\"creating-the-knowledge-graph\" class=\"wp-block-heading ah-process-heading ah-process-7\"><span class=\"ez-toc-section\" id=\"Creating_the_Knowledge_Graph\"><\/span><strong>Creating the Knowledge Graph<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3><p class=\"wp-block-paragraph\">First, we\u2019ll create a class to handle our knowledge graph operations using NetworkX:<\/p><pre class=\"wp-block-code\"><code>import networkx as nximport matplotlib.pyplot as pltfrom typing import List, Dictimport loggingclass LORKnowledgeGraph:    def __init__(self):        \"\"\"Initialize the NetworkX graph with multi-directed edges.\"\"\"        self.G = nx.MultiDiGraph()        logging.basicConfig(level=logging.INFO)        self.logger = logging.getLogger(__name__)    def create_entity_node(self, entity: str):        \"\"\"Create a node with sanitized label.\"\"\"        label = entity.replace(' ', '_').replace('-', '_').replace(\"'\", '')        if not label&#091;0].isalpha():            label = 'N_' + label        self.G.add_node(label, name=entity)            def create_relationship(self, subject: str, relation: str, object: str):        \"\"\"Create a relationship between two nodes.\"\"\"        subject_label = subject.replace(' ', '_').replace('-', '_').replace(\"'\", '')        object_label = object.replace(' ', '_').replace('-', '_').replace(\"'\", '')                if not subject_label&#091;0].isalpha():            subject_label = 'N_' + subject_label        if not object_label&#091;0].isalpha():            object_label = 'N_' + object_label                    relation_type = relation.upper().replace(' ', '_')        self.G.add_edge(subject_label, object_label, relation=relation_type)    def build_from_relations(self, relations: List&#091;Dict]):        \"\"\"Build the graph from extracted relations.\"\"\"        for relation in relations:            self.create_entity_node(relation&#091;'subject'])            self.create_entity_node(relation&#091;'object'])            self.create_relationship(                relation&#091;'subject'],                relation&#091;'relation'],                relation&#091;'object']            )<\/code><\/pre><p class=\"wp-block-paragraph\" id=\"ed84\">Now, let\u2019s extend our pipeline to process multiple text segments and build a knowledge graph:<\/p><pre class=\"wp-block-code\"><code>def process_multiple_texts(texts: List&#091;str]) -&gt; nx.MultiDiGraph:    \"\"\"Process multiple text segments and create a knowledge graph.\"\"\"    # Initialize the knowledge graph    kg = LORKnowledgeGraph()        # Process each text segment    for text in texts:        # Process text through our existing pipeline        result = process_text_pipeline(text)                # Add extracted relations to the knowledge graph        kg.build_from_relations(result&#091;'relations'])        return kg.Gdef visualize_knowledge_graph(G: nx.MultiDiGraph, figsize=(16, 12)):    \"\"\"Visualize the knowledge graph.\"\"\"    plt.figure(figsize=figsize)        # Calculate node sizing based on degree centrality    degrees = dict(G.degree())    node_sizes = &#091;1500 + (degrees&#091;node] * 100) for node in G.nodes()]        # Use Graphviz layout for better visualization    pos = nx.nx_agraph.graphviz_layout(G, prog='neato')        # Draw the graph with curved edges and node labels    nx.draw_networkx_edges(G, pos,                          edge_color='gray',                         alpha=0.4,                         arrows=True,                         connectionstyle='arc3,rad=0.2')        nx.draw_networkx_nodes(G, pos,                         node_color='lightblue',                         node_size=node_sizes,                         alpha=0.7)        # Add labels    node_labels = nx.get_node_attributes(G, 'name')    edge_labels = nx.get_edge_attributes(G, 'relation')        nx.draw_networkx_labels(G, pos, node_labels, font_size=8)    nx.draw_networkx_edge_labels(G, pos, edge_labels, font_size=6)        plt.title(\"Knowledge Graph Visualization\")    plt.axis('off')    return plt<\/code><\/pre><h2 id=\"final-result\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Final_Result\"><\/span><strong>Final Result<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p class=\"wp-block-paragraph\">Here\u2019s what the final result on multiple chunks of text from Lord of the Rings looks like:<br><\/p><figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"615\" title=\"what is sponsored brands in amazon | how long do amazon limited time deals last | how to pick amazon keywords\" src=\"https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/creating-a-knowledge-graph-without-llms-1024x615.png\" alt=\"\" class=\"wp-image-10920\" srcset=\"https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/creating-a-knowledge-graph-without-llms-1024x615.png 1024w, https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/creating-a-knowledge-graph-without-llms-300x180.png 300w, https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/creating-a-knowledge-graph-without-llms-768x461.png 768w, https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/creating-a-knowledge-graph-without-llms-1250x751.png 1250w, https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2024\/12\/creating-a-knowledge-graph-without-llms.png 1400w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><h2 id=\"conclusion\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2><div class=\"wp-block-greenshift-blocks-container gspb_container gspb_container-gsbp-f10918-34\" id=\"gspb_container-id-gsbp-f10918-34\" data-aos=\"fade\" data-aos-easing=\"ease\" data-aos-duration=\"500\" data-aos-once=\"true\"><p class=\"ah-callout wp-block-paragraph\">This lightweight approach to knowledge graph construction demonstrates that valuable semantic networks can be created without relying on large language models. While LLM-based approaches may offer higher accuracy and more nuanced relationship extraction, the combination of SpaCy, fuzzy matching, and basic NLP techniques provides a robust foundation for organizations looking to experiment with knowledge graphs. This method not only offers significant cost savings but also provides greater transparency and control over the extraction process. As knowledge graphs continue to evolve as critical components of modern AI systems, having accessible, LLM-free alternatives ensures that organizations of all sizes can harness the power of structured knowledge representation for their specific use cases.<\/p><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Unlock the power of knowledge graphs for AI, LLMs &#038; RAG. Discover how to build efficient, cost-effective KGs with SpaCy: entity and relation extraction made easy.<\/p>\n","protected":false},"author":30,"featured_media":10919,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_gspb_post_css":".gspb_container-id-gsbp-f10918-23,.gspb_container-id-gsbp-f10918-34,.gspb_container-id-gsbp-f10918-5,.gspb_container-id-gsbp-f10918-8{flex-direction:column;box-sizing:border-box}#gspb_container-id-gsbp-f10918-23.gspb_container>p:last-of-type,#gspb_container-id-gsbp-f10918-34.gspb_container>p:last-of-type,#gspb_container-id-gsbp-f10918-5.gspb_container>p:last-of-type,#gspb_container-id-gsbp-f10918-8.gspb_container>p:last-of-type{margin-bottom:0}#gspb_container-id-gsbp-f10918-23.gspb_container.aos-animate,#gspb_container-id-gsbp-f10918-23.gspb_container[data-gs-aos],#gspb_container-id-gsbp-f10918-34.gspb_container.aos-animate,#gspb_container-id-gsbp-f10918-34.gspb_container[data-gs-aos],#gspb_container-id-gsbp-f10918-5.gspb_container.aos-animate,#gspb_container-id-gsbp-f10918-5.gspb_container[data-gs-aos],#gspb_container-id-gsbp-f10918-8.gspb_container.aos-animate,#gspb_container-id-gsbp-f10918-8.gspb_container[data-gs-aos]{opacity:1;transform:translateZ(0)}#gspb_container-id-gsbp-f10918-23.gspb_container,#gspb_container-id-gsbp-f10918-34.gspb_container,#gspb_container-id-gsbp-f10918-5.gspb_container,#gspb_container-id-gsbp-f10918-8.gspb_container{position:relative;transition-duration:.5s;transition-timing-function:var(--gs-root-animation-easing, cubic-bezier(0.42, 0, 0.58, 1));opacity:var(--gs-root-animation-opacity, 0);transition-property:opacity,transform,filter;box-sizing:border-box}","footnotes":""},"categories":[103,163,85],"tags":[1182,1179,1149,1181,1180],"republish_tag":[],"class_list":["post-10918","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","category-resources","category-tips","tag-ai-systems","tag-knowledge-graph","tag-llm","tag-networks","tag-pipeline"],"_links":{"self":[{"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/posts\/10918","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/users\/30"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/comments?post=10918"}],"version-history":[{"count":1,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/posts\/10918\/revisions"}],"predecessor-version":[{"id":14106,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/posts\/10918\/revisions\/14106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/media\/10919"}],"wp:attachment":[{"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/media?parent=10918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/categories?post=10918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/tags?post=10918"},{"taxonomy":"republish_tag","embeddable":true,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/republish_tag?post=10918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}