Automating Knowledge Graph Population with Local LLMs and SPOC Quads

Learn how to extract structured SPOC quads from raw text using a local Llama 3.2 model via Ollama and load them into a QuadStore knowledge graph.

Automating Knowledge Graph Population with Local LLMs and SPOC Quads

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:

!apt-get update -qq && apt-get install -y -qq zstd
!curl -fsSL https://ollama.com/install.sh | sh

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

!pip install wikipedia requests

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:

import subprocess
import time

# 1. Starting the Ollama server in the background
print("Starting Ollama server...")
process = subprocess.Popen(["ollama", "serve"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
time.sleep(3)  # Give the server a moment to start

# 2. Pulling the Llama 3.2 model (this may take a minute or two on Colab)
print("Pulling Llama 3.2...")
subprocess.run(["ollama", "pull", "llama3.2"])
print("Model ready!")

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:

%%writefile quadstore.py

class QuadStore:
    def __init__(self):
        # Using a simple list to store the facts for our lightweight implementation
        self.quads = []

    def add(self, subject, predicate, obj, context):
        """Adds a new SPOC quad to the knowledge graph."""
        quad = (subject, predicate, obj, context)
        if quad not in self.quads:
            self.quads.append(quad)

    def query(self, subject=None, predicate=None, obj=None, context=None):
        """Queries the graph. Returns a list of quads that match the provided criteria."""
        results = []
        for q_sub, q_pred, q_obj, q_ctx in self.quads:
            if (subject is None or subject == q_sub) and \
               (predicate is None or predicate == q_pred) and \
               (obj is None or obj == q_obj) and \
               (context is None or context == q_ctx):
                results.append((q_sub, q_pred, q_obj, q_ctx))
        return results

With the QuadStore in place and the Ollama server running, the extraction pipeline can fetch a Wikipedia article, chunk the text into manageable passages, and send each passage to Llama 3.2 with a few-shot prompt that instructs the model to return a JSON array of SPOC quads. Each returned quad is then validated and loaded directly into the store, building a structured, queryable knowledge graph from unstructured text in a fully automated fashion.