Graph-RAG for Enterprise Documents: Building a Knowledge Graph from PDFs, Wikis, and Databases

In this article, you will learn how to build a Graph-RAG system that combines a live database, wiki pages, and PDFs into one prioritized knowledge source, so your assistant gives a single, correct answer when those sources disagree.

Topics we will cover include:

  • How to turn database rows and wiki runbooks into prioritized facts in a simple quad store.
  • How to extract, chunk, and embed PDF text with pdfplumber and Chroma as a vector fallback.
  • How to fuse all three tiers into one prompt with explicit priority rules that resolve conflicts.

Graph-RAG for Enterprise Documents: Building a Knowledge Graph from PDFs, Wikis, and Databases

An engineer pings your internal AI assistant at 11 p.m. and asks who’s on-call for the payments service. The assistant has read the wiki, indexed every PDF in the shared drive, and has a live connection to the service registry. It should be a trivial question. But the wiki was last edited four months ago, the on-call rotation has changed twice since then, and the database has the current answer sitting in a single row. If retrieval treats all three sources as equally trustworthy text, there’s a real chance it hands your engineer someone who left the rotation months ago.

That’s the exact failure covered in the article Beyond Vector Search: Building a Deterministic 3-Tiered Graph-RAG System, where vector-only RAG confuses which team a basketball player currently plays for, because multiple team names sit close together in embedding space. The fix: three retrieval tiers (an absolute-facts graph, a background-stats graph, and a vector database), fused through explicit priority rules written into the prompt instead of a routing algorithm. This article takes that same architecture and points it at a live database, a set of wiki pages, and a batch of PDFs, all feeding one fused answer.

The approach has real-world backing: an IDC study sponsored by Neo4j found organizations grounding generative AI in graph-based context cut hallucination rates by 44 percent on average, and one life sciences company went from a 20 to 40 percent hallucination rate down to 2 to 5 percent.

The Scenario

We’ll build for a fictional platform team at “Northwind,” running three services: payments-service, checkout-service, notifications-service. Three sources describe them. A service registry database (Postgres in reality, SQLite here) holds the owner, on-call engineer, status, version, and SLA tier, live and always current, so it becomes Priority 1.

A wiki space of runbooks is thorough but drifts stale the moment nobody updates it after a rotation change, so it becomes Priority 2, useful until it conflicts with something more current. A folder of PDFs (an SLA doc, an incident postmortem) adds narrative context no structured source holds, so it becomes Priority 3, our vector fallback.

We’ll deliberately plant one conflict: the wiki’s payments runbook names an on-call engineer the database no longer agrees with. That conflict is the whole reason this architecture exists.

Three boxes, database/wiki/PDF icons each feeding into a labeled priority box, all converging into one fused prompt box that points to an LLM icon and a single answer bubble.

Three boxes, database/wiki/PDF icons each feeding into a labeled priority box, all converging into one fused prompt box that points to an LLM icon and a single answer bubble (click to enlarge)
Image by Author

Prerequisites

You’ll need Python 3.10+ and a local model server. Like the original article, we’ll use Ollama with llama3.2; swapping in a hosted API later only changes the one function that sends the final request.

chromadb is our Priority 3 vector store. spacy handles named entity recognition so we can pull service names out of a question. pdfplumber extracts PDF text more reliably than most lightweight alternatives. requests talks to Ollama’s local REST API.

Step 1: The Quad Store and the CMDB (Priority 1)

Priorities 1 and 2 both need a home: a Subject-Predicate-Object-Context (SPOC) quad store. Rather than pull in the original tutorial’s QuadStore library, here’s a minimal version built from scratch:

Two methods, four fields, and any argument left None acts as a wildcard. The original library indexes these fields for constant-time lookups, which matters at millions of facts; a plain scan is fine for one team’s service catalog.

Now the database itself: our service registry, or configuration management database (CMDB). SQLite keeps the tutorial dependency-free; swapping in psycopg2 and a Postgres connection string later is an isolated change.

notifications-service is marked deprecated on purpose; we’ll use that later. Run it, and you have a real .db file. Next, convert those rows into Priority 1 facts, querying live every time so they’re never stale by construction:

Every fact gets tagged with context “cmdb”, so downstream code always knows which tier it came from.

Step 2: Ingesting the Wiki (Priority 2)

Wiki pages don’t hand you clean columns, so this means parsing prose. We’ll keep it narrow: regex patterns for the “key: value” style most runbooks use. Here’s data/wiki/runbook-payments.md, written the way a real page drifts out of date:

Jordan Blake here, Amara Chen in the database: our planted conflict. Add a matching runbook-checkout.md (Diego Ruiz, consistent with the database) and an onboarding page mentioning that notifications-service is being sunset. Now extract:

Each fact is tagged with the wiki page it came from, so a disagreement later traces back to a specific document. In production, a real Confluence export won’t follow this tidy convention; you’d widen the patterns per template or add a lightweight LLM extraction pass for pages that don’t fit one.

Step 3: Ingesting PDFs (Priority 3)

PDFs aren’t going to resolve into clean facts, and we’re not trying to make them. For this tutorial, create two sample PDFs: an SLA document stating that payments-service commits to 99.95 percent monthly uptime with a service-credit process, and a postmortem describing a September 14, 2026, incident where on-call engineer Amara Chen diagnosed a database failover and rolled back the connection pool within nine minutes. (Any PDF writer works for generating these; ReportLab or exporting from a doc editor are both fine.) Then extract, chunk, and embed:

pdfplumber handles the page-by-page text extraction. Chunks stay small (around 500 characters) so each one holds one idea, which matters for retrieval precision; longer PDFs would want a token-aware splitter instead. The first run downloads Chroma’s default embedding model automatically.

Step 4: Retrieval and the Fused Prompt

With three stores built, we need to pull entities from a question and query all three, then hand everything to the model with explicit rules for resolving conflicts.

spaCy’s general model won’t recognize our made-up service names on its own, so pairing it with a small known-terms list is a common, practical fix for internal tools. get_facts() checks each entity as both subject and object, since a fact can name it either way.

Now for the part that actually resolves conflicts. It isn’t code logic, but a prompt:

Rule 2 is the one doing the real work: it doesn’t just say “prefer Priority 1,” it forbids mentioning Priority 2’s conflicting version at all. A model that hedges with “the wiki says X but the database says Y” hasn’t resolved anything for the person asking.

Step 5: Running It

Run python setup_cmdb.py, generate your sample PDFs, then run python main.py. Everything builds once at startup and runs locally against Ollama from there.

Testing It

Each question exercises the hierarchy differently. “Who’s on-call for payments-service?” pulls Amara Chen from Priority 1 and Jordan Blake from the stale wiki; rule 2 means the answer names only Amara Chen, with no hedging. “What’s our SLA?” needs Priority 1’s sla_tier fact plus the fuller SLA text from Priority 3; neither tier alone tells the whole story. “What happened in the last incident?” has no structured answer at all; it lives entirely in the postmortem PDF, which happens to name Amara Chen again, reinforcing Priority 1 without contradicting it. And “Is notifications-service still active?” is a test of restraint: Priority 1 says deprecated directly, and the model shouldn’t need to reach for anything else.

As the original article notes, this isn’t foolproof; it depends on the model actually following the rules rather than falling back on its own training data, which is why the rules need to be explicit and repeated rather than a vague instruction to “be accurate.”

A terminal screenshot of the four Q&A pairs, with the stale Jordan Blake fact grayed out next to the winning Amara Chen answer

A terminal screenshot of the four Q&A pairs, with the stale Jordan Blake fact grayed out next to the winning Amara Chen answer (click to enlarge)
Image by Author

Taking This to Production

Swapping SQLite for Postgres is a small, isolated change. The bigger question is refresh cadence: Priority 1 should stay live or near-live, since that’s its whole value, while Priorities 2 and 3 can re-ingest on a nightly or hourly job. Just make sure that job actually runs, since a stale Priority 2 tier is worse than none at all. Real wiki exports won’t follow a tidy “key: value” format, so expect to widen your extraction patterns or add an LLM extraction pass. Access control also needs to happen before facts reach the prompt, not as an instruction inside it, since a soft instruction can be argued with, but data that was never fetched can’t be. And once you’re modeling a genuinely large, densely connected graph rather than one team’s service catalog, a real graph database like Neo4j or ArangoDB starts earning its complexity.

Conclusion

The advantages carry over from the original architecture: predictability, since a live database fact is close to guaranteed to be correct; explainability, since the three tiers stay labeled and traceable; and simplicity, with no routing model to train, just three pipelines and one well-written prompt. The trade-off is token overhead and reliance on the model actually following instructions instead of its own training data. For an enterprise assistant, that trade is usually worth making. A tool that costs a few extra cents in tokens is fine. One that confidently pages the wrong engineer at 2 a.m. is not.

If you want the original example this article builds on, it’s worth reading: Beyond Vector Search: Building a Deterministic 3-Tiered Graph-RAG System. Same core idea, just pointed at databases, wikis, and PDFs.

No comments yet.

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.