Automating Knowledge Graph Population: Extracting Entities and Triples from Unstructured Text with an LLM

In this article, you will learn how to automatically extract structured knowledge from raw text and populate a knowledge graph with SPOC quads using a local LLM via Ollama.

Topics we will cover include:

  • How to set up Ollama with the Llama 3.2 model to run a free, local LLM for structured data extraction.
  • How to design a robust extraction pipeline that converts unstructured Wikipedia text into SPOC (Subject-Predicate-Object-Context) quads using few-shot prompting and JSON output mode.
  • How to load the extracted quads into a QuadStore knowledge graph, ready for use in a Graph-RAG retrieval pipeline.

Automating Knowledge Graph Population: Extracting Entities and Triples from Unstructured Text with an LLM

Introduction

The recent article on Building a Deterministic 3-Tiered Graph-RAG System shows how a hierarchical, graph-based architecture can tackle the issue of hallucinations in standard vector information retrieval.

That article leveraged Quadstore, a lightweight knowledge graph database implemented in Python, to teach LLMs to respect ground-truth facts, thereby ensuring factual accuracy and deterministic retrieval conflict resolution in applications like RAG systems.

A critical question remains, though: where does the factual graph knowledge come from? This article helps close the loop, showing a free, fully automated approach to extract entities and build SPOC quads (Subject-Predicate-Object-Context) from raw text such as Wikipedia pages. We will do this with the help of a local, free LLM from Ollama. Once these quads are built, we will illustrate how to directly populate the Quadstore.

Prerequisites and Setup

The workflow shown in this article is designed to run seamlessly both in a Google Colab notebook and in your local Python IDE. If you choose the latter, you will need to manually install Ollama on your computer first, along with pulling the Llama 3.2 model locally.

In Google Colab, you can set up Ollama and get Llama 3.2 for your open session using these commands:

Either way, you will need to install these two libraries as well:

Llama 3.2 is a lightweight, free model. Its API is configured to strictly operate in JSON input/output mode, a mandatory standard for reliable data extraction. Using subprocess, we can start the Ollama server as a background process and pull our target model:

Automated Knowledge Graph Population

Let’s look at the process of constructing our knowledge graph from a source of raw text. We will convert narrative text into strictly modeled relationships that extend classical RDF triples of the form (Subject, Predicate, Object) by adding a fourth dimension: the context. This is useful for tracking where a fact comes from and whether it is true or not.

A triple like ("LeBron James", "plays_for", "Lakers") thus becomes ("LeBron James", "plays_for", "Lakers", "NBA_2023_Roster").

First, we will create a small, simulated QuadStore engine that mimics the framework used in the related article this one follows up on. If you are working in a notebook, run this code in a separate cell to generate a quadstore.py file in your workspace on the fly:

A file containing exactly the above code will be created, and we will refer to it later on just like any other Python module, to demonstrate how to load our created knowledge graph into our mock Graph-RAG system.

Back to the main process: we will now pull some raw text from Wikipedia using the namesake API. The auto_suggest=False option ensures we correctly fetch the right article name from Wikipedia without automated corrections that may cause a crash.

Output:

Now comes the core of the entire workflow: the robust extraction engine, modeled by the following function that:

  • Works with the target LLM’s formatting engine to extract structured data in the form of quads. To do this, we use few-shot examples as part of the prompt sent to the LLM.
  • Post-processes the LLM output to extract a list of facts and build a list of quads accordingly.

All that remains is running the pipeline to extract, view, and make use of our newly created quads, which will constitute our knowledge graph.

Results:

From just two paragraphs of Alan Turing’s Wikipedia article, we extracted around 11 facts, structured as quads. Note that the exact number may vary slightly due to the non-deterministic behavior of the LLM.

We wrap up by seeing how to add these facts into the QuadStore object:

Output:

Conclusion

This article closed the loop on our deterministic 3-tiered Graph-RAG architecture by showing how to build a knowledge graph consisting of facts extracted directly from unstructured text in the form of quads — all from scratch. You can now integrate this knowledge into your retrieval pipeline to help eliminate issues like LLM hallucinations.

One Response to Automating Knowledge Graph Population: Extracting Entities and Triples from Unstructured Text with an LLM

  1. Sumit Mishra September 30, 2026 at 7:59 pm #

    Hi need jobs my skills airtfice intelligence and cloud computing lot of sikles

Leave a Reply

Machine Learning Mastery is part of Guiding Tech Media, a leading digital media publisher focused on helping people figure out technology. Visit our corporate website to learn more about our mission and team.