Knowledge Graphs

Retrieval-Augmented Generation intermediate Building with AI 11 min read

Overview

A knowledge graph is a structured representation of real-world entities and the relationships between them, stored in a format that machines can traverse and reason over. Unlike flat document stores or relational tables, knowledge graphs model information as interconnected nodes and edges—making it possible to answer questions that require following chains of relationships across multiple data points.

For AI engineering, knowledge graphs address a fundamental limitation of both LLMs and traditional vector-based retrieval: the inability to perform precise multi-hop reasoning. When a user asks “Which hotels managed by company X in region Y have recently updated their virtual tours?”, answering this requires traversing relationships between companies, properties, regions, and content updates—exactly the kind of connected reasoning that knowledge graphs excel at.

Knowledge graphs have been foundational to products like Google Search (the Google Knowledge Graph powers those info boxes you see), Amazon Alexa, LinkedIn’s economic graph, and enterprise data platforms. With the rise of GraphRAG, they are now becoming a critical component of AI systems that need structured, explainable reasoning over domain-specific data.

How It Works

The Triple: Subject-Predicate-Object

The atomic unit of a knowledge graph is the triple—a statement connecting two entities through a relationship:

Triple Structure

A triple consists of (Subject, Predicate, Object). For example: (Hilton_NYC, located_in, Manhattan), (Manhattan, part_of, New_York_City), (Hilton_NYC, managed_by, Hilton_Worldwide). Each element is identified by a unique identifier (URI in RDF, or node ID in property graphs).

By combining millions of triples, a knowledge graph builds a rich web of interconnected facts that can be traversed to discover implicit knowledge. The query “What city is the Hilton NYC in?” can be answered by following two hops: Hilton_NYC → located_in → Manhattan → part_of → New_York_City.

Graph Data Models

There are two dominant models for storing knowledge graphs:

RDF (Resource Description Framework): The W3C standard for linked data. Every piece of information is stored as a subject-predicate-object triple. Entities and relationships are identified by URIs, making graphs globally interoperable. Queried using SPARQL. Used by Wikidata, DBpedia, and many government open data initiatives.

# RDF Triples (Turtle syntax)
@prefix ex: <http://example.org/> .

ex:Hilton_NYC  ex:located_in    ex:Manhattan .
ex:Hilton_NYC  ex:managed_by    ex:Hilton_Worldwide .
ex:Hilton_NYC  ex:has_rooms     "483"^^xsd:integer .
ex:Manhattan   ex:part_of       ex:New_York_City .
ex:Manhattan   rdf:type         ex:Borough .

Property Graphs: A more flexible model where both nodes and edges can have arbitrary key-value properties. This makes it easier to model complex attributes (e.g., a “managed_by” edge with a since date). Queried using Cypher (Neo4j) or Gremlin (Apache TinkerPop). More popular in application development due to their flexibility.

// Cypher (Neo4j) - Property Graph
CREATE (h:Hotel {name: "Hilton NYC", rooms: 483, stars: 4})
CREATE (m:Borough {name: "Manhattan"})
CREATE (hw:Company {name: "Hilton Worldwide", ticker: "HLT"})
CREATE (nyc:City {name: "New York City"})

CREATE (h)-[:LOCATED_IN]->(m)
CREATE (h)-[:MANAGED_BY {since: 2015}]->(hw)
CREATE (m)-[:PART_OF]->(nyc)

Building a Knowledge Graph

Knowledge graph construction typically follows these stages:

  1. Entity Extraction: Identify entities from unstructured text using NER (Named Entity Recognition). Extract people, organizations, locations, products, dates, and domain-specific entities.
  2. Relation Extraction: Determine relationships between identified entities. Modern approaches use transformer models fine-tuned on relation extraction datasets, or leverage LLMs with structured prompting to extract (subject, predicate, object) triples.
  3. Entity Resolution: Merge duplicate entities that refer to the same real-world thing (e.g., “NYC”, “New York”, “New York City” → single node). This is also called entity linking or deduplication.
  4. Ontology Alignment: Map extracted relationships to a predefined schema (ontology) that defines valid entity types and relationship types for your domain.
  5. Validation & Enrichment: Verify extracted facts against authoritative sources, resolve conflicts, and enrich nodes with additional metadata.

Querying Knowledge Graphs

The power of knowledge graphs emerges in querying. Unlike SQL which requires explicit JOIN statements, graph queries naturally express traversals:

// SPARQL: Find all hotels in New York managed by Hilton
SELECT ?hotel ?borough WHERE {
  ?hotel rdf:type ex:Hotel .
  ?hotel ex:managed_by ex:Hilton_Worldwide .
  ?hotel ex:located_in ?borough .
  ?borough ex:part_of ex:New_York_City .
}

// Cypher equivalent:
MATCH (h:Hotel)-[:MANAGED_BY]->(:Company {name: "Hilton Worldwide"}),
      (h)-[:LOCATED_IN]->(b:Borough)-[:PART_OF]->(:City {name: "New York City"})
RETURN h.name, b.name

Knowledge Graphs vs. Vector Databases

These are complementary, not competing technologies:

When to Use Knowledge Graphs

Knowledge graphs are most valuable when your queries require reasoning across relationships—multi-hop questions, aggregation over connected entities, or explainable reasoning chains. If your queries are purely “find similar content,” vector search alone is simpler and sufficient.

Practical Applications

Enterprise Search and Q&A

Companies like Google, Amazon, and Microsoft use knowledge graphs to power search features that go beyond keyword matching. When you search “Who founded the company that makes the iPhone?”, a knowledge graph traverses: iPhone → made_by → Apple → founded_by → Steve Jobs. Enterprise versions of this power internal knowledge bases, connecting employees, projects, documents, and expertise areas.

Recommendation Systems

Knowledge graphs enable sophisticated recommendations by traversing user preferences, item attributes, and contextual relationships. Netflix, Spotify, and e-commerce platforms use graph traversals to find items connected through shared properties, genres, artists, or user behavior patterns—producing more explainable recommendations than pure collaborative filtering.

Fraud Detection

Financial institutions use knowledge graphs to detect fraud rings by identifying suspicious patterns of connections between accounts, transactions, addresses, and devices. Graph traversals can reveal hidden relationships (e.g., two seemingly unrelated accounts sharing a phone number through an intermediary) that would be nearly impossible to detect in tabular data.

Drug Discovery and Life Sciences

Pharmaceutical companies model proteins, genes, diseases, and drug compounds as knowledge graphs. Traversing these graphs reveals potential drug targets, side effect predictions, and repurposing opportunities. The COVID-19 pandemic accelerated this use case, with knowledge graphs helping identify existing drugs that might be effective against the virus.

AI-Powered Knowledge Management

In the context of GraphRAG, knowledge graphs serve as the structured backbone that LLMs reason over. Instead of retrieving flat text chunks, the system retrieves relevant subgraphs—entities, their relationships, and contextual properties—giving the LLM a structured, navigable view of domain knowledge that reduces hallucination and enables precise answers to complex questions.

Key Takeaways

Further Reading